Skip to content

Instantly share code, notes, and snippets.

@MLWhiz
Created January 20, 2019 18:02
Show Gist options
  • Save MLWhiz/ea9653b8b9571b82a7d5ac4489352085 to your computer and use it in GitHub Desktop.
Save MLWhiz/ea9653b8b9571b82a7d5ac4489352085 to your computer and use it in GitHub Desktop.
def min_num_edges_between_nodes(graph,start_node):
distance = 0
shortest_path = []
queue = [start_node] #FIFO
levels = {}
levels[start_node] = 0
shortest_paths = {}
shortest_paths[start_node] = ":"
visited = [start_node]
while len(queue)!=0:
start = queue.pop(0)
neighbours = graph[start]
for neighbour,_ in neighbours.iteritems():
if neighbour not in visited:
queue.append(neighbour)
visited.append(neighbour)
levels[neighbour] = levels[start]+1
shortest_paths[neighbour] = shortest_paths[start] +"->"+ start
return levels, shortest_paths
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment