(1) Directed Graph with a directed edge.
The upper and lower triangles in the method of using the adjacent matrix are asymmetrical. Adding an edge requires only one statement,
// Add an edge
Public void addedge (INT start, int end ){
Adjmat [start] [end] = 1;
}
(2) Directed Graph Algorithm-topological sorting
For directed graph applications, certain projects or events must be sorted or occurred in specific order.
For example, if you want to get a degree, you need to take those required courses. Each year's courses are sorted by order. All required courses are completed before you can answer questions and obtain a degree.
Topology Sorting steps:
(1) Find a vertex without any successor (if an edge points from A to B, then B is the successor of ).
(2) Delete the vertex and insert the vertex mark before the list.
(3) Repeat steps 1 and 2 until all vertices are deleted. The vertex order displayed in the list is the result of topological sorting.
Ring: ring graph can not be topological sorting, if there are n vertices of the directed graph has more than the N-1 side, then there must be a ring.
Graph_topo.java
Package COM. mapbar. structure;/***** class graph_topo ** description topological sorting of Directed Graphs ** company mapbar ** author chenll E-mail: chenll@mapbar.com ** version 1.0 ** date 2011-11-17 03:38:27 * // defines the 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; // defines the adjacent matrix private int [] [] Djmat; // maximum number of vertices private int maxsize; // current vertex subscript private int currvertex; private char [] sortedarr; // constructor 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 addver Tex (char label) {varr [currvertex ++] = new vertex (Label);} // Add an 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 + ",") ;}// public void TOPO () {int orig_nvert = currvertex; while (currvertex> 0) {int cuvertex = nosuc (); if (currvertex =-1) {system. out. println ("a loop exists in the figure and Topology Sorting is not allowed"); return;} sortedarr [currvertex -1] = varr [cuvertex]. label; deletevertex (cuvertex);} disallvertex (orig_nvert);} // find a 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 the vertex. The vertex following the vertex moves forward and the row and column are deleted from the matrix, public void deletevertex (INT delv) {If (delv! = CurrVertex-1) {// Delete vertex for (Int J = delv; j <currvertex; j ++) {varr [J] = varr [J + 1];} // Delete the adjacent matrix of the vertex. // move up for (int row = delv; row <currVertex-1; row ++) {for (INT Col = 0; Col <currvertex; col ++) {adjmat [row] [col] = adjmat [row + 1] [col] ;}// shift left for (INT Col = delv; Col <currVertex-1; col ++) {for (int row = 0; row <currvertex; row ++) {adjmat [row] [col] = adjmat [row] [col + 1] ;}}currvertex -- ;}// display the Topology Sorting result public void disallvertex (INT org_length) {for (Int J = 0; j <org_length; j ++) {system. out. print (sortedarr [J] + ",") ;}// main 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,