Question: Data Abstract Structure in Java. I recieved notes on implementing BFS Algorithm. I'm using our own built-in Queue.java and List.class. Not importing java.util.LinkedList/Queue. I have

Data Abstract Structure in Java. I recieved notes on implementing BFS Algorithm. I'm using our own built-in Queue.java and List.class. Not importing java.util.LinkedList/Queue. I have addLast(),addFirst(), enqueue(), dequeue() methods. I'm stuck completing this method.

Data Abstract Structure in Java. I recieved notes on implementing BFS Algorithm.

/**

* Performs breath first search on this Graph give a source vertex

*

* @param source

* @precondition graph must not be empty

* @precondition source is a vertex in the graph

* @throws IllegalStateException if the graph is empty

* @throws IndexOutOfBoundsException when the source vertex

*/

public void BFS(Integer source) throws IllegalStateException, IllegalArgumentException {

if (isEmpty())

throw new IllegalStateException();

if (source vertices)

throw new IllegalArgumentException();

// set all the vertices to white

// and distance to -1

// and parent to nil i.e., 0

for (int i = 1; i

color.set(i, 'W');

distance.set(i, -1);

parent.set(i, 0);

}

// set the color of the source to grey

color.set(source, 'G');

// set the distance of the source to 0

distance.set(source, 0);

// Create a queue

Queue queue = new Queue();

// Enqueue the source

queue.enqueue(source);

// Until the queue is empty

while (!queue.isEmpty()) {

// get and remove the first element of the queue

int x = queue.getFront();

x.dequeue(); /////I stopped here.

// for all the vertices adjacent to 'x'

for (int y : adj.get(x)) {

// If the color of 'y' is white

if (color.get(y) == 'W') {

// mark it grey for visiting

color.set(y, 'G');

// set the distance of y

distance.set(y, distance.get(x) + 1);

// and 'x' is the parent of y

parent.set(y, x);

// enqueue this vertex

queue.enqueue(y);

}

}

// eventually, mark this vertex as visited by coloring black

color.set(x, 'B');

}

}

1.1. BFS Algorithm BFS (G,_ s) for all x in V (G) color[x] white distance [x]1 parent [x] -Mil color [s] grey distance [s]0 Enqueue (Q,s) while (Q is not empty) xfront of Q Dequeue (Q,x) for a11 y in adj [x] if color [y]white color [y] = grey distance [y] -distance [x] 1 parent yx Enqueue (Q, y) color [x] = black 1.1. BFS Algorithm BFS (G,_ s) for all x in V (G) color[x] white distance [x]1 parent [x] -Mil color [s] grey distance [s]0 Enqueue (Q,s) while (Q is not empty) xfront of Q Dequeue (Q,x) for a11 y in adj [x] if color [y]white color [y] = grey distance [y] -distance [x] 1 parent yx Enqueue (Q, y) color [x] = black

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!