fork download
  1. def func():
  2.  
  3. def dfs( node ):
  4.  
  5. print("%d "% (node+1), end = "")
  6.  
  7. explored[node] = 1
  8.  
  9. for i in range(0, nodes):
  10.  
  11. if matrix[node][i] == 1 and explored[i] == 0:
  12.  
  13. dfs( i )
  14.  
  15.  
  16. nodes = int(input())
  17.  
  18. matrix = []
  19.  
  20. for i in range(nodes):
  21.  
  22. line = [int(i) for i in input().split(" ")]
  23.  
  24. matrix.append(line)
  25.  
  26. explored = [0] * (nodes+1)
  27.  
  28. startNode = 1
  29.  
  30. print("Depth First Search")
  31.  
  32. dfs( startNode )
  33.  
  34. func()
Success #stdin #stdout 0.04s 9812KB
stdin
8
0 1 1 0 0 0 0 0
1 0 1 1 0 0 0 0
1 1 0 1 1 1 0 1
0 1 1 0 1 0 1 0
0 0 1 1 0 1 1 0
0 0 1 0 1 0 1 1
0 0 0 0 1 1 0 1
0 0 1 0 0 1 1 0
stdout
Depth First Search
2 1 3 4 5 6 7 8