SlideShare a Scribd company logo
1 of 9
6
Modify the bfs.java program (Listing A) to find the minimum
spanning tree using a breadth-first search, rather than the depth-
first search shown in
mst.java (Listing B). In main(), create a graph with 9 vertices
and 12 edges,
and find its minimum spanning tree.
Listing A: The bfs.java Program
// bfs.java
// demonstrates breadth-first search
// to run this program: C>java BFSApp
////////////////////////////////////////////////////////////////
class Queue
{
private final int SIZE = 20;
private int[] queArray;
private int front;
private int rear;
// -------------------------------------------------------------
public Queue() // constructor
{
queArray = new int[SIZE];
front = 0;
rear = -1;
}
// -------------------------------------------------------------
public void insert(int j) // put item at rear of queue
{
if(rear == SIZE-1)
rear = -1;
queArray[++rear] = j;
}
// -------------------------------------------------------------
public int remove() // take item from front of queue
{
int temp = queArray[front++];
if(front == SIZE)
front = 0;
return temp;
}
// -------------------------------------------------------------
public boolean isEmpty() // true if queue is empty
{
return ( rear+1==front || (front+SIZE-1==rear) );
}
// -------------------------------------------------------------
} // end class Queue
////////////////////////////////////////////////////////////////
class Vertex
{
public char label; // label (e.g. ‘A’)
public boolean wasVisited;
// -------------------------------------------------------------
public Vertex(char lab) // constructor
{
label = lab;
wasVisited = false;
}
// -------------------------------------------------------------
} // end class Vertex
////////////////////////////////////////////////////////////////
class Graph
{
private final int MAX_VERTS = 20;
private Vertex vertexList[]; // list of vertices
private int adjMat[][]; // adjacency matrix
private int nVerts; // current number of vertices
private Queue theQueue;
// ------------------
public Graph() // constructor
{
vertexList = new Vertex[MAX_VERTS];
// adjacency matrix
adjMat = new int[MAX_VERTS][MAX_VERTS];
nVerts = 0;
for(int j=0; j<MAX_VERTS; j++) // set adjacency
for(int k=0; k<MAX_VERTS; k++) // matrix to 0
adjMat[j][k] = 0;
theQueue = new Queue();
} // end constructor
// -------------------------------------------------------------
public void addVertex(char lab)
{
vertexList[nVerts++] = new Vertex(lab);
}
// -------------------------------------------------------------
public void addEdge(int start, int end)
{
adjMat[start][end] = 1;
adjMat[end][start] = 1;
}
// -------------------------------------------------------------
public void displayVertex(int v)
{
System.out.print(vertexList[v].label);
}
// -------------------------------------------------------------
public void bfs() // breadth-first search
{ // begin at vertex 0
vertexList[0].wasVisited = true; // mark it
displayVertex(0); // display it
theQueue.insert(0); // insert at tail
int v2;
while( !theQueue.isEmpty() ) // until queue empty,
{
int v1 = theQueue.remove(); // remove vertex at head
// until it has no unvisited neighbors
while( (v2=getAdjUnvisitedVertex(v1)) != -1 )
{ // get one,
vertexList[v2].wasVisited = true; // mark it
displayVertex(v2); // display it
theQueue.insert(v2); // insert it
} // end while
} // end while(queue not empty)
// queue is empty, so we’re done
for(int j=0; j<nVerts; j++) // reset flags
vertexList[j].wasVisited = false;
} // end bfs()
// -------------------------------------------------------------
// returns an unvisited vertex adj to v
public int getAdjUnvisitedVertex(int v)
{
for(int j=0; j<nVerts; j++)
if(adjMat[v][j]==1 && vertexList[j].wasVisited==false)
return j;
return -1;
} // end getAdjUnvisitedVertex()
// -------------------------------------------------------------
} // end class Graph
////////////////////////////////////////////////////////////////
class BFSApp
{
public static void main(String[] args)
{
Graph theGraph = new Graph();
theGraph.addVertex(‘A’); // 0 (start for dfs)
theGraph.addVertex(‘B’); // 1
theGraph.addVertex(‘C’); // 2
theGraph.addVertex(‘D’); // 3
theGraph.addVertex(‘E’); // 4
theGraph.addEdge(0, 1); // AB
theGraph.addEdge(1, 2); // BC
theGraph.addEdge(0, 3); // AD
theGraph.addEdge(3, 4); // DE
System.out.print(“Visits: “);
theGraph.bfs(); // breadth-first search
System.out.println();
} // end main()
} // end class BFSApp
////////////////////////////////////////////////////////////////
Listing B: The mst.java program
// mst.java
// demonstrates minimum spanning tree
// to run this program: C>java MSTApp
////////////////////////////////////////////////////////////////
class StackX
{
private final int SIZE = 20;
private int[] st;
private int top;
// -------------------------------------------------------------
public StackX() // constructor
{
st = new int[SIZE]; // make array
top = -1;
}
// -------------------------------------------------------------
public void push(int j) // put item on stack
{ st[++top] = j; }
// -------------------------------------------------------------
public int pop() // take item off stack
{ return st[top--]; }
// -------------------------------------------------------------
public int peek() // peek at top of stack
{ return st[top]; }
// -------------------------------------------------------------
public boolean isEmpty() // true if nothing on stack
{ return (top == -1); }
// -------------------------------------------------------------
} // end class StackX
////////////////////////////////////////////////////////////////
class Vertex
{
public char label; // label (e.g. ‘A’)
public boolean wasVisited;
// -------------------------------------------------------------
public Vertex(char lab) // constructor
{
label = lab;
wasVisited = false;
}
// -------------------------------------------------------------
} // end class Vertex
////////////////////////////////////////////////////////////////
class Graph
{
private final int MAX_VERTS = 20;
private Vertex vertexList[]; // list of vertices
private int adjMat[][]; // adjacency matrix
private int nVerts; // current number of vertices
private StackX theStack;
// -------------------------------------------------------------
public Graph() // constructor
{
vertexList = new Vertex[MAX_VERTS];
// adjacency matrix
adjMat = new int[MAX_VERTS][MAX_VERTS];
nVerts = 0;
for(int j=0; j<MAX_VERTS; j++) // set adjacency
for(int k=0; k<MAX_VERTS; k++) // matrix to 0
adjMat[j][k] = 0;
theStack = new StackX();
} // end constructor
// -------------------------------------------------------------
public void addVertex(char lab)
{
vertexList[nVerts++] = new Vertex(lab);
}
// -------------------------------------------------------------
public void addEdge(int start, int end)
{
adjMat[start][end] = 1;
adjMat[end][start] = 1;
}
// -------------------------------------------------------------
public void displayVertex(int v)
{
System.out.print(vertexList[v].label);
}
// -------------------------------------------------------------
public void mst() // minimum spanning tree (depth first)
{ // start at 0
vertexList[0].wasVisited = true; // mark it
theStack.push(0); // push it
while( !theStack.isEmpty() ) // until stack empty
{ // get stack top
int currentVertex = theStack.peek();
// get next unvisited neighbor
int v = getAdjUnvisitedVertex(currentVertex);
if(v == -1) // if no more neighbors
theStack.pop(); // pop it away
else // got a neighbor
{
vertexList[v].wasVisited = true; // mark it
theStack.push(v); // push it
// display edge
displayVertex(currentVertex); // from currentV
displayVertex(v); // to v
System.out.print(“ “);
}
} // end while(stack not empty)
// stack is empty, so we’re done
for(int j=0; j<nVerts; j++) // reset flags
vertexList[j].wasVisited = false;
} // end tree
// -------------------------------------------------------------
// returns an unvisited vertex adj to v
public int getAdjUnvisitedVertex(int v)
{
for(int j=0; j<nVerts; j++)
if(adjMat[v][j]==1 && vertexList[j].wasVisited==false)
return j;
return -1;
} // end getAdjUnvisitedVertex()
// -------------------------------------------------------------
} // end class Graph
////////////////////////////////////////////////////////////////
class MSTApp
{
public static void main(String[] args)
{
Graph theGraph = new Graph();
theGraph.addVertex(‘A’); // 0 (start for mst)
theGraph.addVertex(‘B’); // 1
theGraph.addVertex(‘C’); // 2
theGraph.addVertex(‘D’); // 3
theGraph.addVertex(‘E’); // 4
theGraph.addEdge(0, 1); // AB
theGraph.addEdge(0, 2); // AC
theGraph.addEdge(0, 3); // AD
theGraph.addEdge(0, 4); // AE
theGraph.addEdge(1, 2); // BC
theGraph.addEdge(1, 3); // BD
theGraph.addEdge(1, 4); // BE
theGraph.addEdge(2, 3); // CD
theGraph.addEdge(2, 4); // CE
theGraph.addEdge(3, 4); // DE
System.out.print(“Minimum spanning tree: “);
theGraph.mst(); // minimum spanning tree
System.out.println();
} // end main()
} // end class MSTApp
////////////////////////////////////////////////////////////////

More Related Content

Similar to 6Modify the bfs.java program (Listing A) to find the minimu.docx

DS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL IntroductionDS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL IntroductionMohammad Imam Hossain
 
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdfarihantmobileselepun
 
PostgreSQL 9.6 새 기능 소개
PostgreSQL 9.6 새 기능 소개PostgreSQL 9.6 새 기능 소개
PostgreSQL 9.6 새 기능 소개PgDay.Seoul
 
1 Part I written exercises.1. Using the Red-Black Tre.docx
1 Part I written exercises.1. Using the Red-Black Tre.docx1 Part I written exercises.1. Using the Red-Black Tre.docx
1 Part I written exercises.1. Using the Red-Black Tre.docxmercysuttle
 
Lab11.cppLab11.cpp.docx
Lab11.cppLab11.cpp.docxLab11.cppLab11.cpp.docx
Lab11.cppLab11.cpp.docxDIPESH30
 
Write CC++ a program that inputs a weighted undirected graph and fi.pdf
Write CC++ a program that inputs a weighted undirected graph and fi.pdfWrite CC++ a program that inputs a weighted undirected graph and fi.pdf
Write CC++ a program that inputs a weighted undirected graph and fi.pdf4babies2010
 
complete the following functions in c++ singleRotation putNo.pdf
complete the following functions in c++ singleRotation putNo.pdfcomplete the following functions in c++ singleRotation putNo.pdf
complete the following functions in c++ singleRotation putNo.pdfabbecindia
 
C++, Implement the class BinarySearchTree, as given in listing 16-4 .pdf
C++, Implement the class BinarySearchTree, as given in listing 16-4 .pdfC++, Implement the class BinarySearchTree, as given in listing 16-4 .pdf
C++, Implement the class BinarySearchTree, as given in listing 16-4 .pdfrohit219406
 
Advanced tips of dbms statas
Advanced tips of dbms statasAdvanced tips of dbms statas
Advanced tips of dbms statasLouis liu
 
[Pgday.Seoul 2019] Citus를 이용한 분산 데이터베이스
[Pgday.Seoul 2019] Citus를 이용한 분산 데이터베이스[Pgday.Seoul 2019] Citus를 이용한 분산 데이터베이스
[Pgday.Seoul 2019] Citus를 이용한 분산 데이터베이스PgDay.Seoul
 
STACK IMPLEMENTATION USING SINGLY LINKED LIST
STACK IMPLEMENTATION USING SINGLY LINKED LISTSTACK IMPLEMENTATION USING SINGLY LINKED LIST
STACK IMPLEMENTATION USING SINGLY LINKED LISTEr. Ganesh Ram Suwal
 
BUMP implementation in Java.docxThe project is to implemen.docx
BUMP implementation in Java.docxThe project is to implemen.docxBUMP implementation in Java.docxThe project is to implemen.docx
BUMP implementation in Java.docxThe project is to implemen.docxhartrobert670
 
Vagrant, Ansible, and OpenStack on your laptop
Vagrant, Ansible, and OpenStack on your laptopVagrant, Ansible, and OpenStack on your laptop
Vagrant, Ansible, and OpenStack on your laptopLorin Hochstein
 

Similar to 6Modify the bfs.java program (Listing A) to find the minimu.docx (20)

Programming Assignment Help
Programming Assignment HelpProgramming Assignment Help
Programming Assignment Help
 
DS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL IntroductionDS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL Introduction
 
Casnewb
CasnewbCasnewb
Casnewb
 
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
 
Code
CodeCode
Code
 
PostgreSQL 9.6 새 기능 소개
PostgreSQL 9.6 새 기능 소개PostgreSQL 9.6 새 기능 소개
PostgreSQL 9.6 새 기능 소개
 
Diseqc
DiseqcDiseqc
Diseqc
 
1 Part I written exercises.1. Using the Red-Black Tre.docx
1 Part I written exercises.1. Using the Red-Black Tre.docx1 Part I written exercises.1. Using the Red-Black Tre.docx
1 Part I written exercises.1. Using the Red-Black Tre.docx
 
Java Print method
Java  Print methodJava  Print method
Java Print method
 
Lab11.cppLab11.cpp.docx
Lab11.cppLab11.cpp.docxLab11.cppLab11.cpp.docx
Lab11.cppLab11.cpp.docx
 
Write CC++ a program that inputs a weighted undirected graph and fi.pdf
Write CC++ a program that inputs a weighted undirected graph and fi.pdfWrite CC++ a program that inputs a weighted undirected graph and fi.pdf
Write CC++ a program that inputs a weighted undirected graph and fi.pdf
 
complete the following functions in c++ singleRotation putNo.pdf
complete the following functions in c++ singleRotation putNo.pdfcomplete the following functions in c++ singleRotation putNo.pdf
complete the following functions in c++ singleRotation putNo.pdf
 
C++, Implement the class BinarySearchTree, as given in listing 16-4 .pdf
C++, Implement the class BinarySearchTree, as given in listing 16-4 .pdfC++, Implement the class BinarySearchTree, as given in listing 16-4 .pdf
C++, Implement the class BinarySearchTree, as given in listing 16-4 .pdf
 
Advanced tips of dbms statas
Advanced tips of dbms statasAdvanced tips of dbms statas
Advanced tips of dbms statas
 
[Pgday.Seoul 2019] Citus를 이용한 분산 데이터베이스
[Pgday.Seoul 2019] Citus를 이용한 분산 데이터베이스[Pgday.Seoul 2019] Citus를 이용한 분산 데이터베이스
[Pgday.Seoul 2019] Citus를 이용한 분산 데이터베이스
 
SQLQueries
SQLQueriesSQLQueries
SQLQueries
 
STACK IMPLEMENTATION USING SINGLY LINKED LIST
STACK IMPLEMENTATION USING SINGLY LINKED LISTSTACK IMPLEMENTATION USING SINGLY LINKED LIST
STACK IMPLEMENTATION USING SINGLY LINKED LIST
 
Cadence flow
Cadence flowCadence flow
Cadence flow
 
BUMP implementation in Java.docxThe project is to implemen.docx
BUMP implementation in Java.docxThe project is to implemen.docxBUMP implementation in Java.docxThe project is to implemen.docx
BUMP implementation in Java.docxThe project is to implemen.docx
 
Vagrant, Ansible, and OpenStack on your laptop
Vagrant, Ansible, and OpenStack on your laptopVagrant, Ansible, and OpenStack on your laptop
Vagrant, Ansible, and OpenStack on your laptop
 

More from evonnehoggarth79783

For this Portfolio Project, you will write a paper about John A.docx
For this Portfolio Project, you will write a paper about John A.docxFor this Portfolio Project, you will write a paper about John A.docx
For this Portfolio Project, you will write a paper about John A.docxevonnehoggarth79783
 
For this portfolio assignment, you are required to research and anal.docx
For this portfolio assignment, you are required to research and anal.docxFor this portfolio assignment, you are required to research and anal.docx
For this portfolio assignment, you are required to research and anal.docxevonnehoggarth79783
 
For this paper, discuss the similarities and differences of the .docx
For this paper, discuss the similarities and differences of the .docxFor this paper, discuss the similarities and differences of the .docx
For this paper, discuss the similarities and differences of the .docxevonnehoggarth79783
 
For this paper, discuss the similarities and differences of the impa.docx
For this paper, discuss the similarities and differences of the impa.docxFor this paper, discuss the similarities and differences of the impa.docx
For this paper, discuss the similarities and differences of the impa.docxevonnehoggarth79783
 
For this paper choose two mythological narratives that we have exami.docx
For this paper choose two mythological narratives that we have exami.docxFor this paper choose two mythological narratives that we have exami.docx
For this paper choose two mythological narratives that we have exami.docxevonnehoggarth79783
 
For this module, there is only one option.  You are to begin to deve.docx
For this module, there is only one option.  You are to begin to deve.docxFor this module, there is only one option.  You are to begin to deve.docx
For this module, there is only one option.  You are to begin to deve.docxevonnehoggarth79783
 
For this Major Assignment 2, you will finalize your analysis in .docx
For this Major Assignment 2, you will finalize your analysis in .docxFor this Major Assignment 2, you will finalize your analysis in .docx
For this Major Assignment 2, you will finalize your analysis in .docxevonnehoggarth79783
 
For this Final Visual Analysis Project, you will choose one website .docx
For this Final Visual Analysis Project, you will choose one website .docxFor this Final Visual Analysis Project, you will choose one website .docx
For this Final Visual Analysis Project, you will choose one website .docxevonnehoggarth79783
 
For this essay, you will select one of the sources you have found th.docx
For this essay, you will select one of the sources you have found th.docxFor this essay, you will select one of the sources you have found th.docx
For this essay, you will select one of the sources you have found th.docxevonnehoggarth79783
 
For this discussion, you will address the following prompts. Keep in.docx
For this discussion, you will address the following prompts. Keep in.docxFor this discussion, you will address the following prompts. Keep in.docx
For this discussion, you will address the following prompts. Keep in.docxevonnehoggarth79783
 
For this discussion, research a recent science news event that h.docx
For this discussion, research a recent science news event that h.docxFor this discussion, research a recent science news event that h.docx
For this discussion, research a recent science news event that h.docxevonnehoggarth79783
 
For this Discussion, review the case Learning Resources and the .docx
For this Discussion, review the case Learning Resources and the .docxFor this Discussion, review the case Learning Resources and the .docx
For this Discussion, review the case Learning Resources and the .docxevonnehoggarth79783
 
For this Discussion, give an example of how an event in one part.docx
For this Discussion, give an example of how an event in one part.docxFor this Discussion, give an example of how an event in one part.docx
For this Discussion, give an example of how an event in one part.docxevonnehoggarth79783
 
For this discussion, consider the role of the LPN and the RN in .docx
For this discussion, consider the role of the LPN and the RN in .docxFor this discussion, consider the role of the LPN and the RN in .docx
For this discussion, consider the role of the LPN and the RN in .docxevonnehoggarth79783
 
For this discussion, after you have viewed the videos on this topi.docx
For this discussion, after you have viewed the videos on this topi.docxFor this discussion, after you have viewed the videos on this topi.docx
For this discussion, after you have viewed the videos on this topi.docxevonnehoggarth79783
 
For this discussion choose  one of the case studies listed bel.docx
For this discussion choose  one of the case studies listed bel.docxFor this discussion choose  one of the case studies listed bel.docx
For this discussion choose  one of the case studies listed bel.docxevonnehoggarth79783
 
For this assignment, you will use what youve learned about symbolic.docx
For this assignment, you will use what youve learned about symbolic.docxFor this assignment, you will use what youve learned about symbolic.docx
For this assignment, you will use what youve learned about symbolic.docxevonnehoggarth79783
 
For this Assignment, you will research various perspectives of a mul.docx
For this Assignment, you will research various perspectives of a mul.docxFor this Assignment, you will research various perspectives of a mul.docx
For this Assignment, you will research various perspectives of a mul.docxevonnehoggarth79783
 
For this assignment, you will be studying a story from the Gospe.docx
For this assignment, you will be studying a story from the Gospe.docxFor this assignment, you will be studying a story from the Gospe.docx
For this assignment, you will be studying a story from the Gospe.docxevonnehoggarth79783
 
For this assignment, you will discuss how you see the Design Princip.docx
For this assignment, you will discuss how you see the Design Princip.docxFor this assignment, you will discuss how you see the Design Princip.docx
For this assignment, you will discuss how you see the Design Princip.docxevonnehoggarth79783
 

More from evonnehoggarth79783 (20)

For this Portfolio Project, you will write a paper about John A.docx
For this Portfolio Project, you will write a paper about John A.docxFor this Portfolio Project, you will write a paper about John A.docx
For this Portfolio Project, you will write a paper about John A.docx
 
For this portfolio assignment, you are required to research and anal.docx
For this portfolio assignment, you are required to research and anal.docxFor this portfolio assignment, you are required to research and anal.docx
For this portfolio assignment, you are required to research and anal.docx
 
For this paper, discuss the similarities and differences of the .docx
For this paper, discuss the similarities and differences of the .docxFor this paper, discuss the similarities and differences of the .docx
For this paper, discuss the similarities and differences of the .docx
 
For this paper, discuss the similarities and differences of the impa.docx
For this paper, discuss the similarities and differences of the impa.docxFor this paper, discuss the similarities and differences of the impa.docx
For this paper, discuss the similarities and differences of the impa.docx
 
For this paper choose two mythological narratives that we have exami.docx
For this paper choose two mythological narratives that we have exami.docxFor this paper choose two mythological narratives that we have exami.docx
For this paper choose two mythological narratives that we have exami.docx
 
For this module, there is only one option.  You are to begin to deve.docx
For this module, there is only one option.  You are to begin to deve.docxFor this module, there is only one option.  You are to begin to deve.docx
For this module, there is only one option.  You are to begin to deve.docx
 
For this Major Assignment 2, you will finalize your analysis in .docx
For this Major Assignment 2, you will finalize your analysis in .docxFor this Major Assignment 2, you will finalize your analysis in .docx
For this Major Assignment 2, you will finalize your analysis in .docx
 
For this Final Visual Analysis Project, you will choose one website .docx
For this Final Visual Analysis Project, you will choose one website .docxFor this Final Visual Analysis Project, you will choose one website .docx
For this Final Visual Analysis Project, you will choose one website .docx
 
For this essay, you will select one of the sources you have found th.docx
For this essay, you will select one of the sources you have found th.docxFor this essay, you will select one of the sources you have found th.docx
For this essay, you will select one of the sources you have found th.docx
 
For this discussion, you will address the following prompts. Keep in.docx
For this discussion, you will address the following prompts. Keep in.docxFor this discussion, you will address the following prompts. Keep in.docx
For this discussion, you will address the following prompts. Keep in.docx
 
For this discussion, research a recent science news event that h.docx
For this discussion, research a recent science news event that h.docxFor this discussion, research a recent science news event that h.docx
For this discussion, research a recent science news event that h.docx
 
For this Discussion, review the case Learning Resources and the .docx
For this Discussion, review the case Learning Resources and the .docxFor this Discussion, review the case Learning Resources and the .docx
For this Discussion, review the case Learning Resources and the .docx
 
For this Discussion, give an example of how an event in one part.docx
For this Discussion, give an example of how an event in one part.docxFor this Discussion, give an example of how an event in one part.docx
For this Discussion, give an example of how an event in one part.docx
 
For this discussion, consider the role of the LPN and the RN in .docx
For this discussion, consider the role of the LPN and the RN in .docxFor this discussion, consider the role of the LPN and the RN in .docx
For this discussion, consider the role of the LPN and the RN in .docx
 
For this discussion, after you have viewed the videos on this topi.docx
For this discussion, after you have viewed the videos on this topi.docxFor this discussion, after you have viewed the videos on this topi.docx
For this discussion, after you have viewed the videos on this topi.docx
 
For this discussion choose  one of the case studies listed bel.docx
For this discussion choose  one of the case studies listed bel.docxFor this discussion choose  one of the case studies listed bel.docx
For this discussion choose  one of the case studies listed bel.docx
 
For this assignment, you will use what youve learned about symbolic.docx
For this assignment, you will use what youve learned about symbolic.docxFor this assignment, you will use what youve learned about symbolic.docx
For this assignment, you will use what youve learned about symbolic.docx
 
For this Assignment, you will research various perspectives of a mul.docx
For this Assignment, you will research various perspectives of a mul.docxFor this Assignment, you will research various perspectives of a mul.docx
For this Assignment, you will research various perspectives of a mul.docx
 
For this assignment, you will be studying a story from the Gospe.docx
For this assignment, you will be studying a story from the Gospe.docxFor this assignment, you will be studying a story from the Gospe.docx
For this assignment, you will be studying a story from the Gospe.docx
 
For this assignment, you will discuss how you see the Design Princip.docx
For this assignment, you will discuss how you see the Design Princip.docxFor this assignment, you will discuss how you see the Design Princip.docx
For this assignment, you will discuss how you see the Design Princip.docx
 

Recently uploaded

Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 

Recently uploaded (20)

Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 

6Modify the bfs.java program (Listing A) to find the minimu.docx

  • 1. 6 Modify the bfs.java program (Listing A) to find the minimum spanning tree using a breadth-first search, rather than the depth- first search shown in mst.java (Listing B). In main(), create a graph with 9 vertices and 12 edges, and find its minimum spanning tree. Listing A: The bfs.java Program // bfs.java // demonstrates breadth-first search // to run this program: C>java BFSApp //////////////////////////////////////////////////////////////// class Queue { private final int SIZE = 20; private int[] queArray; private int front; private int rear; // ------------------------------------------------------------- public Queue() // constructor { queArray = new int[SIZE]; front = 0; rear = -1; } // ------------------------------------------------------------- public void insert(int j) // put item at rear of queue { if(rear == SIZE-1) rear = -1;
  • 2. queArray[++rear] = j; } // ------------------------------------------------------------- public int remove() // take item from front of queue { int temp = queArray[front++]; if(front == SIZE) front = 0; return temp; } // ------------------------------------------------------------- public boolean isEmpty() // true if queue is empty { return ( rear+1==front || (front+SIZE-1==rear) ); } // ------------------------------------------------------------- } // end class Queue //////////////////////////////////////////////////////////////// class Vertex { public char label; // label (e.g. ‘A’) public boolean wasVisited; // ------------------------------------------------------------- public Vertex(char lab) // constructor { label = lab; wasVisited = false; } // ------------------------------------------------------------- } // end class Vertex //////////////////////////////////////////////////////////////// class Graph { private final int MAX_VERTS = 20; private Vertex vertexList[]; // list of vertices private int adjMat[][]; // adjacency matrix
  • 3. private int nVerts; // current number of vertices private Queue theQueue; // ------------------ public Graph() // constructor { vertexList = new Vertex[MAX_VERTS]; // adjacency matrix adjMat = new int[MAX_VERTS][MAX_VERTS]; nVerts = 0; for(int j=0; j<MAX_VERTS; j++) // set adjacency for(int k=0; k<MAX_VERTS; k++) // matrix to 0 adjMat[j][k] = 0; theQueue = new Queue(); } // end constructor // ------------------------------------------------------------- public void addVertex(char lab) { vertexList[nVerts++] = new Vertex(lab); } // ------------------------------------------------------------- public void addEdge(int start, int end) { adjMat[start][end] = 1; adjMat[end][start] = 1; } // ------------------------------------------------------------- public void displayVertex(int v) { System.out.print(vertexList[v].label); } // ------------------------------------------------------------- public void bfs() // breadth-first search { // begin at vertex 0 vertexList[0].wasVisited = true; // mark it displayVertex(0); // display it theQueue.insert(0); // insert at tail
  • 4. int v2; while( !theQueue.isEmpty() ) // until queue empty, { int v1 = theQueue.remove(); // remove vertex at head // until it has no unvisited neighbors while( (v2=getAdjUnvisitedVertex(v1)) != -1 ) { // get one, vertexList[v2].wasVisited = true; // mark it displayVertex(v2); // display it theQueue.insert(v2); // insert it } // end while } // end while(queue not empty) // queue is empty, so we’re done for(int j=0; j<nVerts; j++) // reset flags vertexList[j].wasVisited = false; } // end bfs() // ------------------------------------------------------------- // returns an unvisited vertex adj to v public int getAdjUnvisitedVertex(int v) { for(int j=0; j<nVerts; j++) if(adjMat[v][j]==1 && vertexList[j].wasVisited==false) return j; return -1; } // end getAdjUnvisitedVertex() // ------------------------------------------------------------- } // end class Graph //////////////////////////////////////////////////////////////// class BFSApp { public static void main(String[] args) { Graph theGraph = new Graph(); theGraph.addVertex(‘A’); // 0 (start for dfs) theGraph.addVertex(‘B’); // 1 theGraph.addVertex(‘C’); // 2
  • 5. theGraph.addVertex(‘D’); // 3 theGraph.addVertex(‘E’); // 4 theGraph.addEdge(0, 1); // AB theGraph.addEdge(1, 2); // BC theGraph.addEdge(0, 3); // AD theGraph.addEdge(3, 4); // DE System.out.print(“Visits: “); theGraph.bfs(); // breadth-first search System.out.println(); } // end main() } // end class BFSApp //////////////////////////////////////////////////////////////// Listing B: The mst.java program // mst.java // demonstrates minimum spanning tree // to run this program: C>java MSTApp //////////////////////////////////////////////////////////////// class StackX { private final int SIZE = 20; private int[] st; private int top; // ------------------------------------------------------------- public StackX() // constructor { st = new int[SIZE]; // make array top = -1; } // ------------------------------------------------------------- public void push(int j) // put item on stack
  • 6. { st[++top] = j; } // ------------------------------------------------------------- public int pop() // take item off stack { return st[top--]; } // ------------------------------------------------------------- public int peek() // peek at top of stack { return st[top]; } // ------------------------------------------------------------- public boolean isEmpty() // true if nothing on stack { return (top == -1); } // ------------------------------------------------------------- } // end class StackX //////////////////////////////////////////////////////////////// class Vertex { public char label; // label (e.g. ‘A’) public boolean wasVisited; // ------------------------------------------------------------- public Vertex(char lab) // constructor { label = lab; wasVisited = false; } // ------------------------------------------------------------- } // end class Vertex //////////////////////////////////////////////////////////////// class Graph { private final int MAX_VERTS = 20; private Vertex vertexList[]; // list of vertices private int adjMat[][]; // adjacency matrix private int nVerts; // current number of vertices private StackX theStack; // ------------------------------------------------------------- public Graph() // constructor {
  • 7. vertexList = new Vertex[MAX_VERTS]; // adjacency matrix adjMat = new int[MAX_VERTS][MAX_VERTS]; nVerts = 0; for(int j=0; j<MAX_VERTS; j++) // set adjacency for(int k=0; k<MAX_VERTS; k++) // matrix to 0 adjMat[j][k] = 0; theStack = new StackX(); } // end constructor // ------------------------------------------------------------- public void addVertex(char lab) { vertexList[nVerts++] = new Vertex(lab); } // ------------------------------------------------------------- public void addEdge(int start, int end) { adjMat[start][end] = 1; adjMat[end][start] = 1; } // ------------------------------------------------------------- public void displayVertex(int v) { System.out.print(vertexList[v].label); } // ------------------------------------------------------------- public void mst() // minimum spanning tree (depth first) { // start at 0 vertexList[0].wasVisited = true; // mark it theStack.push(0); // push it while( !theStack.isEmpty() ) // until stack empty { // get stack top int currentVertex = theStack.peek(); // get next unvisited neighbor int v = getAdjUnvisitedVertex(currentVertex); if(v == -1) // if no more neighbors
  • 8. theStack.pop(); // pop it away else // got a neighbor { vertexList[v].wasVisited = true; // mark it theStack.push(v); // push it // display edge displayVertex(currentVertex); // from currentV displayVertex(v); // to v System.out.print(“ “); } } // end while(stack not empty) // stack is empty, so we’re done for(int j=0; j<nVerts; j++) // reset flags vertexList[j].wasVisited = false; } // end tree // ------------------------------------------------------------- // returns an unvisited vertex adj to v public int getAdjUnvisitedVertex(int v) { for(int j=0; j<nVerts; j++) if(adjMat[v][j]==1 && vertexList[j].wasVisited==false) return j; return -1; } // end getAdjUnvisitedVertex() // ------------------------------------------------------------- } // end class Graph //////////////////////////////////////////////////////////////// class MSTApp { public static void main(String[] args) { Graph theGraph = new Graph(); theGraph.addVertex(‘A’); // 0 (start for mst) theGraph.addVertex(‘B’); // 1 theGraph.addVertex(‘C’); // 2 theGraph.addVertex(‘D’); // 3
  • 9. theGraph.addVertex(‘E’); // 4 theGraph.addEdge(0, 1); // AB theGraph.addEdge(0, 2); // AC theGraph.addEdge(0, 3); // AD theGraph.addEdge(0, 4); // AE theGraph.addEdge(1, 2); // BC theGraph.addEdge(1, 3); // BD theGraph.addEdge(1, 4); // BE theGraph.addEdge(2, 3); // CD theGraph.addEdge(2, 4); // CE theGraph.addEdge(3, 4); // DE System.out.print(“Minimum spanning tree: “); theGraph.mst(); // minimum spanning tree System.out.println(); } // end main() } // end class MSTApp ////////////////////////////////////////////////////////////////