Skip to content

Commit d9ae635

Browse files
committed
graph bridges problem
1 parent d5ce1d8 commit d9ae635

File tree

4 files changed

+120
-1
lines changed

4 files changed

+120
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ WIP, the descriptions of the below `unsolved yet` problems can be found in the [
2222
- [] Minimum Spanning tree (Kruskal)
2323
- [] Topological Sort
2424
- [x] [Boggle (Find all possible words in a board of characters)](https://github.com/danrusei/algorithms_with_Go/tree/main/graph/boggle)
25-
- [] Bridges in a Graph
25+
- [x] [Bridges in a Graph](https://github.com/danrusei/algorithms_with_Go/tree/main/graph/bridges)
2626

2727
## [Linked List](https://github.com/danrusei/algorithms_with_Go/tree/main/linkedlist)
2828

graph/bridges/README.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Bridges in a graph
2+
3+
Problem description: [GeeksforGeeks](https://www.geeksforgeeks.org/bridge-in-a-graph/amp/)
4+
5+
An edge in an undirected connected graph is a bridge if removing it disconnects the graph. For a disconnected undirected graph, definition is similar, a bridge is an edge removing which increases number of disconnected components.
6+
Bridges represent vulnerabilities in a connected network and are useful for designing reliable networks. For example, in a wired computer network, an articulation point indicates the critical computers and a bridge indicates the critical wires or connections.
7+
8+
![find the bridges](bridge.png)
9+
10+
## Algorithm
11+
12+
A simple approach is to one by one remove all edges and see if removal of an edge causes disconnected graph.
13+
14+
* For every edge (u, v), do following
15+
* remove (u, v) from graph
16+
* check if the graph remains connected (We can either use BFS or DFS)
17+
* add (u, v) back to the graph and iterate for other edge
18+
19+
This is a brute force algorithm with high time complexity, read the alternate solution from geeksforgeeks.
20+
21+
## Result
22+
23+
```bash
24+
$ go run main.go
25+
Edge 0 --- 3 is a bridge
26+
Edge 3 --- 4 is a bridge
27+
```

graph/bridges/bridge.png

14.4 KB
Loading

graph/bridges/main.go

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/danrusei/algorithms_with_Go/graph"
7+
)
8+
9+
type bridges struct {
10+
*graph.Graph
11+
}
12+
13+
func (g *bridges) findBridge() {
14+
15+
totalNodes := len(g.Nodes)
16+
17+
for _, node := range g.Nodes {
18+
for _, edge := range g.Edges[node] {
19+
//remove the (u, v) edge from graph
20+
g.RemoveEdge(node, edge.ToNode)
21+
22+
//check if bfs traversal returns the same number of nodes as expected
23+
if g.BFStraverse(node) != totalNodes {
24+
fmt.Printf("Edge %v --- %v is a bridge\n", edge.ToNode, node)
25+
}
26+
27+
// add again the (u, v) edge to graph, in order to check the other edges as well
28+
g.AddEdge(node, edge.ToNode)
29+
}
30+
}
31+
}
32+
33+
// BFS(Breadth First Search) traversal of the Graph
34+
// check out https://github.com/danrusei/algorithms_with_Go/tree/main/graph/traverse_bfs
35+
//for detailed explanation of the algorithm
36+
func (g *bridges) BFStraverse(n *graph.Node) int {
37+
// Create a queue for BFS
38+
queue := make([]*graph.Node, 0, len(g.Nodes))
39+
40+
queue = append(queue, n)
41+
visited := make(map[*graph.Node]bool)
42+
43+
for len(queue) > 0 {
44+
node := queue[0]
45+
//pop out the Node from the queue
46+
queue = queue[1:]
47+
// add the Node to visited map, ensuring that it will not be added again to the queue
48+
// this prevents to loop endlesssly through the nodes
49+
visited[node] = true
50+
51+
//edges are the links to other Nodes of the searching Node
52+
for _, edge := range g.Edges[node] {
53+
if !visited[edge.ToNode] {
54+
newNode := edge.ToNode
55+
queue = append(queue, newNode)
56+
visited[newNode] = true
57+
}
58+
59+
}
60+
61+
}
62+
return len(visited)
63+
}
64+
65+
func main() {
66+
// create nodes
67+
n0 := graph.Node{Value: "0"}
68+
n1 := graph.Node{Value: "1"}
69+
n2 := graph.Node{Value: "2"}
70+
n3 := graph.Node{Value: "3"}
71+
n4 := graph.Node{Value: "4"}
72+
73+
// create a new graph instance
74+
g := new(graph.Graph)
75+
76+
// add nodes to graph
77+
g.AddNode(&n0)
78+
g.AddNode(&n1)
79+
g.AddNode(&n2)
80+
g.AddNode(&n3)
81+
g.AddNode(&n4)
82+
83+
// add the edges(links) between nodes
84+
g.AddEdge(&n0, &n1)
85+
g.AddEdge(&n0, &n3)
86+
g.AddEdge(&n0, &n2)
87+
g.AddEdge(&n1, &n2)
88+
g.AddEdge(&n3, &n4)
89+
90+
s := &bridges{g}
91+
s.findBridge()
92+
}

0 commit comments

Comments
 (0)