# The adjacency table representation of graphs and topological ordering of non-circular graphs

Source: Internet
Author: User

The most commonly used representations of graphs are adjacency matrices and adjacency tables.

Adjacency matrix is actually a two-dimensional array, for each edge <u,v>, we make a[u][v] = 1, if the graph is entitled to the graph, we can also let a[u][v] equals that right, so the advantage is very simple, but its space requirements are very large, if the figure is dense, Adjacency matrix is the appropriate representation method, if the graph is sparse, then this method is a waste of space, the following shows an example of the adjacency matrix of the graph.

Adjacency table is one of the commonly used storage structures of graphs. The adjacency table consists of a header node and a table node, where each vertex corresponds to a header node stored in the array. As shown: the adjacency table of the graph is: two, topological sort 1, definition

Topological ordering of a directed acyclic graph (Directed acyclic graph abbreviation dag) g is a linear sequence of all vertices in g, so that any pair of vertices u and V in the graph, if <u,v>∈e (G), appears before v in a linear sequence.

Typically, such a linear sequence is called a sequence that satisfies the topological order (topological order), referred to as a topological sequence.

Attention:

1) A topological sequence exists only in the direction-free graph;

2) for a DAG, there may be multiple topological sequences;

2. The idea of topological sequence algorithm

(1) Calculate the penetration of all vertices (if not present, the graph has a loop) and put all the points in the 0 into a queue.

(2) out of the team a vertex of 0, and output, assuming V;

(3) If there is an edge of <v,w&gt, then the degree of vertex w is reduced by 1, the penetration of all points is scanned, and the vertex with the degree 0 is enqueued; Repeat (2) (3) until all vertices are in degrees 0. The order in which the teams are out is the order in which the topology is sorted. Third, the code implementation to sort the graph is: the representation of the graph selection of the adjacency table representation, the code is as follows:

The output is:

The idea of the above algorithm is very important, to learn more.

It's late at night ....

The adjacency table representation of graphs and topological ordering of non-circular graphs

Related Keywords:

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.