-
Notifications
You must be signed in to change notification settings - Fork 0
/
Connectivity.hpp
193 lines (172 loc) · 4.79 KB
/
Connectivity.hpp
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*!
* \file Connectivity.h
* \author Quentin Fortier
* Connectivity algorithms
*/
#ifndef CONNECT_H
#define CONNECT_H
#include <vector>
#include <queue>
#include <set>
#include <assert.h>
#include "Structures.h"
#include "Search.h"
#include "Flow.h"
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
#include "IO.h"
namespace sgl
{
template<class Edge = Edge_Base, class Graph = Graph_List<Edge> >
class Splitting
{
public:
static int f(int a, int b) { return a >= b ? a-1 : a; }
// Complete splitting at v. G_split has G.V() - 1 vertices
static void split(const Graph &G, Graph &G_split1, Graph &G_split2, int v)
{
// Trouver les edges concernés
Edge *e1, *e2, *e_1, *e_2;
e1 = e2 = e_1 = e_2 = NULL;
Graph::iterator_all it(G);
for(Edge *e = it.beg(); !it.end(); e = it.nxt())
{
if(e->w() == v)
{
if(e1 == NULL) e1 = e;
else e2 = e;
}
else if(e->v() == v)
{
if(e_1 == NULL) e_1 = e;
else e_2 = e;
}
else
{
G_split1.insert(new Edge(f(e->v(), v), f(e->w(), v)));
G_split2.insert(new Edge(f(e->v(), v), f(e->w(), v)));
}
}
G_split1.insert(new Edge(f(e1->v(), v), f(e_1->w(), v)));
G_split1.insert(new Edge(f(e2->v(), v), f(e_2->w(), v)));
G_split2.insert(new Edge(f(e1->v(), v), f(e_2->w(), v)));
G_split2.insert(new Edge(f(e2->v(), v), f(e_1->w(), v)));
}
};
template<class Edge = Edge_Base, class Graph = Graph_List<Edge> >
class Connectivity
{
private:
const Graph &G;
class AvoidVertex : public Proc_Base<Edge>
{
int avoid;
public:
inline int get_target() { return target; }
void set_avoid(int t) { avoid = t; }
AvoidVertex(int V, int avoid) : Proc_Base<Edge>(V), avoid(avoid) { }
inline bool trait(int v) { return false; }
bool toVisit(Edge *e, int toVertex)
{
if(!tPred.isolated(toVertex) || toVertex == avoid)
return false;
tPred.insert(e, toVertex);
return true;
}
};
public:
Connectivity(const Graph &G): G(G) { }
bool isConnected(int forbidden = -1)
{
AvoidVertex proc(G.V(), forbidden);
DFS<Edge, AvoidVertex, Graph> dfs(G, proc);
for(int v = 0; v < G.V(); v++)
{
if(v == forbidden) continue;
dfs(v);
for(int w = 0; w < G.V(); w++)
{
if(w == v || w == forbidden)
continue;
if(proc.tPred.isolated(w))
{
cout<<w<<" non accessible depuis "<<v<<" si on supprime "<<forbidden<<endl;
return false; // w n'est pas accessible
}
}
}
return true;
}
bool is2VertexConnected()
{
for(int v = 0; v < G.V(); v++)
if(!isConnected(v))
return false;
return true;
}
int vertex_connectivity()
{
Graph_List<Edge_Flow<int> > G_(2*G.V(), false); // on double les sommets
Graph_List<Edge>::iterator_all it(G);
for(Edge *e = it.beg(); !it.end(); e = it.nxt())
G_.insert(new Edge_Flow<>(G.V() + e->v(), e->w(), G_.V()*G_.V()));
for(int i = 0; i < G.V(); i++)
G_.insert(new Edge_Flow<>(i, G.V() + i, 1));
int min_flow = -1;
for(int s = G.V(); s < G_.V(); s++)
for(int t = 0; t < G.V(); t++)
{
if(s == t) continue;
Graph_List<Edge_Flow<int> >::iterator_all it(G_);
for(Edge_Flow<int> *e = it.beg(); !it.end(); e = it.nxt())
e->set_flow(0);
//IO<Edge_Flow<int>, Graph_List<Edge_Flow<int> >>::show_capR(G_);
NoNullCap<> noNull(G_.V(), t);
Fulkerson<int, Edge_Flow<> > ful(G_, noNull, s, t);
ful(0);
int flow = ful.get_outflow();
//IO<Edge_Flow<int>, Graph_List<Edge_Flow<int> >>::show_flow(G_);
if(min_flow == -1 || min_flow > flow)
min_flow = flow;
/*if(flow < 3)
cout<<s<<"-"<<t<<endl;*/
}
return min_flow;
}
int edge_connectivity()
{
Graph_List<Edge_Flow<int> > G_(G.V(), false); // non orienté
Graph_List<Edge>::iterator_all it(G);
for(Edge *e = it.beg(); !it.end(); e = it.nxt())
G_.insert(new Edge_Flow<>(e->v(), e->w(), 1));
int min_flow = -1;
for(int s = 0; s < G_.V(); s++)
for(int t = 0; t < G_.V(); t++)
{
if(s == t) continue;
Graph_List<Edge_Flow<int> >::iterator_all it(G_);
for(Edge_Flow<int> *e = it.beg(); !it.end(); e = it.nxt())
e->set_flow(0);
NoNullCap<> noNull(G_.V(), t);
Fulkerson<int, Edge_Flow<> > ful(G_, noNull, s, t);
ful(0);
int flow = ful.get_outflow();
//IO<Edge_Flow<int>, Graph_List<Edge_Flow<int> >>::show_flow(G_);
if(min_flow == -1 || min_flow > flow)
min_flow = flow;
//cout<<s<<"-"<<t<<" flow: "<<flow<<endl;
/* if(flow < 3)
{
Cut_Vertices<> cut(G_);
cut(1);
cout<<"Edges in the computed min cut:"<<endl;
for(std::list<Edge_Flow<>*>::iterator it = cut.cut.begin(); it != cut.cut.end(); ++it)
cout<<(*it)->v()<<" "<<(*it)->w()<<endl;
system("pause");
}*/
}
return min_flow;
}
};
}
#endif