MindMap Gallery Knowledge map of data structure graph
The following summarizes the knowledge content of graphs in data structures, including the basic concepts of graphs, graph storage and basic operations, graph traversal, graph applications, etc.
Edited at 2022-04-04 21:56:33Avatar 3 centers on the Sully family, showcasing the internal rift caused by the sacrifice of their eldest son, and their alliance with other tribes on Pandora against the external conflict of the Ashbringers, who adhere to the philosophy of fire and are allied with humans. It explores the grand themes of family, faith, and survival.
This article discusses the Easter eggs and homages in Zootopia 2 that you may have discovered. The main content includes: character and archetype Easter eggs, cinematic universe crossover Easter eggs, animal ecology and behavior references, symbol and metaphor Easter eggs, social satire and brand allusions, and emotional storylines and sequel foreshadowing.
[Zootopia Character Relationship Chart] The idealistic rabbit police officer Judy and the cynical fox conman Nick form a charmingly contrasting duo, rising from street hustlers to become Zootopia police officers!
Avatar 3 centers on the Sully family, showcasing the internal rift caused by the sacrifice of their eldest son, and their alliance with other tribes on Pandora against the external conflict of the Ashbringers, who adhere to the philosophy of fire and are allied with humans. It explores the grand themes of family, faith, and survival.
This article discusses the Easter eggs and homages in Zootopia 2 that you may have discovered. The main content includes: character and archetype Easter eggs, cinematic universe crossover Easter eggs, animal ecology and behavior references, symbol and metaphor Easter eggs, social satire and brand allusions, and emotional storylines and sequel foreshadowing.
[Zootopia Character Relationship Chart] The idealistic rabbit police officer Judy and the cynical fox conman Nick form a charmingly contrasting duo, rising from street hustlers to become Zootopia police officers!
picture
6.1 Basic concepts of graphs
Definition: G=(V,E), vertex set V, edge set E
Use |V| to represent the number of vertices in graph G, also called the order of the graph. Use |E| to represent the number of edges in graph G.
Undirected graph (undirected edge/edge), directed graph (directed edge/arc)
Simple graph, multiple graph
Degree, out-degree, in-degree of vertices (undirected graph? directed graph?)
Edge weight, weighted graph/net, weight value
point-to-point relationship
path, loop, simple path, simple loop
path length
Point-to-point distance (shortest path)
Connectivity, connected graphs and connected components (undirected graphs)
The maximal connected subgraph in an undirected graph is called a connected component
Starting from a vertex as a subgraph, add the vertices connected by edges of this subgraph one by one until all vertices are included in the graph. The generated subgraph is a maximal connected subgraph.
If a graph has n vertices and the number of edges is less than n-1, then the graph must be a disconnected graph.
Strongly connected graph, strongly connected component (directed graph)
If G is a strongly connected graph, it has at least n edges (forming a cycle)
The maximal strongly connected subgraph in a directed graph is called the strongly connected component of the directed graph.
When a vertex only exits the arc but does not enter the arc, other vertices cannot reach this vertex, and this single vertex constitutes a strongly connected component.
Spanning tree, spanning forest
The spanning tree of a connected graph is a minimal connected subgraph that contains all the vertices in the graph.
If the number of vertices in the graph is n, then its spanning tree has a total of n-1 edges.
For a spanning tree, if an edge is cut off, it will become a non-connected graph; if an edge is added, a cycle will be formed.
In a disconnected graph, the spanning tree of connected components constitutes the spanning forest of the disconnected graph.
Several special forms of pictures
complete graph
sparse graph, dense graph
tree, forest, directional tree
6.2 Picture storage and basic operations
adjacency matrix method
As long as the vertex number is determined, it means that it is unique
"Row into list" criterion: For a directed graph, the out-degree of the i-th node = the number of non-zero elements in the i-th row, and the in-degree of the i-th node = the non-zero elements in the i-th column number
For an undirected graph, the degree of the i-th node = the number of non-zero elements in the i-th row or i-th column
The time complexity of finding the degree of the vertex is O(|V|), and the space complexity is O(|V|²)
Suitable for storing dense graphs. The adjacency matrix of undirected graphs is a symmetric matrix and can be compressed and stored (only the upper/lower triangle area is stored)
Assume that the adjacency matrix of graph G is A (matrix elements are 0/1), then the elements A^n[i][j] of A∧n are equal to the number of paths of length n from vertex i to vertex j.
Adjacency list method (sequential chained storage)
The adjacency representation of a graph is not unique
space complexity
Undirected graph O(|V| 2|E|)
Directed graph O(|V|2|E|)
Store sparse graphs
It is inconvenient to calculate the in-degree of a directed graph (it is inconvenient to find the in-degree of a directed graph), but the rest is very convenient
Cross linked list (directed graph)
Arc nodes and vertex nodes
Space complexity O(|V| |E|)
Adjacency multiple list (undirected graph)
Space complexity O(|V| |E|)
Basic operations on graphs
6.3 Graph traversal
Breadth-first traversal (BFS)
Similar to level-order traversal of a binary tree
Algorithm points
Requires an auxiliary queue
How to find other vertices adjacent to a vertex; the visit array prevents repeated visits
How to deal with disconnected graphs
the complexity
Space complexity O(|V|)-auxiliary queue
time complexity
Time to visit a node Time to visit all edges
Adjacency matrix: O(|V|^2)
Adjacency list: O(|V| |E|)
breadth-first spanning tree
The graph representation method stored in the adjacency list is not unique, nor is the traversal sequence or spanning tree unique.
Traversing a disconnected graph can generate a breadth-first forest
Depth-first traversal (DFS)
Similar to pre-order traversal of a tree, the search strategy is to search a "graph" as deep as possible
Algorithmic thinking
Recursively explore unvisited adjacent points in depth, how to find other adjacent vertices from one vertex; the visit array prevents repeated visits
How to deal with disconnected graphs
Complexity analysis
Space complexity O(|V|) - from the recursive work stack
time complexity
Adjacency matrix: O(|V|^2)
Adjacency list: O(|V| |E|)
depth first spanning tree
Same as breadth-first spanning tree
Graph traversal and graph connectivity
For undirected graph
Number of DFS/BFS function calls = number of connected components
For directed graphs
If there is a path from the starting vertex to other vertices, you only need to call the DFS/BFS function once
For strongly connected graphs, you only need to call the DFS/BFS function once from any vertex.
6.4 Application of diagrams
Minimum spanning tree (minimum cost tree)
Definition: Spanning tree (MST) with the smallest sum of edge weights
Properties of Minimum Spanning Tree
There may be multiple minimum spanning trees, but the sum of edge weights is always unique and smallest.
The number of edges of the minimum spanning tree = the number of vertices - 1; if one is cut off, it will be disconnected, and if one is added, a loop will appear.
If a connected graph is itself a tree, then its minimum spanning tree is itself
Only connected graphs have spanning trees, and unconnected graphs have spanning forests.
As long as an undirected connected graph has no edges with equal weights, its minimum spanning tree is unique because the search process using different algorithms is unique.
Minimum Spanning Tree Algorithm
Prim algorithm (Prim)
Overview: Build a spanning tree starting from a certain vertex; each time the new vertex with the smallest cost is included in the spanning tree until all vertices are included.
Time complexity O(|V|^2)
Suitable for edge-dense graphs
Kruskal algorithm (Kruskal)
Each time, the edge with the smallest weight is selected (provided no loop is generated), and the two ends of this edge are connected (the ones that are already connected are not selected) until all nodes are connected.
Time complexity: O(|E|log|E|)
Suitable for graphs with sparse edges and many vertices
shortest path problem
single source shortest path
BFS algorithm (unweighted graph)
It is a small modification to the BFS algorithm. When visiting a vertex, modify its shortest path length d[] and record the predecessor node in path[]
Time complexity O(|V|^2) or O(|V| |E|)
dijkstra algorithm (weighted graph, unweighted graph)
Create three arrays
final[ ] marks whether each vertex has found the shortest path
dist[] marks the shortest path length
path[ ] marks the predecessor on the path
Algorithm idea: Each round loops through all nodes to find the vertex Vi that has not yet determined the shortest path and has the smallest dist, let final[i]=true, Check all vertices adjacent to Vi, if their final value is false, update the dist and path information
Time complexity O(|V|^2)
Note: This algorithm does not apply to weighted graphs with negative weights on the edges.
The shortest path between vertices
floyd algorithm (weighted graph, unweighted graph)
Use dynamic programming ideas to divide problem solving into multiple stages
#Initial: Transfers at other vertices are not allowed. What is the shortest path?
#0: If transit at Vo is allowed, what is the shortest path? It keeps looping afterwards
Floyd's algorithm cannot solve graphs with "negative weight cycles" (edges with negative weights form a cycle). This kind of graph may not have the shortest path.
Time complexity O(|V|^3)
You can also use dijkstra's algorithm to find the shortest path between all vertices, just repeat |V| times, and the total time complexity is also O(|V|^3)
Directed acyclic graph description expression (DAG)
If there is no cycle in a directed graph, it is called ~
Salted fish problem solving method
① Arrange each operand in a row without duplication
② Mark the order in which each operator takes effect (it doesn’t matter if the order is slightly different)
Add operators in order, pay attention to "layering"
Check whether operators at the same level can be combined layer by layer from bottom to top
topological sort
AOV network
The vertex represents the activity and the directed edge <Vi,Vj> represents that the activity Vi must be performed before Vj
The AOV network must be a DAG graph and cannot have cycles.
topological sort
Select a vertex with no predecessor (in-degree 0) from the AOV network and output it
② Delete the vertex and all directed edges starting from it from the network
③Repeat ① and ② until the current AOV network is empty
reverse topological sort
① Select a node with no successor (out-degree 0) from the AOV network and output it
Remove the vertex and all directed edges ending with it from the network
Repeat ① and ② until the current AOV net is empty or there are no predecessor-less vertices in the current net. (Indicates there is a loop)
If you imitate the idea of topological sorting to implement reverse topological sorting, pay attention to using the storage structure of the adjacency matrix. If you use the adjacency list, because if you delete a vertex, you will also delete the edge pointing to the vertex. The entire table must be traversed every time, which takes a long time. Very high complexity
Inverse adjacency list can be used
Another implementation method: use the DFS algorithm to implement topological and inverse topological sorting (when implementing inverse topological sorting, output the vertex before destacking)
nature
Topological sorting and reverse topological sorting sequences may not be unique
If there is a cycle in the graph, there is no topological sorting sequence/inverse topological sorting sequence.
For a general graph, if its adjacency matrix is a triangular matrix (there is no cycle), then there is a topological sequence; vice versa, it is not necessarily true.
If a directed graph has an ordered topological sorting sequence, then its adjacency matrix must be a triangular matrix
Critical Path
AOE network
In a 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.
Related concepts
The in-degree of the start vertex (source point) is 0, indicating the beginning of the entire project
There is only one vertex with an out-degree of 0, called the end vertex (sink), which represents the end of the entire project.
There may be many directed paths from the source to the sink. 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.
Solution
① Find the earliest occurrence time of all events ve()
Determines the earliest time that all activities starting from vk can start
② Find the latest occurrence time vl() of all events
It refers to the latest time that the event can occur without delaying the completion of the entire project.
③Find the earliest occurrence time e( ) of all activities
Refers to the earliest occurrence time of the event represented by the starting point of the activity arc
④ Find the latest occurrence time l( ) of all activities
It refers to 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
⑤Find the time margin of all activities d( )=l( )-e( )
The activity with d(i)=0 is the key activity, and the critical path can be obtained from the key activities.
① Find ve(k) in sequence according to the topological sorting sequence: ve(source point)=0 ve(k)=MAX{ve(j) Weight(vj,vk)}, vi is the predecessor of vk ② Arrange according to the reverse topological sequence to find vl(k) vl(sink)=ve(sink) vl(k)=Min{vl(j)-Weight(vk,vj)}, vj is any successor of vk
characteristic
If the time required for key activities increases, the duration of the entire project will increase
Shortening the time of key activities can shorten the construction period of the entire project
When shortened to a certain extent, critical activities may become non-critical activities
There may be multiple critical paths. Only increasing the speed of key activities on one critical path will not shorten the construction period of the entire project. Only by speeding up those key activities included on all critical paths can the purpose of shortening the construction period be achieved.
theme