Data structure 6-0 figure

Basic concepts of graphs

A graph can be regarded as a set consisting of points and edges. The point set must be non-empty, but the edge set is not necessarily non-empty. The number of points is represented by the order of the graph.

Some concepts that are not too difficult to understand are placed directly below
Insert picture description here
Insert picture description here
. Directed graphs and undirected graphs are the two most commonly tested vectors in the graph. The main difference is the problem of direction. The difference is based on whether the edge is directed. Directed and undirected graphs.
Insert picture description here
Simple graph and multiple graph are a pair of relative concepts. The difference is whether there are multiple edges in a pair of points. Generally, multiple graphs are not considered in postgraduate exam questions.
Insert picture description here
That is, there is an edge between any two points in an undirected graph, and any two points in a directed graph have two edges back and forth.
Insert picture description here
Insert picture description here
You must be optimistic about the concept of subgraphs. You can not just take subsets to get subgraphs, but you can also produce a non-graph structure by simply taking subsets.
Insert picture description here
Connectivity is a very important concept. Connectivity can be reached. This concept is mostly used in undirected graphs. One point can reach another point through any path is connected. If all points in a graph are connected, the graph is connected. . The maximum connected subgraph is the connected component. The most vivid representation is that in the drawn graph, the two connected components can be completely separated into two parts of the graph. In addition, the relationship between the number of connected graph points and the number of edges mentioned here is often used as a method to construct the smallest disconnected graph.
Insert picture description here
Insert picture description here
As the correspondence of connectivity, a directed graph considers strong connectivity, that is, one point can go to another point and then back to the starting point from this point. With reference to the definition of connectivity, you can similarly understand concepts such as strong connected components.
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

Four storage methods of graphs

The mainstream representation methods are the adjacency matrix and the adjacency list method, and there are also cross-linked lists and adjoining multiple lists that are not very commonly used.

1. Adjacency matrix The
adjacency matrix generally understands discrete mathematics. The corresponding position of an edge in an undirected graph is 1, otherwise it is 0. In a directed graph, which position is 1 is determined according to the start and end points. Which position is 0. If it is a weighted graph, you can also change 1 to the weight of the edge.

In practice, it is generally implemented with a two-dimensional array. Since the two-dimensional array is marked by points, when there are few edges in the graph, most of the elements in the matrix are initialized to 0, and the utilization rate is very low, so the adjacency matrix is ​​generally used to store dense graphs. The space complexity of the adjacency matrix representation is O(n2).

The use of adjacency matrix has the following characteristics:

Second, the adjacency list method is
like an array and a linked list. The adjacency list method has the advantage of flexibility and allocates space according to demand, so it is suitable for storing sparse graphs.

The adjacency table is actually composed of two parts, a vertex table and an edge table. The vertex table is used to record vertices. The edge table is connected behind the vertex to indicate the edge of this point. The vertex table node is composed of the vertex field and the pointer to the first adjacent edge. The edge table node is composed of the adjacent point field and the pointer field to the next adjacent edge. Generally, the vertex table is stored in order, and the edge table is stored in chain instead.

You must draw the adjacency list according to the meaning of the title. When drawing the adjacency list, you need to pay attention to the direct connection between the vertices and the connection between the edges. Just remember an example.
Insert picture description here
In addition, I like to use adjacency table storage in code questions. The template for adjacency table traversal is as follows:

for(int i=1;i<=n;i++) 
{
    
    
	now=head[i];
	for(int j=head[i]->next;j!=NULL;j=j->next)
	{
    
    
		
	}
}

The use of adjacency list has the following characteristics:
Insert picture description here
Third, the cross-linked list The
cross-linked list is a chain storage structure of the directed graph . In the cross-linked list, there is a node corresponding to each arc in the directed graph, and there is also a node corresponding to each vertex. In the cross-linked list, it is easy to find the arc only for the tail and the arc only for the head, so it is easy to find the out and in degrees of the vertices. The cross-linked list of a graph is not unique, but a cross-linked list indicates that a graph is determined.

Insert picture description here
To briefly explain, the arc node is used to record the edge, where the tail field and the head field represent the two end points and the start point of this edge, hlink points to the next edge of the same starting point, and tlink points to the bottom of the same end point. An edge, info is used to store information about this edge, such as weights. There are only three parts in the vertex node, data stores data, firstin points to the first edge starting from this vertex, and firstout points to the first edge focusing on this.

Insert picture description here
4. Adjacent multiple tables
Adjacent multiple tables is another chain storage structure of undirected graphs . Similar to the cross-linked list, in the adjacency multi-list, each edge is represented by a node.
Insert picture description here
Among them, mark represents a mark, used to distinguish whether this edge has been experienced, ivex and jvex respectively represent the start and end of this edge, ilink represents the next edge of the same starting point, jlink represents the next edge of the same end, info Used to store related information such as weights.
Each vertex is also represented by a node:
Insert picture description here
there are only two quantities in total, the storage data and the first edge with the node as the vertex.

In the adjacency multi-list, all edges attached to the same vertex are connected in series in the same linked list. Since each edge is attached to two vertices, each edge node is linked in two linked lists at the same time. For undirected graphs, the difference between the adjacency table and the adjacency table is only that the same edge is represented by two nodes in the adjacency table, while there is only one node in the adjacency table.
Insert picture description here

Graph traversal

The traversal of the graph here mainly talks about the two search methods required by Blue Bridge: breadth first and depth first.

Breadth-first search is similar to a binary tree's sequence traversal algorithm. The basic idea is to start from a point and enqueue all unvisited points around it, which is equivalent to creating an encircling circle to wrap this starting point, and then take it from the queue. Point, in order to enqueue all the surrounding unvisited points, it looks like it is expanded layer by layer. The breadth-first search process of traversing the graph is based on v as the starting point, from near to far to visit the vertices that have a path to ν and the path length is 1, 2, ….
Breadth-first search is a hierarchical search process. Every step forward may visit a batch of vertices. Unlike the depth-first search, there is no backtracking, so it is not a recursive algorithm. In order to achieve layer-by-layer access, the algorithm must use an auxiliary queue to memorize the next layer of vertices that are being accessed.

In the process of wide search, an array is needed to record whether it has been visited, and a queue to store nodes is also needed. All nodes will be enqueued once. In the worst case, the space complexity is O(|V|). When using the adjacency list storage method, each vertex needs to be searched once (or enqueued once), so the time complexity is O(|V|). When searching for the neighboring point of any vertex, each edge is visited at least once. Therefore, the time complexity is 0 (|E|), and the total time complexity of the algorithm is 0 (|V| +IEI). When the adjacency matrix storage method is adopted, the time required to find the adjacent point of each vertex is O(|V|), so the total time complexity of the algorithm is O(|V|2).

In the process of breadth traversal, we can get a traversal tree, called breadth-first spanning tree. It should be noted that the adjacency matrix storage representation of a given graph is unique, so its breadth-first spanning tree is also unique, but because the adjacency list storage representation is not unique, its breadth-first spanning tree is also not unique. That is, the breadth-first spanning tree of the graph stored in the adjacency matrix is ​​unique, and the breadth-first search tree of the graph stored in the adjacency table is not unique.
Insert picture description here

The code template of Guangsou is as follows:

void bfs()
{
    
    
	初试状态入队列
	while(!q.empty())
	{
    
    
		p=q.front();
		q.pop();
		for(当前状态的所有可达状态)
		{
    
    
			if(未访问)
			{
    
    
				q.push(未访问状态);
				visit[未访问状态]=1;
			}
		}	
	}
 } 

Depth-first search is similar to the pre-order traversal of a tree. To put it bluntly, it is violence, one way to the black. Its basic idea is to start from a point, move to an unvisited adjacent point, and then continue to visit the unvisited adjacent point of the adjacent point after moving, and return to the previous layer to continue visiting other unvisited adjacent points after the visit is completed, and repeat this Move until there are no points to visit.

According to the characteristics of DFS, recursion is generally used to implement the code:

void dfs(int x,int y)
{
    
    
	for(所有可达的点)
	{
    
    
		int tx=x+dir[i][0];
		int ty=y+dir[i][1];
		移动到新的点
		if(可访问且未访问)
		{
    
    
			vis[tx][ty]=1;
			dfs(tx,ty);
		} 
	}
}

Similarly, for the same graph, the DFS sequence and BFS sequence obtained by the traversal based on the adjacency matrix are unique, and the DFS sequence and BFS sequence obtained by the traversal based on the adjacency list are not unique.
Insert picture description here

The DFS algorithm is a recursive algorithm that requires a recursive work stack, so its space complexity is O(|V|). The process of traversing the graph is essentially a process of finding its neighboring points for each point, and the time it takes depends on the storage structure used. When represented by an adjacency matrix, the total time complexity is O(|V|2). When represented by an adjacency list, the total time complexity is O(|V|+|E|).

Like breadth-first search, depth-first search also produces depth-first search trees. Of course, this is conditional, that is, calling DFS on the connected graph can generate a depth-first spanning tree, otherwise the depth-first spanning tree will be generated.
Insert picture description here
According to the characteristics of DFS, it is not difficult to find that each DFS process is to visit a connected component of a graph completely once. Based on this, the graph can be used to determine the connected component. If the DFS is once, the graph can be traversed. For each node of, it means that the graph has only one connected component, that is, the graph is a connected graph, otherwise it is a non-connected graph.

Graph application

This part of application mainly involves five knowledges, minimum spanning tree, shortest path, directed acyclic graph, topological sorting, and critical path.

1. The minimum spanning tree
In short, the minimum spanning tree is the one with the smallest weight among all spanning trees in a graph. It may not be unique. When all the edge weights are not equal, the minimum spanning tree is the only one. Although the minimum spanning tree is sometimes not unique, the sum of weights is unique.

The test point of the minimum spanning tree is mainly to manually simulate two algorithms for finding the minimum spanning tree: Prim's algorithm and Kruskal's algorithm.

Prim's algorithm is similar to Dijkstra's algorithm. The main idea is to continuously add the points closest to the set to the set of determined points. Choose a point to join the set in the first try, and then see which of the points that can be reached by this set is the closest, choose the closest point to join the set, and repeat this process until all points are added to the set.
Insert picture description here
The time complexity of Prim's algorithm is O(|V|2), so it is suitable for solving minimum spanning trees with dense edges. Although other methods can improve the time complexity of Prim's algorithm, they increase the complexity of the implementation. Mainly, I will hand-write the process of adding and selecting points.

Kruskal's algorithm is a method of starting from the edge. In the initial state, there are only points without edges. Each time an edge with the smallest weight is added to the graph. If there is no loop, the edge will be kept in the graph. Repeat this The process until the graph becomes a connected graph.
Insert picture description here
The time complexity of Kruskal's algorithm is O(|E|log|E|), which is suitable for graphs with sparse edges and many points.

2. The shortest path problem.
When the graph is a weighted graph, define the sum of the weights on the edges of a path (maybe more than one) from a vertex Vo to any other vertex V in the graph as the band of the path Weighted path length, the path with the shortest weighted path length is called the shortest path. Generally, the shortest path problem is divided into two categories, the single-source shortest path (from one point to the other points) and the shortest path between each pair of vertices, corresponding to Dijkstra's algorithm and Freud's algorithm respectively.

Dijkstra's algorithm is also used in the computer network to find the OSPF protocol. During the operation of the algorithm, two arrays are needed to record the process. dist is used to record the shortest distance to the origin, and path is used to record the precursor, which is convenient for the algorithm. Find the entire path at the end. The first step of the algorithm is initialization. If the starting point is V0, then the points reachable by V0 are initialized to the corresponding distance in the dist array, and the unreachable points are initialized to infinity, and the shortest edge in the dist array is selected each time. Add the points to the set, and then update the dist array. If the distance from the newly added point to another point is less than the direct distance from the origin to another point, then update the dist to the new minimum distance and repeat this operation. Until all points have entered the collection.

The most important point of Dijkstra's algorithm is that it will draw a diagram of the entire process, including the update of the dist array and the edge selection process.
Insert picture description here
Dijkstra's algorithm is based on the idea of ​​greed. Every time the current optimal solution is selected, whether it is stored in an adjacency list or an adjacency matrix, the time complexity of Dijkstra's algorithm is O( |V|2). It should be noted that Dijkstra's algorithm cannot be used when the edge weight is negative.

Freud's algorithm is another algorithm for finding the shortest path problem. Compared with Dijkstra's algorithm, Freud's algorithm is more advanced and more abstract to understand.
Insert picture description here
To put it simply, iterate continuously from the initial state. The initial state is the adjacency matrix of the graph. At this time, it means that the two points are directly connected without transiting through other points. The subsequent iteration means continuous transit. If the transit distance is greater Short, then take the shorter distance as the true distance.
Insert picture description here
The time complexity of Floyd's algorithm is O(|V|3). However, because the code is very compact and does not contain other complex data structures, the implicit constant coefficient is very small, even for medium-scale input, it is still quite effective. Freud's algorithm can solve the situation where the weight of the edge is negative, but it still cannot handle the situation where there are loops.

3. Representation of acyclic directed graphs There
is no difficulty in this part, which is to use directed acyclic graphs to realize the sharing of repeated subtypes, thereby reducing storage space.
Insert picture description here
Fourth, topological sorting
Insert picture description here
Simply put, a graph is regarded as a process for completing a thing. This process includes many activities. Topological sorting is to find an order that can be completed according to this order without conflict. Topological sorting is not unique, the important thing is to determine the topological sorting algorithm. At the same time, in some cases, inverse topological sorting is used, as long as the following topological sorting process is reversed.
Insert picture description here
5. Critical path The
critical path is the most troublesome of these five applications. It requires a lot of data to be calculated, but the process is not difficult.
In a weighted directed graph, events are represented by vertices, activities are represented by directed edges, and the cost of completing the activities (time required to complete the activities) is represented by the weights of edges. This is called a network with edges representing activities. Referred to as AOE Net. Both AOE nets and AOV nets are directed acyclic graphs. The difference is that their edges and vertices represent different meanings. The edges in the AOE net have weights; while the edges in the AOV nets have no weights, only Represents the context between vertices. The point where the in-degree is 0 is the starting point, also called the source point, and the point where the out-degree is 0, is the end point, also called the sink point. Activities in the AOE network diagram can be carried out in parallel, and only when all activities are completed can the project be considered as complete. The shortest time to complete the entire project is the length of the critical path, that is, the total cost of each activity on the critical path. This is because the key activities affect the time of the entire project, that is, if the key activities cannot be completed on time, the completion time of the entire project will be prolonged. Therefore, as long as the key activity is found, the critical path is found, and the shortest completion time can be derived.

When calculating the critical path, four main quantities are calculated, the earliest and latest occurrence time of an event, and the earliest and latest occurrence time of an event. In the actual calculation, first draw the AOE diagram to sort out the sequence, then start from the starting point, first calculate the earliest occurrence time of the event, the earliest occurrence time of the source point is 0, and then you can use topological sorting to see all the precursors of each node , Find the earliest occurrence time of the precursor plus the longest activity time as your earliest occurrence time, complete the earliest occurrence time of all events, and then calculate the latest occurrence time, starting from the meeting point, take the latest occurrence time of all subsequent events Subtract the minimum value of active time. Then calculate the earliest and latest start time of the activity. The earliest start time of the activity is the earliest start time of the event represented by the arc starting point, and the latest start time is the difference between the event represented by the arc end point and the activity time. The difference between the earliest and latest start time represents the flexible time of this activity. If the two are equal, it means that this activity cannot wait. If you wait, the following activities will be postponed, that is, the activities on the
Insert picture description here
Insert picture description here
critical path of the key activity are all For key activities, the time of these activities can be reduced to reduce the total time, but it cannot be reduced too much. If it is reduced too much, it may change the critical path and thus change the key activities. The critical path in the AOE network is not unique, and for a network with several critical paths, only increasing the speed of key activities on one critical path cannot shorten the construction period of the entire project, only speeding up those key activities included in all critical paths In order to achieve the purpose of shortening the construction period, that is, the intersection of the critical paths.

Typical questions

Insert picture description here
The key to the question is that in any case, it is different from the minimum case. The minimum case can be completed with only 6 edges, but it is not in any case. The method used here is to take 6 points first, and the complete graph of 6 points needs 5+ 4+3+2+1=15 edges. At this time, add the seventh point and add one edge to turn it into 16 edges, then it must be connected.
Insert picture description here
Twice the number of edges in an undirected graph is equal to the sum of the degrees of the vertices, so in this question, the sum of the degrees is 16 2=32, minus 4 3+3 4+2 x+1*y, the question asks at least how many Vertices, so y=0, at this time, x=4 can be solved, and the total number of nodes is 4+4+3=11. In the same way, if you ask how many vertices there are at most, then x=0, y=8, and the total number of points is 4+3+8=15.
Insert picture description here
The topic is a bit confusing. First, a tree with n nodes has n-1 edges. Assuming that there are x trees, then connecting the forest into one tree will increase x-1 edges. Then the equation e+(x-1)+1 can be listed =n, so x=ne.
Insert picture description here
When I saw this question, I was compelled. This is not a test of linear algebra matrix multiplication. The question is really a bit abstract and it is not clear how to solve it. Personally, I feel that it is completely dependent on finding the law. First write the low order of a matrix. In the case of the square, look at the specific value to guess the meaning of the value of the element.
Insert picture description here
Insert picture description here
Using depth-first traversal, if starting from a vertex u on the directed graph, an edge from vertex v to u appears before the end of DFS(u). Since v is a descendant of u on the spanning tree, the factor must be There are loops containing u and v, so depth-first search can detect whether a directed graph has a loop.
In topological sorting, the sequence can only be added when a vertex is not the head of any edge. When there is a loop, the vertex in the loop is always the head of a certain edge and cannot be added to the topological sequence. In other words, if there is still no vertex that can be added to the topological sequence, it means that there is a loop in this graph.
The shortest path itself allows loops, so it cannot be used to determine whether there are loops.
The critical path is a bit far-fetched. When looking for a critical path, you generally need to use topological sorting to get a sequence, and calculate the earliest and latest time according to this sequence, so you can judge whether there is a loop.
Insert picture description here
The vertices are not in the topological sequence, which means that these vertices are always the heads of certain edges. These points are put together to form a loop, and these loops are just a strong connected component.
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_43849505/article/details/107794961