-
Notifications
You must be signed in to change notification settings - Fork 3
/
scc.h
160 lines (137 loc) · 4.65 KB
/
scc.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
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
///////////////////////////////////////////////////////////////////
// Copyright (c) 2018 Rohit Sharma. All rights reserved.
// This program is free software; you can redistribute it and/or
// modify it under the terms as GNU General Public License.
///////////////////////////////////////////////////////////////////
// Strongly Connected Components:
// A graph is said to be strongly connected or diconnected if every vertex
// is reachable from every other vertex.
//
// Reference: https://en.wikipedia.org/wiki/Strongly_connected_component
// https://en.wikipedia.org/wiki/Kosaraju%27s_algorithm
#ifndef GRAPH_SCC_H
#define GRAPH_SCC_H
#pragma once
#include "transpose.h"
#include <map>
#include <stack>
using namespace std;
namespace SCC {
map<const basicGraph::bNode*, basicGraph::NODE_MARKS> nodeMarks_;
void resetMarks(const basicGraph::bGraph* graph) {
set<const basicGraph::bNode*, basicGraph::nodeCompare>::iterator niter = graph->nodeBegin();
for (; niter != graph->nodeEnd(); niter++) {
const basicGraph::bNode* node = *niter;
SCC::nodeMarks_[node] = basicGraph::NOT_VISITED;
}
return;
}
// Algorithm:
// 1. For each vertex u of the graph, mark u as unvisited.Let stack_ be empty.
// 2. For each vertex u of the graph do Visit(u), where Visit(u) is the recursive subroutine :
// If u is unvisited then :
// 1. Mark u as visited.
// 2. For each out - neighbour v of u, do Visit(v).
// 3. Prepend u to stack_.
// 3. Transpose graph.
// 4. For each element u of stack_ in order,
// If u has not been assigned to a component then :
// 1. explore all vertices connected to u with DFS and assign them to SCC group.
//
class kosaraju {
private:
const basicGraph::bGraph* graph_;
vector < vector<const basicGraph::bNode*> > listSCC_;
stack<const basicGraph::bNode*> stack_;
// fill up the stack in the reverse order or finishing time.
void first_dfs_pass(const basicGraph::bNode* src)
{
if (SCC::nodeMarks_[src] == basicGraph::VISITED)
return;
if (SCC::nodeMarks_[src] == basicGraph::VISITING) // detected cycle.
return;
SCC::nodeMarks_[src] = basicGraph::VISITING;
set<const basicGraph::bEdge*, basicGraph::edgeCompare>::iterator niter = src->edgeBegin();
for (; niter != src->edgeEnd(); niter++) {
const basicGraph::bNode* nextNode = (*niter)->otherNode(src);
if (SCC::nodeMarks_[nextNode] == basicGraph::NOT_VISITED)
first_dfs_pass(nextNode);
}
SCC::nodeMarks_[src] = basicGraph::VISITED;
stack_.push(src);
}
bool second_dfs_pass(const basicGraph::bNode* src, vector<const basicGraph::bNode*>& sccGroup)
{
if (SCC::nodeMarks_[src] == basicGraph::VISITED)
return false;
if (SCC::nodeMarks_[src] == basicGraph::VISITING) // detected cycle.
return false;
SCC::nodeMarks_[src] = basicGraph::VISITING;
set<const basicGraph::bEdge*, basicGraph::edgeCompare>::iterator niter = src->edgeBegin();
for (; niter != src->edgeEnd(); niter++) {
const basicGraph::bNode* nextNode = (*niter)->otherNode(src);
if (SCC::nodeMarks_[nextNode] == basicGraph::NOT_VISITED)
second_dfs_pass(nextNode, sccGroup);
}
SCC::nodeMarks_[src] = basicGraph::VISITED;
sccGroup.push_back(src);
return true; // found SCC
}
void build_dfs_stack()
{
set<const basicGraph::bNode*, basicGraph::nodeCompare>::iterator niter = graph_->nodeBegin();
for (; niter != graph_->nodeEnd(); niter++)
{
first_dfs_pass(*niter);
}
return;
}
void build_scc_list()
{
size_t scc_index = 0;
while (!stack_.empty())
{
if ( second_dfs_pass(stack_.top(), listSCC_[scc_index]) )
scc_index++;
stack_.pop();
}
return;
}
public:
kosaraju(const basicGraph::bGraph* graph) : graph_(graph), listSCC_(graph->nNodes())
{}
void build()
{
if (!graph_->directed())
{
cerr << "Error: Strongly connected can not be determined for undirected graph.\n";
return;
}
resetMarks(graph_);
build_dfs_stack();
transpose reverse(true);
reverse.build(graph_);
resetMarks(graph_);
build_scc_list();
// fix directed graph before leaving.
reverse.build(graph_);
return;
}
void print()
{
for (size_t i = 0; i < listSCC_.size(); i++)
{
if (listSCC_[i].size() == 0)
continue;
cout << "SCC Group " << i << " : ";
for (size_t j = 0; j < listSCC_[i].size(); j++)
{
cout << listSCC_[i][j]->name() << " ";
}
cout << "\n";
}
return;
}
};
};
#endif