当前位置:网站首页>Basic introduction of figure

Basic introduction of figure

2022-07-06 21:44:00 HairLossException

chart

A graph is a data structure , Where a node can have zero or more adjacent elements . The connection between two nodes is called an edge .
A node can also be called a vertex . Graphs can be divided into digraphs and undirected graphs

  • Undirected graph : Edges connect only two vertices , There is no other meaning .
  • Directed graph : Edges not only connect two vertices , And has direction .
     Insert picture description here

The representation of graphs

There are two ways to represent graphs : Adjacency matrix ( Two dimensional array representation )、 Adjacency list ( The linked list shows )
 Insert picture description here
 Insert picture description here

The code for creating the diagram

public class Graph {
    

    /* Store a collection of vertices */
    private ArrayList<String> vertexList;
    /* Store the adjacency matrix corresponding to the graph */
    private int[][] edges;
    /* The number of edges */
    private int numOfEdges;

    public static void main(String[] args) {
    
        String[] vertexs = {
    "A","B","C","D","E"};
        /* Create a graph object */
        Graph graph = new Graph(vertexs.length);
        /* Add vertex */
        for (String vertex:vertexs) {
    
            graph.insertVertex(vertex);
        }
        /* Add edge */
        graph.insertEdges(0,1,1);
        graph.insertEdges(0,2,1);
        graph.insertEdges(1,2,1);
        graph.insertEdges(1,3,1);
        graph.insertEdges(1,4,1);
        graph.show();
    }

    public Graph(int n){
    
        /* Initialize matrix and set */
        edges = new int[n][n];
        vertexList = new ArrayList<>(n);
        numOfEdges = 0;
    }

    /* Access vertex */
    public void insertVertex(String vertex){
    
        vertexList.add(vertex);
    }

    /** *  Insert edge  * @param v1  Subscript of vertex in adjacency matrix  * @param v2  Subscript of vertex in adjacency matrix  * @param weight  A weight  */
    public void insertEdges(int v1,int v2,int weight){
    
        edges[v1][v2] = weight;
        edges[v2][v1] = weight;
        numOfEdges++;
    }
    /* Get the number of vertices */
    public int getNumOfVertex(){
    
        return vertexList.size();
    }
    /* Get the number of sides */
    public int getNumOfEdges(){
    
        return numOfEdges;
    }
    /* According to the subscript index Get the corresponding data */
    public String getValueByIndex(int index){
    
        return vertexList.get(index);
    }
    /* return v1v2 A weight */
    public int getWeight(int v1,int v2){
    
        return edges[v1][v2];
    }
    /* Displays the adjacency matrix of the graph */
    public void show(){
    
        for (int[] arr : edges) {
    
            System.out.println(Arrays.toString(arr));
        }
    }
}

Print the adjacency matrix of the graph

[0, 1, 1, 0, 0]
[1, 0, 1, 1, 1]
[1, 1, 0, 0, 0]
[0, 1, 0, 0, 0]
[0, 1, 0, 0, 0]

Process finished with exit code 0
原网站

版权声明
本文为[HairLossException]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131121352381.html