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

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!