Graph traversal (Adjacent matrix)

Source: Internet
Author: User

[Java] package com. wzs; import java. util. using list; import java. util. queue; // Graph traversal public class Graph {// adjacent matrix storage Graph // -- a B c d e f g h I // A 0 1 0 0 0 0 1 1 0/ /B 1 0 1 0 0 0 1 0 1/C 0 1 0 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 1 0 1 0 0 0 // G 0 1 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 // number of vertices private int number = 9; // record whether vertex is accessed private bo Olean [] flag; // vertex private String [] vertexs = {"A", "B", "C", "D", "E", "F ", "G", "H", "I"}; // edge 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, 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 graph traversal (recursion) void DFSTraverse () {flag = new boolean [number]; for (int I = 0; I <number; I ++) {if (flag [I] = false) {// The current vertex is not accessed by DFS (I );}}} // void DFS (int I) {flag [I] = true; // The I vertex is accessed by System. out. print (vertexs [I] + ""); for (int j = 0; j <number; j ++) {if (flag [j] = false & edges [I] [j] = 1) {DFS (j );}}} // void BFSTraverse () {flag = new boolean [nu Mber]; Queue <Integer> queue = new Queue list <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) {Graph graph = new Graph (); System. out. println ("Deep graph traversal (recursion):"); graph. DFSTraverse (); System. out. println ("\ n -------------"); System. out. println ("graph breadth traversal operation:"); graph. BFSTraverse () ;}} output result: [java] graph depth traversal (recursion): a B c d e f g h I ------------- graph breadth traversal operation: A B F G C I E D H

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.