java图的深度优先遍历(dfs)和广度优先遍历

java图的深度优先遍历(dfs)和广度优先遍历,第1张


import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
public class Graph {
    //顶点集合
    private ArrayList<String> vertexList;
    //邻接矩阵
    private int[][] edges;
    //边的数目
    private int numOfEdges;
    //访问标记
    private boolean[] isVisited;

    public Graph(int n) {
        this.vertexList = new ArrayList<>(n);
        this.edges = new int[n][n];
        this.numOfEdges = 0;
    }

    public static void main(String[] args) {
//        int n = 5;
//        String[] vertexs = {"A","B","C","D","E"};
        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);
        }
        //添加边
//        graph.insertEdge(0,1,1);
//        graph.insertEdge(0,2,1);
//        graph.insertEdge(1,2,1);
//        graph.insertEdge(1,3,1);
//        graph.insertEdge(1,4,1);
        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.showGraph();
        System.out.println("深度优先遍历:");
        graph.dfs();
        System.out.println();
        System.out.println("广度优先遍历:");
        graph.bfs();

    }
    public void insertVertex(String vertex) {
        vertexList.add(vertex);
    }

    /**
     * 给v1,v2边添加权值
     * @param v1
     * @param v2
     * @param wight
     */
    public void insertEdge(int v1,int v2,int wight) {
        edges[v1][v2] = wight;
        edges[v2][v1] = wight;
        numOfEdges++;
    }

    public void showGraph() {
        for (int[] edge : edges) {
            System.out.println(Arrays.toString(edge));
        }
    }

    public void dfs() {
        isVisited = new boolean[vertexList.size()];
        for (int i = 0; i < vertexList.size(); i++) {
            if (!isVisited[i]) {
                dfs(isVisited,i);
            }
        }
    }
    //i表示第i个节点==行数
    public void dfs(boolean[] isVisited,int i) {
        //输出该节点
        System.out.print(vertexList.get(i)+" ");
        //将该节点置为已访问过
        isVisited[i] = true;
        //拿到该节点的第一个邻接节点
        int isHas = getFirstNeighbour(i);
        //判断该节点是否有邻接节点
        while (isHas != -1) {
            //有邻接节点
            //判断该点是否被访问过 ,此时isHas是邻接节点的下标
            if (!isVisited[isHas]) {
                dfs(isVisited,isHas);
            }
            //如果被访问过了,此行的下一个邻接节点
            isHas = getNextNeighbour(i,isHas);
        }
        //改下标已经没有可用的节点了,则从
    }

    //广度优先遍历
    public void bfs() {
        isVisited = new boolean[vertexList.size()];
        for (int i = 0; i < vertexList.size(); i++) {
            if (!isVisited[i]) {
                bfs(isVisited,i);
            }
        }
    }

    public void bfs(boolean[] isVisited,int i) {
        //用于存放bfs的遍历节点的下标
        Queue<Integer> queue = new LinkedList<>();
        //输出当前的节点
        System.out.print(vertexList.get(i)+" ");
        //设置为已经访问过
        isVisited[i] = true;
        //队列增加节点
        queue.add(i);
        while (!queue.isEmpty()) {
            int index = queue.poll();
            //判断该节点是否有邻接节点
            int isHas = getFirstNeighbour(index);
            while (isHas != -1) {
                if (!isVisited[isHas]) {
                    System.out.print(vertexList.get(isHas)+" ");
                    isVisited[isHas] = true;
                    queue.add(isHas);
                }
                //找到该行所有的邻接节点
                isHas = getNextNeighbour(index,isHas);
            }
        }
        

    }

    public int getFirstNeighbour(int index) {
        for (int i = 0; i < vertexList.size(); i++) {
            if (edges[index][i] == 1) {
                return i;
            }
        }
        return -1;
    }

    public int getNextNeighbour(int i,int index) {
        for (int j = index+1; j < vertexList.size(); j++) {
            if (edges[i][j] == 1) {
                return j;
            }
        }
        return -1;
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/877176.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-13
下一篇 2022-05-13

发表评论

登录后才能评论

评论列表(0条)

保存