圖的深度優先遍歷與廣度優先遍歷

看了一些書與網上一些例子後,對圖進行了一些總結:

1,定義圖的數據結構:

class Graph{                              //定義一個圖結構
	int n ;                               //圖的點的數量        
	boolean visited[] = new boolean[n] ;  //遍歷標誌
	int matrix [][] = new int[n][n] ;     //圖的鄰接矩陣
	char vertex[] = new char[n] ;         //點的標誌字符
	public Graph(int n){                  //構造函數
		this.n = n ;
	}
	
}

2,圖的深度優先遍歷與廣度優先遍歷:

public static void deptSearch(Graph g , int x){    //深度優先遍歷
	   for (int i = 0; i < g.n; i++) {
		  if(g.visited[i]==false&&g.matrix[x][i]==1){
			  g.visited[i] = true ;
			  visit(g,i) ;
			  deptSearch(g, i) ;
		  }
	   }
   }
   
   public static void widthSearch(Graph g , int x){   //廣度優先遍歷
	   LinkedList<Integer> queue = new LinkedList<Integer>() ;   //定義一個隊列存儲圖遍歷時的點
	   queue.add(x) ;                                            //訪問第一個元素
	   g.visited[x] = true ;                                     //第一個標誌爲true
	   visit(g,x) ;
	   while(queue.size()!=0){
		   int k = queue.pollFirst() ;                    //獲取隊列第一個元素
		   for (int i = 0; i < g.n; i++) {
			   if(g.visited[i]==false&&g.matrix[k][i]==1){
				   g.visited[i] = true ;  //遍歷的標誌位true
				   queue.add(i) ;  //插入隊列
				   visit(g,i) ;
			   }
		   }
	   }
   } 
   public static void visit(Graph g,int i){    
	   System.out.print("->"+g.vertex[i]);
   }

3,測試例子:

public class Main {
   private static Scanner scanner = new Scanner(System.in) ;
   public static void main(String[] args) {
	  System.out.println("請輸入圖的點的數目:") ;
	  int n = scanner.nextInt() ;           //輸入圖的點的數目
	  Graph g = new Graph(n) ;
	  
	  int matrix[][] = new int[n][n] ;      //圖的鄰接矩陣
	  System.out.println("請輸入圖的鄰接矩陣:") ;
	  for (int i = 0; i < matrix.length; i++) {
		  for (int j = 0; j < matrix[0].length; j++) {
			 matrix[i][j] = scanner.nextInt() ;
		  }
	  }
	  char []vertex = {'a','b','c','d','e','f','g'} ;  //定義圖的點的字符
	  boolean visited[] = new boolean[n] ;             //定義訪問標誌
	  for (int i = 0; i < visited.length; i++) {   //全部標記爲未遍歷
		  visited[i] = false ;
	  }
	  
	  
	  g.matrix = matrix ;
	  g.visited = visited ;
	  g.vertex = vertex ;
	  
	  
	  System.out.println("深度度優先遍歷---------------------------------------");
	  System.out.print("遍歷序列:");
	  deptSearch(g, 0) ;
	  System.out.println();
	 
	  for (int i = 0; i < visited.length; i++) {   //全部標記爲未遍歷
		  visited[i] = false ;
	  }
	  System.out.println("廣度優先遍歷---------------------------------------");
	  System.out.print("遍歷序列:");
	  widthSearch(g, 0) ;
   }
}

程序運行結果:

測試例子:


--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

請輸入圖的點的數目:
7
請輸入圖的鄰接矩陣:
1 0 1 1 1 1 0
0 1 1 1 0 1 1
1 1 1 0 0 0 0
1 1 0 1 0 0 0
1 0 0 0 1 0 1
1 1 0 0 0 1 1
0 1 0 0 1 1 1

深度度優先遍歷---------------------------------------
遍歷序列:->a->c->b->d->f->g->e
廣度優先遍歷---------------------------------------
遍歷序列:->a->c->d->e->f->b->g


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章