AoV Network and Kahn algorithm topology sequencing

Source: Internet
Author: User

1.AOV and Dag

The activity network can be used to describe the arrangement of the sub-projects in the production planning, construction process, production process, process flow and other projects.

 generally a project can be divided into several sub-projects, which are called activities (activity). With these activities completed, the whole project was completed. For example, the representative of the computer Professional course, learning is a project, each course of learning is an activity throughout the project.  we can use a graph to indicate the first relationship between courses. In this graph, the vertex represents the course learning activity, and there is an edge to indicate the first relationship between the courses. For example, vertex C1 to C8 has a forward edge, which means that the course C1 must be completed before the course C8.  In fact, in this graph, the activity is represented by a vertex, and the active U must precede the active V with a forward edge. This graph is called the Active network (activity on Vertices), which is recorded as AoV network.   precursor and successor: in a AOV network, if there is a forward edge, then the active u must be performed before the activity V, and the U is the direct precursor of V, and V is the direct successor of U. If there is a forward path, it is called U is the precursor of V, V is the successor of U.   There are Shang and direction-free graphs: from the precursor and the subsequent transitivity and anti-reflexivity, it can be seen that the AOV network cannot appear in the direction of the loop (or called a Shang). Directed graphs that do not contain directed loops are called directed acyclic graphs (DAG, Directed acyclic graph).  If there is a forward loop in the AoV network, it is wrong to imply that an activity is a prerequisite for itself. 2. Topology sequencing The method of judging the direction-free graph is to construct its topological ordered sequence on the AOV network. Each vertex is arranged into a linear sequential sequence, so that all the precursors and successors in the AOV network can be satisfied.   This construction AoV the topological ordered sequence of all vertices of the network is called topological ordering (topological sorting).  if all the vertices of the AOV network can be sorted into a topological ordered sequence through topological sorting, there must be no Shang in the AOV network, but if the topological ordered sequence of all the vertices is not available, then there is a forward ring in the AOV network, the project represented by this AOV network is not feasible.  For example, for the students to choose the class of engineering drawings for topological sequencing, the resulting topological sequence of the order is: C1,c2,c3,c4,c5,c6,c8,c9,c7orC1,c8,c9,c2,c5,c3,c4,c7,c6 Thus, the topological ordered sequence of the AOV network may not be unique. 3.Kahn algorithm topology sorting algorithm:  (1) Select a vertex with no precursor (i.e., 0) from the graph and output it; (2) by deleting the vertex from the net and deleting all the forward edges emitted from the vertex; (3) Repeat these two steps until there are no more vertices in the remaining nets that do not have a previous trend. Input:6 8
0 S
1 4
2 6
3 2
3 6
5 1
5 2
5 66 81 31 22 53 44 24 65 45 6Output:great! There is not cycle.
5 1 4 3 2 6Network has a cycle!
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <cmath>6#include <queue>7#include <stack>8#include <map>9#include <Set>Ten#include <sstream> One#include <functional> A using namespacestd; -typedefLong Longll; - Const intMAXN = ++Ten; the Const intINF = 1e9 +7; - intT, N, m, cases; -vector<int>MAP[MAXN]; - intCOUNT[MAXN]; + voidtopo () - { +stack<int>s;//store in a vertex with a degree of 0 Avector<int>v;//answer to the sort of access topology at      for(inti =1; I <= N; i++)//Subscript starting from 1 -         if(Count[i] = =0) S.push (i); -      while(!s.empty ()) -     { -         intnow =s.top (); - V.push_back (now); in S.pop (); -          for(intj =0; J < Map[now].size (); J + +) to         { +             if((--count[map[now][j]) = =0) -             { the S.push (Map[now][j]); *             } $         }Panax Notoginseng     } -     if(V.size ()! = N) cout<<"Network has a cycle!"<<Endl; the     Else +     { Acout<<"great! There is not cycle."<<Endl; the          for(inti =0; I < v.size (); i++) cout<<v[i]<<" "; +cout<<Endl; -     } $ } $ intMain () - { -      while(Cin >> N >>m) the     { -         if(!n &&!m) Break;Wuyi         intu, v; the          for(inti =0; I <= N; i++) map[i].clear (); -memset (Count,0,sizeof(Count)); Wu          for(inti =0; I < m; i++) -         { AboutCIN >> U >>v; $ Map[u].push_back (v); -count[v]++;//Deposit Degree -         } - topo (); A     } +     return 0; the}

Time complexity: Because each point into the stack out of the stack once, each edge scanned once, the time complexity of O (M + N)

AoV Network and Kahn algorithm topology sequencing

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.