Skip to content

Added Tarjan algorithm to find SCC #227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion algorithm/category.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"dijkstra": "Dijkstra",
"floyd_warshall": "Floyd-Warshall",
"page_rank": "PageRank Algorithm",
"topological_sort": "Topological-Sort"
"topological_sort": "Topological-Sort",
"tarjan": "Tarjan"
},
"name": "Graph Search"
},
Expand Down
90 changes: 90 additions & 0 deletions algorithm/graph_search/tarjan/basic/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
function SCCVertex(u, disc, low, st, stackMember, carry)
{
graphTracer._visit(u)._wait();

disc[u] = ++carry.time;
discTracer._notify(u, carry.time)._wait();

low[u] = carry.time;
lowTracer._notify(u, carry.time)._wait();

st.push(u);
stTracer._setData(st)._wait();

stackMember[u] = true;
stackMemberTracer._notify(u, true)._wait();

// Go through all vertices adjacent to this
for (var v = 0; v < G[u].length; v++) {
if (G[u][v]) {

// If v is not visited yet, then recur for it
if (disc[v] == -1) {
SCCVertex(v, disc, low, st, stackMember, carry);

// Check if the subtree rooted with 'v' has a
// connection to one of the ancestors of 'u'
low[u] = Math.min(low[u], low[v]);
lowTracer._notify(u, low[u]);
}

// Update low value of 'u' only of 'v' is still in stack
// (i.e. it's a back edge, not cross edge).
else if (stackMember[v] == true) {
low[u] = Math.min(low[u], disc[v]);
lowTracer._notify(u, low[u])._wait();
}

}
}

// head node found, pop the stack and print an SCC
var w = 0; // To store stack extracted vertices
if (low[u] == disc[u]) {

while (st[st.length-1] != u) {
w = st.pop();
stTracer._setData(st)._wait();

logger._print(w)._wait();

stackMember[w] = false;
stackMemberTracer._notify(w, false)._wait();
}

w = st.pop();
stTracer._setData(st)._wait();

logger._print(w)._wait();
logger._print('------');

stackMember[w] = false;
stackMemberTracer._notify(w, false)._wait();
}
}

function SCC()
{
var disc = new Array(G.length);
var low = new Array(G.length);
var stackMember = new Array(G.length);
var st = [];
var carry = { time: 0 };

for (var i = 0; i < G.length; i++) {
disc[i] = -1;
low[i] = -1;
stackMember[i] = false;
}

discTracer._setData(disc);
lowTracer._setData(low);
stackMemberTracer._setData(stackMember);
stTracer._setData(st);

for (var i = 0; i < G.length; i++) {
if (disc[i] == -1) {
SCCVertex(i, disc, low, st, stackMember, carry);
}
}
}
20 changes: 20 additions & 0 deletions algorithm/graph_search/tarjan/basic/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var G = [
[0,0,1,1,0,0],
[1,0,0,0,0,0],
[0,1,0,0,0,0],
[0,0,0,1,0,0],
[0,0,0,0,0,1],
[0,0,0,0,1,0]
];

var graphTracer = new DirectedGraphTracer();
graphTracer._setData(G);

var discTracer = new Array1DTracer('Disc');
var lowTracer = new Array1DTracer('Low');
var stackMemberTracer = new Array1DTracer('stackMember');
var stTracer = new Array1DTracer('st');

var logger = new LogTracer();

SCC();
12 changes: 12 additions & 0 deletions algorithm/graph_search/tarjan/desc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"Tarjan": "Tarjan's algorithm is an algorithm in graph theory for finding the strongly connected components of a graph",
"Complexity": {
"time": "worst $O(|V|+|E|)$"
},
"References": [
"<a href='https://www.wikiwand.com/en/Tarjan%27s_strongly_connected_components_algorithm'>Wikipedia</a>"
],
"files": {
"basic": "Find the strongly connected components of a graph"
}
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy