Finding the shortest distances

Report a typo

Given below is an undirected graph in the format of an adjacency list:

A: [B, C]
B: [A, C, E]
C: [A, B, D, E]
D: [C, F, G]
E: [B, C, F]
F: [D, E, G]
G: [D, F]

Apply the BFS procedure using the node AA as the initial node and find the shortest distances to each node of the graph. Try not to draw the graph but use only the given representation. The expected output format is the following:

1 3 0 2 4

Here, 11 is the shortest distance from the start node to AA, 33 – the same for BB, and so on.

Hint: recall from the theory that in order to traverse the nodes in the increasing order of distance from the start node, you need to use the queue data structure.

Enter a short text
___

Create a free account to access the full topic