Topological ordering of a direction graph

Source: Internet
Author: User
Tags win32

topological ordering of a direction graph

This article is from "Data Structure and algorithm" (C language Edition) (third edition), the publishing house is Tsinghua University Press. This blog post is organized as a learning material. The source code is VC + + 6.0 executable program, I moved to the VS2010 in the implementation.

New C + + WIN32 console Application project in VS2010 to create a screenshot of the results:

The basic idea of topological ordering of a direction graph is to select a vertex without a precursor in the direction graph first, to remove the vertex from the direction graph, and to delete the edges of all the forward graphs with the end of the vertex. Repeat the above steps until all vertices in the diagram are output or the vertices in the graph are not predecessors. For the latter, it is shown that there are loops in the direction graph and cannot be sorted by topology.

The following is an example of a AOV (activity on Vertex Network) network that demonstrates the steps to solve a topology sort:

C + + WIN32 Console application, code is as follows: TopSort.cpp

  #include "stdafx.h" #include <stdio.h> #include <string.h> #define MAXNODE typedef struct {C
  Har Vertex;

  }vernode;
  typedef struct {int adj;

  }arc;
    typedef struct {Vernode Vexs[maxnode];
    ARC Arcs[maxnode][maxnode];
  int M_numofvexs;

  }graph;
    int Mfind (char aim, vernode* Arry) {int pos=-1;
    int i=0;
        For (i=0 i<maxnode; i++) {if (Aim==arry[i].vertex) {pos=i;
      return POS;
  } return POS;
    } void Showgraph (Graph agraph, int num) {int i=0;
    int j=0;
    printf ("\ nodes:\n");
    For (i=0 i<num; i++) {printf ("%c", Agraph.vexs[i].vertex);
    printf ("\ arcs:\n");
      For (i=0 i<num; i++) {for (j=0; j<num; J + +) {printf ("%d", Agraph.arcs[i][j].adj);
    printf ("\ n");
    }//Add vertex void Addvexs (graph* agraph, char vex) {agraph->m_numofvexs++; Agraph->vexs[agraph->m_nUmofvexs].vertex=vex;
    }//Add edge void Addarcs (graph* agraph, Char Astart, char aend) {int p_x=mfind (ASTART,AGRAPH-&GT;VEXS);
    int P_y=mfind (AEND,AGRAPH-&GT;VEXS);
  agraph->arcs[p_x][p_y].adj=1;
    }//Diagram initialization void Initgraph (graph* agraph) {int i=0;
    int j=0;
    agraph->m_numofvexs=-1;
    For (i=0 i<maxnode; i++) {for (j=0; j<maxnode; j + +) agraph->arcs[i][j].adj=0;
    an int getindegree (int i, graph* agraph) {int indegree=0;
    int j=0;
    For (j=0 j<agraph->m_numofvexs; j + +) indegree+=agraph->arcs[j][i].adj;
  return indegree;
    int Topsort (graph* agraph) {int i;
    int isok=1;
    int Vexsisout[maxnode];
    for (i=0; i<agraph->m_numofvexs; i++) {vexsisout[i]=0;
      while (isok==1) {isok=0; 
          For (i=0 i<agraph->m_numofvexs; i++) {if (Vexsisout[i]==0&&getindegree (i,agraph) ==0) {
          Int J; Printf ("%c", Agraph->vexs[i].vertex);
          Vexsisout[i]=1;
          For (j=0 j<agraph->m_numofvexs; j + +) {agraph->arcs[i][j].adj=0;
        } isok=1;
    (i=0 i<agraph->m_numofvexs; i++) {if (vexsisout[i]==0) return 0;
  return 1;
    int main (void) {char node= ' a ';
    Char input1= ' a ';
    Char input2= ' a ';
    Initializes graph G_graph;

    Initgraph (&g_graph);
    Adds vertex printf based on user input ("Please input the Vexs (end with #): \ n");
      while (1) {if (node== ' @ ') break;
        if (scanf ("%c,", &node) ==1) {if (node== ' \ n ') continue;
      Addvexs (&g_graph,node);
    }///Add edge printf According to user input ("Please enter arcs, just like this \" startnod,endnode\ "\ n");
      while (1) {if (input1== ' @ ') break; if (scanf ("%c,%c", &AMP;INPUT1, &input2) ==2) {if (input1== ' \ n ' | | |
input2== ' \ n ')          Continue
      Addarcs (&g_graph, INPUT1, Input2);

    }///Output graph Showgraph (g_graph, G_graph.m_numofvexs);
    printf ("The topsort is: \ n"); if (Topsort (&g_graph) ==0) printf ("There is a circle in the graph!!
    \ n ");
  return 0;
 }

CTRL+F5 executes the TopSort.cpp code, the result is as follows:


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.