Java Data Structures and Algorithms - Graphs

1. About the data structure of the graph

Why have pictures?

Earlier we learned about linear tables and trees. Linear tables are limited to a relationship between a direct predecessor and a direct successor. A tree can only have one direct predecessor, which is the parent node. When we need to represent a many-to-many relationship, here we will Diagram used.

A graph is a data structure in which a node can have zero or more adjacent elements. The connection between two nodes is called an edge. Nodes can also be called vertices. As shown in the figure:

There are two ways to represent graphs: two-dimensional array representation (adjacency matrix); linked list representation (adjacency list).

  • Adjacency matrix: The adjacency matrix is ​​a matrix that represents the adjacent relationship between vertices in the graph. For a graph of n vertices, the row and col of the matrix represent 1....n points.

  • Adjacency list: The adjacency matrix needs to allocate n edge space for each vertex. In fact, many edges do not exist, which will cause a certain loss of space. The implementation of the adjacency list only cares about the edges that exist, not the edges that don't exist. So there is no wasted space, and the adjacency list consists of an array + a linked list.


2. Code Case

Let's take the following picture as an example to make a simple implementation of the picture. Here we use an adjacency matrix-based approach.

package com.szh.graph;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *
 */
class Graph {
    public List<String> vertexList; //存储图中所有顶点的集合
    public int[][] edges; //存储图对应的邻接矩阵
    public int numOfEdges; //表示图中边的数目

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

    //返回图中结点的个数
    public int getNumOfVertex() {
        return vertexList.size();
    }

    //显示图对应的邻接矩阵
    public void showGraph() {
        for (int[] link : edges) {
            System.out.println(Arrays.toString(link));
        }
    }

    //获取图中边的数目
    public int getNumOfEdges() {
        return numOfEdges;
    }

    //返回结点i(下标)对应的数据 0->"A" 1->"B" 2->"C"
    public String getValueByIndex(int i) {
        return vertexList.get(i);
    }

    //返回v1和v2的权值
    public int getWeight(int v1, int v2) {
        return edges[v1][v2];
    }

    //向图中插入新的结点
    public void insertVertex(String vertex) {
        vertexList.add(vertex);
    }

    //向图中添加新的边
    public void insertEdge(int v1, int v2, int weight) {
        edges[v1][v2] = weight;
        edges[v2][v1] = weight;
        numOfEdges++;
    }
}

public class GraphDemo {
    public static void main(String[] args) {
        int n = 5; //定义图中结点的个数
        //定义图中的结点 (A=0, B=1, C=2, D=3, E=4)
        String[] vertexs = {"A", "B", "C", "D", "E"};
        //创建图对象
        Graph graph = new Graph(n);
        //循环向图中添加结点
        for (String vertex : vertexs) {
            graph.insertVertex(vertex);
        }
        System.out.println("此时图中共有 " + graph.getNumOfVertex() + " 个结点");
        //向图中添加边
        graph.insertEdge(0, 1, 4); // A -> B
        graph.insertEdge(0, 2, 2); // A -> C
        graph.insertEdge(1, 2, 5); // B -> C
        graph.insertEdge(1, 3, 8); // B -> D
        graph.insertEdge(1, 4, 1); // B -> E
        System.out.println("此时图中共有 " + graph.getNumOfEdges() + " 条边");
        //显示整个图
        graph.showGraph();
        //获取B和D两个结点之间边的权值
        System.out.println(graph.getValueByIndex(1) + " 和 " + graph.getValueByIndex(3)
                + " 两个结点之间边的权值为:" + graph.getWeight(1, 3));
    }
}

Guess you like

Origin blog.csdn.net/weixin_43823808/article/details/123641827