Depth-First-Search
No hidden tricks here, read in an adjacency matrix for an unweighted but directed graph (no self-edges). Output the iterative DFS traversal order. 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.
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 0 0
0 1 0 1
1 0 0 0
Sample Output 1
0 3
Sample Input 2
4
0 0 0 1
1 0 0 1
0 0 0 0
1 1 0 0
Sample Output 2
0 3 1
Comments