A breadth-first traversal algorithm for graphs based on Java implementation _java

Source: Internet
Author: User

In this paper, we describe the implementation method of breadth-first traversal algorithm of Java based graph, which is as follows:

Using adjacency matrix to store graph method:

1. Determine the number of vertices and edges of the graph

2. Input vertex information is stored in a one-dimensional array vertex

3. Initialization of adjacency matrix;

4. Sequentially enter each edge stored in the adjacency matrix arc

The ordinal i,j of the two vertices attached to the input edge;
Placing the element value of column J of row I of the adjacency matrix at 1;
The element value of column J row I of the adjacency matrix is placed to 1;

Breadth-first traversal implementation:

1. Initialize Queue Q
2. Access Vertex v;visited[v]=1 vertex v team q;
3.while (Queue Q not empty)

v= Queue Q Team head elements out of the team;
The first adjacency point of w= Vertex v
while (W exists)

If W is not accessed, access vertex w;visited[w]=1, vertex w into queue q

Next contiguous point of w= Vertex v

The implementation code is as follows:

Package com.teradata.lsw.sort;
Import java.util.ArrayList;
Import java.util.LinkedList;
Import java.util.List;

Import Java.util.Queue; public class BFS {//Storage node information private object[] vertices//storage side of the information array private int[][] arcs;//edge of the number of private int vexnum;
Record whether the first node has been visited by private boolean[] visited;

Construct a temporary chain to save the nodes that have been traversed private list<object> temp = new arraylist<object> ();  /** * @param args * * @author TD_LSW/public static void main (string[] args) {//TODO auto-generated method Stub BFS G
= new BFS (8);
Character[] vertices = {' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H '};
G.addvertex (vertices);
G.addedge (0, 1);
G.addedge (0, 2);
G.addedge (1, 3);
G.addedge (1, 4);
G.addedge (3, 5);
G.addedge (4, 5);
G.addedge (2, 6);

G.addedge (2, 7);
System.out.println ("Breadth first traversal of graph:");
G.bfs (); }//Breadth first traversal implementation private void BFS () {//TODO auto-generated method stub for (int i = 0; i < Vexnum; i++) {Visited[i] =
False
} queue<integer> q = new linkedlist<integer> (); for (int i = 0; i< Vexnum; i++) {if (!visited[i]) {Visited[i] = true; visit (i); Q.add (i); while (!q.isempty ()) {int J = (Integer) q.remove (). Intva
Lue (); It is not necessary to loop if all the traversal is done if (temp.size () = = Vexnum) {q.removeall (q); return;} for (int k = This.firstadjvex (j); k >= 0; k
= this. Nextadjvex (j, K)) {if (!visited[k]) {q.add (k); visited[k] = true; visit (k);}}}
Find the next node public int Firstadjvex (int i) {for (int j = 0; J < Vexnum; J +) {if (arcs[i][j) > 0) return J;
} return-1; public int Nextadjvex (int i, int k) {for (int j = k + 1; j < Vexnum; J + +) {if (arcs[i][j) > 0) return j;} Retu
rn-1; }//Initialize the side of the graph private void Addedge (int i, int j) {//TODO auto-generated method Stub if (i = = j) return; arcs[i][j] = 1; a

Rcs[j][i] = 1;

}//initialization graph node private void Addvertex (object[] Object) {//TODO auto-generated method Stub this.vertices = Object;} Diagram initialization public BFS (int n) {//TODO auto-generated constructor stub vexnum = n; vertices = new object[n]; arcs = new int[ N[n];
visited = new Boolean[n];

for (int i = 0; i < Vexnum. i++) {for (int j = 0; J < Vexnum; J +) {Arcs[i][j] = 0;}}}
private void visit (int i) {//TODO auto-generated Method Stub Temp.add (vertices[i)); System.out.print (Vertices[i] + "");}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.