Skip to content

Commit

Permalink
Added solution 262
Browse files Browse the repository at this point in the history
  • Loading branch information
WHAHA-HA committed Aug 8, 2020
1 parent 049bd85 commit 67516fd
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Problems/262.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
PROBLEM 262:

A bridge in a connected (undirected) graph is an edge that, if removed, causes the graph to become disconnected. Find all the bridges in a graph.
90 changes: 90 additions & 0 deletions Solutions/262.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
'''
Problem:
A bridge in a connected (undirected) graph is an edge that, if removed, causes the
graph to become disconnected. Find all the bridges in a graph.
'''

from sys import maxsize
from typing import Dict, List, Optional, Tuple

# local import from the DataStructure module
from DataStructures.Graph import GraphUndirectedUnweighted


def get_bridges_helper(self,
node: int,
visited: set,
parent: Dict[int, Optional[int]],
low: Dict[int, int],
disc: Dict[int, int],
bridges: List[Tuple[int, int]]) -> None:
# DFS based helper function to find all bridges
visited.add(node)
disc[node] = self.time
low[node] = self.time
self.time += 1
for neighbour in self.connections[node]:
if neighbour not in visited:
parent[neighbour] = node
self.get_bridges_helper(neighbour, visited, parent, low, disc, bridges)
# check if the subtree rooted with neighbour has a connection to one of the
# ancestors of node
low[node] = min(low[node], low[neighbour])
# if the lowest vertex reachable from subtree under neighbour is below node
# in DFS tree, then node-neighbour is a bridge
if low[neighbour] > disc[node]:
bridges.append((node, neighbour))
elif neighbour != parent[node]:
# update low value of node for parent function calls.
low[node] = min(low[node], disc[neighbour])


def get_bridges(self) -> List[Tuple[int, int]]:
# function to get all the bridges in a graph
visited = set()
disc = {node: maxsize for node in self.connections}
low = {node: maxsize for node in self.connections}
parent = {node: None for node in self.connections}
bridges = []
self.time = 0
for node in self.connections:
if node not in visited:
self.get_bridges_helper(node, visited, parent, low, disc, bridges)
return bridges


# adding the required methods and attributes to the graph class
setattr(GraphUndirectedUnweighted, 'get_bridges_helper', get_bridges_helper)
setattr(GraphUndirectedUnweighted, 'get_bridges', get_bridges)
setattr(GraphUndirectedUnweighted, 'time', 0)


if __name__ == "__main__":
g1 = GraphUndirectedUnweighted()
g1.add_edge(1, 0)
g1.add_edge(0, 2)
g1.add_edge(2, 1)
g1.add_edge(0, 3)
g1.add_edge(3, 4)
print("Bridges in first graph:")
print(*g1.get_bridges())

g2 = GraphUndirectedUnweighted()
g2.add_edge(0, 1)
g2.add_edge(1, 2)
g2.add_edge(2, 3)
print("\nBridges in second graph:")
print(*g2.get_bridges())

g3 = GraphUndirectedUnweighted ()
g3.add_edge(0, 1)
g3.add_edge(1, 2)
g3.add_edge(2, 0)
g3.add_edge(1, 3)
g3.add_edge(1, 4)
g3.add_edge(1, 6)
g3.add_edge(3, 5)
g3.add_edge(4, 5)
print("\nBridges in third graph:")
print(*g3.get_bridges())

0 comments on commit 67516fd

Please sign in to comment.