Topology Sorting --- (implementation of the graph receiving table)

Source: Internet
Author: User

# Include <iostream> using namespace std; # define MAX_NODE 30 # define MAX_INFO 10 bool isOutput [MAX_NODE]; // record whether the node outputs struct ListNode {ListNode () {next = NULL;} int position; ListNode * next;}; struct VertexNode {VertexNode () {head = NULL; inDegree = 0;} int currentPosition; // int inDegree of the current node's array in the graph storage structure; // The node's inbound char info [MAX_INFO]; // The node's information ListNode * head; // The first node of the adjacent node}; struct Graphic {int vertex, edge; VertexNode vers [M AX_NODE] ;}; void createGraphic (Graphic & graphic) {cout <"Enter the number of nodes and edges:"; cin> graphic. vertex> graphic. edge; cout <"Enter the node information:" <endl; int I; for (I = 0; I <graphic. vertex; I ++) {cout <"Enter the" <I <"Node information:"; cin> graphic. vers [I]. info;} cout <"Enter edge information:" <endl; int first = 0; int second = 0; for (I = 0; I <graphic. edge; I ++) {cout <"Enter the" <I <"edge information:"; cin> first> second; listNode * temp = new ListNode; temp-> position = second; temp-> next = graphi C. vers [first]. head; graphic. vers [first]. head = temp; ++ graphic. vers [second]. inDegree;} for (I = 0; I <graphic. vertex; I ++) {isOutput [I] = false;} for (I = 0; I <graphic. vertex; I ++) {graphic. vers [I]. currentPosition = I ;}} void printGraphic (Graphic graphic) {int I; ListNode * temp = NULL; for (I = 0; I <graphic. vertex; I ++) {cout <graphic. vers [I]. info <"--->"; temp = graphic. vers [I]. head; if (! Temp) {cout <"NULL";} while (temp) {cout <temp-> position <""; temp = temp-> next ;} cout <endl ;}for (I = 0; I <graphic. vertex; I ++) {cout <"Node" <I <"inbound:"; cout <graphic. vers [I]. inDegree <endl ;}cout <endl ;}vertexnode * findZeroInDegreeNode (Graphic & graphic) {int I; for (I = 0; I <graphic. vertex; I ++) {if (graphic. vers [I]. inDegree = 0) {graphic. vers [I]. inDegree = 1; return & graphic. vers [I] ;}} return NULL;} void TopologicalSortForVert Ex (Graphic & graphic, int position) {ListNode * head = graphic. vers [position]. head; while (head) {if (! IsOutput [head-> position]) {cout <graphic. vers [head-> position]. info <"; isOutput [head-> position] = true;} TopologicalSortForVertex (graphic, head-> position); head = head-> next ;}} void TopologicalSort (Graphic & graphic) {VertexNode * zeroNode = findZeroInDegreeNode (graphic); if (! ZeroNode) return; if (! IsOutput [zeroNode-> currentPosition]) {cout <zeroNode-> info <""; isOutput [zeroNode-> currentPosition] = true;} TopologicalSortForVertex (graphic, zeroNode-> currentPosition); TopologicalSort (graphic);} void main () {Graphic myGraphic; createGraphic (myGraphic); printGraphic (myGraphic); TopologicalSort (myGraphic );}

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.