forked from zzjjzzgggg/cpplib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
38a1b00
commit 619b762
Showing
6 changed files
with
198 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
--- | ||
BasedOnStyle: Google | ||
ColumnLimit: 79 | ||
ColumnLimit: 80 | ||
IndentWidth: 4 | ||
--- | ||
Language: Cpp | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/** | ||
* Copyright (C) by J.Z. (03/31/2018 16:50) | ||
* Distributed under terms of the MIT license. | ||
*/ | ||
|
||
#ifndef __CNCOM_H__ | ||
#define __CNCOM_H__ | ||
|
||
#include "comm.h" | ||
|
||
namespace graph { | ||
|
||
/** | ||
* Strongly connected componetn DFS visitor. Using Tarjan's SCC Algorithm. Refer | ||
* to Knuth's book "The Stanford Graph Base", pp.512-519. | ||
*/ | ||
template <class Graph> | ||
class SCCVisitor { | ||
public: | ||
class Element { | ||
public: | ||
int id, parent, rank, min, edge, deg; | ||
|
||
public: | ||
Element(int v = -1, int r = -1, int d = 0) | ||
: id(v), parent(-1), rank(r), min(v), edge(0), deg(d) {} | ||
}; /* Node */ | ||
|
||
private: | ||
const Graph& graph_; | ||
int rank_; | ||
std::unordered_map<int, Element> record_; | ||
std::stack<int> stack_; | ||
|
||
public: | ||
std::vector<std::pair<int, int>> nd_cc_vec_; // node-component pairs | ||
|
||
public: | ||
SCCVisitor(const Graph& graph) : graph_(graph), rank_(0) { | ||
nd_cc_vec_.reserve(graph_.getNodes()); | ||
} | ||
|
||
void performDFS() { | ||
for (auto it = graph_.beginNI(); it != graph_.endNI(); it++) { | ||
int v = it->first; | ||
if (record_.find(v) == record_.end()) { | ||
makeActive(v); | ||
while (v != -1) v = explore(v); | ||
} | ||
} | ||
// topology sort | ||
std::reverse(nd_cc_vec_.begin(), nd_cc_vec_.end()); | ||
} | ||
|
||
/** | ||
* Make vertex v active. | ||
*/ | ||
void makeActive(const int v, const int parent = -1) { | ||
record_[v] = Element(v, ++rank_, graph_[v].getOutDeg()); | ||
if (parent != -1) record_[v].parent = parent; | ||
stack_.push(v); | ||
} | ||
|
||
/** | ||
* Explore one step from current vertex v, possible move to another current | ||
* vertex and calling it v. Return the next vertex to be explored. | ||
*/ | ||
int explore(const int v) { | ||
Element& ve = record_[v]; | ||
if (ve.edge != ve.deg) { // v has untagged edge | ||
const int u = graph_[v].getOutNbr(ve.edge++); | ||
if (record_.find(u) != record_.end()) { // seen u before | ||
int u_rank = record_[u].rank, v_min_rank = record_[ve.min].rank; | ||
if (u_rank < v_min_rank) ve.min = u; | ||
return v; | ||
} else { // u is presently unseen | ||
makeActive(u, v); | ||
// u become the current vertex, i.e., the node to be explored | ||
return u; | ||
} | ||
} else { // all edges from v are tagged, so v matures | ||
int u = ve.parent; | ||
if (ve.min == v) { // subtree with root v forms a SCC | ||
finish(v); | ||
} else { // the edge from u to v has just matured, make v.min | ||
// visible from u | ||
int v_min_rank = record_[ve.min].rank, &u_min = record_[u].min, | ||
u_min_rank = record_[u_min].rank; | ||
if (v_min_rank < u_min_rank) u_min = ve.min; | ||
} | ||
return u; | ||
} | ||
} | ||
|
||
/** | ||
* Remove v and all its successors on the active stack from the tree, and | ||
* mark them as a SCC. | ||
*/ | ||
void finish(const int v) { | ||
while (true) { | ||
int t = stack_.top(); | ||
stack_.pop(); | ||
nd_cc_vec_.emplace_back(t, v); | ||
record_[t].rank = INT_MAX; | ||
record_[t].parent = v; | ||
if (t == v) break; | ||
} | ||
} | ||
|
||
/** | ||
* Print out one representative edge between two SCCs. | ||
*/ | ||
void getConnections(std::vector<std::pair<int, int>>& cc_edge_vec) { | ||
int cur_cc = -1; | ||
std::unordered_set<int> discovered_ccs; | ||
for (auto&& it = nd_cc_vec_.begin(); it != nd_cc_vec_.end(); it++) { | ||
int v = it->first; | ||
int cc_from = it->second; | ||
if (cc_from != cur_cc) { | ||
cur_cc = cc_from; | ||
discovered_ccs.clear(); | ||
} | ||
discovered_ccs.insert(cc_from); | ||
for (int d = 0; d < record_[v].deg; d++) { | ||
int cc_to = record_[graph_[v].getOutNbr(d)].parent; | ||
if (discovered_ccs.find(cc_to) == discovered_ccs.end()) { | ||
discovered_ccs.insert(cc_to); | ||
cc_edge_vec.emplace_back(cc_from, cc_to); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
|
||
} /* namespace graph */ | ||
#endif /* __CNCOM_H__ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,4 +13,6 @@ | |
#include "triad.h" | ||
#include "network.h" | ||
|
||
#include "cncom.h" | ||
|
||
#endif /* __GRAPH_H__ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters