Figure, Find, sort

Source: Internet
Author: User

CH5 Chart

1. Definition and basic concepts of graphs

Definition: g= (v,e), V is a finite non-empty set of vertices in Figure g, and E is a collection of relationships (edges) between vertices in Figure G. V must not be empty.

Basic concepts:
There are graphs, undirected graphs, simple graphs, multiple graphs, complete graphs, sub-graphs, connected graphs, connected components, strong connected graphs, strongly connected components, spanning trees, forest generation, degrees (undirected graphs), in-out degrees (graph), weights and nets of edges, paths, path lengths.

undirected graph: Connected (vertex v to vertex w has path present), connected graph (any two vertices in the graph are connected).

undirected graphs: Strong connectivity (vertex v to vertex W and Vertex W to Vertex v both have paths present), strong connected graphs (any pair of vertices are strongly connected).

Path Length: The number of top edges of the path.

2. Storage of graphs and basic operation

1) Sequential storage structure: adjacency Matrix, adjacency table

Adjacency Matrix Method: VI to VJ have side connected, then A[i][j] is 1, otherwise 0; suitable for dense graphs.

Adjacency Table method: Suitable for sparse graphs.

2) Chained storage structure: cross-linked list, adjacency multiple table

Cross linked list: is a chain storage structure of the graph.

Adjacency Multiple tables: A chained storage structure that is a non-graph.

3) basic operation of Figure

For example, figure g whether there is an edge <x,y>, the edge of the node x is listed in Figure g, insert (delete) vertex x in Figure G, delete or add an edge and so on.

3. Graph traversal

1) BFS (Breadth-first-search)

is a hierarchical lookup process, with a secondary queue. (The BFS of the figure is exactly the same as the sequence traversal of the two-fork tree)

Basic idea: First access to Vertex V, and then access v each of the inaccessible adjacency vertices W1, W2, W3 、、、、, and then access the W1 each of the inaccessible adjacency vertices, and then access W2 each of the inaccessible adjacency vertices, 、、、; from these visited vertices, Then access all of their inaccessible adjacency vertices 、、、、 and so on until all the vertices in the graph have been accessed.

Whether the storage method is an adjacency matrix (sequential storage) or an adjacency table (chained storage), the BFS need to use a secondary queue q,n vertices are queued once, in the worst case, the space complexity is O (V).

When the adjacency table is used for storage, each vertex needs to search once (or one time), so the time complexity is O (V), when searching the adjacency point of any vertex, each edge is visited at least once, so the time complexity is O (E), so the total time complexity of the algorithm is O (v+e).

When the adjacency matrix is used for storage, the time required to find the adjacency point of each vertex is O (V), so the total time complexity of the algorithm is O (v*v).

The BFS algorithm solves the single-source shortest path problem and the breadth-first spanning tree problem.

2) DFS (Depth-first-search)

Similar to the sequential traversal of a tree, recursion is used, and the spatial complexity is O (V) with the help of recursive work stacks. Traversing a graph is essentially the process of finding its adjacency point for each vertex, and the time it takes depends on the storage structure in use.

Basic idea: Access vertex v First, then from V, access any vertex adjacent to V that has not been accessed, and then access any vertex w2 that is adjacent to W1 and not visited, W1. Repeat the above process. When it is no longer possible to continue to access it, it is returned to the most recently visited vertex, and if it has an adjacent vertex that has not been accessed, the search continues from that point until all the vertices in the graph have been accessed.

When represented in an adjacency table, the time required to find the adjacency point of all vertices is O (E) and the time required to access the vertices is O (V), so the total time complexity is O (v+e).

When represented in an adjacency matrix, the time required to find the adjacency point for each vertex is O (V), so the total time complexity is O (v*v).

4. Application of graphs (minimum spanning tree, shortest path, topological sort, critical path)

1) Minimum Spanning tree

Prim algorithm, Kruskal algorithm

2) Shortest Path

Dijkstra algorithm, Floyd algorithm

3) Topology sequencing

4) Critical Path

Figure, Find, sort

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.