数据结构与算法-图(深度优先遍历dfs &广度优先遍历bfs)


在这里插入图片描述

一、优先遍历

示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。
在这里插入图片描述

二、代码

1.Graph

代码如下(示例):

 Graph;
//使用二维矩阵来表示图

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

public class Graph {
    
    
    //用链表表示其中的节点的数据
    public ArrayList<String> VertexList;
    public int[][] edges;
    public int numofedge;
    //深度优先遍历(dfs)中看每个节点被访问没
    public boolean[] isVisited;

    //添加一个构造方法初始化图
    public Graph(int num) {
    
    
        VertexList = new ArrayList<String>(num);
        edges = new int[num][num];
        numofedge = 0;
        isVisited = new boolean[num];
    }
    //找到当前节点的最近的节点

    /**
     * @param i 从当前节点开始查找出相邻节点
     * @return 找到返回该结点的下标,没有则返回-1
     */
    public int getFirstNeighbor(int i) {
    
    
        for (int j = 0; j < VertexList.size(); j++) {
    
    
            if (edges[i][j] > 0) {
    
    
                return j;
            }
        }
        return -1;
    }
    //找到当前节点的另外一个临接节点

    /**
     * @param i  表示当前节点
     * @param v1 表示领临接节点的下一个节点
     * @return 找到返回下标,没有返回-1
     */
    public int getNextNeightbor(int i, int v1) {
    
    
        for (int j = v1 + 1; j < VertexList.size(); j++) {
    
    
            if (edges[i][j] > 0) {
    
    
                return j;
            }
        }
        return -1;
    }

    //深度优先遍历算法
    public void dfs(boolean[] isVisited, int n) {
    
    
        System.out.print(getVertex(n) + "->");
        //将这个节点设置为true
        isVisited[n] = true;
        //查找这个节点的第一个临接节点w
        int w = getFirstNeighbor(n);
        while (w != -1) {
    
    
            if (isVisited[w] == false) {
    
    
                dfs(isVisited, w);
            }
            w = getNextNeightbor(n, w);
        }
    }

    //对dfs算法进行一个重载,遍历非联通图,遍历所有节点
    public void dfs() {
    
    
        for (int i = 0; i < getNumofVertex(); i++) {
    
    
            if (isVisited[i] == false) {
    
    
                dfs(isVisited, i);
            }
        }
    }

    //广度优先算法
    public void bfs(boolean[] isVisited, int n) {
    
    
        //构建一个LinkedList集合,模拟队列
        LinkedList<Integer> Queue = new LinkedList<>();
        isVisited[n] = true;
        Queue.addLast(n);
        while (Queue.size() != 0) {
    
    
            int u = Queue.removeFirst();
            int w = getFirstNeighbor(u);
            if (w != -1) {
    
    
                if (isVisited[w] == false) {
    
    
                    System.out.print(getVertex(w));
                    isVisited[w] = true;
                    Queue.addLast(w);
                }
                w = getNextNeightbor(u, w);
            }
        }
    }

    //对bfs算法进行一个重载,遍历非联通图,遍历所有节点
    public void bfs() {
    
    
        for (int j = 0; j < getNumofVertex(); j++) {
    
    
            if (isVisited[j] == false) {
    
    
                bfs(isVisited, j);
            }
        }
    }

    //添加节点到VertexList中
    public void InsertVertex(String Vertex) {
    
    
        VertexList.add(Vertex);
    }

    //添加边的信息
    public void InsertEdge(int n1, int n2, int weight) {
    
    
        //无向图
        edges[n1][n2] = weight;
        edges[n2][n1] = weight;
        numofedge++;
    }

    //返回定点数
    public int getNumofVertex() {
    
    
        return VertexList.size();
    }

    //返回边数
    public int getNumofedge() {
    
    
        return numofedge;
    }

    //返回节点i对应的字符串值 0->'A'
    public String getVertex(int i) {
    
    
        return VertexList.get(i);
    }

    //返回2个节点对应的权重
    public int getweight(int n1, int n2) {
    
    
        return edges[n1][n2];
    }

    //打印整个图
    public void show() {
    
    
        for (int[] row : edges) {
    
    
            System.out.println(Arrays.toString(row));
        }
    }
}

2.Demo

代码如下(示例):

package Graph;

public class GraphDemo {
    
    
    public static void main(String[] args){
    
    
        int n =5; //节点的个数
        String[] Vertex = {
    
    "A","B","C","D","E"};
        Graph graph = new Graph(5);
        for (String s:Vertex) {
    
    
            graph.VertexList.add(s);
        }
        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.show();
        //graph.dfs();
        graph.dfs();
    }
}

总结

按步骤来写

猜你喜欢

转载自blog.csdn.net/slighting1128/article/details/112632680