Java implementation of topological ordering of AOV graphs

Source: Internet
Author: User

Topological ordering as an application of diagrams, understanding the topological ordering must first understand the AoV diagram.

The AoV network represents a vertex in a forward graph, with arcs representing the precedence relationship between vertices. As shown in the AOV network, if there is a path from the vertex VI to the vertex VJ, the vertex VI is the precursor of the vertex VJ, and the vertex VJ is the successor of the Vertex VI. Note that the AoV diagram cannot have loops, otherwise the sequence will be trapped in a dead loop called a deadlock.

To sort the topology, the general steps are as follows:

<1> Select a vertex with a degree of 0 in the AOV network

<2> Delete this vertex and its connected forward edges in the AOV network

<3> Repeat steps <1> and <2> until there are no vertices in the AOV net with a degree of 0

Because the topological ordering is uncertain, the ordered sequence should be different from one person to another, but the logical relationship of most sequences can go through.


The procedure is as follows:

package  topology sort;/* *  The steps to find a topological sort sequence in the AOV network are as follows: * <1> Select a vertex in the AOV network with an entry level of 0  * <2 > Remove this vertex from AoV and the forward edge  * <3> Repeat steps <1> and <2> associated with it. Up to 0 vertices in the AoV net  */public class TopoSort { /**  *  @author   Liu Yanbing   *  @date  2015-02-14 20:22  */  /*  * max: The maximum capacity for defining vertices is 100  * ver: Use the inner Vertexs class to create an array of ver, storing the vertex's keyword   * map:aov adjacency matrix table    * n: Number of vertices   * toposort: storing the resulting sequence   */ static int Max=100;  static Vertexs ver[]; static int map[][]; static int n;  Static char toposort[];  public static void main (String[] args)  {   // TODO Auto-generated method stub  ver=new Vertexs[Max];   map=new int[max][max];  n=0;  toposort=new char[max];  //Initialize adjacency matrix   for (int i=0;i<max;i++) {   for (int  j=0;j<max;j++) {    map[i][j]=0;   }  }   Toposort ts=new toposort ();   //read into AoV mesh vertex keyword   ts.addvertex (' A ');   Ts.addvertex (' B ');   ts.addvertex (' C ');   ts.addvertex (' D ');   ts.addvertex (' E ');   ts.addvertex (' F ');   ts.addvertex (' G ');   ts.addvertex (' H ');   Ts.addvertex (' I ');   ts.addvertex (' J ');   ts.addvertex (' K ');   ts.addvertex (' L ');     //read into the AOV network edge of the connection information, because it is a graph, simply read into the   ts.addedge (0, 1);   ts.addedge (0 ,  3);   ts.addedge (0, 10);   ts.addedge (1, 10);   ts.addedge (2, &NBSP;3);   ts.addedge (2, 4);   ts.addedge (2, 5);   ts.addedge (3, 7) ;   ts.addedge (5, 7);  &nBsp;ts.addedge (5, 6);   ts.addedge (5, 8);   ts.addedge (6, 8);   Ts.addedge (6, 9);   ts.addedge (10, 11);   ts.addedge (11, 9);   Ts.addedge (11, 6);     ts.toposort ()  }  //construct vertex key word group  public  Void addvertex (char v) {  ver[n++]=new vertexs (v); adjacency matrix of  }  //tectonic edges   Public void addedge (int start,int end) {  map[start][end]=1; }   Public void toposort () {  //num: The number of vertices n is decremented, so the start is stored in num, as the output sequence is traversed using   //isedge: Determine if the edge is connected   int num=n;  boolean isedge;    while (n>0) {    //Select a vertex with an entry level of 0    int currver=0;   //traverse adjacency Matrix     for (int row=0;row<n;row++) {    isedge=false;    for (int  col=0;col<n;col++) {     if (map[row][col]>0)       isEdge=true;     }    //extracts vertices that do not have a degree in the adjacent edge table (assigns the number of contiguous table rows to the current vertex)     if (!isedge)       currver=row;   }   //the final position of the sequence result with no out-of-degree vertices     toposort[n-1]=ver[currver].value;     /*   *   Delete vertex operations     *     */   if (currver!=n-1) when the current vertex is not the last vertex {     //vertex key word group move backward one     for (int i=currver;i<n;i++)       ver[i]=ver[i+1];        /*     *  Moving adjacency table rows and columns , overwrite the original value      */    //adjacency table row number move backward one (move right)     for ( int row=currver;row<n-1;row++)      for (int col=0;col<n;col++)        map[row][col]=map[row+1][col];    //Number of contiguous table columns move backward one (move left)     for (int  col=currver;col<n-1;col++)      for (int row=0;row<n;row++)        map[row][col]=map[row][col+1];   }   n--;    //the next vertex   }    //output   system.out.println ("The topology sort sequence for this graph is:");   for (int i=0;i<num;i++) {  } }}class vertexs{ public char value ;  public vertexs (char v) {  value=v; }}

The test results are as follows:

Java implementation of topological ordering of AOV graphs

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.