AOV network and Topology Sorting

Source: Internet
Author: User
/* AOV network and Topology Sorting 1. In a directed acyclic graph, vertices are used to represent activity, and directed edges <u, v> are used to represent activity u first and activity v, this directed graph is called the AOV network. 2. If <u, v>, U is the direct precursor of V, and V is the direct successor of U. If <u, u1, U2, · UN, v> U is the precursor of V, and V is the successor of U. 3. There are transmission and anti-self-defense relationships between the frontend and successor. It can be inferred that the AOV network must be a directed acyclic graph. 4. Implementation of topological sorting: 1) Select a vertex with an inbound degree of 0 from the AOV network and output it; 2) delete the vertex from the AOV network and all edges sent from the vertex; 3) Repeat 1) and 2) until the vertex with an inbound degree of 0 is not found; 4) if all vertices are output, the topology is sorted. Otherwise, the graph has a ring chart. Input Description: The number of vertices N and number of edges m, and the number of m rows is the starting point U and V of each directed edge. The vertex sequence number starts from 1, the last line of the input file is 0 0, indicating the end. Input example: sample output: 6 8 5 1 4 3 2 61 2 network has a cycle! 1 42 63 23 65 15 25 66 81 31 22 53 44 24 65 45 60 0 */# include <stdio. h> # include <string. h> # define maxn 10 struct arcnode {int to; struct arcnode * Next;}; arcnode * list [maxn]; int n, m, Count [maxn]; char output [100]; void topsort () {int I, Top =-1; arcnode * temp; bool bcycle = false; int Pos = 0; for (I = 0; I <n; I ++) if (count [I] = 0) {count [I] = top; Top = I ;}for (I = 0; I <n; I ++) {If (Top =-1) {bcycle = true; break;} el Se {Int J = top; Top = count [Top]; POS + = sprintf (output + POs, "% d", J + 1); temp = list [J]; while (temp! = NULL) {int K = temp-> to; If (-- count [k] = 0) {count [k] = top; Top = K ;} temp = temp-> next ;}}if (bcycle) printf ("network has a cycle! \ N "); else {int Len = strlen (output); Output [len-1] = 0; printf (" % s \ n ", output) ;}} int main () {int I, V, U; // freopen ("input.txt", "r", stdin); While (scanf ("% d", & N, & M )! = EOF) {If (n = 0 & M = 0) break; memset (list, 0, sizeof (list); memset (count, 0, sizeof (count); memset (output, 0, sizeof (output); arcnode * temp; for (I = 0; I <m; I ++) {scanf ("% d", & U, & V); U --; V --; count [v] ++; temp = new arcnode; temp-> to = V; temp-> next = NULL; If (list [u] = NULL) list [u] = temp; else {temp-> next = list [u]; list [u] = temp ;}} topsort (); for (I = 0; I <n; I ++) {temp = list [I]; while (temp! = NULL) {list [I] = temp-> next; Delete temp; temp = list [I] ;}} return 0 ;}

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.