Data Structure: Graph -- topological sorting

Source: Internet
Author: User

Data Structure: Graph -- topological sorting

Topological sorting

Topological sorting

In practical application, the edges of a directed graph can be seen as the constraints between vertices. When we regard a vertex as a task It indicates that task Vj must be completed after task Vi is completed, that is, task Vi is completed before task Vj. For a directed graph, find a vertex sequence and the sequence is satisfied: If the vertex Vi and Vj have an edge In this sequence, vertex Vi must be before vertex Vj. Such a sequence is called the topological sequence of a directed graph (topological order ).

Procedure

 

Select a vertex output without a forward (with an inbound degree of 0) from the directed graph. Delete all the arcs starting with it in the graph. Repeat the preceding two steps. There are two situations: 1. All vertices have been output, and the output sequence is the topological sequence. 2. There are no vertices without a precursor, but any vertex has no output, which indicates that the directed graph has a ring. It can be seen that topological sorting can detect whether a directed graph has loops. It must be noted that, even if the storage structure is determined, the topological sequence is not necessarily unique. The topological sorting sequence is related not only to the storage structure but also to the specific algorithm used.
Code Class Definition
# Include
 
  
# Include
  
   
# Include
   
    
Using namespace std;/* Graph Topology Sorting using the adjacent matrix must be directed Graph */class Graph {private: // Number of vertices int numV; // Number of edges int numE; // vertex input int * indegree; // The adjacent matrix int ** matrix; public:/* constructor numV indicates the number of vertices, and whether isWeighted has a weight, whether isDirected has a direction */Graph (int numV); // create a Graph void createGraph (int numE); // The Destructor ~ Graph (); // get the number of vertices int getVerNums () {return numV;} // get the number of edges int getEdgeNums () {return numE ;} // Topology Sorting void topologicalSort (); // print the adjacent matrix void printAdjacentMatrix (); // check the input bool check (int, int );};
   
  
 
Class implementation
// Constructor, specify the number of vertices Graph: Graph (int numV) {// checks the number of input vertices while (numV <= 0) {cout <incorrect number of vertices! Re-input; cin> numV;} this-> numV = numV; // construct the adjacent matrix and initialize matrix = new int * [numV]; int I, j; for (I = 0; I <numV; I ++) matrix [I] = new int [numV]; // because the weights have no effect on topological sorting, therefore, the unauthorized graph is used for (I = 0; I <numV; I ++) for (j = 0; j <numV; j ++) matrix [I] [j] = 0; // construct the vertex's inbound array and initialize indegree = new int [numV]; for (I = 0; I <numV; I ++) indegree [I] = 0;} void Graph: createGraph (int numE) {/* checks the number of input edges for a directed Graph of numV vertices, up to numV * (numV-1) edges */while (NumE <0 | numE> numV * (numV-1) {cout <The number of sides is incorrect! Re-input; cin> numE;} this-> numE = numE; int tail, head, I; I = 0; cout <enter the start point of each edge (arc tail), endpoint (ARC header) <endl; while (I <numE) {cin> tail> head; while (! Check (tail, head) {cout <the input edge is incorrect! Enter <endl; cin> tail> head;} matrix [tail] [head] = 1; indegree [head] ++; I ++;} again ;}} graph ::~ Graph () {int I; for (I = 0; I <numV; I ++) delete [] matrix [I]; delete [] matrix; delete [] indegree ;} // Topology Sorting void Graph: topologicalSort () {int I, vertex; queue
 
  
Q; // for (I = 0; I <numV; I ++) if (indegree [I] = 0) q. push (I); bool * visited = new bool [numV]; for (I = 0; I <numV; I ++) visited [I] = false; while (! Q. empty () {vertex = q. front (); q. pop (); cout <setw (4) <vertex; visited [vertex] = true; for (I = 0; I <numV; I ++) if (matrix [vertex] [I] = 1) {// adjust the inbound level. if the inbound level is 0, you need to join if (! (-- Indegree [I]) q. push (I) ;}} cout <endl; for (I = 0; I <numV; I ++) if (! Visited [I]) cout <This directed graph has a ring !; Cout <endl; delete [] visited;} // print the adjacent matrix void Graph: printAdjacentMatrix () {int I, j; cout. setf (ios: left); cout <setw (4) <; for (I = 0; I <numV; I ++) cout <setw (4) <I; cout <endl; for (I = 0; I <numV; I ++) {cout <setw (4) <I; for (j = 0; j <numV; j ++) cout <setw (4) <matrix [I] [j]; cout <endl ;}} bool Graph: check (int tail, int head) {if (tail <0 | tail> = numV | head <0 | head> = numV) return false; return true ;}
 
Main Function
Int main () {cout <******* topological sorting ***** by David ***** <endl; int numV, numE; cout <graph creation... <endl; cout <number of input vertices; cin> numV; Graph graph (numV); cout <number of input edges; cin> numE; graph. createGraph (numE); cout <print the adjacent matrix... <endl; graph. printAdjacentMatrix (); cout <topological sorting... <
 
  
Download the complete code: If Topology Sorting is helpful, click it first! Column Directory: Data Structure and algorithm directory
  

 

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.