Advertisement
nocturnalmk

Depth First Search

Dec 19th, 2012
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.11 KB | None | 0 0
  1.     public void dfs() {
  2.         E[] nodes = (E[]) new Object[num_nodes];
  3.         Stack<GraphNode<E>> pom = new Stack<GraphNode<E>>();
  4.         ArrayList<GraphNode<E>> checked = new ArrayList<GraphNode<E>>();
  5.        
  6.         pom.push(adjList[0]);
  7.         for (int i = 0; i < num_nodes; i++) {
  8.             GraphNode<E> tmp = pom.pop();
  9.             izvadi(tmp.info);
  10.             nodes[i] = tmp.info;
  11.             checked.add(tmp);
  12.             oznaci(tmp.info);
  13.             pecati(tmp.info);
  14.            
  15.             for (GraphNodeNeighbor<E> neigh : tmp.neighbors) {
  16.                 if (!checked.contains(neigh.node)) {
  17.                     pom.push(neigh.node);
  18.                     dodadi(neigh.node.info);
  19.                 } else {
  20.                     odmini(neigh.node.info);
  21.                 }
  22.             }
  23.             System.out.println();
  24.            
  25.         }
  26.        
  27. //      for (int i = 0; i < nodes.length; i++) {
  28. //          System.out.print(nodes[i]);
  29. //      }
  30.     }
  31.    
  32.     private void izvadi(E x) {
  33.         System.out.println("== VADEME:  " + x);
  34.     }
  35.     private void dodadi(E x) {
  36.         System.out.println("== STAVAME: " + x);
  37.     }
  38.     private void pecati(E x) {
  39.         System.out.println("== PECATI:  " + x);
  40.     }
  41.     private void oznaci(E x) {
  42.         System.out.println("== OZNACI:  " + x);
  43.     }
  44.     private void odmini(E x) {
  45.         System.out.println("== ODMINI:  " + x);
  46.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement