-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreachable_node.py
32 lines (23 loc) · 913 Bytes
/
reachable_node.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
def reachable(adj_list, start_node):
""" Compute the nodes reachable from a start node
For the above example, reachable([[1, 3], [2], [0], [4], [3], []], 0)) must return {0, 1, 2, 3, 4}
and reachable([[1, 3], [2], [0], [4], [3], []], 3) must return {3, 4}
Parameters:
-----------
adj_list : the adjacency list of the graph
start_node: the index of the start node
Returns:
--------
reachable: set(int) the set of nodes reachable from start_node
"""
reachable_node = set()
def reachable_search(node):
for i in range (len(adj_list[node])):
if (adj_list[node][i] in reachable_node):
break
else:
reachable_node.add(adj_list[node][i])
reachable_search(adj_list[node][i])
reachable_node.add(node)
reachable_search(start_node)
return reachable_node