Topological sorting and key paths are based on Directed Graphs without loops.
It is mainly used to indicate the relationship between events in the project progress.
Topology Sorting and key paths use an adjacent table to store data, and the Minimum Spanning Tree and shortest path use an adjacent matrix to store data.
1. topological sorting
AOV Network: In a directed graph that represents a project, activities are represented by fixed points, and priority relationships between activities are represented by arcs. Such Directed Graphs represent active networks for vertices, we call it AOV Network Activity On Vertex Network)
Topological sorting: sorts the vertices of an AOV network in a certain order. If <Vi, Vj> exists, the vertices Vi in the sorting must be before the vertices Vj. For a single graph, multiple topologies can be sorted.
Examples of Topology Sorting used in display life:
In order to continue to take professional courses after completing basic courses, take the computer software major as an example, before the BASIC program design and discrete mathematics courses are completed, you cannot begin to study data structures. These prerequisites define the priority between courses.
Algorithm:
1) Select a vertex output with NO precursor inbound degree of 0 from the directed graph.
2) Delete the vertex and delete the arc with the vertex ending.
3) Repeat steps 1), 2) know that all output vertices or that the AOV network does not have a vertex with an inbound degree of 0.
Specific implementation code:
Stack is used to store vertices whose input degree is 0 and vertices whose input degree is 0 after update.
# Include <stdio. h> # define MAXVEX 100 typedef struct EdgeNode/* edge table node */{int adjvex;/* the adjacent vertex field stores the table */int weight corresponding to the vertex; /* used to store weights. For non-network graphs, you do not need */struct EdgeNode * next;/* link domain, pointing to the next adjacent node */} EdgeNode; typedef struct VertexNode/* vertex table node */{int in;/* vertex inbound */int data;/* vertex domain, store vertex confidence */EdgeNode * firstedge; /* edge header pointer */} VertexNode, Adjlist [MAXVEX]; typedef struct {Adjlist adjList; int numVertexes; int numEdges;/* Number of current vertices and edges in the graph */} graphAdjL Ist, * GraphAdjList;/* topological sorting. If GL does not have a loop, the topological sorting sequence is output and OK is returned. If there is a loop, ERROR */int TopoLogicalSort (GraphAdjList GL) is returned) {EdgeNode * e; int I, k, gettop; int top = 0;/* Used for the following table of stack pointers */int count = 0; /* used to count the number of output vertices */int * stack;/* used to store vertices with an inbound degree of 0 */stack = (int *) malloc (GL-> numVertexes * sizeof (int); // dynamically allocates memory. The size is n vertices (up to n vertices are 0, because the graph has n vertices in total) for (I = 0; I <GL-> numVertexes; I ++) if (GL-> adjList [I]. in = 0)/* Add a vertex with an inbound degree of 0 to the stack */stack [++ top] = I; while (P! = 0) {gettop = stack [top --];/* output stack */printf ("% d->", GL-> adjList [gettop]. data);/* print this vertex */count ++;/* count the number of output vertices */for (e = GL-> adjList [gettop]. firstedge; e !; E = e-> next) {/* traverse this vertex arc table */k = e-> adjvex; if (-- GL-> adjList [k]. in = 0) // subtract 1 from the inbound degree of the adjacent vertex of vertex k and 1 from the vertex, the vertex with an input of 0 must be stored in the stack [++ top] = k ;}} if (count <GL-> numVertexes)/* if count is smaller than the number of vertices, it indicates that the ring */return-1; elsereturn 0;} exists ;}
The entire algorithm is analyzed. For an AOV network with n vertices e-arc, the time complexity of putting a vertex with an inbound degree of 0 into the stack is O (n ), in the subsequent while LOOP, each vertex goes into the stack once, And the stack goes out once, And the inbound operation minus 1 executes e times, therefore, the time complexity of the entire algorithm is O (n + e ).
2. Key Path
Topological sorting solves the problem of sequential engineering execution. However, it sometimes takes the shortest time to complete the project.
AOE Network: In a directed graph with engineering weights, vertex is used to represent events, directed edge is used to represent activities, and edge weight is used to represent activity duration, this Network uses the Edge of the directed graph to represent the Activity. We call it AOE Network Activity On Edge Network)
650) this. width = 650; "title =" QQ20130910122320.jpg "src =" http://www.bkjia.com/uploads/allimg/131228/20111253b-0.jpg "/>
650) this. width = 650; "title =" QQ20130910122417.jpg "src =" http://www.bkjia.com/uploads/allimg/131228/20111244C-1.jpg "/>
650) this. width = 650; "title =" QQ20130910122507.jpg "src =" http://www.bkjia.com/uploads/allimg/131228/2011123X6-2.jpg "/>
Use ve (I) to indicate the earliest start time of event (vertex) I, and use vl (I) to indicate the start time of event I. If activity k is represented by an arc <m, n>, use dut (<m, n>) to indicate the activity duration:
E (k) = ve (m)
L (k) = vl (n)-dut (<m, n>)
The specific algorithm for solving the key path is assumed that there are n vertices in the graph)
(1) starting from the start vertex V0, assuming ve (0) = 0, and then finding the earliest start time of other vertex I according to the topological order ve (I ), if the number of vertices in the topological sequence is smaller than the number of vertices in the graph, the graph has a loop and the algorithm ends. Otherwise, the algorithm continues to run.
2) starting from the end vertex Vn, assume vl (n-1) = ve (n-1); then find the latest occurrence time of each vertex I.
3) Based on the earliest occurrence time of the vertex and the latest occurrence time, obtain the earliest start time and the latest start time of each arc in sequence. If the two are equal, is a key activity. The path composed of key activities is the Key Path.
Specific implementation code:
# Include <stdio. h> # define MAXVEX 100/* first declare several global variables */int * etv, * ltv;/* the earliest occurrence time and the latest occurrence time */int * stack2; /* stack used to store the topological sequence */int top2;/* pointer used for stack2 */typedef struct EdgeNode/* edge table node */{int adjvex;/* adjacent point domain, store the following table */int weight corresponding to the vertex;/* used to store the weight value. For non-net graphs, you do not need */struct EdgeNode * next;/* Chain Domain, point to the next adjacent node */} EdgeNode; typedef struct VertexNode/* vertex table node */{int in;/* vertex inbound */int data;/* vertex field, store vertex confidence */EdgeNode * firstedge;/* edge header pointer */} VertexNode, Adjlist [MAXVEX]; typedef struct {Adjlist adjList; int numVertexes; int numEdges;/* Number of vertices and edges in the graph */} graphAdjList, * GraphAdjList;/* topological sorting, if GL has no loop, the topological sorting sequence is output and OK is returned. If there is a loop, ERROR */int TopoLogicalSort (GraphAdjList GL) {EdgeNode * e; int I, k, gettop; int top = 0;/* Used for the following table of the stack pointer */int count = 0;/* used to count the number of output vertices */int * stack; /* stack creation is used to store vertices with 0 inbound */stack = (int *) malloc (GL-> numVertexes * sizeof (int); // dynamically allocates memory, the size is n vertices (a maximum of n vertices are 0, because the graph has n vertices in total.) For (I = 0; I <GL-> numVertexes; I ++) if (GL-> adjList [I]. in = 0)/* Add a vertex with an inbound degree of 0 to the stack */stack [++ top] = I; top2 = 0; etv = (int *) malloc (GL-> numVertexes * sizeof (int);/* earliest event occurrence time */for (I = 0; I <GL-> numVertexes; I ++) etv [I] = 0; stack2 = (int *) malloc (GL-> numVertexes * sizeof (int);/* initialize */while (top! = 0) {gettop = stack [top --];/* output stack */printf ("% d->", GL-> adjList [gettop]. data);/* print this vertex */stack2 [++ top2] = gettop;/* press the popped-up vertex serial number into the stack of the topological sequence */count ++; /* count the number of output vertices */for (e = GL-> adjList [gettop]. firstedge; e = e-> next) {/* traverse this vertex arc table */k = e-> adjvex; if (-- GL-> adjList [k]. in = 0) // subtract 1 from the inbound degree of the adjacent vertex of vertex k and 1 from the vertex, the vertex with an input of 0 needs to be stored in the stack [++ top] = k; if (etv [gettop] + e-> weight)> etv [k]) /* obtain the earliest occurrence time of each vertex time */etv [k] = etv [gettop] + e-> weight;/* a certain top The earliest occurrence time of a vertex = all related activities must be completed */} if (count <GL-> numVertexes)/* if count is smaller than the number of vertices, it indicates that the ring */return-1; elsereturn 0;}/* is used to find the key path. GL is directed to the network and the key activities of GL are output */void CriticalPath (GraphAdjList GL) {EdgeNode * e; int I, gettop, k, j; int ete, lte;/* declare the earliest occurrence time and latest occurrence time of the activity */TopoLogicalSort (GL ); /* calculate the topological sequence and calculate the values of the etv and stack2 arrays */ltv = (int *) malloc (GL-> numVertexes * sizeof (int )); /* the latest occurrence time of time */for (I = 0; I <GL-> numVertexes; I ++) ltv [I] = etv [GL-> numVertexes-1]; /* Initialize ltv [I] as the earliest time for project completion, and etv [I] as 0 */while (top2! = 0)/* calculate ltv */{gettop = stack2 [top2 --]; for (e = GL-> adjList [gettop]. firstedge; e! = NUll; e = e-> next) {/* calculate the ltv value of the latest occurrence time of each fixed point event */k = e-> adjvex; if (ltv [k]-e-> weight <ltv [gettop]) ltv [gettop] = ltv [k]-e-> weight;/* Find the latest occurrence time, is derived from the last vertex of the topological sequence */} for (j = 0; j <GL-> numVertexes; j ++) /* search for key activities */{for (e = GL-> adjList [j]. firstedge; e! = NULL; e = e-> next) {k = e-> adjvex; ete = etv [j]; /* earliest start time of the activity */lte = ltv [k]-e-> weight;/* latest event time */if (ete = lte) printf ("<v % d, v % d> length: % d,", GL-> adjList [j]. data, GL-> adjList [k]. data, e-> weight );}}}
This article is from "Li Haichuan" blog, please be sure to keep this source http://lihaichuan.blog.51cto.com/498079/1293633