Class notes: directed acyclic graphs and their applications, graph connectivity

AOV Net

AOV network : In a directed graph representing engineering, activities are represented by vertices, and priority relations between activities are represented by arcs. Such a directed graph is called a network of vertices representing activities, or AOV network for short. Features of
AOV network and topological sorting
AOV network :
1. The arc in AOV network indicates a certain restrictive relationship between activities.
2. No loops can appear in the AOV network.
Topological sorting
topological sequence : Let G = (V, E) be a directed graph with n vertices, the sequence of vertices v1, v2,…, vn in V is called a topological sequence if and only if the following conditions are met: If there is a path from vertex vi to vj, then vertex vi must precede vertex vj in the vertex topological sequence. Topological sorting: The process of constructing a topological sequence of a directed graph is called topological sorting.
The topological sequence makes all the predecessors and successors in the AOV network can be satisfied.
Basic idea : (
1) Select a vertex without a precursor
from the AOV net and output it; ( 2) Delete the vertex from the AOV net and delete all arcs ending in the vertex;
(3) Repeat the above two steps until all vertices are There are no vertices without precursors in the output, or in the AOV network.
Design data structure
1. Graph storage structure: Adjacency table storage is adopted, and an in-degree field is added to the vertex table.
2. Stack S: store all vertices without precursors (vertices with zero degree).
The basic idea of ​​topological sorting based on adjacency list
(1) Find the vertices without precursor in G: find the vertex vi whose indegree [i] is zero;
(2) Modify the indegree of vertices adjacent to vertex i (delete all arcs with i as the starting point): For all adjacent vertices k of the chain behind vertex i, reduce the corresponding indegree [k] by 1.
In order to avoid repeated detection of vertices with an in-degree of zero, an auxiliary stack can be set, and if the in-degree of a certain vertex is reduced to 0, it is pushed onto the stack. Whenever a vertex with an input degree of 0 is output, it is deleted from the stack.
Topological sorting algorithm-pseudocode
1. Initialization of stack S; initialization of accumulator count;
2. Scan vertex table, push the vertices without predecessor onto the stack;
3. Loop when stack S is not empty
3.1 vj = exit the top element of the stack; output vj; accumulator increases by 1;
3.2 reduces the in-degree of each adjacent point of vertex vj by 1;
3.3 puts the new vertex with 0-degree into the stack;
4. if (count <vertexNum) outputs loop information;

void TOpSort(){ 
	int  top=-1, count=0; 
	for(int i=0;i<vertexnum;i++)      
		if(adjlist[i].in==0) 
			s[++top]=i; 
	while(top!=-1){     
		j=s[top--]; 
		cout <<adjlist[j].vertext;   
		count++;
		p=adjlist[j].firstedge;     
		while(p!=NULL){           
			k=p->adjvex; 
			adjlist[k].in--;          
			if(adjlist[k].in==0) 
				s[top++]=k;          
			p=p->next;
		}  
	} 
	if (count<vertexNum) 
		cout<<"有回路";
}

Critical Path

AOE network : In a directed graph with weights representing projects, events are represented by vertices, activities are represented by directed edges, and the weights on the edges represent the duration of activities. Such a directed graph is called a network of edges representing activities. , Referred to as AOE network.
The vertices in the AOE network that have no incoming edges are called starting points (or source points) , and the vertices that have no outgoing edges are called end points (or sink points) . Starting
point : The event represented by this vertex has no prerequisites and can be started first.
End point : indicates the end of a project.
A normal AOE network has only one starting point and one ending point.
The nature of the AOE network : (
1) Only after the event represented by a vertex occurs, the activities starting from the vertex can start; (2) The events represented by the vertex can only occur when all activities entering a vertex are ended.
The AOE network can answer the following questions :
1. How long does it take to complete the entire project? There
may be more than one path from the start point to the end point. Only when all activities on each path have been completed will the entire project be considered completed. Therefore, the minimum time required to complete the entire project depends on the longest path length from the start point to the end point, which is the sum of the duration of all activities on this path. The path with the longest path is called the critical path .
2. In order to shorten the time required to complete the project, what activities should be accelerated?
Critical path : In the AOE network, the path with the maximum path length (the sum of the duration of the activities on the path) from the start point to the end point is called Critical Path.
Key activities : Activities on the key path are called key activities.
To find out the critical path, we must find out the key activities, that is, the activities that will affect the completion of the whole project if not completed on schedule.
First calculate the following quantities related to key activities: (
1) the earliest occurrence time of the event ve [k]
⑵ the latest occurrence time of the event vl [k]
⑶ the earliest start time of the activity e [i]
⑷ the latest start time of the activity l [ i]

Finally, calculate the time margin l [k]-e [k] of each activity . The key activity is the time margin 0 .
Selection of storage structure :
For the convenience of processing, two storage structures of adjacency matrix and edge set array are used at the same time .
The adjacency matrix can easily find adjacency points and complete the calculation of the earliest and latest occurrence time.
The edge set array can conveniently calculate the latest time of the time of the activity.

struct Edge{  
	int from;  
	int to;  
	int e;  
	int l; 
};
class Grap{  
	int vertexnum,e;  
	int **adjlist;
	int start,end;  
	Edge *edge;
public:  
	Grap(int n,int e);  
	int  path(); 
};

⑴ The earliest occurrence time of the event ve [k]
ve [k] refers to the maximum path length from the beginning to the vertex vk. This length determines the earliest time that all activities sent from the vertex vk can start.
ve [1] = 0
ve [k] = max {ve [j] + len <vj, vk>} (<vj, vk> ∈p [k])
p [k] represents all directed edges that reach vk The collection
guarantees that all pre-events have ended.
⑵ The latest occurrence time of the event vl [k]
vl [k] refers to the latest time allowed for the event vk without delaying the entire construction period.
vl [n] = ve [n]
vl [k] = min {vl [j] -len <vk, vj>} (<vk, vj> ∈s [k])
s [k] is all sent from vk The
minimum set of directed edges also ensures that if an event occurs at this moment, subsequent work can proceed normally.
⑶ The earliest start time of the activity e [i]
If the activity ai is represented by the arc <vk, vj>, then the earliest start time of the activity ai should be equal to the earliest time of occurrence of the event vk. Therefore, there are: e [i] = ve [k]
⑷ The latest start time of the activity l [i]
The latest start time of the activity ai refers to the latest time that ai must start without delaying the entire construction period . If ai is represented by arc <vk, vj>, the latest start time of ai must ensure that the latest occurrence time of event vj is not delayed. Therefore, there is: l [i] = vl [j] -len <vk, vj>

Graph connectivity

Connectivity of undirected graphs
To determine whether an undirected graph is a connected graph, or has several connected components, the result can be obtained by traversing an undirected graph.
Connected graph : You only need to start from any vertex in the graph and perform a depth-first search (or breadth-first search) to access all the vertices in the graph.
Non-connected graph : need to search from multiple vertices, and the vertex access sequence obtained from the search process from a new starting point each time is exactly the set of vertices in each connected component.
Connectivity of graphs-application of traversal method to
find the connected components of undirected graphs (traversal method of unconnected graphs)
1. count = 0;
2, for (each vertex v in the graph)
2.1 if (v has not been visited)
2.1.1 count ++;
2.1.2 traverse the graph starting from v (function call);
3. if (count == 1) cout << “the graph is connected”;
else cout << “the graph has” << count << "connected components";
the connected graph of
a directed graph The solution process of the connected subgraph of the directed graph
⑴ Start with a depth-first traversal from a vertex and follow the order in which all its neighbors are visited (ie, popped) Arrange the vertices.
⑵ Starting from the vertex that completed the visit lastly, the depth-first traversal is performed along the arc headed by the vertex. If all the vertices cannot be accessed, starting from the last visited vertex in the remaining vertices, continue the reverse depth-first traversal until all vertices in the directed graph are visited.

Published 48 original articles · Like 25 · Visit 2453

Guess you like

Origin blog.csdn.net/qq_43628959/article/details/103341639