[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