-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessorgraphquickconcat.h
More file actions
142 lines (113 loc) · 4.49 KB
/
processorgraphquickconcat.h
File metadata and controls
142 lines (113 loc) · 4.49 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#ifndef PROCESSORGRAPHQUICKCONCAT_H
#define PROCESSORGRAPHQUICKCONCAT_H
#include <iostream>
#include <NetworKit/graph/Graph.h>
#include <vector>
#include "sortedvectorwrapper.h"
#include "weightededgewrapper.h"
namespace NetworKit {
class ProcessorGraphQuickConcat
{
private:
SortedVectorWrapper<node> nodes;
std::vector<std::vector<edgeweight>> weights;
size_t numberOfNodes;
node concatNode;
size_t concatNodeIndex;
edgeweight minEdgeWeight = std::numeric_limits<edgeweight>::max();
node lightestNeighbour;
size_t lightestNeighbourIndex;
std::vector<bool> concated;
size_t amountConcated = 0;
void concat(size_t pos) {
concated[pos] = true;
amountConcated++;
}
bool isConcated(node n) { return concated[nodes.getIndexOf(n)]; }
public:
ProcessorGraphQuickConcat(const Graph& g) {
assert(g.isWeighted());
assert(!g.isDirected());
numberOfNodes = g.numberOfNodes();
nodes.vector_.reserve(numberOfNodes);
concated.reserve(numberOfNodes);
for(int i = 0; i < numberOfNodes; ++i) concated.push_back(false);
auto nodeFunc = [&](node n) {
nodes.push_back(n);
};
g.forNodes(nodeFunc);
nodes.sort();
weights.reserve(numberOfNodes);
for(size_t i = 0; i < numberOfNodes; ++i) {
weights.push_back(std::vector<edgeweight>());
weights[i].reserve(numberOfNodes);
for(int j = 0; j < numberOfNodes; ++j) {
weights[i].push_back(0);
}
}
auto edgeFunc = [&](node u, node v, edgeweight w) {
auto uIndex = nodes.getIndexOf(u);
auto vIndex = nodes.getIndexOf(v);
weights[uIndex][vIndex] = w;
weights[vIndex][uIndex] = w;
if(minEdgeWeight > w) {
minEdgeWeight = w;
concatNode = u;
concatNodeIndex = uIndex;
lightestNeighbour = v;
lightestNeighbourIndex = vIndex;
}
};
g.forEdges(edgeFunc);
concated[concatNodeIndex] = true;
}
void concatWithConcatNode(node n) {
auto otherIndex = nodes.getIndexOf(n);
concated[otherIndex] = true;
#pragma omp parallel for
for(int i = 0; i < numberOfNodes; ++i) {
weights[concatNodeIndex][i] += weights[otherIndex][i];
}
}
node getConcatNode() {return concatNode;}
node getBeginningLightestNode(){return lightestNeighbour;}
node getNodeWhichMinimizesSum(const std::vector<edgeweight>& vcWeights, const std::vector<node>& vcMapped) {
assert(vcWeights.size() == vcMapped.size());
edgeweight minSum = std::numeric_limits<edgeweight>::max();
node cheapestNode = 0;
std::vector<size_t> mappedIndices;
mappedIndices.reserve(vcMapped.size());
for(auto n : vcMapped) mappedIndices.push_back(nodes.getIndexOf(n));
for(int nodeIndex = 0; nodeIndex < numberOfNodes; ++nodeIndex) {
if(concated[nodeIndex]) continue;
edgeweight sum = 0;
for(int i = 0; i < vcWeights.size(); ++i) {
edgeweight vcWeight = vcWeights[i];
auto mappedWeight = weights[nodeIndex][mappedIndices[i]];
sum = sum > vcWeight * mappedWeight ? sum : vcWeight * mappedWeight;
}
if(minSum > sum) {
minSum = sum;
cheapestNode = nodes[nodeIndex];
}
}
return cheapestNode;
}
node getLeftOverNode() {
for(int i = 0; i < concated.size(); ++i) if(!concated[i]) return nodes[i];
}
#ifdef DEBUG
void printEdges() {
std::cout << "concatnode: " << concatNode << " amt nodes: " << numberOfNodes - amountConcated << '\n';
for(int i = 0; i < numberOfNodes; ++i) {
std::cout << "node " << nodes[i] << ": ";
for(int j = 0; j < numberOfNodes; ++j) {
std::cout << weights[i][j] << " ";
}
std::cout << '\n';
}
}
};
#endif // DEBUG
}
#endif // PROCESSORGRAPHQUICKCONCAT_H