Point
Class Name: vertex//property://Method: Class Vertex{public char label;//point name, such as Apublic boolean wasvisited;public Vertex (char Lab) {// Constructor label = lab;wasvisited = false;}}
Create an entitlement graph, add new vertices, add edges, display vertices, return an unreachable vertex adjacent to V, deep search for the unauthorized graph, breadth search, minimum spanning tree based on depth search, delete vertex, topological ordering of the graph
Class Name: graph//property://Method: Class graph{private final int max_verts = 20;private Vertex vertexlist[];//vertex list array private int adjmat[ ][];//adjacency Matrix private int nverts;//current vertex private char sortedarray[];p ublic Graph () {//constructor vertexlist = new Vertex[max_verts] ; Adjmat = new int[max_verts][max_verts];nverts = 0;for (int j=0;j<max_verts;j++) {for (int k=0;k<max_verts;k++) Adjmat[j][k] = 0;} Sortedarray = new Char[max_verts];} public void Addvertex (char Lab) {//Add new vertices, lab for incoming vertices, and modify nvertsvertexlist[nverts++] = new Vertex (Lab);} public void Addedge (int start,int end) {//Add edge, here is the no-map adjmat[start][end] = 1;//adjmat[end][start] = 1;} public void Displayvertex (int v) {//display vertex System.out.print (Vertexlist[v].label);} public int Getadjunvisitedvertex (int v) {//Returns an unreachable vertex adjacent to V for (int j=0;j<nverts;j++) if (adjmat[v][j] = = 1 && Vertexlist[j].wasvisited = = False) {return J;} return-1;//if not, return -1}public void Dfs () {//Deep search stack<integer> thestack = new stack<integer> (); vertexlist[0 ].wasvisited = True;displayvertex (0);Stack.push (0);//Put the root into the stack while (!thestack.empty ()) {int v = Getadjunvisitedvertex (Thestack.peek ());//Get a non-accessible element that is adjacent to the top of the stack if (v = =-1)//If there is no element adjacent to the top of the Stack, it pops up the top Thestack.pop (); else{//If there is this element, the output of this element, marked as accessed, is incorporated into the stack vertexlist[v].wasvisited = true; Displayvertex (v); Thestack.push (v);}} for (int j=0;j<nverts;j++)//all set to not accessed vertexlist[j].wasvisited = false;} public void BFs () {//breadth search queue<integer> thequeue = new linkedlist<integer> (); vertexlist[0].wasvisited = True;displayvertex (0); Thequeue.offer (0);//Put the root into the queue int v2;while (!thequeue.isempty ()) {int v1 = Thequeue.remove ();// V1 records the elements of Layer 1th and then records the 1th element of the 2nd layer ... while ((V2=getadjunvisitedvertex (v1))! = 1) {//output all and 1th layer adjacency elements, output and 2nd layer 1th elements adjacency elements ... vertexlist[v2].wasvisited = True;displayvertex (v2); Thequeue.offer (v2);}} for (int j=0;j<nverts;j++)//all set to not accessed vertexlist[j].wasvisited = false;} public void MST () {//minimum spanning tree based on deep search stack<integer> thestack = new stack<integer> (); vertexlist[0].wasvisited = True;thestack.push (0);//Put the root into the stack while (!thestack.empty ()) {int Currentvertex = theStack.peek ();//record stack top element, when there is an adjacency element, the output int v = Getadjunvisitedvertex (Thestack.peek ());//Get a non-accessible element that is adjacent to the top element of the stack if (v = =-1)/ /If there is no element adjacent to the top of the stack, the top Thestack.pop () is ejected, and if this element is else{//, the output of this element, marked as accessed, is incorporated into the stack vertexlist[v].wasvisited = true; Thestack.push (v);d Isplayvertex (Currentvertex);d Isplayvertex (v); System.out.println ();}} for (int j=0;j<nverts;j++)//all set to not accessed vertexlist[j].wasvisited = false;} public int nosuccessors () {//Use adjacency matrix to find no successor vertices, there are successive vertices to return the number of rows, no return -1boolean isedge;for (int row=0;row<nverts;row++) {// Starting from line 1th Isedge = false;for (int col=0;col<nverts;col++) {//If a row is a column of 1, returns the number of rows in this row if (Adjmat[row][col] > 0) {Isedge = True;break;}} if (!isedge) return row;} return-1;} public void moverowup (int row,int length) {for (int col=0;col<length;col++) Adjmat[row][col] = Adjmat[row+1][col];} public void movecolleft (int col,int length) {for (int row=0;row<length;row++) Adjmat[row][col] = adjmat[row][col+1];} public void Deletevertex (int delvert) {if (Delvert! = nVerts-1) {for (int j=delvert;j<nverts-1;j++)// Remove the vertex from the array vertexlist[j] = vertexlist[j+1];for (int row=delvert;row<nverts-1;row++)//In the adjacency matrix, remove all rows from the deleted line Moverowup (row,nverts); for ( int col=delvert;col<nverts-1;col++)//In the adjacency matrix, remove all columns under this column to the left Movecolleft (col,nverts-1);} nverts--;} public void Topo () {//topological sort must be performed in a non-circular, orig_nverts = nverts;//record with the number of vertices while (Nverts > 0) {int Currentvertex = Nosuccessors (); if (Currentvertex = =-1) {System.out.println ("error: the graph contains rings!) "); return;} SORTEDARRAY[NVERTS-1] = Vertexlist[currentvertex].label;deletevertex (Currentvertex);} System.out.println ("Topological sort result:"); for (int j=0;j<orig_nverts;j++) System.out.println (Sortedarray[j]);}}
The connectivity of the graph, the Warshall algorithm
Main function
public class Graph_demo {public static void main (string[] args) {//TODO auto-generated method stub graph Thegraph = new graph (); thegraph.ad Dvertex (' A ');//array element 0thegraph.addvertex (' B ');//array element 1thegraph.addvertex (' C ');//array element 2thegraph.addvertex (' D ');// Array element 3thegraph.addvertex (' E ');//array element 4//thegraph.addedge (0, 1);//ab//thegraph.addedge (1, 2);//bc//thegraph.addedge (0, 3);//ad//thegraph.addedge (3, 4);//de//system.out.println ("Order of DFS Access:");//thegraph.dfs ();//system.out.println ( );////system.out.println ("BFS Access Order:");//thegraph.bfs ();//thegraph.addedge (0, 1);//ab//thegraph.addedge (0, 2);// Ac//thegraph.addedge (0, 3);//ad//thegraph.addedge (0, 4);//ae//thegraph.addedge (1, 2);//bc//thegraph.addedge (1, 3) ;//bd//thegraph.addedge (1, 4);//be////thegraph.addedge (2, 3);//cd////thegraph.addedge (2, 4);//ce// Thegraph.addedge (3, 4);//de//system.out.println ("minimum spanning Tree:");//thegraph.mst (); Thegraph.addvertex (' F ');// Array element 5thegraph.addvertex (' G ');//array element 6thegraph.addvertex (' H ');//array element 6thegraph.addedge (0, 3);//adthegraph.addedge ( 0, 4);//aethEgraph.addedge (1, 4);//bethegraph.addedge (2, 5);//cfthegraph.addedge (3, 6);//dgthegraph.addedge (4, 6);// Egthegraph.addedge (5, 7);//fhthegraph.addedge (6, 7);//ghthegraph.topo ();}}
Java data structure--diagram