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

Another question on Computers and Technology

question
Computers and Technology, 23.06.2019 03:30
Ihave a singular monitor that is a tv for my computer. recently, i took apart my computer and put it back together. when i put in the hdmi cord and booted the computer to see if it worked, the computer turned on fine but the screen was blue with "hdmi no signal." i've tried everything that doesn't require buying spare parts, any answer is appreciated!
Answers: 1
question
Computers and Technology, 23.06.2019 09:30
You wanted to look up information about alzheimer's, but you were unsure if it was spelled "alsheimer's" or "alzheimer's." which advanced search strategy would be useful? a) a boolean search b) using a wild card in your search c) trying different search engines d) doing a search for "alsheimer's not alzheimer's" asap. ill give brainlist.
Answers: 1
question
Computers and Technology, 24.06.2019 03:30
Other - a written response, no less than arial 12-point font, to the following: of the following, which would you consider is most important to customer service goals? choose one and explain why. (1) accuracy (2) punctuality and attendance (3) courtesy (4) productivity (5) organization
Answers: 1
question
Computers and Technology, 24.06.2019 10:00
1. which of these is not true about torsion bars? a. they can be used to adjust ride height b. they can be adjusted anytime since they don't affect alignment angles c. they attach between the frame and the lower control arm d. they twist to produce a spring effect
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
question
Mathematics, 19.05.2020 15:19
question
History, 19.05.2020 15:19
question
Mathematics, 19.05.2020 15:19
question
Biology, 19.05.2020 15:19
question
Biology, 19.05.2020 15:19
question
Mathematics, 19.05.2020 15:19
question
Mathematics, 19.05.2020 15:19
Questions on the website: 13722367