-
Notifications
You must be signed in to change notification settings - Fork 0
/
GomoryHuTree.h
113 lines (90 loc) · 2.89 KB
/
GomoryHuTree.h
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
//
// Created by kopcion on 08.04.2020.
//
#ifndef GOMORYHU_GOMORYHUTREE_H
#define GOMORYHU_GOMORYHUTREE_H
#include <vector>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include "MinCuts/MinCutFunc.h"
#include "utils.h"
using namespace std;
/*
* For any edge u,v with capacity c in Input Graph, there is edge v,u with capacity >= 0
* */
class GomoryHuTree {
public:
GomoryHuTree(vector<unordered_map<int,int> >, MinCutFunc**, int num);
int getMinCut(int source, int target) {
vector<bool> visited(cutTree.size(), false);
return dfs(source, target, INT32_MAX, visited);
}
void transformTree(){
vector<unordered_map<int, int> > newTree(cutTree.size());
for(int i=1; i < cutTree.size(); ++i){
for(auto v : cutTree[i]){
newTree[*verticesInNodes[i].begin()][*verticesInNodes[v.first].begin()] = v.second;
}
}
cutTree = move(newTree);
}
int dfs(int vertex, int target, int currentFlow, vector<bool> &visited) {
visited[vertex] = true;
if(vertex == target) {
return currentFlow;
}
for(auto& v : cutTree[vertex]){
if(visited[v.first]) continue;
int flow = dfs(v.first, target, min(currentFlow, v.second), visited);
if(flow > 0){
return flow;
}
}
return -1;
}
void printGraph(){
printf("\nPrinting graph\n");
int i=0;
for(auto vertex : cutTree){
if(i==0){
i++;
continue;
}
cout<<"vertex: "<<i++<<endl;
for(auto edge : vertex){
cout<<"\tto: "<<edge.first<<" with cap: "<<edge.second<<endl;
}
cout<<endl;
}
cout<<"";
}
int getComponents(int v, vector<int>& component){
vector<bool> visited(cutTree.size(), false);
visited[v] = true;
int compNo = 1;
for(auto u : cutTree[v]){
if(u.second > 0) dfs(u.first, compNo++, component, visited);
}
return compNo - 1;
}
void dfs(int v, int compNo, vector<int>& component, vector<bool>& visited){
if(visited[v]) return;
visited[v] = true;
component[v] = compNo;
for(auto u : cutTree[v]){
dfs(u.first, compNo, component, visited);
}
}
pair<int,int> getSourceTarget(int);
void constructContracted(int, int, vector<unordered_map<int,int> >&, vector<int>&, vector<unordered_map<int,int> >&, vector<int>&);
private:
vector<int> superNodes;
vector<unordered_map<int,int> > cutTree;
unordered_map<int, set<int> > verticesInNodes;
priority_queue<pair<int,int> > treeNodes;
int NUM_OF_THREADS;
void splitRoot(pair<int, int>, int, set<int>&, vector<int>&, vector<int>&);
};
#endif //GOMORYHU_GOMORYHUTREE_H