Comparison of adjacency matrix and adjacency table of graphs

Source: Internet
Author: User
Tags comparison

The storage structure of the

graph is mainly divided into two kinds, one is adjacency matrix and the other is adjacency table.
1. adjacency Matrix
Graph's adjacency matrix is stored in two arrays to represent the graph. A one-dimensional array stores the vertex information in a graph, and a two-dimensional array (adjacency matrix) stores information about edges or arcs in the diagram.
Figure G has n vertices, then the adjacency matrix is a n*n square, defined as:

Look at an instance, the left side of the graph is an no-go graph.

As can be seen from the above, the edge array of the non-graph is a symmetric matrix. The so-called symmetric matrix is the element of n-order matrix satisfies AIJ = Aji. That is, from the upper-left corner of the matrix to the lower-right corner of the main diagonal axis, the upper-right corner of the element and the lower-left corner of the corresponding elements are all equal.
as can be seen from the above, the edge array of the non-graph is a symmetric matrix. The so-called symmetric matrix is the element of n-order matrix satisfies AIJ = Aji. That is, from the upper-left corner of the matrix to the lower-right corner of the main diagonal axis, the upper-right corner of the element and the lower-left corner of the corresponding elements are all equal.
from this matrix, it is easy to know the information in the diagram.
(1) It is easy to judge whether any two vertices have edges or not;
(2) to know the degree of a vertex is actually the sum of the elements of this vertex VI in the adjacency matrix of Line I or (column i);
(3) All adjacency points of Vertex VI are scanned in the matrix I element. ARC[I][J] is 1 is the adjacency point, the
and the direction of the graph to pay attention to the degree and the degree, Vertex VI in the degree of 1, is exactly the sum of the number of columns I. The dimension of vertex VI is 2, which is the sum of the number of rows I.
If figure g is a network Diagram with n vertices, then the adjacency matrix is a n*n square, defined as:

1.2 adjacency table
The adjacency matrix is a good graph storage structure, but for graphs with fewer edges relative to vertices, this structure has a huge waste of storage space. Therefore, finding a storage method that combines an array with a linked list is called an adjacency table.
The adjacency table is treated like this:
(1) The vertex is stored in a one-dimensional array, and of course, vertices can be stored in a single-linked list, but the array can be easier to read the vertex information. The
(2) Diagram of each vertex VI of all the adjacency points constitute a linear table, due to the number of adjacent points variable, so, with a single-linked table storage, the non-map is called the Vertex vi edge table, a forward graph is called Vertex VI as the edge of the arc tail table.
For example, the following illustration is the structure of an adjacency table for an unstructured graph.

As you can see from the diagram, the nodes of the vertex table are represented by data and Firstedge two fields, and it is the information of the field that stores the vertices, Firstedge is the pointer field, and points to the first node of the edge table, that is, the first adjacency point of the vertex. Benzi nodes are composed of Adjvex and next two domains. Adjvex is the adjacency point field, which stores the index of the adjacency point of a vertex in the vertex table, and next stores a pointer to the next node in the edge table.
for the grid with weights, you can add a weight data field in the Edge table node definition and store the weight information. As shown in the following figure.

3. The difference between
for a non-graph with n vertices e edges
Its adjacency table represents a N vertex table node 2e edge table node
for a forward graph with n vertices e edges
Its adjacency table indicates that there are n vertex table nodes e edge table nodes
If the number of edges in the graph is much smaller than N2, which is called sparse graph, it is compared with adjacency matrix representation to save space;
If the number of edges in the graph is close to N2, and the n-1 is close to N/a dense graph, it is advisable to use adjacency matrix notation for the adjacency table to attach the link field.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.