(to be updated) Data Structure Chapter 6 Diagram

Basic concepts of graphs, storage structure of graphs, traversal of graphs, applications of graphs

1. The basic concept of graph

Connected, connected graph - corresponding to undirected graph
[connected graph: there is a path between any two vertices in an undirected graph.
Strongly connected, strongly connected graph—corresponding to a directed graph
【Strongly connected graph: There are two paths in opposite directions between any two vertices in a directed graph. ]
Note that it is different from a complete graph (more stringent requirements):
[Undirected complete graph: there is an edge between any two vertices.
【Directed Complete Graph: There are two arcs in opposite directions between any two vertices.

An undirected graph with n vertices:
①If it is a connected graph (there is a path between any two vertices), then it has at least n-1 edges. In the case of not considering multiple edges, there are at most C_{n}^{2}one edge (the number of vertices in the undirected graph has been determined, and the case with the largest number of edges is that there is an edge between every two vertices).
②If it is a non-connected graph, it can only have at most C_{n-1}^{2}one edge. (Isolate a point, and let the remaining n-1 points form a connected graph with the largest number of edges (undirected complete graph: there is an edge between any two vertices).) [Prove that the number of edges is
impossible No matter C_{n-1}^{2}how big the ratio is, if the ratio is C_{n-1}^{2}greater than 1, the number of edges is definitely enough to connect the isolated point, and the condition of a non-connected graph is no longer satisfied.

★★★Carefully distinguish the key points of the two kinds of questions:
①P202T11: It is known that an undirected graph has n vertices. If it is a connected graph, what is the minimum number of edges? - at least n-1.
②P202T7: It is known that an undirected graph has n vertices. To ensure that it is connected in any case, how many edges are needed at least? - At least 1+ C_{n-1}^{2}edges are required.

A directed graph of n vertices:

① If it is a strongly connected graph (there are two paths in opposite directions between any two vertices), then it has at least n edges (that is, when a loop is formed).

For a connected graph with n vertices, its spanning tree (minimum connected subgraph containing all vertices) must have n vertices and n-1 edges.
Adding an edge to the spanning tree of a connected graph ①, a cycle (loop) will definitely appear. ② Removing an edge will definitely cause the tree to be disconnected.

The tree learned before can be regarded as an undirected graph that does not have a loop (loop) and is connected. [A connected graph without a cycle]


Second, the storage structure of the graph

Adjacency matrix: a two-dimensional array, the adjacency matrix of a directed graph is a symmetric matrix, storing a directed graph: each row represents the out-degree of a vertex. The adjacency matrix corresponding to a graph is unique.

Adjacency list: a one-dimensional array + singly linked list, vertex table nodes, edge table nodes. The adjacency list corresponding to a graph is not unique. The adjacency list of the directed graph is inconvenient to find the incoming edges, and needs to be traversed once.

optimization:

Cross linked list - store directed graph

Adjacency Multitable - Storing Undirected Graphs


Three, graph traversal

Choose a starting vertex yourself. Different starting vertices, the obtained BFS/DFS traversal sequence must be different!

1. The breadth-first traversal of the graph is BFS (Breadth First Search), which is similar to the hierarchical order traversal of the tree. A secondary queue is required .

Space complexity: Worst case, auxiliary queue size isO\left (|V| \right )

Breadth-first traversal based on adjacency matrix , unique sequence, time complexity:O\left (|V|^{2} \right )

Breadth-first traversal based on adjacency list , sequence is not unique, time complexity:O\left ( |V|+|E| \right )

Time complexity = time required to visit each node + time required to explore each edge . It needs to be combined with specific storage structure analysis.

2. The depth-first traversal of the graph is DFS (Depth First Search), which is similar to the root-first traversal of the tree. A recursive stack is required .

Space complexity: From function call stack, worst case, recursion depth is O\left (|V| \right ); best case,O(1)

Depth-first traversal based on adjacency matrix , unique sequence, time complexity:O\left (|V|^{2} \right )

Depth-first traversal based on adjacency list , sequence is not unique, time complexity:O\left ( |V|+|E| \right )

The code implementation of BFS and DFS: Both need a visited[i] array to mark which vertices have been visited to prevent repeated visits to the same node. Initialization is false.

To perform a BFS/DFS traversal on an undirected graph, the number of calls to the BFS/DFS function = the number of connected components .

In particular, for connected graphs, only one call to the BFS/DFS function is required.

To perform a BFS/DFS traversal on a directed graph, the number of calls to the BFS/DFS function should be analyzed in detail: if there is a path from the starting vertex to all other vertices, then only one BFS/DFS function needs to be called .

In particular, for a strongly connected graph, only one BFS/DFS function needs to be called from any node.

      

3. Spanning trees and spanning forests

Breadth-first spanning tree of a graph: A tree determined by breadth-first traversal. Since the representation of the adjacency list is not unique, the breadth-first spanning tree based on the adjacency list is also not unique, and there may be multiple trees .

Breadth-first generation forest of graph: Breadth-first traversal of disconnected graph can obtain breadth-first generation forest.

The depth-first spanning tree of the graph, the same reason.


4. Application of graphs

1. Minimum spanning tree (minimum cost tree, for a weighted connected undirected graph , the spanning tree with the smallest sum of weights of all edges .)

[Difference between weighted graph and weighted tree ]: The weight of the weighted graph is on the edge , and the weight of the weighted tree is on the node .

[Difference between minimum spanning tree (minimum cost tree) and Huffman tree]:

Huffman tree: the optimal binary tree, the binary tree with the smallest weighted path length (WPL).

Huffman tree is not unique, but WPL must be minimum.

Recall the spanning tree of a connected graph: a minimal connected subgraph containing all the vertices in the graph . If the number of vertices in the graph is n, its spanning tree contains n-1 edges. For a spanning tree, if one of its edges is cut off, it will become a disconnected graph, and if an edge is added, it will form a cycle.

Similar: There may be multiple minimum spanning trees, but the sum of edge weights is always unique and the smallest. The number of edges of the minimum spanning tree = the number of vertices - 1. If one edge is cut off, it will be disconnected, and if an edge is added, a circuit will appear.

①Prim algorithm (Prim) to find the minimum spanning tree : start to build a spanning tree from a certain vertex; each time a new vertex with the least cost is included in the spanning tree.

Time complexity: O\left (|V|^{2} \right )suitable for edge-dense graphs.

Code implementation: Starting from V0, a ​​total of n-1 rounds of processing are required. Loop 2 times per round: Loop through all nodes and find the vertex with the lowest lowCost that has not yet been added. Loop through again to update the lowCost value of each vertex that has not been added yet.

②Kruskal algorithm (Karus-Karr) to find the minimum spanning tree : select an edge with the smallest weight each time, so that the two ends of this edge are connected (the ones that are already connected are not selected).

Time complexity: O( |E|log2|E| )suitable for edge-sparse graphs.

Code implementation: Initially, each edge is sorted by weight, and a total of |E| rounds are executed. In each round, it is judged whether the two ends of an edge are connected → whether the two vertices belong to the same set (using union search), each round Time complexity O(log2|E|).

2. The shortest path

Single-source shortest path: ① BFS (breadth-first traversal) seeks the single-source shortest path of an unweighted graph . The starting vertex is determined.

②Dijkstra algorithm (Dijkstra) finds the single-source shortest path with weighted graph (positive value) (can have ring) . The starting vertex is determined.

time complexity:O\left (|V|^{2} \right )

3 arrays:

final[ ] Whether each vertex finds the shortest path, initialize false;

dist[ ] the shortest path length, initialize dist[k]=arcs[0][k];

path[ ] precursor node, the vertex path connected to the start vertex v0 is initialized to 0, and the vertex path not connected to the start vertex is initialized to -1.

Each round can determine the shortest path from the starting vertex to a vertex, and a total of n-1 rounds are required.

Loop 2 times per round: loop through the remaining vertices, find the vertex Vi with the smallest dist, set final[i]=true; loop through again, check the adjacent vertices of the vertices determined in this round, update dist[ ] and path[ ], every Round time complexity O(2n).

[After the shortest path to each vertex is determined, dist[ ] and path[ ] must be updated. The algorithm ends until no node whose final is false is found.

Dijkstra's algorithm is not suitable for weighted graphs with negative weights.

The shortest path between all vertices: Floyd's algorithm (Floyd) finds the shortest path between all vertices in a weighted graph (can be negative) (no rings with negative edges) . The starting vertex is not fixed and can be any vertex.

......

3. Directed acyclic graph DAG description expression

......

4. Topological sorting

......

5. Find the critical path

1) AOE network - In the weighted directed graph, events are represented by vertices , activities are represented by directed edges , and the cost of completing the activity is represented by the weight on the edge (such as the time required to complete the activity), called It is a network that uses edges to represent activities, referred to as AOE Network (Activity On Edge Network).

In the AOE network, there is only one vertex with an in-degree of 0, called the start vertex (source point) , which represents the beginning of the entire project;

There is also only one vertex with an out-degree of 0, called the end vertex (sink point) , which represents the end of the entire project.

2) Critical path

There may be multiple directed paths from the source point to the sink point. Among all paths, the path with the largest path length is called the critical path , and the activities on the critical path are called critical activities . The shortest time to complete the entire project is the length of the critical path . If the key activities cannot be completed on time, the completion time of the entire project will be extended】

Apex: an event, a moment. Edge: activity, lasting for a period of time.

Earliest/latest: event [occurrence] time, activity [start] time.

① The earliest occurrence time ve(k) of event vk—determines the earliest time when all activities starting from event vk can start.

②The latest occurrence time vl(k) of the event vk——the latest time that the event must occur without delaying the completion of the entire project.

③Earliest start time e(i) of activity ai—— the earliest occurrence time of the event represented by the starting point of the activity arc .

④The latest start time l(i) of activity ai ——the difference between the latest occurrence time of the event represented by the end point of the activity arc and the time required for the activity .

⑤ The time margin of activity ai d(i) = l(i)-e(i)—— the time that activity ai can be delayed without increasing the total time required to complete the entire project.

★d(i)=0, that is, the activity ai of l(i)=e(i) is the key activity, and the path composed of key activities is the critical path.

Steps to find the critical path: ①=③, ② push out ④, get ⑤ from ④-③, find ⑤=0.

① Find the earliest occurrence time ve( ) of all events.

According to the topological sorting sequence , find the ve(k) of each vertex in turn: using the topological sorting sequence can make this process proceed in an orderly manner.

(1) The earliest occurrence time of the start vertex (source point) = 0;

(2) The earliest occurrence time of the event = the earliest occurrence time of the predecessor node (event) + the edge (activity) time . The earliest time of an event with multiple precursor nodes (events) is calculated in the same line as max {the earliest occurrence time of precursor nodes (events) + edge (activity) time}       

② Find the latest occurrence time vl( ) of all events.

According to the reverse topological sorting sequence, find the vl(k) of each vertex in turn: 

(1) The latest occurrence time of the end vertex (sink point) = the earliest occurrence time of the end vertex (sink point);

(2) The latest occurrence time of the event = the latest occurrence time of the subsequent event (node) - activity (edge) time . The latest time of the event with multiple successor nodes (events) is min {the latest occurrence time of the successor node (event) - edge (activity) time}      to calculate in the same line

③Find the earliest start time e( ) of all activities

③ is deduced from ① . e(i)=ve(k).

(1) The earliest start time of the activity = the earliest occurrence time of the previous event (node) of the activity (edge)

④ Find the latest start time l( ) of all activities

④ is deduced from ② . l(i) =vl(j)-Weight(vk,vj).

(1) The latest start time of the activity = the latest occurrence time of the next event (node) - activity (edge) time .

⑤ Calculate the time allowance d( ) of all activities

⑤ reason ④-③ arrived at . d(i)=l(i)-e(i).


example:

Seeking ①:  Seeking ②:

Push from ① to ③:   Push from ② to ④:

Push ⑤ from ④-③:

Introduced by activities with d=0: Key activities: a2, a5, a7.

The critical path is composed of key activities: critical path: V1—>V3—>V4—>V6.


3) Characteristics of key activities and critical paths

As the time of key activities increases, the overall project duration increases.

Reducing the time for key activities shortens the overall project duration. But when shortened to a certain extent, critical activities may become non-critical activities .

[ It is not that the more compressed the critical path is, the shorter the project completion time will be. Cause: The path is no longer critical after overcompression.

Shorten activity a3→

★There may be multiple critical paths, and only shortening the time of key activities on one critical path cannot shorten the construction period . Only by expediting those critical activities that are included in all critical paths can the purpose of shortening the duration be achieved.

Guess you like

Origin blog.csdn.net/m0_65207522/article/details/127410574