Comic: What is the "map"?

Comic: What is the "map"?
Comic: What is the "map"?
Comic: What is the "map"?
Comic: What is the "map"?
Comic: What is the "map"?

Concept map

What is the view of it? First think about our popular Internet products.

For chestnut, I must have used micro letter, assuming your micro-channel circle of friends there are a number of friends: Joe Smith, John Doe, Wang Wu, Zhao six or seven Regardless of eight aunt.

Comic: What is the "map"?

The Micro Signal seven Regardless of where you are, there are a number of friends: you, eight aunt, Jack, Rose.

Comic: What is the "map"?

Micro letter, many users make up a network of friends-many relationship, the relationship among the data network is a configuration diagram (Graph).

As another chestnut, when we use Baidu map, often use the navigation function. For example, you are near a metro station A, place you want to go near a metro station F, then the navigation will tell you one of the best subway line transfer scheme.

Comic: What is the "map"?

This is composed of many subway station transportation network, it may be considered to be among the data structure.

FIG, than the tree is a more complex data structures. It is between the nodes of the tree-many relationship, and there is a hierarchy division Father and the Son; and vertices of the graph (note that this is not called a node) is many to many relationship between, and all vertices are equal, it does not matter who is the father who is a child.

Comic: What is the "map"?

FIG term

Let's introduce the basic terminology graph:

Comic: What is the "map"?

In the drawings, the basic unit is a vertex (Vertex), corresponds to the node in the tree. Association between the vertices is called edge (edge).

In some figures, each side is not exactly the same. Examples of such earlier subway line, the distance from station A to station B 3 km from B station to C station distance is 5 km ...... This introduces a new concept: the edge weight (Weight). FIG weights involved, referred to as a weighted graph (Weighted Graph).

There is also a diagram, correlation between the vertices are not completely symmetrical. Also took to micro-channel example, your friends list have me, but my friends list may not be for you.

Comic: What is the "map"?
Comic: What is the "map"?
Comic: What is the "map"?
Comic: What is the "map"?

Thus, while there is a distinction between the vertex direction, this direction is referred to with FIG directed graph.

Comic: What is the "map"?

Accordingly, among the QQ, as long as I remove you from your friends, you in your friends list will not see me. (Looks like this)

Thus, QQ friend relationship may be considered a no distinction FIG direction, which is referred to as an undirected graph in FIG.

Diagram showing the

Comic: What is the "map"?
Comic: What is the "map"?

Adjacency matrix

FIG vertices with n, the number of connections it contains at most n (n-1) th. Therefore, to express the relationship between the individual vertices, the most clear and understandable way is to use a two-dimensional array (matrix).

Specifically how to express it? We first look undirected graph matrix:

Comic: What is the "map"?

如图所示,顶点0和顶点1之间有边关联,那么矩阵中的元素A[0][1]与A[1][0]的值就是1;顶点1和顶点2之间没有边关联,那么矩阵中的元素A[1][2]与A[2][1]的值就是0。

像这样表达图中顶点关联关系的矩阵,就叫做邻接矩阵。

需要注意的是,矩阵从左上到右下的一条对角线,其上的元素值必然是0。这样很容易想明白:任何一个顶点与它自身是没有连接的。

同时,无向图对应的矩阵是一个对称矩阵,V0和V1有关联,那么V1和V0也必定有关联,因此A[0][1]和A[1][0]的值一定相等。

那么,有向图的邻接矩阵又是什么样子呢?

Comic: What is the "map"?

从图中可以看出,有向图不再是一个对称矩阵。从V0可以到达V1,从V1却未必能到达V0,因此A[0][1]和A[1][0]的值不一定相等。

邻接矩阵的优点是什么呢?简单直观,可以快速查到一个顶点和另一顶点之间的关联关系。

邻接矩阵的缺点是什么呢?占用了太多的空间。试想,如果一个图有1000个顶点,其中只有10个顶点之间有关联(这种情况叫做稀疏图),却不得不建立一个1000X1000的二维数组,实在太浪费了。

邻接表和逆邻接表

为了解决邻接矩阵占用空间的问题,人们想到了另一种图的表示方法:邻接表。

Comic: What is the "map"?

在邻接表中,图的每一个顶点都是一个链表的头节点,其后连接着该顶点能够直接达到的相邻顶点。

Comic: What is the "map"?
Comic: What is the "map"?

很明显,这种邻接表的存储方式,占用的空间比邻接矩阵要小得多。

要想查出从顶点0能否到达顶点1,该怎么做呢?很简单,我们从顶点0开始,顺着链表的头节点向后遍历,看看后继的节点中是否存在顶点1。

要想查出顶点0能够到达的所有相邻节点,也很简单,从顶点0向后的所有链表节点,就是顶点0能到达的相邻节点。

那么,要想查出有哪些节点能一步到达顶点1,又该怎么做呢?这样就麻烦一些了,我们要遍历每一个顶点所在的链表,看看链表节点中是否包含节点1,最后发现顶点0和顶点3可以到达顶点1。

Comic: What is the "map"?

像这种逆向查找的麻烦,该如何解决呢?我们可以是用逆邻接表来解决。

Comic: What is the "map"?

Inverse adjacency list the name suggests, and adjacency list is just the opposite. Table abutment against each vertex as a head node of the linked list, the stored successor nodes can be reached directly adjacent vertices of the vertex.

As a result, in order to find out which nodes can reach the top step 1 it is easy, from all the vertices of a backward list node, node is able to reach the apex of step 1.

Therefore, we can use the inverse adjacency adjacency list or table according to the actual needs, choose.

Comic: What is the "map"?
Comic: What is the "map"?

Cross List

Cross List look like it? Most intuitive schematically, it is the following:

Comic: What is the "map"?

As shown, each vertex list of the cross, the two lists are the root, wherein a linked list stores the vertex adjacent vertices that can be reached, another list stores neighboring node to reach the apex.

However, a diagram is merely facilitate understanding of the figure, we do not need to store the list of nodes is repeated twice. After optimization of the cross linked list, each vertex node list is no longer, but a side, which contains the starting and ending vertex index.

Cross correspondence relationship list of nodes and edges, as shown below:

Comic: What is the "map"?

Therefore, cross-linked list after optimization, looks like this:

Comic: What is the "map"?

FIG Each chain with a blue arrow, stored starting from the vertex side; each chain with orange arrows, into the stored vertices edges. Beginner Cross List of time, may feel some chaos.

to sum up

1. This time, we introduce the definition and classification map. Are there directions from edges of the graph can be divided into a directed and non-directed graph. Depending on whether the right side of FIG weight, can be divided into a weighted graph and powerless FIG. Of course, also possible to combine two dimensions is described, such as a directed weighted graph, undirected not entitled to FIG like.

2. The representation of FIG. There are many. Including adjacency matrix, the adjacency list, against the adjacent table, orthogonal list. (There is also a multi-adjacency table, interested can study at junior partner)

Comic: What is the "map"?
Comic: What is the "map"?
Comic: What is the "map"?

Guess you like

Origin www.cnblogs.com/CQqf2019/p/11136803.html