Question
Use Java and Please input output. Thanks Implement a Java class Graph as specified below. A Graph object contains an adjacency matrix representing a directed
Use Java and Please input output. Thanks
Implement a Java class Graph as specified below. A Graph object contains an adjacency matrix representing a directed graph with edge weights. Use the exact class name and method signatures as specifiedand use the default package.
public class Graph {
int[][] adj; // adjacency matrix
int[] d;
int[] f;
int[] pi;
// construct an graph with the adjacency matrix
public Graph(int[][] adj)
// breadth-first search from s. results in d[], pi[]
public void bfs( int s)
// depth-first search. results in d[], f[], pi[]
public void dfs()
// a test program
public static void main(String[] args) {
int[][] a =
{{0, 0, 1, 1, 0},
{0, 0, 1, 0, 1},
{1, 1, 0, 1, 0},
{1, 0, 1, 0, 0},
{0, 1, 0, 0, 0}};
Graph g = new Graph(a);
g.bfs(3);
for (int i = 0; i < g.d.length; i++) {
System.out.print(g.d[i] + " ");
}
System.out.println();
g.dfs();
for (int i = 0; i < g.f.length; i++) {
System.out.print(g.d[i] + "/" + g.f[i] + " ");
}
System.out.println();
}
}
OUTPUT:
1 2 1 0 3
1/10 3/6 2/9 7/8 4/5
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