图的dfs和bfs

<span style="font-size:32px;">package com.liebao36;

public class Vertex {
	public char label;
	public boolean wasVisited;
	public Vertex(char lab){
		label = lab;
		wasVisited= false;
	}
	
}
</span>

<span style="font-size:32px;">package com.liebao36;

public 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;
	}
	
}
</span>

<span style="font-size:32px;">package com.liebao36;

public 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 tmp = queArray[front++];
		if(front==size){
			front=0;
		}
		return tmp;
	}
	
	public boolean isEmpty(){
		return (rear+1==front||front+size-1==rear);
	}
}
</span>

<span style="font-size:32px;">package com.liebao36;

public class Graph {
	private final int max_vtx = 20;
	private Vertex[] vertexList;//vertex number
	private int[][] adjMat;//matrix
	private int nVerts;//vertex number
	private StackX theStack;
	private Queue theQueue;
	public Graph(){
		vertexList = new Vertex[max_vtx];
		adjMat = new int[max_vtx][max_vtx];
		nVerts = 0;
		for(int j=0;j<max_vtx;j++){
			for(int k=0;k<max_vtx;k++){
				adjMat[j][k] = 0;
			}
		}
		theStack = new StackX();
		theQueue = new Queue();
	}
	//add vertex
	public void addVertex(char lab){
		vertexList[nVerts++] = new Vertex(lab);
	}
	//add edge
	public void addEge(int start,int end){
		adjMat[start][end] = 1;
		adjMat[end][start] = 1;
	}
	public void displayVertex(int v){
		System.out.println(vertexList[v].label);
	}
	
	public void dfs(){//depth first search
		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;//find one 
			}
		}
		return -1;
	}
	
	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;
		}
		
	}
}
</span>

<span style="font-size:32px;">package com.liebao36;

public class DfsApp {
	public static void main(String[] args) {
		Graph theGraph = new Graph();
		theGraph.addVertex('A');
		theGraph.addVertex('B');
		theGraph.addVertex('C');
		theGraph.addVertex('D');
		theGraph.addEge(0, 1);//AB
		theGraph.addEge(0, 2);//AC
		theGraph.addEge(0, 3);//AD
		theGraph.addEge(1, 3);//BD
		System.out.print("Visted:");
		theGraph.dfs();
		System.out.println();
	}
}
</span>

<span style="font-size:32px;">package com.liebao36;

public class BfsApp {
	public static void main(String[] args) {
		Graph theGraph = new Graph();
		theGraph.addVertex('A');
		theGraph.addVertex('B');
		theGraph.addVertex('C');
		theGraph.addVertex('D');
		theGraph.addEge(0, 1);//AB
		theGraph.addEge(0, 2);//AC
		theGraph.addEge(0, 3);//AD
		theGraph.addEge(1, 3);//BD
		System.out.print("Visted:");
		theGraph.bfs();
		System.out.println();
	}
}
</span>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值