图的DFS与BFS Java版

package graph;

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

/**
 * @author yzy
 * @version 1.0
 */
public class Graph {
    private ArrayList<String> vertexList;//存储顶点的集合
    private int[][] edges;//存储边的邻接矩阵
    private int numOfEdges;//边的数目
    private boolean[] isVisited;//保存状态,看结点是否被访问过了
    public static void main(String[] args) {
        //测试1
        /*int n = 5;
        String[] vertexValue = {"a","b","c","d","e"};
        Graph graph = new Graph(n);
        for(String value:vertexValue){
            graph.insertVertex(value);
        }
        graph.insertEdge(0,1,1);
        graph.insertEdge(0,2,1);
        graph.insertEdge(1,2,1);
        graph.insertEdge(1,3,1);
        graph.insertEdge(0,4,1);
        graph.bfs(0);*/
        //测试2
        int n = 8;
        String[] vertexValue = {"1","2","3","4","5","6","7","8"};
        Graph graph = new Graph(n);
        for(String value:vertexValue){
            graph.insertVertex(value);
        }
        graph.insertEdge(0,1,1);
        graph.insertEdge(0,2,1);
        graph.insertEdge(1,3,1);
        graph.insertEdge(1,4,1);
        graph.insertEdge(3,7,1);
        graph.insertEdge(4,7,1);
        graph.insertEdge(2,5,1);
        graph.insertEdge(2,6,1);
        graph.insertEdge(5,6,1);
        graph.bfs(0);
    }
    //构造器,n为顶点的数目
    public Graph(int n){
        edges = new int[n][n];
        vertexList = new ArrayList<String>(n);
        numOfEdges = 0;
        isVisited = new boolean[n];
    }
    //插入结点
    public void insertVertex(String vertex){
        vertexList.add( vertex);
    }
    //插入边
    //v1v2为顶点对应的下标
    public void insertEdge(int v1,int v2,int weight){
        edges[v1][v2] = weight;
        edges[v2][v1] = weight;
        numOfEdges++;
    }
    //返回顶点的个数
    public int getNumOfVertex(){
        return vertexList.size();
    }
    //返回边的个数
    public int getNumOfEdge(){
        return numOfEdges;
    }
    //返回下标对应的结点
    public String getValueByIndex(int i){
        return vertexList.get(i);
    }
    //显示图对应的矩阵
    public void showGraph(){
        for (int i = 0; i < getNumOfVertex(); i++) {
            for (int j = 0; j < getNumOfVertex(); j++) {
                System.out.print(edges[i][j] + " ");
            }
            System.out.println();
        }
    }

    //得到结点i的第一个未访问过的邻接结点的下标,否则返回-1
    public int getFirstIndex(int i){
        int k;
        for (k = 0; k < getNumOfVertex(); k++) {
            if(edges[i][k] == 1 && isVisited[k] == false){
                return k;
            }
        }
        return -1;
    }

    //DFS,i初始为0
    public void dfs(int i){
        //访问并且标注
        System.out.println(getValueByIndex(i));
        isVisited[i] = true;
        //看看是否全遍历过了
        int judge;
        for (judge = 0; judge < getNumOfVertex(); judge++) {
            if(isVisited[judge] == false){
                break;
            }
        }
        if(judge == getNumOfVertex()){
            return;
        }
        //以上看看是否全遍历过了
        while(true){
            int next = getFirstIndex(i);
            if(next > 0){
                dfs(next);
            }else {
                return;
            }
        }

    }

    //BFS
    public void bfs(int i){
        int u;
        int w;
        //队列
        LinkedList queue = new LinkedList();
        //访问头结点
        System.out.println(getValueByIndex(i));
        isVisited[i] = true;
        //头结点入队列
        queue.addLast(i);
        //
        while (true){
            u = (Integer) queue.removeFirst();//返回的是object
            w = getFirstIndex(u);
            while (w != -1){//找到
                System.out.println(getValueByIndex(w));
                isVisited[w] = true;
                queue.addLast(w);
                w = getFirstIndex(u);
            }
            //如果全部访问则退出
            int judge;
            for (judge = 0; judge < getNumOfVertex(); judge++) {
                if(isVisited[judge] == false){
                    break;
                }
            }
            if(judge == getNumOfVertex()){
                return;
            }
            //以上如果全部访问则退出
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值