subject

In java write the code in order to find the breadth first search of the graph from the adjacency matrix.

this question has been asked and all of the tutors are copy and pasting the incorrect answer please do not do that

the first cmd line arg is a text file is called g0.txt and it should have this in it

8
0 1
3 0
3 4
3 5
1 4
2 1
7 4
4 6
5 6
7 6
2 7

the 2nd cmd line arg is 4

and the 3rd cmd line arg is 2

with these the final output should be

8
0 1
3 0
3 4
3 5
1 4
2 1
7 4
4 6
5 6
7 6
2 7

4, 6, 7, 1, 3, 5, 2, 0

2, 7, 1, 4, 6, 0, 3, 5

import java. io.*;
import java. util.*;

public class UndirectedGraph {

private class Vertex {
private EdgeNode edges1;
private EdgeNode edges2;
private boolean queued;

private Vertex() {
edges1 = null;
edges2 = null;
queued=false;

}

private Vertex(EdgeNode e1, EdgeNode e2) {
edges1 = e1;
edges2 = e2;

}

}

private class EdgeNode {
private int vertex1;
private int vertex2;
private EdgeNode next1;
private EdgeNode next2;

private EdgeNode(int v1, int v2) {
//PRE: v1 < v2
vertex1 = v1;
vertex2 = v2;
}
}

private Vertex[] g;

public UndirectedGraph(int size) {
g = new Vertex[size];
for (int i = 0; i < size; i++) {
g[i] = new Vertex();
}

}

public void addEdge(int v1, int v2) {
//if the vertices aren't in accending order, swap
if(v1 > v2) {
int temp = v1;
v1 = v2;
v2 = temp;
}

//if a vertex is null, instantiate it
if(g[v1] == null) {
g[v1] = new Vertex(new EdgeNode(v1, v2), null);
}
//if edges1 is still null, instantiate
else if(g[v1].edges1 == null) {
g[v1].edges1 = new EdgeNode(v1, v2);
}
//else add edge to the list in order
else {
EdgeNode temp = g[v1].edges1;

//traverse list until you come to the end, or you come to the appropriate location
while(temp. next1 != null && temp. vertex2 < v2) {
temp = temp. next1;
}

EdgeNode e = new EdgeNode(v1, v2);
//add before temp if v2 is less than vertex2
if(temp. vertex2 > v2) {
e. next1 = temp;
if(g[v1].edges1 != null && g[v1].edges1.vertex2 < v2) {
EdgeNode temp2 = g[v1].edges1;
//goto end of existing list, but before place of insertion
while(temp2.next1 != null && temp2.next1 != temp)
temp2 = temp2.next1;
temp2.next1 = e;
}
else
g[v1].edges1 = e;
}
//else add after temp
else {
if (temp. next1 != null) {
e. next1 = temp. next1;
}
temp. next1 = e;
}
}

//if a vertex is null, instantiate it
if(g[v2] == null) {
g[v2] = new Vertex(null, new EdgeNode(v1, v2));
}
//if edges2 is still null, instantiate
else if(g[v2].edges2 == null) {
g[v2].edges2 = new EdgeNode(v1, v2);
}
//else add edge to the list in order
else {
EdgeNode temp = g[v2].edges2;
//traverse list until you come to the end, or you come to the appropriate location
while(temp. next2 != null && temp. vertex1 < v1) {
temp = temp. next2;
}

EdgeNode e = new EdgeNode(v1, v2);
//add before temp if v1 is less than vertex1
if(temp. vertex1 > v1) {
e. next2 = temp;
if(g[v2].edges2 != null && g[v2].edges2.vertex1 < v1) {
EdgeNode temp2 = g[v2].edges2;
//goto end of existing list, but before place of insertion
while(temp2.next2 != null && temp2.next2 != temp)
temp2 = temp2.next2;
temp2.next2 = e;
}
else
g[v2].edges2 = e;
}
//else add after temp
else {
if (temp. next2 != null) {
e. next2 = temp. next2;
}
temp. next2 = e;
}
}
}
public void printBFS(int start) {
//Print a comma delimited list of nodes in BFS order

}
public void printGraph() {
for(int i = 0; i < g. length; i++) {
EdgeNode temp1 = g[i].edges1;
EdgeNode temp2 = g[i].edges2;

System. out. printf("%s ", i);
while(temp1 != null) {
System. out. printf("%s ", temp1.vertex2);
temp1 = temp1.next1;
}

System. out. print("\n ");
while(temp2 != null) {
System. out. printf("%s ", temp2.vertex1);
temp2 = temp2.next2;
}
System. out. println('\n');
}
}

public static void main(String args[]) throws IOException {
BufferedReader b = new BufferedReader(new FileReader(args[0]));
String line = b. readLine();
int numNodes = Integer. parseInt(line);
UndirectedGraph g = new UndirectedGraph(numNodes);
line = b. readLine();
while (line != null) {
Scanner scan = new Scanner(line);
g. addEdge(scan. nextInt(), scan. nextInt());
line = b. readLine();
}
g. printGraph();
g. printBFS(Integer. parseInt(args[1]));
g. printBFS(Integer. parseInt(args[2]));
}

}

ansver
Answers: 2

Other questions on the subject: Computers and Technology

image
Computers and Technology, 24.06.2019 00:10, roxymiller3942
Read each statement below. if the statement describes a peer-to-peer network, put a p next to it. if the statement describes a server-based network, put an s next to it. p - peer-to-peer s - server-based
Answers: 1
image
Computers and Technology, 24.06.2019 15:30, taylorpayne525p8qxky
What is not a type of text format that will automatically be converted by outlook into a hyperlink?
Answers: 1
image
Computers and Technology, 24.06.2019 17:50, mrmendrala
Create a class hand in its own module. one object of class hand represents a hand of cards and so one object stores a number of card objects. for this assignment you will submit three separate modules, one with the definition of class card, one with the definition of class hand and one with the main application that thoroughly tests class hand. class hand must contain the following four methods: 1) , numcardsinhand) takes an integer as parameter and initializes a hand object with numcardsinhand card objects inside it. these card objects are generated randomly. for simplicity, assume an infinite number of decks of cards.2) bjvalue(self) returns the blackjack value for the whole hand of cards3) ) returns a string containing all the cards in the hand4) hitme(self) adds one randomly generated card to the handcreate a main program in its own module that thoroughly tests class hand. you will have three modules/files to upload to your etudes assignment submission: card. py, hand. py and the module that contains your main program. two alternatives for extra credit - you cannot get credit for both! (+1 point): after you have thoroughly tested the class hand and all of its methods, add code to your main program that stores one hand object as a pickle file and reads it back into a new hand object. you are only eligible for this extra credit if class hand has all four of the methods above working. or(+2 points): after you have thoroughly tested the class hand and all of its methods, add code to your main program that stores one hand object as a text file on the disk and reads it back into a new hand object. you are only eligible for this extra credit if class hand has all four of the methods above working. notes: -start by making any and all modifications suggested by my comments to your previous submission of class card from assignment #6 "a robust card object"-once your class card is tested and working well, you will not make any further modifications to it for the purposes of class hand.-you can keep the test code for class card intact. if it is indented inside an if __name__ == "__main__", then it will not be executed when your main program's module imports it.-to save time, write and test one method for class hand at a time.-under no circumstances are you to attempt the extra credit until you are completely finished with writing and testing all the methods in class hand.
Answers: 3
image
Computers and Technology, 24.06.2019 18:30, trevorhenyan51
Dereck works for long hours on his computer.  he frequently experiences physical strain by the end of the day because he does not follow an important rule of ergonomics with respect to the use of keyboards.  which of the following actions of dereck could lead to physical strain? a.  placing the keyboard exactly in front of him while typingb.  keeping hands and wrists straight while typingc.  using wrist pads throughout the dayd.  pounding at the keys on the keyboard while typinge.  resting his hands on the keyboard when he is not typing
Answers: 1
You know the right answer?
In java write the code in order to find the breadth first search of the graph from the adjacency m...

Questions in other subjects: