Skip to content
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 @@ -7,7 +7,8 @@
"dijkstra": "Dijkstra",
"bellman_ford": "Bellman-Ford",
"floyd_warshall": "Floyd-Warshall",
"topological_sort": "Topological-Sort"
"topological_sort": "Topological-Sort",
"bridges": "Find-Bridges"
}
},
"mst": {
Expand Down
16 changes: 16 additions & 0 deletions algorithm/graph_search/bridges/desc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"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",
"Applications": [
"Find vulnerabilities in Graphs and Electrical Circuits"
],
"Complexity": {
"time": "worst O(|E|.(|V|+|E|))",
"space": "worst O(|V|.|E|)"
},
"References": [
"<a href='https://en.wikipedia.org/wiki/Bridge_(graph_theory)'>Wikipedia</a>"
],
"files": {
"naive": "Find all the bridges in an Undirected Graph"
}
}
66 changes: 66 additions & 0 deletions algorithm/graph_search/bridges/naive/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//Depth First Search Exploration Algorithm to test connectedness of the Graph (see Graph Algorithms/DFS/exploration), without the tracer & logger commands
function DFSExplore (graph, source) {
var stack = [ [source, null] ], visited = {};
var node, prev, i, temp;

while (stack.length > 0) {
temp = stack.pop ();
node = temp [0];
prev = temp [1];

if (!visited [node]) {
visited [node] = true;
//logger._print (node);

/*
if (prev !== undefined && graph [node] [prev]) { tracer._visit (node, prev)._wait (200); console.log ('tracer ' + prev + ', ' + node); }
else { tracer._visit (node)._wait (200); console.log ('tracer ' + node); }
*/

for (i = 0; i < graph.length; i++) {
if (graph [node] [i]) {
stack.push ([i, node]);
}
}
}
}

return visited;
}

function findBridges (graph) {
var tempGraph, bridges = [], visited;

for (var i = 0; i < graph.length; i++) {
for (var j = 0; j < graph.length; j++) {
if (graph [i] [j]) { //check if an edge exists
logger._print ('Deleting edge ' + i + '->' + j + ' and calling DFSExplore ()');
tracer._visit (j, i)._wait (200);
tracer._leave (j, i)._wait (200);

tempGraph = JSON.parse (JSON.stringify (graph));
tempGraph [i] [j] = 0;
tempGraph [j] [i] = 0;
visited = DFSExplore (tempGraph, 0);

if (Object.keys (visited).length === graph.length) {
logger._print ('Graph is CONNECTED. Edge is NOT a bridge');
}
else {
logger._print ('Graph is DISCONNECTED. Edge IS a bridge');
bridges.push ([i,j]);
}
}
}
}

return bridges;
}

var bridges = findBridges (G);

logger._print ('The bridges are: ');
for (var i in bridges) {
logger._print (bridges [i] [0] + ' to ' + bridges [i] [1]);
}
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');
12 changes: 12 additions & 0 deletions algorithm/graph_search/bridges/naive/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var tracer = new UndirectedGraphTracer ();
var logger = new LogTracer ();
var G = [
[0,1,0,0,0,0],
[1,0,0,1,1,0],
[0,0,0,1,0,0],
[0,1,1,0,1,1],
[0,1,0,1,0,0],
[0,0,0,1,0,0]
];

tracer._setData (G);