|
| 1 | +package org.algorithm_visualizer; |
| 2 | + |
| 3 | +class Test { |
| 4 | + static GraphTracer tracer = new GraphTracer().log(new LogTracer()); |
| 5 | + static int G[][] = { // G[i][j] indicates whether the path from the i-th node to the j-th node exists or not |
| 6 | + {0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 7 | + {0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0}, |
| 8 | + {0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0}, |
| 9 | + {0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0}, |
| 10 | + {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1}, |
| 11 | + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 12 | + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 13 | + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 14 | + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 15 | + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 16 | + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 17 | + }; |
| 18 | + |
| 19 | + static void DFS(int node, int parent) { // node = current node, parent = previous node |
| 20 | + tracer.visit(node, parent).delay(); |
| 21 | + for (int i = 0; i < G[node].length; i++) { |
| 22 | + if (G[node][i] == 1) { // if current node has the i-th node as a child |
| 23 | + DFS(i, node); // recursively call DFS |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + public static void main(String[] args) { |
| 29 | + tracer.set(G).layoutTree(0).delay(); |
| 30 | + DFS(0, -1); |
| 31 | + } |
| 32 | +} |
0 commit comments