Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Depth First Search - Find if path exists
""" Depth First Search Finding if path exists between two points in graph Author: Jayesh Chandrapal """ def readGraph(): graph = {} N = int(input()) for i in range(N): line = input() tokens = line.split() node = tokens[0] graph[node] = {} for i in range(1, len(tokens) - 1, 2): graph[node][tokens[i]] = int(tokens[i + 1]) return graph def __reachDFS(graph, current, visited): if current in graph: for neighbor in graph[current]: if neighbor not in visited: visited.add( neighbor ) __reachDFS(graph, neighbor, visited) def reachDFS(graph, start, goal): if start not in graph or goal not in graph: return False visited = set() visited.add( start ) __reachDFS(graph, start, visited) return goal in visited graph = readGraph() print( reachDFS(graph, 'Arad', 'Mumbai') )
run
|
edit
|
history
|
help
0
Circular loop for week.
reduce lambda
Ciklusok
line 4
sum of odd numbers
Pej
V
#converting list into set
thermal_containers
Greatest Common Factor (Euclidean algorithm, aka Euclid's algorithm)