Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java (I use Eclipse): Implement the bfs(int s) function. The program should return an integer array that records the minimum distance between every node to

Java (I use Eclipse):

Implement the bfs(int s) function. The program should return an integer array that records the minimum distance between every node to the source s.

Below is the skeleton to add bfs(int s) for the above question. Please add to the skeleton. Do not modify skeleton.

Please make sure the program works. A screen shot of the working program and code will be helpful!

Thank you.

public class Graph {

public int n; //number of vertice

public int[][] A; //the adjacency matrix

private final int WHITE = 2;

private final int GRAY = 3;

private final int BLACK = 4;

public Graph () {

n = 0;

A = null;

}

public Graph (int _n, int[][] _A) {

this.n = _n;

this.A = _A;

}

/*

* Input: s denotes the index of the source node

* Output: the array dist, where dist[i] is the distance between the i-th node to s

*/

public int[] bfs (int s) {

}

public void print_array (int[] array) {

for (int i = 0; i < array.length; i++)

System.out.println(i + ": " + array[i]);

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

int n = 8;

int[][] A =

{{0, 1, 0, 0, 1, 0, 0, 0},

{1, 0, 0, 0, 0, 1, 0, 0},

{0, 0, 0, 1, 0, 1, 1, 0},

{0, 0, 1, 0, 0, 0, 1, 1},

{1, 0, 0, 0, 0, 0, 0, 0},

{0, 1, 1, 0, 0, 0, 1, 0},

{0, 0, 1, 1, 0, 1, 0, 1},

{0, 0, 0, 1, 0, 0, 1, 0}};

Graph g = new Graph(n, A);

g.print_array(g.bfs(1));

}

}

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

Ai And The Lottery Defying Odds With Intelligent Prediction

Authors: Gary Covella Ph D

1st Edition

B0CND1ZB98, 979-8223302568

Students also viewed these Databases questions

Question

What is Centrifugation?

Answered: 1 week ago

Question

To find integral of ?a 2 - x 2

Answered: 1 week ago

Question

To find integral of e 3x sin4x

Answered: 1 week ago

Question

To find the integral of 3x/(x - 1)(x - 2)(x - 3)

Answered: 1 week ago

Question

What are Fatty acids?

Answered: 1 week ago

Question

1. Define and explain culture and its impact on your communication

Answered: 1 week ago