C # and data structure--traversal of graphs

Source: Internet
Author: User
Tags header

8.2 Storage structure of graphs

The storage structure of the graph, in addition to storing information about each vertex in the graph, it also stores all the relationships between vertices and vertices (edge information), therefore, the structure of the graph is complex, it is difficult to represent the relationship between elements in the physical location of the data elements in the storage area, but also because of their arbitrary characteristics, So there are a lot of physical means. The storage structure of commonly used graphs has adjacency matrix, adjacency table, cross linked list and adjacency multiple table.

8.2.1 adjacency Matrix representation method

For a graph with n vertices, the n*n matrix (two-dimensional array) can be used to represent the adjacency relationship between them. In Figure 8.10 and figure 8.11, matrix A (I,J) =1 represents an edge (VI,VJ) in the diagram, while a (I,J) =0 indicates that there is no edge (VI,VJ) in the diagram. When actually programming, when the graph is not with the right graph, the bool value can be stored in a two-dimensional array, a (I,J) =true represents an existing edge (VI,VJ), A (I,J) =false indicates that there is no edge (VI,VJ), and when the graph is weighted, it can be directly stored in a two-dimensional array, a (I,J) =null indicates that there is no edge (VI,VJ).

Fig. 8.10 shows the adjacency matrix representation of the i,j graph, and it can be observed that the matrix is extended diagonal symmetry, i.e. a () = A (j,i). The number of non-0 elements in row I or column I of the adjacency matrix of the non-direction graph is actually the degree of the vertex. This indicates that there is some data redundancy in the adjacency matrix of the non-direction graph.

Fig. 8.11 shows the adjacency matrix representation of the direction graph, the matrix is not extended diagonal symmetry, a (i,j) =1 indicates vertex vi adjacency to vertex vj;a (j,i) =1 is the vertex vi adjacency from vertex VJ. The two do not represent the same meaning as the adjacency matrix of the graph without direction. The number of non-0 elements in the row I of the adjacency matrix of a direction graph is actually the degree of the first vertex, the number of non 0 elements in column I is the degree of the first vertex, that is, the degree of the vertex is the sum of the number of rows I and I not 0 elements.

Because there are n vertices of the graph need to N2 array elements to store, when the graph is sparse graph, the use of adjacency matrix storage method will appear a large number of 0 elements, according to the vast space waste, then should use adjacency table notation to store the data in the graph.

8.2.2 adjacency Table Representation method

The adjacency matrix storage method of graphs is similar to the child chain representation of a tree, and is a kind of storage structure combining sequential allocation and chain distribution. The adjacency table consists of the header node and the table node, in which each vertex corresponds to a header node stored in the array. If there are adjacent vertices in the vertex corresponding to the node, the adjacent vertices are stored sequentially in the one-way list pointed by the node of the table head. As shown in Figure 8.12, the table node holds the index of the adjacency vertex in the array. For the direction-free graph, the data redundancy can be found by using adjacency table, and there is a table node in table header node A which points to C, and there is a table node in the linked list of table header node C.

The adjacency table of the direction graph has the points of the edge table and the Entry table (also called the inverse adjacency table). The table node of the edge table holds the end vertex of the pointed edge from the head node, and the table node of the edge table holds the point of the head of the header node. As shown in Figure 8.13, the graphs (b) and (c) are both the edge table and the Entry table for the direction diagram (a).

The adjacency table discussed above is a graph with no weights, and if you want to represent a weighted graph, you can add a field of storage in the table node, and the effect is as shown in Figure 8.14.

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.