Can you tell what is going wrong with my code? Everytime I try to run it, I
Question:
Can you tell what is going wrong with my code? Everytime I try to run it, I get a run error.
This is the output, I'm trying to get:
The DFS: I am exploring A Still exploring A I am exploring B Still exploring B I am exploring C Still exploring C I am exploring D Finish exploring D Still exploring C I am exploring E Finish exploring E Finish exploring C Finish exploring B Finish exploring A
This is the output I keep getting:
The DFS: I am exploring A Still exploring A I am exploring B Still exploring B I am exploring C Still exploring C I am exploring D Still exploring D I am exploring E Still exploring E Finish exploring A
The entire code:
import java.util.ArrayList; import java.util.Queue; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.HashSet; import java.util.Comparator; // ========================================================== // Class MyGraph: provide graphs algorithms class MyGraph { // ----------------------------------------- ArrayList nodes=new ArrayList<>(); // A list of nodes int adjMatrix[][]; // adjmatrix // -----------------------------------------constructors public MyGraph(ArrayList nodes, int[][] adjMatrix) { this.adjMatrix = adjMatrix; this.nodes = nodes; } public MyGraph(ArrayList nodes) { this.adjMatrix = new int[nodes.size()][nodes.size()]; this.nodes = nodes; } // -----------------------------------------DFS public void dfs_visit(Node u) { System.out.println("I am exploring " + u.name); // I HAVE TO EDIT FROM HERE System.out.println( " Still exploring " + u.name); ArrayList between = u.getNeighbours(); int i = 0; while(i < between.size()) { Node node = between.get(i); if(node.getColor() == -1) { dfs_visit(node); //WHERE ECLIPSE KEEPS SAYING ERROR } else { System.out.println("I am exploring " + node.name + " Still exploring " + node.name); } i++; } // I HAVE TO STOP EDITING HERE System.out.println("Finish exploring " + u.name ); } } // ========================================================== // Class Node class Node { int data; String name; int color; // GRAY: -1, RED: -2, BLACK: -3 ArrayList neighbours; int d; // record distance information Node p; // record parent information Node(int data, String name) { this.data = data; this.name = name; this.neighbours = new ArrayList<>(); this.color = -1; // By default it is gray. // And we can skip the initialization step this.d = 100000000; this.p = null; } public int getColor() { return 0; } public void setColor(int color) { this.color = color; } public void addneighbours(Node neighbourNode) { this.neighbours.add(neighbourNode); } public ArrayList getNeighbours() { return this.neighbours; } public void setNeighbours(ArrayList neighbours) { this.neighbours = neighbours; } } class MyComparator implements Comparator { @Override public int compare(Node node1, Node node2) { if (node1.d < node2.d) return -1; if (node1.d > node2.d) return 1; return 0; } } public class DFS { public static void main(String arg[]) { Node A =new Node(1, "A"); Node B =new Node(2, "B"); Node C =new Node(3, "C"); Node D =new Node(4, "D"); Node E =new Node(5, "E"); A.addneighbours(B); A.addneighbours(C); A.addneighbours(D); A.addneighbours(E); B.addneighbours(A); B.addneighbours(C); C.addneighbours(B); C.addneighbours(A); C.addneighbours(D); C.addneighbours(E); D.addneighbours(A); D.addneighbours(C); E.addneighbours(A); E.addneighbours(C); ArrayList nodes=new ArrayList<>(); nodes.add(A); nodes.add(B); nodes.add(C); nodes.add(D); nodes.add(E); MyGraph graph1 = new MyGraph(nodes); // ----------------------------------------- Test DFS System.out.println("The DFS: "); graph1.dfs_visit(A); } }
Calculus Early Transcendentals
ISBN: 978-0321947345
2nd edition
Authors: William L. Briggs, Lyle Cochran, Bernard Gillett