尚硅谷Java數據結構學習記錄33- 廣度深度優先算法

廣度是儘可能的找同一層的結點,深度是儘可能找下一層的結點,需要設置數組記錄某個結點是否被訪問過,如果沒有被訪問過,就進行訪問,注意廣度需要用隊列的方式完成。

package graph;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;

public class graph {
	private ArrayList<String> vertexList; //存儲結點集合
	private int[][] edges; //存儲圖對應的鄰接矩陣
	private int numOfEdges;//表示表的數目
	//定義數組boolean[],記錄某個結點是否被訪問
	private boolean[] isVisited;

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int n = 8;
		String Vertexs[] = {"1","2","3","4","5","6","7","8"};
		//創建圖對象
		graph Graph = new graph(n);
		//循環的添加結點
		for(String vertex : Vertexs) {
			Graph.insertVertex(vertex);
		}
		
		
		//添加邊
		// A-B A-C B-C B-D B-E
		Graph.insetEdge(0, 1, 1);
		Graph.insetEdge(0, 2, 1);

		

		Graph.insetEdge(1, 3, 1);
		Graph.insetEdge(1, 4, 1);
		
		Graph.insetEdge(3, 7, 1);
		Graph.insetEdge(4, 7, 1);
		Graph.insetEdge(2, 5, 1);
		Graph.insetEdge(2, 6, 1);
		Graph.insetEdge(5, 6, 1);
		
		Graph.showGraph();
		
		
		//深度遍歷
		System.out.println("深度遍歷");
		Graph.DFS();
		System.out.println("廣度遍歷");

		
		Graph.BFS();
	}

	public graph(int n) {
		//初始化矩陣和vertexList
		edges = new int[n][n];
		vertexList = new ArrayList<String>(n);
		numOfEdges = 0;
		 isVisited = new  boolean[n];
	}
	//根據前一個鄰接結點的下標獲取下一個
	public int getNexthbor(int v1,int v2) {
		for(int j = v2 +1; j < vertexList.size(); j++) {
			if(edges[v1][j] > 0)
				return j;
	
		}
		return -1;
	}
	
	///得到第一個鄰接結點的下標
	//如果存在 返回對應下標
	public int getFirstNeighbor(int index) {
		for(int j = 0; j < vertexList.size(); j++)
			if(edges[index][j] > 0)
				return j;
		return -1;
	}
	//遍歷所有結點進行BFS
	private void BFS() {
		for(int i = 0; i < getNumOfVertex(); i++) {
			BFS(isVisited,i);
		}
	}
	
	//對一個結點進行廣度優先遍歷
	private void BFS(boolean[] isVistied, int i) {
		int u; // 表示隊列的頭結點對應下標
		int w; //鄰接結點
		
		//隊列,記錄結點訪問的順序
		LinkedList queue = new LinkedList();
		
		//訪問結點,輸出結點信息
		System.out.println(getValueByIndex(i)+"=>");
		isVistied[i] = true;
		//將結點加入隊列
		queue.add(i);
		
		while(!queue.isEmpty()) {
			//取出隊列的頭結點
			u = (Integer)queue.removeFirst();
			w = getFirstNeighbor(u);
			while(w != -1) {
				//是否訪問
				if(!isVisited[w]) {
					//System.out.println(getValueByIndex(w)+"=>");
					isVisited[w] = true;;
					queue.addLast(w);

				}
					//以u爲中心,找下一個鄰居
					w = getNexthbor(u,w);
				
				
			}
		}
	}
	
	
	//DFS
	public void DFS(boolean[] isVisited,int i) {
		//首先訪問該結點
		System.out.println(getValueByIndex(i) + "->");
		isVisited[i] = true;
		
		//查找這個結點的鄰接結點
		int w = getFirstNeighbor(i);
		while(w != -1) {
			if(!isVisited[w]) {
				DFS(isVisited,w);
			}
			//如果已經被訪問過
			else {
				w = getNexthbor(i,w);
			}
		}
	}
	
	
	//對DFS進行重載 遍歷所有的結點並進行DFS
	public void DFS() {
		//遍歷所有的結點進行DFS
		for(int i = 0; i < getNumOfVertex(); i++) {
			if(!isVisited[i]) {
				DFS(isVisited,i);
			}
		}
	}
	
	
	public int getNumOfVertex() {
		return vertexList.size();
	}
	
	//返回結點i(下標對應的數據)
	public String getValueByIndex(int i) {
		return vertexList.get(i);
	}
	
	//返回v1和v2的值
	public int getWeight(int v1, int v2) {
		return edges[v1][v2];
	}

	public int getNumOfEdges() {
		return numOfEdges;
	}

	public void insetEdge(int v1, int v2, int weight) {
		edges[v1][v2] = weight;
		edges[v2][v1] = weight;
		numOfEdges++;

	}

	//  插入結點
	public void insertVertex(String vertex) {
		vertexList.add(vertex);
	}
	
	//顯示圖對應的矩陣
	public void showGraph() {
		for(int[] link : edges) {
			System.out.println(Arrays.toString(link));
		}
	}
	


}

 

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