-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCC.cpp
More file actions
59 lines (47 loc) · 698 Bytes
/
Copy pathCC.cpp
File metadata and controls
59 lines (47 loc) · 698 Bytes
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
#include "CC.h"
CC::CC(UnDirectedGraph& graph)
{
this->graph = graph;
vecCC.resize(graph.GetV(), -1);
marked.resize(graph.GetV(), false);
vecSize.resize(graph.GetV(), 0);
nCount = 0;
for (int v = 0; v < graph.GetV(); v++)
{
if (!marked[v])
{
DFS(v);
nCount++;
}
}
}
void CC::DFS(int v)
{
marked[v] = true;
vecCC[v] = nCount;
vecSize[nCount]++;
for (int w : graph.adj(v))
{
if (!marked[w])
{
DFS(w);
}
}
}
bool CC::connect(int v, int w)
{
validVertex(v);
validVertex(w);
return vecCC[v] == vecCC[w];
}
void CC::validVertex(int v)
{
if (v < 0 || v > graph.GetV())
{
throw "Invalid vertex";
}
}
int CC::size(int v)
{
return vecSize[vecCC[v]];
}