Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Lab Task 0 3 : Complete the Graph.java method: private boolean isReachable ( int src , int dest, boolean [ ] visited ) in the

Lab Task 03:
Complete the Graph.java method: private boolean isReachable(int src, int dest, boolean[] visited) in the
folder Task 03 such that it returns true if the destination vertex dest is reachable from the source vertex src;
otherwise, it returns false.
Complete the test program to prompt for and read the source and destination vertices. It then checks whether the
destination vertex is reachable from the source vertex. [Note: Assume that the values read are valid.]
import java.util.List;
import java.util.ArrayList;
import java.util.Queue;
import java.util.ArrayDeque;
// Determines if a vertex is reachable from another vertex in a directed graph
public class Graph {
private List> adjList = null;
private int numVertices;
public Graph(List edges, int numVertices){
this.numVertices = numVertices;
adjList = new ArrayList>();
for (int i =0; i numVertices; i++){
adjList.add(new ArrayList>());
}
// add edges to the directed graph
for (Edge edge: edges){
int src = edge.source;
int dest = edge.dest;
adjList.get(src).add(dest);
}
}
public boolean isReachable(int src, int dest){
boolean[] visited = new boolean[numVertices];
return isReachable(src, dest, visited);
}
// Function to perform BFS traversal from the source vertex in the graph to
// determine if the destination vertex is reachable from the source or not
private boolean isReachable(int src, int dest, boolean[] visited)
{
// to be completed by students
}
}
public class Edge{
public int source, dest;
private Edge(int source, int dest){
this.source = source;
this.dest = dest;
}
public static Edge getEdge(int a, int b){
return new Edge(a, b); // calls private constructor
}
}
import java.util.List;
import java.util.Arrays;
import java.util.Scanner;
public class GraphDriver{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
List edges = Arrays.asList(Edge.getEdge(0,3), Edge.getEdge(1,0),
Edge.getEdge(1,2), Edge.getEdge(1,4),
Edge.getEdge(2,7), Edge.getEdge(3,4),
Edge.getEdge(3,5), Edge.getEdge(4,3),
Edge.getEdge(4,6), Edge.getEdge(5,6),
Edge.getEdge(6,7));
// Number of nodes in the graph (labelled from 0 to N-1)
int numVertices =8;
// To be completed by students
}
}
image text in transcribed

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

Datacasting How To Stream Databases Over The Internet

Authors: Jessica Keyes

1st Edition

007034678X, 978-0070346789

More Books

Students also viewed these Databases questions

Question

Define the goals of persuasive speaking

Answered: 1 week ago