Data structure review - Chapter 6: Figure

Article directory


Part One: Some professional terms for graphs

1. Directed and undirected graphs

  • In a directed graph, the vertex pair <x, y> is ordered, which is calleda directed edge from vertex x to vertex y. Therefore <x, y> and <y, x> are two different sides. <x, y> can also be called an arc. >Arc head
  • In an undirected graph, the pair of vertices (x, y) is ordered, which is calledan edge associated with vertex x and vertex y. This edge has no specific direction, so (x, y) and (y, x) are the same edge.

2. Simple graphs and multiple graphs

  • Simple graph: ① There are no duplicate edges; ② There are no edges from the vertex to itself.
  • Multiple graphs: ① There are more than one edges between two nodes; ② Allow vertices to be associated with themselves through the same edge.

3. Complete graph (also called simple complete graph)

  • In an undirected graph, if there are edges between any two vertices (that is, the graph has a total ofn(n-1)/2 edges), it is called an undirected complete graph;
  • In a directed graph, if there are two directed edges going back and forth between any two vertices (that is, the graph has a total ofn(n-1)), it is called a directed complete graph; arcs

4. Dense graphs and sparse graphs

  • A graph consisting of very few edges or arcs is called a sparse graph, and vice versa is called a dense graph

5. Adjacent points

  • Inundirected graph, if there is an edge (a, b) between two vertices a and b, then it is called a vertex a and b are adjacent points to each other, that is, a and b adjacent points;
    • Edge (a, b) is attached to vertices a and b, or edge (a, b) and vertices a and b < /span>. Associated
  • Indirected graph, if there is an arc <a, b> between two vertices a, b, then it is called Vertex a is adjacent to vertex b, vertex b is adjacent to vertex a;
    • arc <a, b> given point a sum b souken connection.

6. Connectivity, connected graphs and connected components

  • Inundirected graph, if there is a path from vertex v to vertex w, then v and w are said to be Connected.
  • Connectivity is different fromAdjacency:Adjacency must be connected, but connectivity is not necessarily adjacent .
  • If any two vertices in the graph are connected, then the graph G is calleda connected graph, otherwise it is called a non-connected graph.
  • The maximally connected subgraphin the undirected graph is called theconnected component .
    ① There is only one connected component of any connected graph, which is itself; (at least 1)
    ② A non-connected graph has Multiple connected components; (up to the number of vertices)

7. Strong connectivity, strongly connected graphs and strongly connected components

  • Indirected graph, if there are paths from vertex v to vertex w and from vertex w to vertex v, then These two vertices are said to bestrongly connected.
  • If any pair of vertices in the graph is strongly connected, then the graph is calledstrongly connected graph.
  • The maximally strongly connected subgraph in a directed graph is called the strong directed graph Connected components.
    ① There is only one strongly connected component of any strongly connected graph, which is itself; (at least 1)
    ② Not strongly A connected graph has multiple strongly connected components; (up to the number of vertices)

8. Paths, path lengths and loops

  • A path between vertex v and vertex w refers tovertex sequence .
  • The number of edges or arcs on a path is calledpath length.
  • A path whose first vertex and last vertex are the same is calleda loop or loop.

9. Simple paths and simple loops

  • In the path sequence, the path whose vertices do not appear repeatedly is calledsimple path. (Do not pass through a certain location repeatedly)
  • A loop in which the vertices do not appear repeatedly except for the first vertex and the last vertex is called a simple loop or a simple ring. (Do not return to the starting point through a certain location repeatedly)

10. Distance

  • The shortest path from vertex u to vertex vIf it exists, the length of this path is called the distance from u to v.
  • If there is no path from u to v, then the distance is recorded as infinity ( ∞ \infty

11. Spanning tree and spanning forest

  • The spanning tree of (undirected) connected graph is a minimally connected subgraph that contains all the vertices in the graph< a i=3>.
  • If the number of vertices in the graph is n, then its spanning tree contains n-1 edges.
    ① If a graph has n vertices and less than n-1 edges, it is a non-connected graph;< /span>; a>. (It must be a connected graph) ③ However, it should be noted that a graph with n-1 edges is not necessarily a spanning tree. This is a necessary and insufficient condition;must have a cycle
    ② If a graph has n vertices and more than n-1 edges, then
  • In (undirected) disconnected graph, eachconnected component can get aminimally connected subgraph, that is, a spanning tree. These spanning trees form a spanning forest of non-connected graphs.

12. Sub-picture

  • When the set of vertices and the set of relations (edges) of a graph A are both of another graph B< When a i=3> is a subset of the set of vertices and the set of relations (edges), A is said to be a subgraph of B;
  • does not constitute a subgraph as long as it is a subset of the set of vertices and the set of relations (edges) of graph B, because this The two subsets of may not form a graph (the vertices associated with the edges may not be the vertices included in the subset) , so this is a Necessary and insufficient conditions (these two subsets must also be the set of vertices and edges of a certain graph).
  • Minimally connected subgraph: Minimally connected subgraph The subgraph must not only ensure that the graph is connected, but also have the minimum number of edges< /span> subgraph, if any edge is deleted, the subgraph will no longer be connected;
  • Maximal connected subgraph: The maximal connected subgraph needs to contain all the edges in the connected components, that is to say, it can be the same as a minimally connected subgraph in some cases.

13. Degree, in-degree and out-degree

  • The degree of vertex v refers to the number of edges associated with the vertex;
  • For a directed graph, the degree of vertex v is divided into in-degree and out-degree a>:
    ① The indegree is the number of arcs that points to the vertex v; ; emanating from vertex v
    ② The out-degree is the number of arcs

14. Directed tree

  • A directed graph with one vertex with in-degree 0 and the other vertices with in-degree 1 is called directed graph >;Directed tree
  • The spanning forest of a directed graph is composed of several directed trees, containing all the vertices in the graph, but only enough to form several directed trees. The arcs of adisjoint directed tree.

15. Quanhe.com

  • In practical applications, each edge of the graph can be marked with a numerical value with a certain meaning, which is called the weight of the edge;
  • These weights can represent the distance or cost from one vertex to another. Usually we call this weighted graph a net.

Part 2: How to store graphs

1. Adjacency matrix

Use a one-dimensional array to store the information of the vertices in the graph. The one-dimensional array that stores the vertex information is calledVertex table; Use a two-dimensional array to store the information of edges in the graph (that is, the adjacency relationship between vertices), the two-dimensional array that stores the adjacency relationship between vertices is called the adjacency matrix. A graph containing n vertices, its adjacency matrix (Adjacency Matrix) is an n-order square with the following properties Array: ( v i , v j v_i, v_j

       
inivj is the vertex of the graph) A [ i ] [ j ] = { 1 , if arc < v i , v j > or edge ( v i , v j ) exists 0, and vice versa The adjacent edge does not exist A[i][j]=\begin{cases} 1, if the arc <v_i,v_j> or the edge (v_i,v_j) exists\\ 0, otherwise the adjacent edge does not exist\end{cases} < /span>A[i][j]={ 1, young arc<ini,inj>或边(vi,inj)Existence0, otherwise the adjacent edge does not exist
If it is a net with n vertices, its adjacency matrix (Adjacency Matrix) is an n-order square matrix with the following properties: ( w i , j w_{i,j} Ini,j is the weight of the edge) A [ i ] [ j ] = { w i , j , if arc < v i , v j > or edge ( v i , v j ) exists ∞ , otherwise the adjacent edge does not exist A[i][j]=\begin{cases} w_{i,j}, if the arc <v_i,v_j> or the edge (v_i,v_j) exists\\ \infty, otherwise the adjacent edge Edge does not exist\end{cases} A[i][j]={ Ini,j, if arc<ini,inj>或边(vi,inj)Existence, otherwise the adjacent edge does not exist
    

Forundirected graph adjacency matrix:

  • Symmetric matrix, main diagonal elements are 0;
  • Degree of vertex = sum of i row elements = sum of i column elements.

Adjacency matrix for directed graph:

  • It may be asymmetric, but the main diagonal elements are all 0;
  • Out-degree of vertex = sum of i row elements;
  • In-degree of vertex = sum of i column elements.

2. Adjacency list

The adjacency list is a linked storage structure of the graph, for each vertex v i v_i ini creates a singly linked list to store the edges attached to it ( v i , v j ) (v_i, v_j) (vivj()ini 为尾的弧 < v i , v j > <v_i,v_j> <inivj> ), and store the header node in the sequence table. Therefore, the adjacency list consists of two parts:

  • Header node table (also called vertex table): It is stored in a sequential structure from all header nodes. The header node includes data field data and link field firstarc, where :
    • The data field is used to store vertex names and other relevant information;
    • The link field is used to point to the first node in the linked list.
  • Edge table (directed graph is called edge table): It consists of n edge linked lists that represent the relationships between vertices in the graph. The edge linked list contains header nodes and edge nodes, where the edge nodes include adjacent point fields adjvex and chains Field nextarc (and data field info), where:
    • The adjacent point domain indicates the edge ( v i , v j ) (v_i, v_j) (vivj) contact point v j v_j injThe position of in the figure is v j v_j inj The array subscript in the vertex table; (in a directed graph, it is an arc < v i , v j > <v_i, v_j> <inivj> 的头 v j v_j injposition in the picture)
    • The chain domain is used to point to the node of the next edge adjacent to the current vertex; (The connection order in the edge table does not mean the order of edges/arcs in the graph)< /span>
    • If a data field is included, the data field stores relevant information such as the weight of the current edge/arc.

presentwithout directionmiddle point v i v_i iniThe degree of is exactly the number of edge nodes in the i-th linked list.
In directed graph, the number of edge nodes in the i-th linked list is just the vertices< a i=4> v i v_i iniFor the out-degree of , if you require the in-degree, you need to traverse the entire adjacency list (or build an inverse adjacency list).
Construction of adjacency list

[Note]
① The adjacency list of the graph and the child linked list of the binary tree use the same storage structure: Sequence list + linked list, so the storage methods of graphs and trees They can be mutually referenced (the blogger is not sure in which order the methods were proposed). head-to-tail relationship is very similar to the directed graphThe parent-child relationship ③ We mentioned before that graphs can generate trees and forests, because trees;The adjacency linked list representation is not unique
② Since the connection order of edge nodes in a singly linked list can be arbitrary, the graph's (mainly chain storage);

3. Adjacency multiple tables

The adjacency list of an undirected graph makes it easy to obtain various information about vertices and edges. However, since an edge is stored in the edge linked list of two associated vertices, executing need to traverse and find nodes in two linked lists respectively, which is inefficient. The improved adjacency multiple list is more suitable for undirected graphs to perform such operations Linked storage structure (mainly chained storage, including sequentially stored vertex tables).

The vertex node contains two fields:

  • Storage vertex-related informationdata data field;
  • Points to the first edge attached to the vertexfirstedge pointer field.

The edge node contains six fields: (both the data field and the pointer field"i in front, j in the back")

  • mark: Flag field, used to mark whether the edge has been operated. For example, when traversing the vertices in the graph, in order to prevent multiple operations on the same node, a mark field of 0 means that it has not been traversed; a mark field of 1 means that the node has been traversed;
  • ivex and jvex: data fields, which respectively store the positions of the vertices attached to both ends of the edge in the graph, that is, the array subscripts in the vertex table;
  • ilink: pointer field, pointing to the next edge attached to the vertex ivex;
  • jlink: pointer field, pointing to the next edge attached to the vertex jvex;
  • info: pointer field, used to point to the storage address of various information related to the edge (note that it is different from the adjacency list, where info is the data field).

Adjacency multiple lists for undirected graphs

[Explanation]
① In the above picture, the mark flag fields and are not added The adjacency multiple list of info pointer field.
② In fact, the various basic operations and implementations of adjacency lists and adjacency multiple lists of undirected graphs are similar. The difference is that the same edge is adjacent There is only one node in the multiple table.
③ Usually the edge node is stored in the edge list of the vertex with the front index of the array, and the other vertex Just point tothe edge node that has been created before. If multiple edge nodes containing the current vertex are created above, they generally point to the one closest to themselves (for example, vertex d in the figure above points to the node where edge (c, d) is located).

Adjacency multiple linked list example
The drawn adjacency multiple table should be aesthetically pleasing: (easy to observe)

  • We can first point to the edge node containing the vertex above, and then create a new edge node. For example, the vertex v2 first points to the node of the edge (v1, v2), and then creates a node pointing to the edge (v2, v3);
  • We can also create a new edge node first, and then point to the edge node containing the vertex above. For example, vertex v4 first creates and points to the node of the edge (v4, v5), and then points to the node of the edge (v3, v4). ;
  • The edge node does not have to be parallel to the associated vertex, as long as the pointer points to it, for example, the node of edge (v4, v5) is parallel to vertex v6.

4. Cross linked list

In the adjacency linked list, we mentioned that it is very difficult to calculate the in-degree of a directed graph. It is often necessary totraverse the entire adjacency list or to calculate the in-degree and out-degree (mainly chained storage, including sequentially stored vertex tables). Linked storage structuredirected graph The improved cross linked list is a more convenient . Construct an inverse adjacency list

The vertex node contains three fields: (pointer field"head in front, tail in back")

  • data: data field, stores data information related to vertices;
  • firstin: pointer field, pointing to the first arc node whose vertex is arc head ;
  • firstout: pointer field, pointing to the first arc node whose vertex is arc tail .

The arc node contains five fields: (data field"Send (tail) first, receive (head) last" )

  • tailVex (tail field): data field, indicating that the arc'sarc tail vertex is in the graph The position in , that is, the array subscript in the vertex table;
  • headVex (head field): data field, indicating that the arc'sarc head vertex is in the graph The position in , that is, the array subscript in the vertex table;
  • hLink: pointer field, pointing to the next arc with the same arc head;
  • tLink: pointer field, pointing to the same next arc as arc tail;
  • info: pointer field, pointing to the storage address of various information related to the arc.

Cross linked list of directed graph

[Explanation]
① The above picture is a cross linked list without adding info pointer field.
② It can be seen that when drawing the arc node of the cross linked list, the location of the data field (the first two) and the adjacent multi-linked list (the first and third) are different, and do not contain flags domain mark.
③ The cross-linked list is the same as the multi-linked list. There is no order among the arc nodes. Therefore, the cross-linked list of directed graph represents, undirected The multi-linked list representation of the graph is not unique. But converselya cross-linked list can determine a directed graph, and a multi-linked list can determine an undirected graph.

Cross linked list example

  • When drawing the cross linked list, be sure to leave a gap between the vertex tables, otherwise it will be inconvenientfirstin The pointer field points to the arc node; (there must also be enough space horizontally)
  • You can first complete each vertex as the pointer of arc tail, and then use each vertex as Arc head pointer completion.

Part 2 Exercises

Adjacency list and adjacency matrix exercises
[Analysis]Adjacency list and adjacency matrix can store both directed and undirected graphs, and we get is a symmetric adjacency matrix, so there are two possibilities for graph G.


Summary of Parts One and Two

The picture contains a lot of professional terms, and the first part is divided into 15 small points;
The picture also has four storage methods, all of which are Sequential + chain storage structure, two types can store directed graphs and undirected graphs, and two types can only store undirected graphs or directed graphs respectively.


Part 3: Graph Traversal

1. Depth-first search

Depth-first search is also calledDepth-first traversal. Its core idea is:Get to the bottom of it< /span>.

① We first select a vertex as the starting point and visit the vertex;
② Then select an unvisited vertex among the adjacent points of the current vertex and visit the vertex. Repeat this step until the current vertex has no unvisited adjacent points;
③ Go back to the most recently visited vertex and require that the vertex still has unvisited adjacent points, select one of them ( (unvisited) vertex, visit the vertex;
④ Repeat steps two and three until all vertices in the graph have been visited.

Due to the different selection of adjacent points, the order of access will be different, so the sequence obtained by depth-first traversal isnot unique.

[Supplement] A directed graph can only follow the direction of the arc, that is, the current vertex is adjacent to The next vertex;

2. Breadth-first search

Breadth-first search is also calledBreadth-first traversal. Its core idea is:Cast a wide net .
Breadth-first traversal is somewhat similar to hierarchical traversal of a tree, but it has relatively strict order requirements. Queues are used here to assist in explaining the traversal process:

① We first select a vertex as the starting point, add it to the queue, and visit the vertices in the queue;
② Click on all unvisited neighbors of the current vertex Add the queue in a certain order, and then remove the current vertex from the queue;
③ Access the next vertex in the queue;
④ Repeat steps two and three until the queue is If empty, the dequeuing order is the traversal order;

Because the order of joining the queue is different, the order of access will be different, so the sequence obtained by breadth-first traversal is alsonot unique.

[Supplement] A directed graph can only enqueue the vertices associated with the arc with the current vertex as the tail of the arc, that is, it is adjacent to it To the vertex of ;

3. Spanning tree for graph traversal

TransferNo directionNext:

  • If it is a connected graph, in the original graph retain the vertices and edges passed during traversal a>, will form a minimally connected subgraph, that is, depth/breadth-first spanning tree;
  • If it isa non-connected graph, it needs to be traversed from multiple vertices to form multiple minimally connected subgraphs, consisting of a>;Depth/breadth first generation forest

Spanning tree of two traversal methods for undirected graph
DirectionDirected pictureNext:

  • If it isstrongly connected graph, in the original graph retain the vertices and edges passed during traversal< /span> a>; depth/breadth-first directed (spanning) tree, will form a minimally connected subgraph, that is,
  • If it isnon-strongly connected graph, it needs to be traversed from multiple vertices to form multiple minimally connected subgraphs, forming < /span>;Depth/breadth first generation forest
  • In fact, a strongly connected directed graph is equivalent to a connected undirected graph , the two forms based on adjacency matrix storage are exactly the same (refer to the second part of the exercises); a>
  • It should be noted that the spanning tree based onadjacency matrix storage is unique; while the spanning tree based onadjacency list The spanning tree of storage is not unique (refer to the adjacency list part of the node order to be free).

Part 4: Minimum Spanning Tree

1. Definition and properties of minimum spanning tree

The minimum spanning tree is also called the minimum cost tree. It is the set of spanning trees ofweighted connected graph (i.e. connected network). The spanning tree with the smallest sum of weights.
The minimum spanning tree has the following properties:

  • The edge weights of the weighted connected graph G may be the same, so the minimum spanning tree is not unique, that is, the minimum spanning tree'stree shape is not unique;
    • Only when the edge weights in graph G are not equal to each other, the minimum spanning tree of G is unique;
    • If the number of edges of an undirected connected graph G is 1 less than the number of vertices, that is, G itself is a tree, then the The minimum spanning tree is itself.
  • Although the minimum spanning tree is not unique, the sum of the weights of its corresponding edges is always unique and the smallest;
  • The number of edges of the minimum spanning tree is the number of vertices minus 1;

2. Prim algorithm constructs a minimum spanning tree

① Randomly select a vertex ofweighted connected graph as the starting point and add it to the vertex set;
② Find the vertices that areoutside the vertex set but adjacent to the vertices in the vertex set, and select those vertices Among the associated edges, the one with the smallest weight is added to the edge set, and the vertex corresponding to Add the vertex set;
③ Repeat step 2 until the vertex set and edge set form a set containing n vertices and n-1 edges< The minimal connected subgraph of a i=12> is the minimum spanning tree.
Prim's algorithm flow

3. Kruskal algorithm constructs a minimum spanning tree

① Add all the vertices ofweighted connected graph to the vertex set;
② Within the vertex set Among allunconnected vertices, select the edge with the smallest weight to join the edge set;
③ Repeat step 2 , until the vertex set and the edge set form a minimal connected subgraph containingn vertices and n-1 edges, which is the minimum spanning tree.
Kruskal algorithm flow

Part 4 Exercises

Prim's algorithm and Kruskal's algorithm exercises


Part 5: Shortest path

1. Definition of shortest path

Forunweighted graph, the shortest path is from one vertex A to another vertex B is the path that requires the least number of edges. This can usually be achieved using breadth-first search:

  • Starting from vertex A, start the breadth-first search and stop the search when it reaches vertex B. The path from vertex A to vertex B in the resulting subgraph is the shortest path.

Forweighted graph, the shortest path is from one vertex A to another vertex The path that B passes through has the smallest sum of edge weights. This type of shortest path can generally be divided into two categories:

  • One type issingle source shortest path, that is, finding a vertex in the graph (single source The shortest path from point ) to each other vertex (multiple end points ), usually using the classic DijkstraAlgorithmic solution;
  • The other type is to findthe shortest path between each pair of vertices, usually using Floy De algorithm to solve.

2. Dijkstra algorithm generates the shortest path

We usually call the starting vertex of a paththe source point, and the ending vertex is called End point , Dijkstra's algorithm generates the single-source shortest path starting from the same vertex.
We stipulate that only vertices directly connected to a certain vertex through an edge/arc are considered reachable vertices:
① First select a vertex A as the source point, and record the distance from the source point to each vertex in a list, among which the vertices that can be reachedthe weight of the edge/arc, the unreachable vertex ∞ \infty ;
② Select the distance recorded in the table The most recent vertex B is added to the path, and the distance from the source point to the vertex is no longer recorded in subsequent tables; ③ Calculate the distance between the vertex that can be reached after adding vertex B and the source point, if is less than the previously recorded value, update the records about distance and preceding path in the table; ④ Repeat steps two and three until the shortest distance from the source point to each (reachable) vertex is calculated. (PS: Compared with the table in the above picture and the book, the rows and columns are interchanged: here the rows are the end points, and the columns are the set of determined paths; the opposite is true in the book)


DJ algorithm process example

【Replenish】

  • This algorithm only requires that the graph must be connected but is not necessarily strongly connected, so when used in directed graphs, there may be a source point with a certain There is no path between these vertices;
  • Those vertices in the directed graph that have no path to the source point (generally speaking have no in-degree but only out-degree), you need Calculate a new single-source shortest path using itself as the source point;
  • If the graph is not even connected, it is like asking you to drive back and forth between Japan and China to deliver goods. There is no road at all, and you can't go there and you can't come back;

3. Floyd’s algorithm generates the shortest path

Freud's algorithm mainly solves the shortest path between any two vertices in a weighted directed graph. We also stipulate that only vertices that are directly connected to a certain vertex through an arc are considered reachable vertices:
① First use an adjacency matrix to record the distance between the vertices of the weighted directed graph. If arc < v i , v j > <v_i, v_j> <inivj>Existence A [ i ] [ j ] A[i][j] A[i][j]为权值,若弧 < v i , v j > <v_i,v_j> <inivj>nonexistence A [ i ] [ j ] A[i][j] A[i][j]为无穷;
② 在 v i v_i inisum v j v_j injEnter time v k v_k ink, if the inserted path ( v i , v k , v j ) (v_i,v_k,v_j) (vi,ink,inj)existence and length ratio A [ i ] [ j ] A[i][j] A[i][ j]要小,则更多 A [ i ] [ j ] A[i] [j] A[i][ j] is the current path length (saved The path must also be updated);
③ Repeat step 2, where k value increases from 0 to n-1 in order of execution, that is, step 2 needs to be executed n times in total.

Part 5 Exercises

Use Dijkstra's algorithm to find the shortest path of the following directed graph.
Dijkstra's Algorithm Exercises
(PS: The rows and columns of the table in the same picture are interchanged with those in the book)


Summary of Parts 3 to 5

The depth and breadth traversal methods of graphs often examine the algorithm code, but it is still necessary to understand the theoretical implementation;
The most reliable method for graph traversal is to establish a spanning tree (minimally connected subgraph) A method;
Minimum spanning tree is an extension of the knowledge point of spanning tree, mainly including Prim and Kruskal algorithms;
The shortest path part often examines the Dijkstra algorithm, which needs to be mastered.

Thinking: The difference between single-source shortest path and minimum spanning tree

First of all, it must be clear that spanning trees must be for connected graphs/strongly connected graphs, otherwise there cannot be a path that connects all vertices - that is, a spanning forest;
And minimum spanning tree is to select a spanning tree with the smallest sum of edge weights among all spanning trees, so it also requires a weighted graph It is a connected undirected graph or a strongly connected directed graph.
And single source shortest path only requires that the weighted graph must be connected For undirected or directed graphs, select the path with the smallest weight from all the paths from a certain vertex to each vertex, which means:

  • For weightedundirected connected graph,the single-source shortest path is the minimum generator One of the structures of a tree. Because the weights of each edge may have the same value, the minimum spanning tree structure is often not unique. Although the sum of the weights of each edge is the minimum value, there is no guarantee that the path to each vertex will be the shortest when the root node is used as the source point; on the contrary, If there is a shortest path from a certain vertex to every other vertex, then the sum of the weights of each edge must be the minimum value, so the spanning tree with this vertex as the root node must also be the minimum spanning tree.
  • For weighteddirected connected graph,single source shortest path and minimum generation Trees need to be discussed on a case-by-case basis.
    • If the directed graph is alsostrongly connected graph, then it also satisfiessingle source shortest path It is one of the structures of the minimum spanning tree;
    • Otherwise, there is no minimum spanning tree in this directed graph. At the same time, it should be noted that a single vertex cannot reach all other vertices, that is a>The single-source shortest path does not contain all vertices.

Therefore, when solving the single-source shortest path problem, the DJ algorithm is used honestly; when solving the minimum spanning tree problem, in addition to using the original two methods, you can also try the DJ algorithm.


Part 6: Topological sorting

1. Definition of directed acyclic graph and topology

A directed graph that is acyclic (no vertices have arcs pointing to itself) is calleddirected acyclic graph, or for short. a>. relationships connecting entities are abstracted into "lines", and the entities into shapes that have nothing to do with size. "Points" is to abstract "topology" The so-called . DAG diagram

2. Steps of topological sorting

A common method for topological sorting of aAOV network:
① From the AOV network Select a vertex with no predecessor (in-degree 0) 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 or there is no vertex without a predecessor in the current network (the latter case shows that there must be a cycle in the directed graph).

AOV network: If DAG diagram is used to represent a project, its Vertices represent activities, using directed edges < V i , V j > <V_i,V_j> <INi,INj>Display activity V i V_i INiMust precede activity V j V_j INj carries out such a relationship, then this directed graph is called The network whose vertices represent activities is denoted as . AOV Network

Topological sorting step diagram

3. The practical significance of topological sorting

Different from the previous problems such as minimum spanning tree and minimum path, topological sorting does not care about the distance between each vertex and the entity represented by the vertex itself. It cares more aboutdifferent vertices. Is there a topological relationship between them - adjacency.
Topological sorting is mainly used to process directed acyclic graphs. If each vertex in the graph represents various transactions , then studying the "adjacency" topological relationship between vertices is to study the order in which each transaction is carried out a>

  • The in-degree of a certain vertex A is 0, indicating that the vertex is notadjacent to any other vertex, that is to say, the vertex A is not There is any predecessor vertex;
  • The actual meaning represented by the conversion back to vertex A is that there are no other transactions that need to be processed before the current transaction A;
  • After the current vertex A is removed, the other vertices adjacent to the original vertex all have in-degrees -1, that is to say a>The vertex A is the arc head connected to the arc tailThe vertices are possibleBecome the next vertex with in-degree 0 (it does not rule out that there are other vertices B with in-degree 0 as the tail of the arc connecting these vertices) ;
  • The actual meaning of converting back to vertex A is that after the current transaction A is completed,uses this transaction A as the preceding transaction It is possible that it can be started (it is not ruled out that there are other pre-transactions B, etc.) < a i=6>;
  • Only when all the arcs pointing to a vertex have been removed can the vertex be removed - only when all the predecessor transactions of a transaction have been completed can this transaction begin;
  • Eventually the directed graph becomes empty - all transactions are processed;
  • In the end, there are no predecessor-less vertices in the directed graph (there is a cycle, and the predecessor is itself) - there are transactions that cannot be executed (there are errors, and the condition for starting the transaction is that it has been completed).

Part 6 Exercises

[Analysis] The sequence obtained by topological sorting is not unique. When there are multiple vertices with in-degree 0, different selection orders will produce different sequences - when there are multiple transactions with the same priority, the order in which transactions are executed will produce different transaction processes.


Part Seven: Critical Path

1. Definition of AOE network

AOE network: If a weightedDAG diagram is used to represent a project, its Although AOV net and AOE net are both directed acyclic graphs, there are some differences between them: . AOE network, and is recorded asnetwork that uses edges to represent activities, and use directed edges (arcs) to represent activities. The weight on the arc represents the cost of completing the activity (such as activity duration). Then this A directed graph is called a The vertices represent events

  • The meanings of edges and vertices are different:AOV networkThe vertex represents a certain activity (can be understood as a certain transaction), and the arc / Directed edges only reflect the sequence of activities; AOE network The vertex does represent a certain event (can be understood as the cause of the event), while the arc /Directed edge represents a certain activity (can be understood as a certain transaction);
  • Whether there is a weight:AOV networkThe directed edges have no weight;AOE The directed edges in the net have weights, and the weights represent the cost of the activity of the directed edges.

2. Definition of critical path

There is only one vertex with in-degree 0 in the AOE network, which is calledthe starting vertex (source point), which represents the entire project The beginning; there is only one vertex with an out-degree of 0 in the network, called the end vertex (sink), which represents the end of the entire project. In the AOE network, some activities can be carried out in parallel. There may be multiple directed paths from the source to the sink, and these paths may be of different lengths. Although the time required to complete activities on different paths is different, only when all activities on all paths are completed can the entire project be considered completed.

  • [Definition] Therefore, among all the paths from the source to the sink, the path with the maximum path length is called. Key Activities, and the activities on the critical path are called Critical Path
  • [Meaning] The shortest time to completethe entire project is the length of the critical path , that is, the sum of the costs of each activity on the critical path.

3. Solving the critical path

Because key activities affect the time of the entire project, if the key activities cannot be completed on time, the completion time of the entire project will be extended, that is, the length of the critical path will increase.
So as long as the key activities are found, the critical path is found, and the minimum completion time can be obtained. Finding key activities usually focuses on the following parameters:

  • 事件 v k v_k inkThe earliest start time of : refers to the origin point from v 1 v_1 in1Achieved point v k v_k inkthe longest path length.
    • Initially set the earliest event start time of all vertices to 0;
    • The earliest event start time of each vertex is the earliest event of all itspredecessor vertices The maximum value among "start time + corresponding activity duration";
    • can be operated based on topological sorting. When a vertex with in-degree 0 is output, the events of calculating all its subsequent vertices start earliest. Time, if it is greater than the earliest start time of the event currently recorded at the vertex, the record will be updated.
  • 事件 v k v_k inkThe latest start time of : refers to the meeting point from v n v_n innAchieved point v k v_k inkthe longest path length.
    • Initially set the latest start time of all vertices' events as the convergence point v n v_n inn has the same earliest start time;
    • The latest start time of the event of each vertex is the event of all its successor vertices Latest start time - the minimum value of the corresponding activity duration";
    • can be calculated using topological sorting. When outputting a vertex with an out-degree of of 0, calculate the event maximum of all its predecessor vertices. Late start time, if it is less than the latest start time of the event currently recorded by the vertex, the record will be updated.
  • 活动 < v i , v j > <v_i,v_j> <ini,inj>earliest start time:等于arch tailQuick point, immediatelyincident v i v_i iniEarliest start time.
  • 活动 < v i , v j > <v_i,v_j> <ini,inj>’s latest start time: equal to arc headVertex, that is, event v j v_j injLatest start time - activity duration.
  • 活动 < v i , v j > <v_i,v_j> <ini,injTime margin for >: equal to activity's earliest start time - latest start time .

【Payment】
① Activities < v i , v j > <v_i,v_j> <ini,injThe latest start time of > is not necessarily equal to the event v i v_i iniLatest start time of but event v i v_i ini's latest start time must be equal to all activities associated with it < v i , > <v_i,> <ini,>’s minimum value of the latest start time;
② The latest start time of an activitycannot be less thanthe earliest start time of an activity; the latest start time of an event is alsonot May be less than the earliest start time of the event;
③ When the event v i v_i iniWhen the earliest and latest start times of are equal, it means there is an activity < v i , v j > <v_i,v_j> <ini,inj>, its latest start time is the minimum value and equal to the earliest start time, that is, activity < v i , v j > ; <v_i,v_j> <ini,inj>’s latest start time and earliest start time and events v i v_i iniconsistent.

If the time margin of an activity is 0, it means that the activity must be completed as scheduled, otherwise it will delay the entire project process. Therefore, the activity whose time margin is 0 is key activity, which is composed of these activities (weighted arcs) The path from the source to the sink is the critical path.

【★★★】There may be more than one critical path! ! ! (See Part 7 exercises for details)

Yu Payment - ③ Kachi, young activity < v i , v j > <v_i ,v_j> <ini,inj> has a time margin of 0: (The following two points can be used to quickly determine key activities)

  • The latest start time and event of the activity v i v_i iniThe latest start time of is the same, i.e. event v j v_j injLatest start time of - activity < v i , v j > <v_i,v_j> <ini,inj>Holding time=incident v i v_i iniLatest start time of ;
  • 并且incident v i v_i iniand event v j v_j inj both satisfy the latest start time = earliest start time , because the event v i v_i ini’s earliest start time + activity < v i , v j > <v_i,v_j> <ini,inj>duration=event v j v_j injthe earliest start time.

critical path solution

Part 7 Exercises

The following AOE network represents a project containing 8 activities. The overall project duration can be shortened by accelerating several activities simultaneously. Among the following options, the project duration can be shortened by accelerating the progress (C).

Question pictures and options

[Analysis] Shortening the construction period means that both the earliest start time and the latest start time of the event at the junction are reduced.

  • First of all, all activities on the critical path are critical activities, so the duration of the entire project can be shortened by accelerating key activities. But be careful not to shorten it arbitrarily, because when shortened to a certain extent, some key activities may become non-critical activities;
  • Secondly, thecritical path in the network is not unique. Simply increasing the speed of key activities on one critical path cannot shorten the construction period, and It can also cause the critical path to become non-critical.
  • So the core of this question isfind out all critical paths, and speed up those included in All key activities on the critical path can achieve the purpose of shortening the project duration.

The critical path in this question is: v 1 → v 3 → v 2 → v 4 → v 6 v_1 \rightarrow v_3 \rightarrow v_2 \rightarrow v_4 \rightarrow v_6 in1in3in2in4in6 v 1 → v 3 → v 2 → v 5 → v 6 v_1 \rightarrow v_3 \rightarrow v_2 \rightarrow v_5 \rightarrow v_6 in1in3in2in5in6 以及 v 1 → v 3 → v 5 → v 6 v_1 \rightarrow v_3 \rightarrow v_5 \rightarrow v_6 in1in3in5in6, In the comprehensive option analysis, only two key activities d and f include all critical paths.


Summary of Parts Six to Seven

Guess you like

Origin blog.csdn.net/qq_50571974/article/details/126684732