Data structure _ Nonlinear Structure _ diagram

Source: Internet
Author: User
Read CatalogueOne, figure definition two, diagram related concepts and terminology III, diagram creation and traversal four, minimum spanning tree and shortest path five, algorithm implementation

In this article we are going to summarize the graph, the graph may be more complex than the linear structure and the tree structure we learned before, but it doesn't matter, we summarize at 1.1 points, then I want to summarize from the following points:

The definition of the 1> diagram.
Concepts and terminology related to 2> diagrams.
The creation and traversal of 3> diagrams.
4> minimum spanning tree and shortest path.
5> algorithm implementation.

one, the definition of the graph

What is a graph?

Graph is a kind of complex nonlinear structure.

In a linear structure, the data elements satisfy a unique linear relationship, and each data element (except the first and last) has only one direct predecessor and one direct successor;

In a tree structure, there is a distinct hierarchical relationship between data elements, and each data element is related only to one element in the previous layer (the parent node) and multiple elements of the next layer (child nodes).

In the graphical structure, the relationship between the nodes is arbitrary, and any of the two data elements in the graph may be related to each other.

Figure G consists of two set V (vertex vertex) and E (Edge edge), defined as g= (v,e)

two, diagram-related concepts and terminology
1. non-direction graphs and graphs
For a graph, if each edge has no direction, it is said to be a non-directed graph. This is illustrated below:

Therefore, (VI,VJ) and (VJ,VI) represent the same edge. Note that the non-graphic is enclosed in parentheses, and the graph described below is with angle brackets.

The vertex and edge sets of the non-graph are represented as:

V (G) ={V1,V2,V3,V4,V5}

E (G) ={(V1,V2), (V1,V4), (V2,v3), (V2,V5), (V3,V4), (V3,V5), (V4,V5)}

For a graph G, if each edge has a direction, the graph is called a directed graph. This is illustrated below.

therefore (VI,VJ) and (VJ,VI) are two different forward sides. Note that a forward edge is also called an arc.

The vertex and edge sets of a forward graph are represented as:

V (G) ={v1,v2,v3}

E (G) ={(V1,V2), (V2,v3), (V3,V1), (V1,V3)}

non-direction full and full graphs
We will have n (n-1)/2 edges of the non-direction graph is called the non-direction complete graph. Similarly, a forward graph with n (n-1) edges is called a forward full graph.

the degree of the vertex
Sub-chart
No explanation of path, path length and loop
The path, such as in the graph G, has a vertex sequence vp,vi1,vi2,vi3...,vim,vq, which makes (VP,VI1), (VI1,VI2), ..., (VIM,VQ) all belong to the Edge set E (G), and the vertex Vp to Vq has a path.

Path length refers to the number of edges that pass through a path.

A loop that refers to the beginning and end of a path as the same vertex.
6. Connected graph (undirected graph)
The connected graph refers to any two vertices VI and VJ connected in Figure G, which is called the connected graph. For example, figure (b) is a connected graph. The following is an example of a non-connected graph.

Above, because V5 and V6 are separate, they are non-connected graphs.
7. strongly connected graphs (with graphs)
A strongly connected graph is similar to a connected graph of undirected graphs for a graph.
8. Web
Connected graphs with "weights" are called nets. As shown in the figure.

creation and traversal of graphs
1, two kinds of storage structure of graphs

1) adjacency matrix, the principle is to use two arrays, an array to save the vertex set, an array to save the edge set. The following algorithm implementation inside we also use this storage structure. As shown in the following illustration:

2) adjacency table, adjacency table is a chain storage structure of graphs. This storage structure resembles a child linked list of trees. For each vertex vi in Figure g, all vertex vj adjacent to VI is chained to a single-linked list, which is called the adjacency table of Vertex VI.

2, two kinds of traversal methods of graphs

1) Depth-first search traversal

A depth-first search for DFS traversal is similar to a tree-like pre-sequence traversal. The basic ideas are:

A) assuming that the initial state is not accessed by all vertices in the graph, the initial starting point can be from any vertex v in Figure G, first accessing the starting point V and marking it as having been visited.

b) then starting from V to search for each adjacency point W of V, if W has not been visited, then start with W as a new starting point, continue the depth-first traversal, until all of the nodes in the diagram and V have a path to connect to the vertex is accessed.

c) If there are still vertices in the graph that are not accessed, select another vertex that has not been visited as the starting point, repeating the above steps until all the vertices in the diagram are accessed.

This is illustrated below:

Note: The red number represents the sequence of traversal, so the vertex access sequence for the depth-first traversal of the graph (e) without the map is: V0,V1,V2,V5,V4,V6,V3,V7,V8

If the adjacency matrix is used for storage, the time complexity is O (N2), and the time complexity is O (n+e) when the adjacency table is used.

2) Breadth-First search traversal

Breadth-First search traversal of BFS is similar to tree-by-hierarchy traversal. The basic ideas are:

A) First visit the starting point VI

b) Then access all of the Vi1,vi2,vi3,...,vit adjacency points of VI and mark them as visited.

c) Then follow the order of Vi1,vi2, ..., Vit, access all the vertices that have not been visited for each vertex and mark them as visited, and so on until all the vertices in the diagram with the original starting point VI have been accessed.

This is illustrated below:

Thus, the graph (f) uses a generalized first search to traverse a sequence of vertices with V0 as the starting point: v0,v1,v3,v4,v2,v6,v8,v5,v7

If the adjacency matrix is used for storage, the time complexity is O (N2), and if the adjacency table is used, the time complexity is O (n+e).

Four, minimum spanning tree

1, minimum spanning tree

What is the minimum spanning tree? Before figuring out what is the smallest spanning tree, we need to figure out what the spanning tree is.

Simply summarize the spanning tree in one sentence: the spanning tree is a sub-graph that connects all the vertices in the graph with the fewest edges.

For example, figure (g) can get two spanning tree graphs (h) and graphs (i)

Once we know what a spanning tree is, it's easy to understand what a minimal spanning tree is. The so-called minimal spanning tree, summed up in one sentence: the weight and the smallest spanning tree is the smallest spanning tree.

For example, the above two spanning tree, spanning tree 1 and spanning Tree 2, generating tree 1 weights and is: 12, the weight of the spanning Tree 2 is: 14, we can prove that the graph (h) Spanning Tree 1 is the smallest spanning tree of figure (g).

So how to construct the minimum spanning tree. You can use the Primm algorithm.

2, Shortest path

Finding the shortest path is also the shortest path length. The following is a weighted graph with the shortest path lengths for vertices V1 other vertices in the table.


Table: Shortest path table for vertex V1 to other vertices

As can be seen from the figure, the path of the vertex V1 to V4 has 3 (V1,V2,V4), (V1,V4), (V1,V3,V2,V4), the path length is 15, 20 and 10 respectively, so the shortest path V1 to V4 is (V1,V3,V2,V4).

Then how to find the shortest path length of the weighted graph. You can use the Dijkstra (Dijkstra) algorithm.

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.