Skip to content

Commit af5b276

Browse files
committed
added Critical Routers
1 parent d8db4ee commit af5b276

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

Graph/CriticalRouters/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Critical Routers
2+
3+
[Leetcode Link](https://leetcode.com/discuss/interview-question/436073/)
4+
5+
## Problem:
6+
7+
You are given an undirected **connected** graph. An articulation point (or cut vertex) is defined as a vertex which, when removed along with associated edges, makes the graph disconnected (or more precisely, increases the number of connected components in the graph). The task is to find all articulation points in the given graph.
8+
9+
**Input:**
10+
The input to the function/method consists of three arguments:
11+
12+
- `numNodes`, an integer representing the number of nodes in the graph.
13+
- `numEdges`, an integer representing the number of edges in the graph.
14+
- `edges`, the list of pair of integers - A, B representing an edge between the nodes A and B.
15+
16+
**Output:**
17+
Return a list of integers representing the critical nodes.
18+
19+
## Example:
20+
21+
```
22+
Input: numNodes = 7, numEdges = 7, edges = [[0, 1], [0, 2], [1, 3], [2, 3], [2, 5], [5, 6], [3, 4]]
23+
Output: [2, 3, 5]
24+
```
25+
26+
![example](./example.png)

Graph/CriticalRouters/example.png

23.1 KB
Loading

Graph/CriticalRouters/solution.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from collections import defaultdict
2+
3+
4+
def criticalRouters(numNodes, numEdges, edges):
5+
criticalVertices = list()
6+
adjList = defaultdict(list)
7+
# convert edges array form into adjacency list
8+
for edge in edges:
9+
adjList[edge[0]].append(edge[1])
10+
adjList[edge[1]].append(edge[0])
11+
# print(adjList)
12+
for source, destinations in adjList.items():
13+
if len(destinations) <= 1:
14+
continue
15+
firstDest = destinations[0]
16+
for dest in destinations[1:]:
17+
if not reachable(adjList, firstDest, dest, source):
18+
criticalVertices.append(source)
19+
break
20+
21+
return criticalVertices
22+
23+
# use BFS to see if 'src' can reach 'dest' without going through 'blockedVertex'
24+
# if false, that means 'blockedVertex' is a critical vertex
25+
26+
27+
def reachable(adjList, src, dest, blockedVertex):
28+
queue = list()
29+
visited = set()
30+
visited.add(blockedVertex)
31+
queue.append(src)
32+
while queue:
33+
current = queue.pop(0)
34+
if current == dest:
35+
return True
36+
visited.add(current)
37+
for neighbor in adjList[current]:
38+
if neighbor not in visited:
39+
queue.append(neighbor)
40+
return False
41+
42+
43+
numNodes = 7
44+
numEdges = 7
45+
edges = [[0, 1], [0, 2], [1, 3], [2, 3], [2, 5], [5, 6], [3, 4]]
46+
print(criticalRouters(numNodes, numEdges, edges))

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ Languages used: Java and Python
7878

7979
- [All Paths From Src To Target](Graph/AllPathsFromSrcToTarget)
8080
- [Course Schedule II](Graph/CourseSchedule2)
81+
- [Critical Routers](Graph/CriticalRouters)
8182
- [Escape Large Maze](Graph/EscapeLargeMaze)
8283
- [Minimum Cost to Repair Edges](Graph/MinCostToRepairEdges)
8384
- [Number to Make Network Connected](Graph/NumToMakeNetworkConnected)

0 commit comments

Comments
 (0)