Data structure, data structure and Algorithm

Source: Internet
Author: User

Data structure, data structure and Algorithm
Application background

Student electives
Vertex -- indicates the course
Directed arc -- indicates a prerequisite. If course I is a prerequisite for course j, an arc (I, j) is shown in the figure)
In what order should students study these courses in order to complete their studies without conflict and smoothly-topological sorting

A topological sequence is an ordered sequence composed of vertices in a directed acyclic graph. The sequence meets the following conditions: If a path exists between the vertex vi and the other vertex vj in the graph, vj is located behind the vi in the topological sequence of the graph.
Directed Acyclic Graph (DAG) and AOV Network

Directed Acyclic Graph (DAG)
Activity On Vertex network: a "feasible" AOV network must be a DAG. Otherwise, there is a loop in the figure, so you cannot determine which of the activities in the loop are implemented first.
Vertex indicates activity,
Arc indicates the priority relationship between activities,
Note:
The arc in the AOV network indicates that there is a certain constraint between activities: If

Void ToplogicalSort (Graph G, int TopNum []) {int Counter;/* topological sequence number, used to identify the output order of each vertex */int v, w; int * InDegree = (int *) malloc (G. n * sizeof (int); GetInDegree (G, InDegree);/* calculate the inbound degrees of each vertex in graph G */for (Counter = 0; Counter <G. n; Counter ++) {v = FindNewVertexOfDegreeZero ();/* returns the subscript of the vertex in the vertex array if it is found. If the vertex with an input degree of 0 is not found, return-1 */if (v =-1) {printf ("the graph has a loop"); break ;} topNum [v] = Counter; for (w of each adjacent vertex of v in G) Indegree [w] --; /* increment of each vertex connected to vertex v minus 1 */}}
Void ToplogicalSort (GraphAdjList GL, int * TopNum) {Queue queue; EdgeNode * p; int Counter = 0; int v, w; // int * InDegree = (int *) malloc (G. n * sizeof (int); // GetInDegree (G, InDegree);/* calculate the inbound degrees of each vertex in graph G */queue = InitQueue (GL-> numVertexes ); /* Create an empty queue */for (v = 0; v <GL-> numVertexes; v ++) if (GL-> adjList [v]. in = 0) EnQueue (& queue, v); while (! QueueEmpty (queue) {v = DeQueue (& queue); TopNum [v] = ++ Counter; /* assign the vertex topological Sequence Number */for (p = GL-> adjList [v]. firstedge; p! = NULL; p = p-> next) if (-- GL-> adjList [p-> adjvex]. in = 0) EnQueue (& queue, p-> adjvex);} if (Counter! = GL-> numVertexes) printf ("Graph loops \ n"); DisposeQueue (& queue);/* release queue space * // free (InDegree );}
Key Path
In contrast to the AOV network, the Activity On Edge indicates the directed acyclic graph of the Activity. Vertex indicates an Event. Each Event indicates that the previous activity has been completed, and the activity after it can start. arc indicates activity, the weight on the arc indicates the time or cost required for the corresponding activity.
AOV network and AOE Network

Topology Sorting is mainly used to solve the problem of whether a project can proceed smoothly.
The key path is to solve the shortest time required for project completion.
Differences between AOV and AOE:
In the AOV network, vertices represent activities and edges describe constraints between activities.
AOE network. The weight value on the edge indicates the duration of the activity,
Based on the fact that there is no conflict between activities,
Discuss at least how long it takes to complete the project, or what activities should be accelerated to shorten the time required to complete the project.

Path Length-sum of the duration of each activity on the path
Key Path-path with maximum path length from source point to sink point
Key activities-activities on the Key Path. Key activities are the key to affecting the entire project. latency affects the entire project.
Earliest occurrence time-set v0 as the starting point. The longest path length from v0 to vi is the earliest occurrence time of event vi, that is, the earliest occurrence time of all activities ending with vi.

Find the key paths and activities in AOE

(1) algorithm ideas
① Use topological sorting to find a topological sequence of the aoe network;
② From the first vertex (source point) of the topological sequence, calculate the earliest occurrence time ve (I) of each event in order of topological order );
Starting from the last vertex (vertex) of the topological sequence, the latest occurrence time of each event is calculated in descending order of the topological order vl (I );
Find the earliest occurrence time e (I) and the latest occurrence time l (e) of each activity, and find the time margin of ai. Those activities with the time margin = 0 are the key activities.

Shortest Path

A weighted directed graph is used to represent a transportation network:
Vertex: City
Edge: traffic connections between cities
Permission: the length of the Line, the time or cost of transportation along the line, etc.
Problem:
Is there a path between two locations?
Which of the following paths is the shortest?
The START vertex of a path is called The Source Vertex, And the last vertex is called the end vertex.

Shortest Path of a single source node
For the given directed graph G = (V, E) and a Single Source Vertex V0, find the shortest path of the other vertices from V0 to G. To address the problem of the shortest path of a single source point, Dijkstra proposes an algorithm that generates the Shortest Path in ascending order of path length, namely, the dijela (Dijkstra) algorithm.
Steps for finding the Shortest Path

In the first run, S = {V0}, T = {other vertices}, and the distance value corresponding to the vertices in T
If V0, Vi, is V0, the weight on the Vi arc exists
If V0 or Vi does not exist
Select a vertex W whose distance value is the smallest from T, and add S
Modify the distance value of the vertex in T: If W is added as the intermediate vertex, the distance from V0 to Vi is shorter than the path without W.
Repeat the preceding steps until S contains all vertices, that is, S = V.

Algorithm Implementation
Graph storage with weighted neighbor Matrix
The array dist [] stores the currently found shortest path length from the source point V0 to each end point (these paths only go through the vertices in the Set S). Its initial state is the direct path weight in the graph.
Array pre [] indicates the shortest path from V0 to each endpoint, and the sequence number of the previous vertex of This vertex. If there is no path from V0 to an endpoint, then 0 is used as the sequence number of the previous vertex.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.