Typical Graph Algorithm Java code practices: BFS, DFS, and several Shortest Path Algorithms

Source: Internet
Author: User



public class City {String name; int id;static int idCounter = 0;public City(String name) {this.name=name;id = idCounter++;}}



import java.util.ArrayList;public class Graph {public static void main(String[] args){// TODO Auto-generated method stubMap M = new Map(12);City a = new City("a");City b = new City("b");City c = new City("c");City d = new City("d");City e = new City("e");City f = new City("f");City g = new City("g");City h = new City("h");City i = new City("i");City j = new City("j");City k = new City("k");City l = new City("l");M.createEdge(a,b,3);M.createEdge(a,c,5);M.createEdge(a,d,4);M.createEdge(b,f,6);M.createEdge(c,d,2);M.createEdge(c,g,4);M.createEdge(d,e,1);M.createEdge(d,h,5);M.createEdge(e,f,2);M.createEdge(e,i,4);M.createEdge(f,j,5);M.createEdge(g,h,3);M.createEdge(g,k,6);M.createEdge(h,i,6);M.createEdge(h,k,7);M.createEdge(i,j,3);M.createEdge(i,l,5);M.createEdge(j,l,9);M.createEdge(k,l,8);System.out.println("the graph is:\n");System.out.println(M);System.out.println();System.out.println("findPathByDFS:a to k");M.findPathByDFS(a,k);System.out.println();System.out.println("findPathByBFS:a to k");M.findPathByBFS(a,k);System.out.println();System.out.println("bellmanFord from a:");M.bellmanFord(a);System.out.println();System.out.println("dijkstra from a:");M.dijkstra(a);System.out.println();System.out.println("bellmanFord,print example from a:");M.floydWarshall();M.printFloydWarshallForOneCity(a);}}



Import java. util. arrayList; import java. util. using list; import java. util. queue; public class Map {double [] [] A; public Map (int n) {A = new double [n] [n]; for (int I = 0; I <. length; I ++) {for (int j = 0; j <. length; j ++) {if (I = j) A [I] [j] = 0; else A [I] [j] =-1 ;}} ArrayList
 
  
Cities = new ArrayList
  
   
(); Private double [] D; private void relax (int u, int v) {if (D [v]> D [u] + A [v] [u]) D [v] = D [u] + A [v] [u];} private double [] [] DD = null; public void floydWarshall () {DD = new double [. length] [. length]; int I, j, k; for (I = 0; I <. length; I ++) {for (j = 0; j <. length; j ++) {if (A [I] [j]> 0) DD [I] [j] = A [I] [j]; else if (I = j) DD [I] [j] = 0; else DD [I] [j] = 99999999 ;}} for (k = 0; k <. length; k ++) for (I = 0; I <. length; I ++) for (j = 0; j <. length; j ++) {if (DD [I] [j]> DD [I] [k] + DD [k] [j]) {DD [I] [j] = DD [I] [k] + DD [k] [j] ;}} public void printFloydWarshallForOneCity (City city City) {System. out. println ("floydWarshall:"); if (DD = null) {floydWarshall () ;}for (int I = 0; i0) {relax (u, i) ;}}} public void bellmanFord (City city) {bellmanFord (City. id); System. out. println ("bellmanFord:"); for (int I = 0; i0) {relax (j, k) ;}}} Queue
   
    
BfsQueue = new queue list
    
     
(); Boolean [] bfsFlag; int bsfPre []; public void findPathByBFS (City src, City dst) {System. out. printf ("bfs find path between '% s' and' % s '! \ N ", src. name, dst. name); findPathByBFS (src. id, dst. id); printBFS (dst. id);} public void findPathByBFS (int srcId, int dstId) {bsfPre = new int [. length]; bfsQueue. clear (); bfsFlag = new boolean [. length]; for (int I = 0; i0) // connect the two {if (index = dstId) // find the target {bfsFlag [index] = true; bsfPre [index] = current; return; // direct return} if (bfsFlag [index] = false) // if you have not accessed {bfsFlag [index] = true; bsfPre [index] = current; bfsQueue. offer ( Index) ;}}}} private void printBFS (int dstId) {int index = dstId; do {System. out. printf ("<-% s", cities. get (index ). name); index = bsfPre [index];} while (index! =-1); System. out. println ();} ArrayList
     
      
DfsPath = new ArrayList
      
        (); Boolean [] dfsFlag; private void printDFS () {for (Integer node: dfsPath) {System. out. printf ("-> % s", cities. get (node ). name);} System. out. println ();} public void findPathByDFS (City src, City dst) {System. out. printf ("dfs find path between '% s' and' % s '! \ N ", src. name, dst. name); findPathByDFS (src. id, dst. id);} public void findPathByDFS (int srcId, int dstId) {dfsPath. clear (); dfsFlag = new boolean [. length]; for (int I = 0; i0) // connect the two {if (index = dstId) // find the target {dfsFlag [index] = true; dfsPath. add (index); return;} if (dfsFlag [index] = false) // if the node has not accessed {dfsFlag [index] = true; dfsPath. add (index); dfs (index, dstId); if (dfsFlag [dstId] = false) // The dfsPath is not found for the target. remove (index); el Se return ;}}} public void createEdge (City a, City B, double w) {A [. id] [B. id] = w; A [B. id] [. id] = w; // added by me! Cities. add (. id, a); cities. add (B. id, B);} public String toString () {String r = "I am a map of" +. length + "cities. "; r + =" My connections are: \ n "; for (int I = 0; I
       


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.