圖的深度優先搜索DFS和廣度優先搜索BFS 筆記

(下圖皆來自:java數據結構和算法(第二版))

一、深度優先搜索DFS:


package com.hqy.graph;

class StackX{
	private final int SIZE=20;
	private int[] st;
	private int top;
	
	public StackX(){
		st=new int[SIZE];
		top=-1;
	}
	
	public void push(int j){
		st[++top]=j;
	}
	
	public int pop(){
		return st[top--];
	}
	
	public int peek(){
		return st[top];
	}
	
	public boolean isEmpty(){
		return top==-1;
	}
}


class Vertex{
	public char label;
	public boolean wasVisited;
	
	public Vertex(char lab){
		label=lab;
		wasVisited=false;
	}
}


class Graph{
	private final int MAX_VERTS=20;
	private Vertex vertexList[];
	private int adjMat[][];
	private int nVerts;
	private StackX theStack;
	
	public Graph(){
		vertexList=new Vertex[MAX_VERTS];
		adjMat=new int[MAX_VERTS][MAX_VERTS];
		nVerts=0;
		for(int j=0;j<MAX_VERTS;j++){
			for(int k=0;k<MAX_VERTS;k++){
				adjMat[j][k]=0;
			}
		}
		theStack=new StackX();
	}
	
	public void addVertex(char lab){
		vertexList[nVerts++]=new Vertex(lab);
	}
	
	public void addEdge(int start,int end){
		adjMat[start][end]=1;
		adjMat[end][start]=1;
	}
	
	public void displayVertex(int v){
		System.out.print(vertexList[v].label);
	}
	
	public void dfs(){
		vertexList[0].wasVisited=true;
		displayVertex(0);
		theStack.push(0);
		
		while(!theStack.isEmpty()){
			int v=getAdjUnvisitedVertex(theStack.peek());
			if(v==-1){
				theStack.pop();
			}else{
				vertexList[v].wasVisited=true;
				displayVertex(v);
				theStack.push(v);
			}
		}
		
		for(int j=0;j<nVerts;j++){
			vertexList[j].wasVisited=false;
		}
	}
	
	public int getAdjUnvisitedVertex(int v){
		for(int j=0;j<nVerts;j++){
			if(adjMat[v][j]==1&&vertexList[j].wasVisited==false){
				return j;
			}
		}
		return -1;
	}
}

public class DFSApp {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Graph theGraph=new Graph();
		theGraph.addVertex('A');
		theGraph.addVertex('B');
		theGraph.addVertex('C');
		theGraph.addVertex('D');
		theGraph.addVertex('E');
		theGraph.addVertex('F');
		
		theGraph.addEdge(0, 1);
		theGraph.addEdge(1, 5);
		theGraph.addEdge(0, 5);
		theGraph.addEdge(3, 4);
		theGraph.addEdge(2, 3);
		theGraph.addEdge(2, 5);
		theGraph.addEdge(4, 5);
		
		System.out.print("visits:");
		theGraph.dfs();
		System.out.println();	
		
	}

二、廣度優先搜索BFS:

package com.hqy.graph;

class Queue{
	private final int SIZE=20;
	private int[] queArray;
	private int front;
	private int rear;
	
	
	public Queue(){
		queArray=new int [SIZE];
		front=0;
		rear=-1;
	}
	
	public void insert(int j){
		if(rear==SIZE-1){
			rear=-1;
		}
		queArray[++rear]=j;
	}
	
	public int remove(){
		int temp=queArray[front++];
		if(front==SIZE){
			front=0;
		}
		return temp;
	}
	
	public boolean isEmpty(){
		return rear+1==front||front+SIZE-1==rear;
	}
}


class Vertex2{
	public char label;
	public boolean wasVisited;
	
	public Vertex2(char lab){
		label=lab;
		wasVisited=false;
	}
}


class Graph2{
	private final int MAX_VERTS=20;
	private Vertex vertexList[];
	private int adjMat[][];
	private int nVerts;
	private Queue theQueue;
	
	public Graph2(){
		vertexList=new Vertex[MAX_VERTS];
		adjMat=new int[MAX_VERTS][MAX_VERTS];
		nVerts=0;
		for(int j=0;j<MAX_VERTS;j++){
			for(int k=0;k<MAX_VERTS;k++){
				adjMat[j][k]=0;
			}
		}
		theQueue=new Queue();
	}
	
	public void addVertex(char lab){
		vertexList[nVerts++]=new Vertex(lab);
	}
	
	public void addEdge(int start,int end){
		adjMat[start][end]=1;
		adjMat[end][start]=1;
	}
	
	public void displayVertex(int v){
		System.out.print(vertexList[v].label);
	}
	
	public void bfs(){
		vertexList[0].wasVisited=true;
		displayVertex(0);
		theQueue.insert(0);
		int v2;
		
		while(!theQueue.isEmpty()){
			int v1=theQueue.remove();
			
			while((v2=getAdjUnvisitedVertex(v1))!=-1){
				vertexList[v2].wasVisited=true;
				displayVertex(v2);
				theQueue.insert(v2);
			}
		}
		
		for(int j=0;j<nVerts;j++){
			vertexList[j].wasVisited=false;
		}
	}
	
	public int getAdjUnvisitedVertex(int v){
		for(int j=0;j<nVerts;j++){
			if(adjMat[v][j]==1&&vertexList[j].wasVisited==false){
				return j;
			}
		}
		return -1;
	}
}

public class BFSApp {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Graph2 theGraph=new Graph2();
		theGraph.addVertex('A');
		theGraph.addVertex('B');
		theGraph.addVertex('C');
		theGraph.addVertex('D');
		theGraph.addVertex('E');
		theGraph.addVertex('F');
		
		theGraph.addEdge(0, 1);
		theGraph.addEdge(1, 5);
		theGraph.addEdge(0, 5);
		theGraph.addEdge(3, 4);
		theGraph.addEdge(2, 3);
		theGraph.addEdge(2, 5);
		theGraph.addEdge(4, 5);
		
		System.out.print("visits:");
	    theGraph.bfs();
	    System.out.println();
	}

}

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