Code:
Import Java. util. arraylist; import Java. util. collections; import Java. util. hashset; import Java. util. linkedhashmap; import Java. util. list; import Java. util. map; import Java. util. set; // vertex class vertex {string name; // name Boolean visited; // whether public vertex (string name) {This. name = Name; this. visited = false ;}/// graph class public class graph {// graph private vertex [] arr; // vertex array private int [] [] matrix; // adjacent matrix // create Figure data private set <string> vertexset; // Private map containing a set of non-repeated vertices <string, string []> connmap; // contains the connected hash table // Add the connection relationship between the vertex public void addconn (string from, string [] toarr) {If (vertexset = NULL) {vertexset = new hashset <string> ();} vertexset. add (from); For (string to: toarr) {vertexset. add (to) ;}if (connmap = NULL) {connmap = new linkedhashmap <string, string []> () ;} connmap. put (from, toarr);} // create a graph (I .e., the vertex array and the adjacent matrix) P Ublic void rebuildgraph () {// initialize the vertex array list <string> ls = new arraylist <string> (); LS. addall (vertexset); collections. sort (LS); int size = ls. size (); arr = new vertex [size]; for (INT I = 0; I <ls. size (); I ++) {arr [I] = new vertex (LS. get (I) ;}// initialize the adjacent matrix = new int [size] [size]; for (string key: connmap. keyset () {string [] values = connmap. get (key); For (string value: values) {int x = findvertexindex (key); int y = findve Rtexindex (value); If (X! =-1 & Y! =-1) {matrix [x] [Y] = 1; matrix [y] [x] = 1 ;}}}} // find the vertex subscript private int findvertexindex (string name) {for (INT I = 0; I <arr. length; I ++) {If (name. equals (ARR [I]. name) {return I ;}} return-1;} public void displaymatix () {int n = arr. length; system. out. print (""); For (INT I = 0; I <n; I ++) {system. out. print (ARR [I]. name + ",");} system. out. println (); system. out. print ("-"); For (INT I = 0; I <n; I ++) {system. out. print ("--");} system. out. println (); For (INT I = 0; I <n; I ++) {system. out. print (ARR [I]. name + ":"); For (Int J = 0; j <n; j ++) {system. out. print (Matrix [I] [J] + ",");} system. out. println () ;}/// obtain the path Public String getpath (string from, string to) between two points {// initialize int fromindex = findvertexindex (from ); if (fromindex =-1) {return "vertex not found:" + from;} int toindex = findvertexindex (to); If (toindex =-1) {return "vertex not found:" + to;} // stack used to remember the path <integer> stack = new stack <integer> (integer. class, arr. length); // start searching for arr [fromindex]. visited = true; stack. push (fromindex); While (stack. isempty () = false) {Int J = getconnvertex (stack. peek (); If (j =-1) {stack. pop ();} else {arr [J]. visited = true; stack. push (j); If (ARR [J]. name. equals (to) {// find stringbuilder sb = new stringbuilder (); While (stack. isempty () = false) {int Index = stack. pop (); sb. insert (0, arr [Index]. name + "->");} return sb. substring (0, sb. length ()-2) ;}}} return "not from" + from + "to" + ;} // obtain the private int getconnvertex (int I) {int n = arr. length; For (Int J = 0; j <n; j ++) {If (Matrix [I] [J] = 1 & arr [J]. visited = false) {return J;} return-1;} public static void main (string [] ARGs) {graph G = New Graph (); G. addconn ("A", new string [] {"B", "D"}); G. addconn ("B", new string [] {"A", "C"}); G. addconn ("C", new string [] {"B", "D", "E"}); G. addconn ("D", new string [] {"A", "C"}); G. addconn ("e", new string [] {"C"}); G. addconn ("F", new string [] {"E", "G"}); G. addconn ("g", new string [] {"F"}); G. addconn ("H", new string [] {"I", "J"}); G. addconn ("I", new string [] {"H", "J"}); G. addconn ("J", new string [] {"H", "I"}); G. rebuildgraph (); G. displaymatix (); string Path = G. getpath ("A", "G"); system. out. println (PATH); G. rebuildgraph (); Path = G. getpath ("A", "H"); system. out. println (PATH); G. rebuildgraph (); Path = G. getpath ("J", "H"); system. out. println (PATH); G. rebuildgraph (); Path = G. getpath ("F", "B"); system. out. println (PATH );}}
Output:
A, B, C, D, E, F, G, H, I, J, ------------------- A:, B, 0, 0, 0, 0, 0, C:, 0, 0, D:, 0, 0, 0, 0, E:, 0, 0, 0, F: 0, 0, 0, 1, 0, 0, 0, G: 0, 0, 0, 0, 0, 0, 0, H: 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, I: 0, 0, 0, 0, 0, 0, 0, 1, 0, J: 0, 0, 0, 0, 0, 1, 0, a-> B-> C-> E-> F-> G cannot be from A to HJ-> Hf-> E-> C-> B
Vertex: