Java Implementation Graph traversal (depth-first traversal and breadth-first traversal)

Source: Internet
Author: User

Package arithmetic.graphtraveral;
Import java.util.LinkedList;
Import Java.util.Queue;

/**
* This example is the two ways of graph traversal
* Through it, let me understand the traversal of the graph
* Created on 2013-11-18
* @version 0.1
*/
public class graphtraveral{
Adjacency Matrix Storage Diagram
--a B C D E F G H I
A 0 1 0 0 0 1 1 0 0
B 1 0 1 0 0 0 1 0 1
C 0 1 0 1 0 0 0 0 1
D 0 0 1 0 1 0 1 1 1
E 0 0 0 1 0 1 0 1 0
F 1 0 0 0 1 0 1 0 0
G 0 1 0 1 0 1 0 1 0
H 0 0 0 1 1 0 1 0 0
I 0 1 1 1 0 0 0 0 0

Top points
private int number = 9;
Record whether vertices are accessed
Private boolean[] Flag;
Vertex
Private string[] Vertexs = {"A", "B", "C", "D", "E", "F", "G", "H", "I"};
Side
Private int[][] edges = {
{0, 1, 0, 0, 0, 1, 1, 0, 0}, {1, 0, 1, 0, 0, 0, 1, 0, 1}, {0, 1, 0, 1, 0, 0, 0, 0, 1},
{0, 0, 1, 0, 1, 0, 1, 1, 1}, {0, 0, 0, 1, 0, 1, 0, 1, 0}, {1, 0, 0, 0, 1, 0, 1, 0, 0},
{0, 1, 0, 1, 0, 1, 0, 1, 0}, {0, 0, 0, 1, 1, 0, 1, 0, 0}, {0, 1, 1, 1, 0, 0, 0, 0, 0}
};

Deep traversal operation of graphs (recursion)
void Dfstraverse () {
Flag = new Boolean[number];
for (int i = 0; i < number; i++) {
if (flag[i] = = False) {//The current vertex is not accessed
DFS (i);
}
}
}

Depth-first recursive algorithm for graphs
void DFS (int i) {
Flag[i] = true;//First vertex is accessed
System.out.print (Vertexs[i] + "");
for (int j = 0; J < number; J + +) {
if (flag[j] = = False && Edges[i][j] = = 1) {
DFS (j);
}
}
}

Breadth traversal operation of graphs
void Bfstraverse () {
Flag = new Boolean[number];
queue<integer> queue = new linkedlist<integer> ();
for (int i = 0; i < number; i++) {
if (flag[i] = = False) {
Flag[i] = true;
System.out.print (Vertexs[i] + "");
Queue.add (i);
while (!queue.isempty ()) {
Int J = Queue.poll ();
for (int k = 0; k < number; k++) {
if (edges[j][k] = = 1 && flag[k] = = False) {
Flag[k] = true;
System.out.print (Vertexs[k] + "");
Queue.add (k);
}
}
}
}
}
}

Test
public static void Main (string[] args) {
Graphtraveral graph = new graphtraveral ();
SYSTEM.OUT.PRINTLN ("depth traversal operation of graphs (recursion):");
Graph. Dfstraverse ();
System.out.println ("\ n-------------");
SYSTEM.OUT.PRINTLN ("Breadth traversal operation of graphs:");
Graph. Bfstraverse ();
}
}

Java Implementation Graph traversal (depth-first traversal and breadth-first traversal)

Related Article

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.