Question: Java question SHow code and explain! Link : http://stackoverflow.com/questions/8922060/how-to-trace-the-path-in-a-breadth-first-search I want to transfer python code to java: # graph is in adjacent list representation graph
Java question SHow code and explain!
Link : http://stackoverflow.com/questions/8922060/how-to-trace-the-path-in-a-breadth-first-search
I want to transfer python code to java:
# graph is in adjacent list representation graph = { '1': ['2', '3', '4'], '2': ['5', '6'], '5': ['9', '10'], '4': ['7', '8'], '7': ['11', '12'] } def bfs(graph, start, end): # maintain a queue of paths queue = [] # push the first path into the queue queue.append([start]) while queue: # get the first path from the queue path = queue.pop(0) # get the last node from the path node = path[-1] # path found if node == end: return path # enumerate all adjacent nodes, construct a new path and push it into the queue for adjacent in graph.get(node, []): new_path = list(path) new_path.append(adjacent) queue.append(new_path) print bfs(graph, '1', '11') result = 1,4,7,11
In the bfs(graph, start, end) , start and end must String type.
SHow code !
Thanks!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
