Answered step by step
Verified Expert Solution
Question
1 Approved Answer
task in the attached photos, The directed graph is: The undirected graph is: Hint:Inthe duver class duebare an antay hbels of Strings: Pass this arfay
task in the attached photos, The directed graph is:
The undirected graph is:
Hint:Inthe duver class duebare an antay hbels of Strings:
Pass this arfay to the Graph constructor:
Modify the instance variables and Constructor of Graph.java as follows:
int numVertices;
LinkedList adjacencyList;
String labels;
Graphint numVertices, String labels
this.labels labels;
this.numVertices numVertices;
adjacencyList new LinkedListnumVertices;
for int i ; i adjacencyList.length; i
adjacencyListi new LinkedList;
Then modify the following methods of Graph.java accordingly:
To add a directed edge to graph
void addDirectedEdgeint v int w
To be completed by students
To add undirected edge to graph
void addUndirectedEdgeint v int w
to be completed by students
void displayGraph
to be completed by students
import java.util.;
class Graph
int numVertices;
LinkedList adjacencyList;
Graphint numVertices
this.numVertices numVertices;
adjacencyList new LinkedListnumVertices;
for int i ; i adjacencyList.length; i
adjacencyListi new LinkedList;
To add a directed edge to graph
void addDirectedEdgeint v int w
adjacencyListvaddw; Add w to vs list.
To add undirected edge to graph
void addUndirectedEdgeint v int w
adjacencyListvaddw;
adjacencyListwaddv;
void displayGraph
for int i ; i adjacencyList.length; i
System.out.printlni adjacencyListi;
System.out.println;
public class GraphDriver
Driver program to test methods of graph class
public static void mainString args
Total vertices in graph
Graph g new Graph;
gaddDirectedEdge;
gaddDirectedEdge;
gaddDirectedEdge;
gaddDirectedEdge;
gaddDirectedEdge;
System.out.printlnThe directed graph is: ;
gdisplayGraph;
Graph g new Graph;
gaddUndirectedEdge;
gaddUndirectedEdge;
gaddUndirectedEdge;
gaddUndirectedEdge;
gaddUndirectedEdge;
System.out.printlnThe undirected graph is: ;
gdisplayGraph;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started