Topology Sorting C ++ Implementation of Directed Acyclic Graphs

Source: Internet
Author: User

 

// Directed acyclic graph Topology Sorting. cpp: Defines the entry point for the console application.

//

# Include "stdafx. h"

# Include <iostream>

# Deprecision MAX 100

Using namespace std;

Enum Color {white, gray, black };

Struct edgeNode

{

Int no; // The number of the end of the edge

Char info; // edge name

Struct edgeNode * next; // next

};

Struct vexNode

{

Char info; // node name

Struct edgeNode * link; // endpoint connected to it

};

// Store node Information

VexNode adjlist [MAX];

// Access level

// If white is not accessed yet, gray is not searched for all its descendants.

// Complete the search for all its descendants as black

Color color [MAX];

// Access start time

Int d [MAX];

// Access completion time

Int f [MAX];

// Precursor Node

Int parent [MAX];

// The array after Topology Sorting, sorted by the size of f [], that is, the node that completes the search is ranked first

Int topu [MAX];

// Create an adjacent table Storage

// Adjlist indicates the node set, n indicates the number of nodes, and e indicates the number of edges.

Void createGraph (vexNode * adjlist, int n, int e)

{

Int I;

For (I = 1; I <= n; I ++)

{

Cout <"Enter the node" <I <"name :";

Cin> adjlist [I]. info;

Adjlist [I]. link = NULL;

}

EdgeNode * p1;

Int v1, v2;

For (I = 1; I <= e; I ++)

{

Cout <"Enter the Start Node Number of Edge" <I <:";

Cin> v1;

Cout <"Enter the end node number of the edge" <I <:";

Cin> v2;

P1 = (edgeNode *) malloc (sizeof (edgeNode ));

P1-> no = v2;

P1-> info = adjlist [v2]. info;

P1-> next = adjlist [v1]. link;

Adjlist [v1]. link = p1;

}

}

// Depth-first search for directed unauthorized Graphs

// Parent [I] is the forward node of node I. time is a global timestamp, v is the first node, and index is the global offset of the topu array.

Void DFS (vexNode * adjlist, int * parent, int & time, int v, int & index)

{

Color [v] = gray;

Time + = 1;

D [v] = time;

Int I;

EdgeNode * p;

P = adjlist [v]. link;

While (p! = NULL)

{

If (color [p-> no] = white)

{

Parent [p-> no] = v;

DFS (adjlist, parent, time, p-> no, index );

}

P = p-> next;

}

Color [v] = black;

Time + = 1;

F [v] = time;

Topu [index ++] = v;

}

Int _ tmain (int argc, _ TCHAR * argv [])

{

Int cases;

Cout <"Enter the number of cases :";

Cin> cases;

While (cases --)

{

Int n, e;

Cout <"Enter the number of nodes :";

Cin> n;

Cout <"Enter the number of sides :";

Cin> e;

// The timestamp of the Access Node

Int time = 0;

// Index is the global offset of the topu array.

Int index = 1;

// The access flag is cleared and the frontend node is initialized to 0.

Int I;

For (I = 1; I <= n; I ++)

{

Color [I] = white;

Parent [I] = 0;

}

// Create an adjacent table

CreateGraph (adjlist, n, e );

For (I = 1; I <= n; I ++)

{

If (color [I] = white)

DFS (adjlist, parent, time, I, index );

}

Cout <"output Topology Sorting Result:" <endl;

For (I = 1; I <= n; I ++)

Cout <adjlist [topu [I]. info <"";

Cout <endl;

}

System ("pause ");

Return 0;

}

---------------------------------------------------- Program test ---------------------------------------------------

 

Enter the number of cases: 1

Enter the number of nodes: 9

Enter the number of edges: 9

Enter the name of Node 1:

Enter Node 2 Name: B

Enter Node 3 name: c

Enter node 4 name: d

Enter node 5 Name: e

Enter the name of node 6: f

Enter the name of node 7: g

Enter node 8 name: h

Enter node 9 name: I

Enter the Start Node Number of edge 1: 1

Enter the end node number of edge 1: 2

Enter the Start Node Number of edge 2: 1

Enter the end node number of edge 2: 4

Enter the Start Node Number of edge 3: 2

Enter the end node number of edge 3: 3

Enter the Start Node Number of edge 4: 4

Enter the end node number of edge 4: 3

Enter the Start Node Number of edge 5: 5

Enter the end node number of edge 5: 6

Enter the Start Node Number of edge 6: 5

Enter the end node number of edge 6: 8

Enter the Start Node Number of edge 7: 6

Enter the end node number of edge 7: 4

Enter the Start Node Number of edge 8: 6

Enter the end node number of edge 8: 8

Enter the Start Node Number of edge 9: 7

Enter the end node number of edge 9: 8

Output Topology Sorting result:

C d B a h f e g I

Press any key to continue...

 

Author heyongluoyao8

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.