Graph theory-topological ordering of a direction graph

Source: Internet
Author: User
(1), directed to the graph, the side is a direction.
Adjacency matrix representation method Upper and lower triangles are asymmetrical. To add a side is to just one statement,
Add an Edge
public void Addedge (int start,int end) {
Adjmat[start][end] = 1;
}


(2), the algorithm of the direction graph-topology sorting
In the application of a direction graph, some items or events must be sorted or occurred in a particular order.
College courses, for example, require the required courses to get a degree. Each year, the curriculum is sorted according to the order, all compulsory, finally can reply, get a degree.
Steps for topology sorting:
(1), find a no successor vertex (if there is a side from a to point B, then B is a successor).
(2), remove this vertex from the diagram, and insert the vertex mark before the list.
(3), repeat steps 1 and 2. Until all vertices are removed from the diagram. The order of vertices displayed in the list is the result of the topology ordering.

Ring: Loop diagram is not a topological order, if there are n vertices of the graph has more than N-1 edge, then there must be a ring.

Graph_topo.java

Package com.mapbar.structure; /** * * Class graph_topo * * Description topological ordering of the graph * * Company Mapbar * * Author CHENLL
	
	OM * * Version 1.0 * * Date 2011-11-17 03:38:27//Define node Class vertex{public char label;
	
	public Boolean isvisited;
		Public Vertex (char label) {this.label = label;
	this.isvisited = false;
	
	} public class Graph_topo {//vertex array private vertex[] Varr;
	
	 Define adjacency matrix private int[][] Adjmat; 
    
     Maximum number of vertices private int maxSize;
    
    The current vertex subscript private int currvertex;
    
    Private char[] Sortedarr;
    	Construction method Public Graph_topo (int maxSize) {Sortedarr = new char[maxsize];
    	This.maxsize = maxSize;
    	Varr = new Vertex[maxsize];
    	Adjmat = new Int[maxsize][maxsize];
    		for (int i = 0; i<adjmat.length; i++) {for (int j = 0; j<adjmat.length; j + +) {Adjmat[i][j] = 0;
    } Currvertex = 0;
    //Add a vertex public void Addvertex (char label) {	varr[currvertex++] = new Vertex (label);
    //Add one edge public void Addedge (int start,int end) {Adjmat[start][end] = 1;
    //Display a vertex public void Disvertex (int v) {System.out.print (varr[v].label+ ",");
	   }//topology sort public void topo () {int orig_nvert = Currvertex;
		   while (currvertex>0) {int cuvertex = NOSUC ();
			   if (currvertex==-1) {System.out.println ("There are loops in the diagram, cannot be sorted by topology");
		   Return
		   } sortedarr[currvertex-1] = Varr[cuvertex].label;
	   Deletevertex (Cuvertex);
   } Disallvertex (Orig_nvert);
	   ///Find a no successor node public int nosuc () {Boolean isedge;
		   for (int i = 0; i<currvertex; i++) {Isedge = false;
				   for (int j = 0; j<currvertex; j + +) {if (adjmat[i][j]>0) {Isedge = true;
			   Break
		   } if (!isedge) {return i;
   }} return-1; ///delete vertices, the following vertices move forward while rows and columns are removed from the matrix, and the rows below and to the right column public void Deletevertex (int delv) {if Delv!=CURRVERTEX-1) {///delete vertex for (int j = Delv; j<currvertex; j +) {Varr[j] = varr[j+1]; //delete Vertex's adjacency matrix//Move up for (int row = Delv; row<currvertex-1; row++) {for (int col = 0; col<c
			   urrvertex;col++) {Adjmat[row][col] = Adjmat[row+1][col]; }//left for (int col = Delv; col<currvertex-1; col++) {for (int row = 0; Row<currvertex;ro
			   w++) {Adjmat[row][col] = adjmat[row][col+1];
   }} currvertex--; }//Display topology sort results public void Disallvertex (int org_length) {for (int j = 0; j<org_length; j + +) {System.out.print (
		Sortedarr[j]+ ",");
    	}//Keynote function public static void main (string[] args) {Graph_topo g = new Graph_topo (10);
    	G.addvertex (' a ');
    	G.addvertex (' B ');
    	G.addvertex (' C ');
    	G.addvertex (' d ');
    	G.addvertex (' e ');   	
    	G.addvertex (' f ');
    	G.addedge (0,1);
    	G.addedge (0,2);
    	G.addedge (1,3);
    	G.addedge (4,5); G.Addedge (5,1);
    G.topo ();
 }
}

Output

E,f,a,b,d,c,

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.