Question: JAVA: main method needed to display graph/marks import java.util.*; public class Graph { private HashMap nodeLookup = new HashMap (); public static class Node{ //

JAVA: main method needed to display graph/marks

import java.util.*; public class Graph { private HashMap nodeLookup = new HashMap(); public static class Node{ // node id private int id; LinkedList adjacent = new LinkedList(); private Node(int id){ this.id=id; } } private Node getNode(int id){ return nodeLookup.get(id); } public void addEdge(int source, int destination){ Node s = getNode(source); Node d = getNode(destination); s.adjacent.add(d); } public boolean hasPathDFS(int source, int destination){ Node s = getNode(source); Node d = getNode(destination); HashSet visited = new HashSet(); return hasPathDFS(s, d, visited); } // recursive method private boolean hasPathDFS(Node source, Node destination, HashSet visited) { // no path if(visited.contains(source.id)) { return false; } //esle mark visited visited.add(source.id); if(source==destination) { return true; } // search children for(Node child : source.adjacent){ if(hasPathDFS(child, destination, visited)){ return true; } } // no path return return false; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!