Breadth-first search

Posted on 2017-07-07 22:04  Wujunde  阅读(159)  评论(0编辑  收藏  举报

given a graph G  and a distinguished source vertex s, breadth-first
search systematically explores the edges of G to “discover” every vertex that is
reachable from s.

To keep track of progress, breadth-first search colors each vertex white, gray, or
black. All vertices start out white and may later become gray and then black. A
vertex is discovered the first time it is encountered during the search, at which time
it becomes nonwhite. Gray and black vertices, therefore, have been discovered, but
breadth-first search distinguishes between them to ensure that the search proceeds
in a breadth-first manner

  1 package element_graph;
  2 
  3 
  4 import java.util.Iterator;
  5 import java.util.LinkedList;  
  6 import java.util.List;
  7 
  8 
  9 
 10 import edu.princeton.cs.algs4.Queue;
 11   
 12   
 13 public class breadth_first_search {  
 14     private static class vertex{
 15         private LinkedList<vertex> link;
 16         private String name;
 17         private String color;
 18         private vertex p;
 19         private int d;
 20         public vertex(String na,LinkedList<vertex> lin){
 21             name = na;
 22             link = lin;
 23             color = "white";
 24             p = null;
 25             d = 9999;
 26         }
 27     }
 28     public static void BFS(vertex s){
 29         s.color = "grey";
 30         s.d = 0;
 31         Queue<vertex> q = new Queue<vertex>();
 32         q.enqueue(s);
 33         while(!q.isEmpty()){
 34             vertex u = q.dequeue();
 35             for (vertex v : u.link) {  //every adj
 36                 if(v.color == "white"){
 37                     v.color = "gray";
 38                     v.d = v.d + 1;
 39                     v.p = u;
 40                     q.enqueue(v);
 41                 }
 42             }
 43             u.color = "black";
 44             }
 45         
 46     }
 47     public static void printpath(vertex s,vertex v){
 48         if(v == s){
 49             System.out.println(s.name);
 50         }
 51         else if(v.p == null){  //will not get s 
 52             System.out.println("no way");
 53         }
 54         else{
 55             printpath(s,v.p);
 56             System.out.println(v.name);
 57         }
 58     }
 59     
 60     
 61     
 62     
 63      public static void main(String[] args) {
 64             LinkedList<vertex> sl = new LinkedList<vertex>();
 65         
 66             LinkedList<vertex> rl = new LinkedList<vertex>();
 67             
 68             LinkedList<vertex> vl = new LinkedList<vertex>();
 69             
 70             LinkedList<vertex> wl = new LinkedList<vertex>();
 71             
 72             LinkedList<vertex> tl = new LinkedList<vertex>();
 73             
 74             LinkedList<vertex> xl = new LinkedList<vertex>();
 75             
 76             LinkedList<vertex> ul = new LinkedList<vertex>();
 77             
 78             LinkedList<vertex> yl = new LinkedList<vertex>();
 79             
 80             vertex sv = new vertex("s",sl);
 81             vertex rv = new vertex("r",rl);
 82             vertex vv = new vertex("v",vl);
 83             vertex wv = new vertex("w",wl);
 84             vertex tv = new vertex("t",tl);
 85             vertex xv = new vertex("x",xl);
 86             vertex uv = new vertex("u",ul);
 87             vertex yv = new vertex("y",yl);
 88             sl.add(rv);
 89             sl.add(wv);
 90             rl.add(sv);
 91             rl.add(vv);
 92             vl.add(rv);
 93             wl.add(xv);
 94             wl.add(tv);
 95             wl.add(sv);
 96             tl.add(xv);
 97             tl.add(uv);
 98             tl.add(wv);
 99             xl.add(tv);
100             xl.add(uv);
101             xl.add(yv);
102             xl.add(wv);
103             xl.add(tv);
104             xl.add(xv);
105             xl.add(yv);
106             xl.add(uv);
107             xl.add(xv);
108              BFS(sv);
109              printpath(sv,tv);
110             
111         }
112 }
113