图论-邻接矩阵定义及深度和广度遍历

深度优先遍历步骤:
(1),访问一个邻接的未访问顶点,标记它,并且把它放入栈中。
(2),当不能执行规则1时,如果栈不为空,从栈中弹出一个顶点,重复执行规则1。
(3),如果规则1和规则2都不能执行时整个搜索结束。


广度优先遍历步骤:
(1)、访问一个未来访问的邻接点(如果存在),这个邻接点必须是当前顶点的邻接点,标记它,并且把它插入到队列中。
(2)、如果不能执行规则1,那么从队列头取一个顶点(如果存在),并事它成为当前顶点

(3)、如果不能执行规则1和规则2,则搜索结束。

Graph_adjMat.java

package com.mapbar.structure;

import java.util.LinkedList;
import java.util.Stack;
import java.util.Queue;
/**
 * 
 * Class Graph_adjMat.java
 * 
 * Description
 * 
 * Company mapbar
 * 
 * author Chenll E-mail: Chenll@mapbar.com
 * 
 * Version 1.0
 * 
 * Date 2011-11-16 下午03:38:27
 */

//定义节点
class Vertex{
	public char label;
	
	public boolean isVisited;
	
	public Vertex(char label){
		this.label = label;
		this.isVisited = false;
	}
}

//
public class Graph_adjMat {
	//顶点数组
	private Vertex[] vArr;
	
	//定义邻接矩阵
	private int[][] adjMat;
	
	 //顶点的最大数目 
    private int maxSize; 
    
     //当前顶点 下标
    private int currVertex; 
    
    //构造方法
    public Graph_adjMat(int maxSize){
    	this.maxSize = maxSize;
    	vArr = new Vertex[maxSize];
    	adjMat = new int[maxSize][maxSize];
    	for (int i = 0; i<adjMat.length; i++){
    		for(int j = 0; j<adjMat.length; j++){
    			adjMat[i][j] = 0;
    		}
    	}
    	currVertex = 0;
    }
    
    //添加一个顶点
    public void addVertex(char label){
    	vArr[currVertex++] = new Vertex(label);
    }
    
    //添加一个边
    public void addEdge(int start,int end){
    	adjMat[start][end] = 1;
    	adjMat[end][start] = 1;
    }
    
    //显示一个定点
    public void disVertex(int v){
    	System.out.print(vArr[v].label+",");
    }
    
    //深度遍历
    
    public void dfs(){
    	Stack<Integer> stack = new Stack<Integer>();
    	//开始访问第一个顶点
    	vArr[0].isVisited = true;
    	disVertex(0);
    	//入栈
    	stack.push(0);
    	while(!stack.isEmpty()){
    		int v = getUnvisitedVertex(stack.peek());
    		if(v==-1){
    			stack.pop();
    		} else {
    			vArr[v].isVisited = true;
    			disVertex(v);
    			stack.push(v);
    		}
    	}
    	reset();
    }
    
    //广度遍历
    public void bfs(){
    	//注意LinkedList类实现了Queue接口,因此我们可以把LinkedList当成Queue来用
    	Queue<Integer> queue = new LinkedList<Integer>();
    	vArr[0].isVisited = true;
    	disVertex(0);
    	queue.add(0);
    	int v2;
    	while(!queue.isEmpty()){
    		int v1 = queue.remove();
    		while((v2=getUnvisitedVertex(v1))!=-1){
    			vArr[v2].isVisited = true;
    			disVertex(v2);
    			queue.add(v2);
    		}
    	}
    	reset();
    }
    
    //查找没有访问过的邻接点
    public int getUnvisitedVertex(int x){
    	for(int j = 0; j<currVertex; j++){
    		if(adjMat[x][j]==1 && vArr[j].isVisited ==false){
    			return j;
    		}
    	}
    	return -1;
    }
    
    //顶点都置为false
    public void reset(){
    	for(int i = 0; i<currVertex; i++){
    		vArr[i].isVisited = false;
    	}
    }
   
    
    //主调函数
    public static void main(String[] args){
    	Graph_adjMat g = new Graph_adjMat(10);
    	g.addVertex('a');
    	g.addVertex('b');
    	g.addVertex('c');
    	g.addVertex('d');
    	g.addVertex('e');
    	g.addVertex('f');   	
    	g.addEdge(0,1);
    	g.addEdge(0,2);
    	g.addEdge(1,3);
    	System.out.println("深度优先遍历:");
    	g.dfs();
    	System.out.println("\r\n广度优先遍历:");
    	g.bfs();
    }
}

output:

深度优先遍历:
a,b,d,c,
广度优先遍历:
a,b,c,d,


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值