Java实现【邻接矩阵、邻接表的创建、遍历(DFS,BFS)】

epeppanda
发布于 2021-2-5 09:23
1.8w浏览
0收藏

1.思路图解兼样例:接下来将用此图例作为测试用例  Java实现【邻接矩阵、邻接表的创建、遍历(DFS,BFS)】-鸿蒙开发者社区

2.输出结果:

 

(1)邻接矩阵:Java实现【邻接矩阵、邻接表的创建、遍历(DFS,BFS)】-鸿蒙开发者社区(2)邻接表:Java实现【邻接矩阵、邻接表的创建、遍历(DFS,BFS)】-鸿蒙开发者社区一、邻接矩阵

 

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

public class AdjacencyMatrix {

    private ArrayList<String> vexs; // 顶点表
    private int[][] edges; // 边表
    int numVertexes;
    int numEdges;
    boolean[] visited;

    public AdjacencyMatrix(int numVertexes, int numEdges) {
        this.numVertexes = numVertexes;
        this.numEdges = numEdges;
        this.vexs = new ArrayList<String>(numVertexes);
        this.edges = new int[numVertexes][numVertexes];
        this.visited = new boolean[numVertexes];
    }

    private void insertVex(String v) {
        vexs.add(v);
    }

    private void insertEdge(int v1, int v2, int weight) {
        edges[v1][v2] = weight;
        edges[v2][v1] = weight;
    }

    private void show() {
        for (int[] link : edges) {
            System.out.println(Arrays.toString(link));
        }
    }

    private void DFS(int i) {
        visited[i] = true;
        System.out.print(vexs.get(i) + " ");
        for (int j = 0; j < numVertexes; j++) {
            if (edges[i][j] > 0 && !visited[j]) {
                DFS(j);
            }
        }
    }

    private void DFSTraverse() {
        int i;
        for (i = 0; i < numVertexes; i++) {
            visited[i] = false;
        }
        for (i = 0; i < numVertexes; i++) {
            if (!visited[i]) {
                DFS(i);
            }
        }
    }

    private void BFSTraverse() {
        int i, j;
        LinkedList queue = new LinkedList();
        for (i = 0; i < numVertexes; i++) {
            visited[i] = false;
        }
        for (i = 0; i < numVertexes; i++) {
            if (!visited[i]) {
                visited[i] = true;
                System.out.print(vexs.get(i) + " ");
                queue.addLast(i);
                while (!queue.isEmpty()) {
                    i = (Integer) queue.removeFirst();
                    for (j = 0; j < numVertexes; j++) {
                        if (edges[i][j] > 0 && !visited[j]) {
                            visited[j] = true;
                            System.out.print(vexs.get(j) + " ");
                            queue.addLast(j);
                        }
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        int numVertexes = 9;
        int numEdges = 15;
        AdjacencyMatrix graph = new AdjacencyMatrix(numVertexes, numEdges);
        graph.insertVex("A");
        graph.insertVex("B");
        graph.insertVex("C");
        graph.insertVex("D");
        graph.insertVex("E");
        graph.insertVex("F");
        graph.insertVex("G");
        graph.insertVex("H");
        graph.insertVex("I");
        graph.insertEdge(0, 1, 1);
        graph.insertEdge(0, 5, 1);
        graph.insertEdge(1, 2, 1);
        graph.insertEdge(1, 6, 1);
        graph.insertEdge(1, 8, 1);
        graph.insertEdge(2, 3, 1);
        graph.insertEdge(2, 8, 1);
        graph.insertEdge(3, 4, 1);
        graph.insertEdge(3, 6, 1);
        graph.insertEdge(3, 7, 1);
        graph.insertEdge(3, 8, 1);
        graph.insertEdge(4, 7, 1);
        graph.insertEdge(4, 5, 1);
        graph.insertEdge(5, 6, 1);
        graph.insertEdge(6, 7, 1);
        System.out.println("邻接矩阵");
        graph.show();
        System.out.print("深度优先遍历:");
        graph.DFSTraverse();
        System.out.println();
        System.out.print("广度优先遍历:");
        graph.BFSTraverse();
    }
    
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.

 

二、邻接表

 

Java实现【邻接矩阵、邻接表的创建、遍历(DFS,BFS)】-鸿蒙开发者社区

Java实现【邻接矩阵、邻接表的创建、遍历(DFS,BFS)】-鸿蒙开发者社区

Java实现【邻接矩阵、邻接表的创建、遍历(DFS,BFS)】-鸿蒙开发者社区

Java实现【邻接矩阵、邻接表的创建、遍历(DFS,BFS)】-鸿蒙开发者社区

分类
已于2021-2-5 09:23:33修改
收藏
回复
举报


回复
    相关推荐
    恭喜您,今日已阅读两篇内容,特奖励+2声望, 快来领取吧。