图与其DFS、BFS

class Graph

package DataStructures.graph;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;


/**
 * 图与其DFS、BFS
 * @author coffee
 * @Date 2021-04-05 15:14
 */
public class Graph {

    public static void main(String[] args) {
        String[] vertexes = {"A","B","C","D","E"};
        Graph graph = new Graph(5);
        for (String v : vertexes){
            graph.insertVertex(v);
        }
        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.showGraph();
//        graph.DFS();
        graph.BFS(0);

    }



    private ArrayList<String> vertexList;//存储顶点的集合
    private int[][] edges;//图对应的邻接矩阵
    private int numOfEdges;//边的数目
    private boolean[] isVisited;

    public Graph(int n) {
        edges = new int[n][n];
        vertexList = new ArrayList<>(n);
        isVisited = new boolean[n];
    }

    /**
     * 广度优先(BFS)
     */
    public void BFS(int index){
        ArrayDeque<Integer> queue = new ArrayDeque<>();
        while (index < vertexList.size()){
            if (edges[index][index] != 0 || !isVisited[index]){
                queue.add(index);
                System.out.print(getValueByIndex(index) + "->");
                isVisited[index] = true;
            }
            index++;
        }
        System.out.println("结束");
    }

    /**
     * 深度优先(DFS)
     */
    public void DFS(int index){
        System.out.print(getValueByIndex(index) + "->");
        isVisited[index] = true;
        int s = 0;
        //找下一个顶点
        while (s < vertexList.size() && (edges[index][s] == 0 ||(edges[index][s] != 0 && isVisited[s]))){
            s++;
        }
        if (s > vertexList.size() - 1)
            return;
        DFS(s);
    }
    //重载可以确保所有的顶点都会被访问
    public void DFS(){
        for (int i = 0; i < vertexList.size(); i++) {
            if (!isVisited[i]){
                DFS(i);
            }
        }
        System.out.println("结束");
    }

    /**
     * 插入顶点
     */
    public void insertVertex(String v){
        vertexList.add(v);
    }
    /**
     * 添加边
     */
    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 getNumOfEdges(){
        return numOfEdges;
    }
    /**
     * 返回节点i对应的数据
     */
    public String getValueByIndex(int i){
        return vertexList.get(i);
    }
    /**
     * 返回两个顶点的权值
     */
    public int getWeight(int v1,int v2){
        return edges[v1][v2];
    }
    /**
     * 显示图对应的邻接矩阵
     */
    public void showGraph(){
        for (int[] l : edges){
            System.out.println(Arrays.toString(l));
        }
        System.out.println();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值