FIG stored - data structure (IV)

Some of the more complex algorithms chart, here are summarized in several blog

Related concept map

In fact, before several data structures, in fact, relatively simple, before the memory is still relatively clear, but the view at this place, some concepts a bit blurred, here or now summarize the relevant concept map.

definition

Definition: FIG. (Graph) by some point (Vertex) and the line between these points (Edge) consisting of; wherein the point commonly referred to as "vertex (Vertex)", and the line between the dots were a "side or arc" (edege). Typically referred to as a, G = (V, E).

Are there directions from the edge, can be divided into a directed graph and an undirected graph

Undirected graph

Here Insert Picture Description

Directed graph

Here Insert Picture Description

Neighbors and degree

Neighbor

Two vertices is called an edge neighbors.
For example, the above undirected graph G0 vertices A and vertex C is a neighbor.

In the drawing, there is in addition to the adjacent point; and "selvedge" and the concept of "the edge".
The edge of vertices, the vertex refers to the end edge. And the vertex edges, refers to the starting vertex of the edge.
* For example, the above directed graph G2 in B and E are contiguous points; *** is B out of the edge, or E -edges.

degree

In the undirected graph, the degree of a vertex are contiguous to the number of edges (or arcs) of the vertex.
For example, the above undirected graph G0 vertex A degree is 2 .

In a directed graph, as well as the degree of "penetration" and "out of" points.
The degree of a vertex, refers to the number of sides to the vertex end. And the degree of the vertex, refers to the number of edges starting from the vertex.
Penetration of the apex = a + degrees.
For example, the above digraph G2 , the apex B -degree is 2 , the degree is 3 ; apex B of a * = 2 + 3 = 5 **. *

Storage map

Adjacency matrix

Refers to the adjacency matrix is represented by the matrix of FIG. It is the use of matrices to describe the relationship between the vertices of the figure (and arc or the right side).
FIG assumed number of vertices n, the adjacency matrix is defined as:

Here Insert Picture Description

Usually two arrays to implement the adjacency matrix: a one-dimensional array used to store the vertex information, to use a two-dimensional array information stored edge.
Disadvantage of the adjacency matrix is space consuming.

Undirected graph adjacency matrix

Here Insert Picture Description

Related code design

public class MatrixDG {
    int size;
    char[] vertexs; //图顶点名称
    int[][] matrix; //图关系矩阵
}

Specific construction method

/**
 * autor:liman
 * createtime:2020/2/9
 * comment: 无向图的数组表示
 */
public class MatrixNDG {

    int size;
    char[] vertexs; //图顶点名称
    int[][] matrix; //图关系矩阵

    public MatrixNDG(char[] vertexs, char[][] edges) {
        size = vertexs.length;
        matrix = new int[size][size];
        this.vertexs = vertexs;

        for (char[] c : edges) {
            int p1 = getPosition(c[0]);
            int p2 = getPosition(c[1]);

            //无向图,两个对称的位置都需要存储
            matrix[p1][p2] = 1;
            matrix[p2][p1] = 1;
        }
    }

    public void print() {
        for (int[] i : matrix) {
            for (int j : i) {
                System.out.print(j + " ");
            }
            System.out.println();
        }
    }

    //根据顶点名称获取对应的矩阵下标
    private int getPosition(char ch) {
        for (int i = 0; i < vertexs.length; i++) {
            if (vertexs[i] == ch) {
                return i;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        char[] vexs = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K'};
        char[][] edges = new char[][]{
                {'A', 'C'},
                {'A', 'D'},
                {'A', 'F'},
                {'B', 'C'},
                {'C', 'D'},
                {'E', 'G'},
                {'D', 'G'},
                {'I', 'J'},
                {'J', 'G'},
                {'E', 'H'},
                {'H', 'K'}};

        MatrixNDG matrixNDG = new MatrixNDG(vexs,edges);
        matrixNDG.print();
    }
}

There the graph adjacency matrix

Here Insert Picture Description

There, only a small line of code when constructing the storage structure constructed in accordance with FIG above, as follows:

public MatrixDG(char[] vertexs, char[][] edges) {
    size = vertexs.length;
    matrix = new int[size][size];
    this.vertexs = vertexs;

    for (char[] c : edges) {
        int p1 = getPosition(c[0]);
        int p2 = getPosition(c[1]);

        //有向图,只需要存储一个即可
        matrix[p1][p2] = 1;
    }
}

Adjacency list

It is a chain store adjacency list representation of FIG. It is an improved "adjacency matrix", it is inconvenient drawback determines whether an edge between two vertices, but is relatively more space adjacency matrix.

Undirected graph adjacency list

Here Insert Picture Description

Specific storage design

/**
 * autor:liman
 * createtime:2020/2/9
 * comment: 邻接表的方式存储无向图
 */
public class ListNDG {

    Vertex[] vertexList;	//这里就是一个存储节点的数组
    int size;

    class Vertex{
        char ch;
        Vertex next;

        public Vertex(char ch) {
            this.ch = ch;
        }

        void add(char ch){
            Vertex node = this;
            while(node.next!=null){//找到链表结尾
                node = node.next;
            }
            node.next = new Vertex(ch);
        }
    }
}

Specific constructed as follows:

public ListNDG(char[] vertexs,char[][] edgs){
    size = vertexs.length;
    this.vertexList = new Vertex[size];//确定邻接表的大小
    //设置邻接表的每一个节点
    for(int i = 0;i<size;i++){
        this.vertexList[i] = new Vertex(vertexs[i]);
    }

    //存储边的信息
    for(char[] c:edgs){
        int p1 = getPosition(c[0]);
        vertexList[p1].add(c[1]);
        int p2 = getPosition(c[1]);
        vertexList[p2].add(c[0]);
    }
}

//根据顶点名称获取链表下标
private int getPosition(char ch){
    for(int i =0;i<size;i++){
        if(vertexList[i].ch==ch){
            return i;
        }
    }
    return -1;
}

public void print(){
    for(int i = 0;i<size;i++){
        Vertex temp = vertexList[i];
        while(temp!=null){
            System.out.print(temp.ch+" ");
            temp = temp.next;
        }
        System.out.println();
    }
}

public static void main(String[] args) {
    char[] vexs = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K'};
    char[][] edges = new char[][]{
            {'A', 'C'},
            {'A', 'D'},
            {'A', 'F'},
            {'B', 'C'},
            {'C', 'D'},
            {'E', 'G'},
            {'D', 'G'},
            {'I', 'J'},
            {'J', 'G'},
            {'E', 'H'},
            {'H', 'K'}};

    ListNDG listNDG = new ListNDG(vexs,edges);
    listNDG.print();
}

There adjacency list digraph

Here Insert Picture Description

As with the embodiment of the adjacency matrix, with no code sent to the two lines of FIG.

public ListDG(char[] vertexs, char[][] edgs){
    size = vertexs.length;
    this.vertexList = new Vertex[size];//确定邻接表的大小
    //设置邻接表的每一个节点
    for(int i = 0;i<size;i++){
        this.vertexList[i] = new Vertex(vertexs[i]);
    }

    //存储边的信息,这里是有向图只存储单向的连接边信息
    for(char[] c:edgs){
        int p1 = getPosition(c[0]);
        vertexList[p1].add(c[1]);
    }
}
Published 133 original articles · won praise 37 · views 90000 +

Guess you like

Origin blog.csdn.net/liman65727/article/details/104240535
Recommended