-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDValue.cpp
107 lines (91 loc) · 2.36 KB
/
DValue.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "DValue.h"
#include <bits/stdc++.h>
DValue::DValue(int nodes)
{
this->nodes = nodes;
}
void DValue::initializeDValue(vector<int> &D)
{
int i;
for(i=0;i<nodes;++i)
{
D[i]=0;
}
}
void DValue::findDValue(vector<int> &D, map< pair<int, int>, int> &edgeSet, vector<int> &partition1, vector<int> &partition2)
{
pair<int, int> p;
int node1, node2;
int container_node1, container_node2;
int edgeWeight;
for(map< pair<int, int>, int>::iterator it=edgeSet.begin();it!=edgeSet.end();it++)
{
p = it->first;
edgeWeight = it->second;
node1 = p.first;
container_node1 = container(node1, partition1, partition2);
node2 = p.second;
container_node2 = container(node2, partition1, partition2);
if(container_node1 == container_node2)
{
D[node1] = D[node1] - edgeWeight;
D[node2] = D[node2] - edgeWeight;
}
else
{
D[node1] = D[node1] + edgeWeight;
D[node2] = D[node2] + edgeWeight;
}
}
}
int DValue::container(int node, vector<int> &partition1, vector<int> &partition2)
{
if (find(partition1.begin(), partition1.end(), node) != partition1.end() )
return 1;
else
return 2;
}
void DValue::changeDValue(vector<int> &D, map< pair<int, int>, int> &edgeSet, int a, int b, vector<int> &partition1, vector<int> &partition2)
{
int i, c1, c2, x, y;
for(i=0;i<partition1.size();++i)
{
x=partition1[i];
if(x != a && x != b)
{
if(edgeSet.find(make_pair(x, a)) != edgeSet.end())
c1 = edgeSet[make_pair(x, a)];
else if(edgeSet.find(make_pair(a, x)) != edgeSet.end())
c1 = edgeSet[make_pair(a, x)];
else
c1 = 0;
if(edgeSet.find(make_pair(x, b)) != edgeSet.end())
c2 = edgeSet[make_pair(x, b)];
else if(edgeSet.find(make_pair(b, x)) != edgeSet.end())
c2 = edgeSet[make_pair(b, x)];
else
c2 = 0;
D[x] = D[x] + 2*c1 - 2*c2;
}
}
for(i=0; i < partition2.size(); ++i)
{
y = partition2[i];
if(y != a && y != b)
{
if(edgeSet.find(make_pair(y, a)) != edgeSet.end())
c1 = edgeSet[make_pair(y, a)];
else if(edgeSet.find(make_pair(a, y)) != edgeSet.end())
c1 = edgeSet[make_pair(a, y)];
else
c1 = 0;
if(edgeSet.find(make_pair(y, b)) != edgeSet.end())
c2 = edgeSet[make_pair(y, b)];
else if(edgeSet.find(make_pair(b, y)) != edgeSet.end())
c2 = edgeSet[make_pair(b, y)];
else
c2 = 0;
D[y] = D[y] + 2*c2 - 2*c1;
}
}
}