Breadth-First-Search


Submit solution

Points: 1
Time limit: 2.0s
Python 10.0s
Memory limit: 1G

Author:
Problem types

No hidden tricks here, read in an adjacency matrix for an unweighted but directed graph (no self-edges). Output the BFS traversal order starting from vertex \(0\) in numerically increasing node labels. Graphs are labelled from \(0\) to \(V\).

Input Specification

The first line contains a single integer \(V\), the number of vertices in the graph.

The next \(V\) lines will contain \(V\) space-separated integers specifying an adjacency matrix.

For any element \(v_{i,j}\), a \(0\) indicates no edge from \(u\) to \(v\), a \(1\) indicates there is an edge from \(u\) to \(v\).

Output Specification

A single line of space-separated integers, the labels of each vertex in the order they are discovered in the search. If there are more than two valid searches, output the lexicographically least ordering of nodes (the one in increasing label order). If a node is not reached from vertex \(0\), do not print it.

Bounds

This problem has two sub-problems, the first is the test data, the second has the following bounds.

  • \(1 \leq V \leq 2000\)
  • \(v_{i,j} \in \{0,1\}\)

Sample Input 1

4
0 0 0 1 
0 0 1 0 
0 1 0 1 
1 0 1 0

Sample Output 1

0 3 2 1

Sample Input 2

4
0 0 0 1 
1 0 0 0 
0 0 0 0 
1 1 0 0

Sample Output 2

0 3 1

Comments

There are no comments at the moment.