Skip to content

Commit d104334

Browse files
author
Raghav Dua
committed
Merge pull request algorithm-visualizer#54 from duaraghav8/master
Bridge-Finding in Undirected Graph
2 parents 7a001b0 + 0a7ab2e commit d104334

File tree

4 files changed

+96
-1
lines changed

4 files changed

+96
-1
lines changed

algorithm/category.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"dijkstra": "Dijkstra",
88
"bellman_ford": "Bellman-Ford",
99
"floyd_warshall": "Floyd-Warshall",
10-
"topological_sort": "Topological-Sort"
10+
"topological_sort": "Topological-Sort",
11+
"bridges": "Find-Bridges"
1112
}
1213
},
1314
"mst": {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"Bridges": "An edge in an undirected connected graph is a bridge iff removing it disconnects the graph. A naive solution to finding bridges in a graph is to:<br />1.Delete an edge E<br />2.Perform DFS Exploration to check is Graph is connected<br />3.Restore Edge E. E is a bridge only if DFS explore determines that the graph is disconnected without E",
3+
"Applications": [
4+
"Find vulnerabilities in Graphs and Electrical Circuits"
5+
],
6+
"Complexity": {
7+
"time": "worst O(|E|.(|V|+|E|))",
8+
"space": "worst O(|V|.|E|)"
9+
},
10+
"References": [
11+
"<a href='https://en.wikipedia.org/wiki/Bridge_(graph_theory)'>Wikipedia</a>"
12+
],
13+
"files": {
14+
"naive": "Find all the bridges in an Undirected Graph"
15+
}
16+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//Depth First Search Exploration Algorithm to test connectedness of the Graph (see Graph Algorithms/DFS/exploration), without the tracer & logger commands
2+
function DFSExplore (graph, source) {
3+
var stack = [ [source, null] ], visited = {};
4+
var node, prev, i, temp;
5+
6+
while (stack.length > 0) {
7+
temp = stack.pop ();
8+
node = temp [0];
9+
prev = temp [1];
10+
11+
if (!visited [node]) {
12+
visited [node] = true;
13+
//logger._print (node);
14+
15+
/*
16+
if (prev !== undefined && graph [node] [prev]) { tracer._visit (node, prev)._wait (200); console.log ('tracer ' + prev + ', ' + node); }
17+
else { tracer._visit (node)._wait (200); console.log ('tracer ' + node); }
18+
*/
19+
20+
for (i = 0; i < graph.length; i++) {
21+
if (graph [node] [i]) {
22+
stack.push ([i, node]);
23+
}
24+
}
25+
}
26+
}
27+
28+
return visited;
29+
}
30+
31+
function findBridges (graph) {
32+
var tempGraph, bridges = [], visited;
33+
34+
for (var i = 0; i < graph.length; i++) {
35+
for (var j = 0; j < graph.length; j++) {
36+
if (graph [i] [j]) { //check if an edge exists
37+
logger._print ('Deleting edge ' + i + '->' + j + ' and calling DFSExplore ()');
38+
tracer._visit (j, i)._wait (200);
39+
tracer._leave (j, i)._wait (200);
40+
41+
tempGraph = JSON.parse (JSON.stringify (graph));
42+
tempGraph [i] [j] = 0;
43+
tempGraph [j] [i] = 0;
44+
visited = DFSExplore (tempGraph, 0);
45+
46+
if (Object.keys (visited).length === graph.length) {
47+
logger._print ('Graph is CONNECTED. Edge is NOT a bridge');
48+
}
49+
else {
50+
logger._print ('Graph is DISCONNECTED. Edge IS a bridge');
51+
bridges.push ([i,j]);
52+
}
53+
}
54+
}
55+
}
56+
57+
return bridges;
58+
}
59+
60+
var bridges = findBridges (G);
61+
62+
logger._print ('The bridges are: ');
63+
for (var i in bridges) {
64+
logger._print (bridges [i] [0] + ' to ' + bridges [i] [1]);
65+
}
66+
logger._print ('NOTE: A bridge is both ways, i.e., from A to B and from B to A, because this is an Undirected Graph');
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var tracer = new UndirectedGraphTracer ();
2+
var logger = new LogTracer ();
3+
var G = [
4+
[0,1,0,0,0,0],
5+
[1,0,0,1,1,0],
6+
[0,0,0,1,0,0],
7+
[0,1,1,0,1,1],
8+
[0,1,0,1,0,0],
9+
[0,0,0,1,0,0]
10+
];
11+
12+
tracer._setData (G);

0 commit comments

Comments
 (0)