Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I'm trying to get this block of code to work: public void dfs_visit(Node u) { System.out.println(I am exploring + u.name); // This is where

I'm trying to get this block of code to work:

 public void dfs_visit(Node u) { System.out.println("I am exploring " + u.name); // This is where I have to edit 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); //line where Eclipse says error } else { System.out.println("I am exploring " + node.name + " Still exploring " + node.name); } i++; } System.out.println("Finish exploring " + u.name ); } }

The Full Code

// =============================================== Requirements // Implement the DFS algorithm based on the provided template. // The output should be: // 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 // // =============================================== Note // // 1. DO NOT DELETE ANY COMMENT!!! // 2. Modify the file name to "DFS.java" before compiling it. // // =============================================== 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); // // This is where I have to edit 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); } else { System.out.println("I am exploring " + node.name + " Still exploring " + node.name); } i++; } 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); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microeconomics An Intuitive Approach with Calculus

Authors: Thomas Nechyba

1st edition

538453257, 978-0538453257

More Books

Students also viewed these Programming questions

Question

In the G/M/1 model if G is exponential with rate show that = /.

Answered: 1 week ago