无向无权图

该博客介绍了图的表示方法,包括顶点类Vertex的定义,并实现了深度优先搜索(dfs)、广度优先搜索(bfs)以及最小生成树(mst)的算法。通过一个具体的实例展示了如何在图中添加顶点和边,以及如何进行遍历和构建最小生成树。
摘要由CSDN通过智能技术生成

图的表示

class Vertex {
    public char label;
    // 用于遍历,防止在环中循环
    public boolean wasVisited;
    // 最小生成树中使用
    public boolean isInTree;

    public Vertex(char label) {
        this.label = label;
        wasVisited = false;
        isInTree = false;
    }
}

dfs、bfs、mst

public class GraphDemo {
    private final Vertex[] vertexList;
    private final int[][] adjMat;
    private int nVerts;

    public GraphDemo() {
        int MAX_VERTS = 20;
        this.vertexList = new Vertex[MAX_VERTS];
        adjMat = new int[MAX_VERTS][MAX_VERTS];
        nVerts = 0;
    }

    public void addVertex(char label) {
        vertexList[nVerts++] = new Vertex(label);
    }

    public void addEdge(int start, int end) {
        adjMat[start][end] = 1;
        adjMat[end][start] = 1;
    }

    public void displayVertex(int v) {
        System.out.print(vertexList[v].label);
    }

    /**
     * 寻找入度为0的节点作为深搜的开始节点
     *
     * @param v
     * @return
     */
    public int getAdjUnvisitedVertex(int v) {
        for (int i = 0; i < nVerts; i++) {
            if (adjMat[v][i] == 1 && !vertexList[i].wasVisited) return i;
        }
        return -1;
    }


    /**
     * 深搜遍历图
     */
    public void dfs() {
        Stack<Integer> theStack = new Stack<>();
        vertexList[0].wasVisited = true;
        displayVertex(0);
        theStack.push(0);
        while (!theStack.isEmpty()) {
            int v = getAdjUnvisitedVertex(theStack.peek());
            if (v == -1) theStack.pop();
            else {
                vertexList[v].wasVisited = true;
                displayVertex(v);
                theStack.push(v);
            }
        }
        for (int i = 0; i < nVerts; i++) {
            vertexList[i].wasVisited = false;
        }
    }

    /**
     * 广搜遍历图
     */
    public void bfs() {
        Deque<Integer> theQueue = new LinkedList<>();
        vertexList[0].wasVisited = true;
        displayVertex(0);
        theQueue.addFirst(0);
        int v2;
        while (!theQueue.isEmpty()) {
            int v1 = theQueue.remove();
            while ((v2 = getAdjUnvisitedVertex(v1)) != -1) {
                vertexList[v2].wasVisited = true;
                displayVertex(v2);
                theQueue.addFirst(v2);
            }
        }
        for (int i = 0; i < nVerts; i++) {
            vertexList[i].wasVisited = false;
        }
    }

    /**
     * 最小生成树
     */
    public void mst() {
        Stack<Integer> theStack = new Stack<>();
        vertexList[0].wasVisited = true;
        theStack.push(0);
        while (!theStack.isEmpty()) {
            int currentVertex = theStack.peek();
            int v = getAdjUnvisitedVertex(currentVertex);
            if (v == -1) theStack.pop();
            else {
                vertexList[v].wasVisited = true;
                theStack.push(v);
                displayVertex(currentVertex);
                displayVertex(v);
                System.out.print(" ");
            }
        }
        for (int i = 0; i < nVerts; i++) {
            vertexList[i].wasVisited = false;
        }
    }


    public static void main(String[] args) {
        GraphDemo gd = new GraphDemo();
        gd.addVertex('0');
        gd.addVertex('1');
        gd.addVertex('2');
        gd.addVertex('3');
        gd.addVertex('4');
        gd.addVertex('5');
        gd.addVertex('6');
        gd.addVertex('7');
        gd.addEdge(0, 3);
        gd.addEdge(0, 4);
        gd.addEdge(1, 4);
        gd.addEdge(2, 5);
        gd.addEdge(3, 6);
        gd.addEdge(4, 6);
        gd.addEdge(5, 7);
        gd.addEdge(6, 7);
        System.out.println("visit: dfs ");
        gd.dfs();
        System.out.println("\n+++++ bfs +++++++");
        gd.bfs();
        System.out.println("\n最小生成树");
        gd.mst();
    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值