Applying BFS

Report a typo

Below is an undirected graph:

Applying BFS

Apply the BFS procedure for the graph using II as a starting node. As an answer, print edges of the resulting spanning tree. Edges' order does not matter, but the two nodes of each edge must be in the order of motion (lower distance to higher distance). The last line of your answer should contain the shortest distance from II to every node of the graph (nodes are sorted in alphabetical order, the shortest distance from II to II is 0).

Below is an example that clarifies the expected output format:

A B
B C
C D
0 1 2 3

Here, the first three lines correspond to three edges of a spanning tree (an edge from AA to BB, an edge from BB to CC and an edge from CC to DD), while the last line corresponds to the shortest distance from the starting node to every node of the graph. The starting node is AA, the shortest distance from AA to AA is 0, from AA to BB – 1, from AA to CC – 2, from AA to DD – 3.

Enter a short text
___

Create a free account to access the full topic