Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In the following lab task you are required to modify the Graph.java in the folder LabTask 0 1 such that it represents a graph using

In the following lab task you are required to modify the Graph.java in the folder LabTask01 such that it
represents a graph using adjacency matrix representation similar to the above representation.
Page 1 of 4
(a) Complete the method: public void addEdge(int i, int j) to add an undirected edge i-j to the graph.
Note: For an undirected graph, if an edge i-j is added to the graph then the reverse edge j- i must also
be added.
(b) Complete the method: public void removeEdge(int i, int j) to remove an undirected edge i-j from the
graph
Note: For an undirected graph, an edge i-j is removed from the graph then the reverse edge j-i must
also be removed.
(c) Complete the method: public boolean isEdge(int i, int j). The method returns true if i-j is an undirected
edge in the graph; otherwise, it returns false.
(d) Complete the driver class by creating the following graph:
Run the program to get an output of the following form:
public class Graph {
private boolean adjacencyMatrix[][];
private int numberOfVertices;
public Graph(int numberOfVertices){
this.numberOfVertices = numberOfVertices;
adjacencyMatrix = new boolean[numberOfVertices][numberOfVertices];
}
public void addEdge(int i, int j){
// To be completed by students
}
public void removeEdge(int i, int j){
// To be completed by students
}
public boolean isEdge(int i, int j){
// to be completed by students
}
public void displayGraph(){
System.out.printf("%10s","");
for(int i =0; i numberOfVertices; i++)
System.out.printf("%10s", i);
System.out.println();
for(int i =0; i numberOfVertices; i++){
System.out.printf("%10s", i);
for(int j =0; j numberOfVertices; j++){
System.out.printf("%10s", adjacencyMatrix[i][j]);
}
System.out.println();
}
}
}
public class GraphDriver{
public static void main(String[] args){
// Create an undirected graph with 4 vertices
// To be completed by students
// Add the 5 edges of the graph
// To be completed by students
// Display the graph before deleting edge 2---3
System.out.println("Before deleting edge 2---3 the graph is:
");
// To be completed by students
// Display the graph after deleting edge 2---3
// To be completed by students
System.out.println("
After deleting edge 2---3 the graph is:
");
// 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

Databases On The Web Designing And Programming For Network Access

Authors: Patricia Ju

1st Edition

1558515100, 978-1558515109

More Books

Students also viewed these Databases questions

Question

Distinguish between hearing and listening.

Answered: 1 week ago

Question

Use your voice effectively.

Answered: 1 week ago