Besides creating a depth-first forest, depth-first search also timestamps each vertex.
Each vertex has two timestamps: the first timestamp :d records when
is first discovered (and grayed), and the second timestamp :f records when the
search finishes examining ’s adjacency list (and blackens ).
1 package element_graph; 2 3 import java.util.LinkedList; 4 5 6 7 8 9 10 11 public class deapth_first_search { 12 public static int time = 0; 13 private static class vertex{ 14 private LinkedList<vertex> link; 15 private String name; 16 private String color; 17 private vertex p; 18 private int d,f; 19 public vertex(String na,LinkedList<vertex> lin){ 20 name = na; 21 link = lin; 22 color = "white"; 23 p = null; 24 d = 0; //discover time 25 f = 0; 26 } 27 } 28 public static void DFS(vertex v){ 29 if(v.color == "white"){ 30 time = time + 1; 31 v.d = time; //discover time 32 v.color = "gray"; 33 for (vertex u : v.link) { 34 if(u.color == "white"){ 35 u.p = v; 36 DFS(u); 37 } 38 } 39 v.color = "black"; 40 time = time +1; 41 v.f = time; 42 } 43 } 44 public static void printpath(vertex s,vertex v){ 45 if(v == s){ 46 System.out.println(s.name); 47 } 48 else if(v.p == null){ //will not get s 49 System.out.println("no way"); 50 } 51 else{ 52 printpath(s,v.p); 53 System.out.println(v.name+v.f); 54 } 55 } 56 public static void main(String[] args) { 57 LinkedList<vertex> sl = new LinkedList<vertex>(); 58 59 LinkedList<vertex> rl = new LinkedList<vertex>(); 60 61 LinkedList<vertex> vl = new LinkedList<vertex>(); 62 63 LinkedList<vertex> wl = new LinkedList<vertex>(); 64 65 LinkedList<vertex> tl = new LinkedList<vertex>(); 66 67 LinkedList<vertex> xl = new LinkedList<vertex>(); 68 69 LinkedList<vertex> ul = new LinkedList<vertex>(); 70 71 LinkedList<vertex> yl = new LinkedList<vertex>(); 72 73 vertex sv = new vertex("s",sl); 74 vertex rv = new vertex("r",rl); 75 vertex vv = new vertex("v",vl); 76 vertex wv = new vertex("w",wl); 77 vertex tv = new vertex("t",tl); 78 vertex xv = new vertex("x",xl); 79 vertex uv = new vertex("u",ul); 80 vertex yv = new vertex("y",yl); 81 sl.add(rv); 82 sl.add(wv); 83 rl.add(sv); 84 rl.add(vv); 85 vl.add(rv); 86 wl.add(xv); 87 wl.add(tv); 88 wl.add(sv); 89 tl.add(xv); 90 tl.add(uv); 91 tl.add(wv); 92 xl.add(tv); 93 xl.add(uv); 94 xl.add(yv); 95 xl.add(wv); 96 xl.add(tv); 97 xl.add(xv); 98 xl.add(yv); 99 xl.add(uv); 100 xl.add(xv); 101 DFS(sv); 102 printpath(sv,tv); 103 104 } 105 }
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
两种策略的比较:
(一)复杂度
深度优先搜索:
时间复杂度O(bm): 如果 m比d大很多则比较严重
空间复杂度O(bm), 线性空间
宽度优先搜索:
时间复杂度1+b+b2+b3+… +bd + b(bd-1) = O(bd+1)
空间复杂度O(bd+1)
空间是大问题(和时间相比)
也就是说,宽度优先搜索的优势在于当问题有解时,一定能找到解,当问题为单位耗散值,且当问题有解时,一定能找到最优解。在进行宽度优先搜索时需要创建一个队列以保存所有的待扩展节点,而不是像深度优先搜索那样只需要保存当前状态即可,这样就使BFS比DFS需要占用更多的空间,常常是指数级增长的,但由于BFS是按层次遍历的,一般来说能比DFS更快地找到解,因为它找到的第一个可行解一般来说都是最优解,而无需像DFS那样为了找到最优解而进行回溯,尽管如此,宽度优先还是很容易扩展那些没有用的节点,因此很容易造成状态的指数增长,甚至“组合爆炸”。
(二)优缺点
深度优先搜索:
缺点:
如果目标节点不在搜索所进入的分支上,而该分支又是一个无穷分支,则就得不到解.因此该算法是不完备的。
优点:
如果目标节点不在搜索所进入的分支上,则可以较快地得到解。
宽度优先搜索:
缺点:
当目标节点距离初始节点较远时会产生许多无用的节点,搜索效率低。
优点:
只要问题有解,则总可以得到解,而且是最短路径的解。
from
http://zhiluoxuepiao.blog.163.com/blog/static/171866065201162010423600/