Skip to content

Commit

Permalink
update 22.3
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuxiuwei committed Nov 3, 2016
1 parent 87f5cce commit e1154cd
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 3 deletions.
37 changes: 36 additions & 1 deletion Chapter22_ElementaryGraphAlgorithms.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,40 @@ Vertex: y, d:3, π:x
See [Code](https://github.com/zhuxiuwei/CLRS/blob/master/src/chap22_ElementaryGraphAlgo/BFS.java#L85). O(V x (V + E))

##22.3 深度优先搜索(DFS)

#####22.3-1 Show how depth-first search works on the graph of Figure 22.6. Assume that the for loop of lines 5-7 of the DFS procedure considers the vertices in alphabetical order, and assume that each adjacency list is ordered alphabetically. Show the discovery and finishing times for each vertex, and show the classification of each edge.
有向图:
![](https://github.com/zhuxiuwei/CLRS/blob/master/Images/22.3-1a.png)
无向图:
![](https://github.com/zhuxiuwei/CLRS/blob/master/Images/22.3-1b.png)
* 有向图,无向图均不可能存在black到white的边,因为一个点必须所有邻居都处理完才能变成black。
* 有向图没有C和F(定理22.10)。
* B的出现,在图形成环的时候才会出现。

#####22.3-1 Show how depth-first search works on the graph of Figure 22.6. Assume that the for loop of lines 5-7 of the DFS procedure considers the vertices in alphabetical order, and assume that each adjacency list is ordered alphabetically. Show the discovery and finishing times for each vertex, and show the classification of each edge.
For d and t,see [code](https://github.com/zhuxiuwei/CLRS/blob/master/src/chap22_ElementaryGraphAlgo/DFS.java#L53):
Vertex: q, π: null, d: 1, t: 16
Vertex: r, π: null, d: 17, t: 20
Vertex: s, π: q, d: 2, t: 7
Vertex: t, π: q, d: 8, t: 15
Vertex: u, π: r, d: 18, t: 19
Vertex: v, π: s, d: 3, t: 6
Vertex: w, π: v, d: 4, t: 5
Vertex: x, π: t, d: 9, t: 12
Vertex: y, π: t, d: 13, t: 14
Vertex: z, π: x, d: 10, t: 11
For edge types, see [code](https://github.com/zhuxiuwei/CLRS/blob/master/src/chap22_ElementaryGraphAlgo/DFS.java#L86):
q -> s, T
q -> t, T
q -> w, F
r -> u, T
r -> y, C
s -> v, T
t -> x, T
t -> y, T
u -> y, C
v -> w, T
w -> s, B
x -> z, T
y -> q, B
z -> x, B

Binary file added Images/22.3-1a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Images/22.3-1b.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions Summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,8 @@ O(V^2)的算法非常简单, 但是**O(V)**算法的思路还是挺有意思

[BFS](https://github.com/zhuxiuwei/CLRS/blob/master/src/chap22_ElementaryGraphAlgo/BFS.java) ★★
需要注意BFS在初始化顶点的时候,顶点的d的值(除了源顶点)应该初始化为无穷大,而不是0.

[DFS](https://github.com/zhuxiuwei/CLRS/blob/master/src/chap22_ElementaryGraphAlgo/DFS.java) ★★
三个注意点.
两个是[关于DFS](https://github.com/zhuxiuwei/CLRS/blob/master/src/chap22_ElementaryGraphAlgo/DFS.java#L39)的。对一个顶点是否调用递归函数,要先判断是否是WHITE(BFS处理一个节点时也要判断); 另一个是timer累加的时机注意一下。
一个是[关于打印节点类型](https://github.com/zhuxiuwei/CLRS/blob/master/src/chap22_ElementaryGraphAlgo/DFS.java#L87)的。
121 changes: 121 additions & 0 deletions src/chap22_ElementaryGraphAlgo/DFS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package chap22_ElementaryGraphAlgo;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* 161103
* @author Zhu Xiuwei
* 三个注意点:
* DFS遍历有2个注意点,一个是关于timer累加的时间。
* 打印边的类型,有一个注意点。
* @param <T>
*/
public class DFS<T> {

private static int time = 0;
/**
* DFS
* @param grahp The graph
*/
public static <E> void dfs(Map<Vertex<E>, List<Vertex<E>>> grahp){
time = 0;
for (Vertex<E> v: grahp.keySet()) {
if(v.color == COLOR.WHITE)
dfs_travel(grahp, v);
}
}
/**
* DFS helper
* @param grahp The graph
* @param u start point
*/
public static <E> void dfs_travel(Map<Vertex<E>, List<Vertex<E>>> graph, Vertex<E> u){
time ++;
u.d = time;
u.color = COLOR.GREY;
for (Vertex<E> neighbor: graph.get(u)) {
if(neighbor.color == COLOR.WHITE){ //!!!!!!!!!Note1 : don't forget to judge this!
//!!!!!!!!!!!! Note2: DO NOT need to add time here. Note only add time add start and end of this recursive-method.
neighbor.π = u;
dfs_travel(graph, neighbor);
}
}
u.color = COLOR.BLACK;
time ++;
u.t = time;
}

/**
* Practice 22.3-2 - DFS on image 22.6
*/
public static void dfs_Image22_6(){
Vertex<Character> q = new Vertex<Character>('q'), r = new Vertex<Character>('r'), s = new Vertex<Character>('s'), t = new Vertex<Character>('t'), u = new Vertex<Character>('u'), v = new Vertex<Character>('v'), w = new Vertex<Character>('w'), x = new Vertex<Character>('x'), y = new Vertex<Character>('y'), z = new Vertex<Character>('z');
List<Vertex<Character>> ql = new LinkedList<Vertex<Character>>(); ql.add(s); ql.add(t); ql.add(w);
List<Vertex<Character>> rl = new LinkedList<Vertex<Character>>(); rl.add(u); rl.add(y);
List<Vertex<Character>> sl = new LinkedList<Vertex<Character>>(); sl.add(v);
List<Vertex<Character>> tl = new LinkedList<Vertex<Character>>(); tl.add(x); tl.add(y);
List<Vertex<Character>> ul = new LinkedList<Vertex<Character>>(); ul.add(y);
List<Vertex<Character>> vl = new LinkedList<Vertex<Character>>(); vl.add(w);
List<Vertex<Character>> wl = new LinkedList<Vertex<Character>>(); wl.add(s);
List<Vertex<Character>> xl = new LinkedList<Vertex<Character>>(); xl.add(z);
List<Vertex<Character>> yl = new LinkedList<Vertex<Character>>(); yl.add(q);
List<Vertex<Character>> zl = new LinkedList<Vertex<Character>>(); zl.add(x);
Map<Vertex<Character>, List<Vertex<Character>>> g = new HashMap<Vertex<Character>, List<Vertex<Character>>>();
g.put(q, ql); g.put(r, rl); g.put(s, sl); g.put(t, tl); g.put(u, ul); g.put(v, vl); g.put(w, wl); g.put(x, xl); g.put(y, yl); g.put(z, zl);
dfs(g);

//print d and t.
for (Vertex<Character> ver: g.keySet()) {
System.out.println(ver);
}

//print edge types
for (Vertex<Character> uu: g.keySet()) {
for (Vertex<Character> vv: g.get(uu)) {
System.out.println(String.format("%s -> %s, %s", uu.value, vv.value, getEdgeType(uu, vv)));
}
}
}

/**
* Given two edges, judge the edge type.
* @return
*/
public static <E> char getEdgeType(Vertex<E> u, Vertex<E> v){
if(v.π == u) //树边 //!!!!!!Note bug: 不能写成if(v.π == u || u.π == v),否则以图22.6为例,z -> x会返回T树边,实际应该返回B后向边。
return 'T';
if(u.t < v.d || v.t < u.d) //横向边
return 'C';
if(u.d < v.d && u.t > v.t) //前向边
return 'F';
if(u.d > v.d && u.t < v.t) //后向边
return 'B';
return ' '; //unreachable
}

//DFS on image 22.4
public static void dfs_Image22_4(){
Vertex<Character> u = new Vertex<Character>('u'), v = new Vertex<Character>('v'), w = new Vertex<Character>('w'), x = new Vertex<Character>('x'), y = new Vertex<Character>('y'), z = new Vertex<Character>('z');
List<Vertex<Character>> ul = new LinkedList<Vertex<Character>>(); ul.add(v); ul.add(x);
List<Vertex<Character>> vl = new LinkedList<Vertex<Character>>(); vl.add(y);
List<Vertex<Character>> wl = new LinkedList<Vertex<Character>>(); wl.add(y); wl.add(z);
List<Vertex<Character>> xl = new LinkedList<Vertex<Character>>(); xl.add(v);
List<Vertex<Character>> yl = new LinkedList<Vertex<Character>>(); yl.add(x);
List<Vertex<Character>> zl = new LinkedList<Vertex<Character>>(); zl.add(z);
Map<Vertex<Character>, List<Vertex<Character>>> g = new HashMap<Vertex<Character>, List<Vertex<Character>>>();
g.put(u, ul); g.put(v, vl); g.put(w, wl); g.put(x, xl); g.put(y, yl); g.put(z, zl);
dfs(g);
for (Vertex<Character> ver: g.keySet()) {
System.out.println(ver);
}
}

//test
public static void main(String[] args) {
//Practice 22.3-2
dfs_Image22_6();
}

}
7 changes: 5 additions & 2 deletions src/chap22_ElementaryGraphAlgo/Vertex.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,24 @@
public class Vertex<T> {
public T value;
public Vertex<T> π;
public int d; //
public int d; //for BFS means depth, for DFS means start time.
public int t; //
public COLOR color;

public Vertex(T value){
this.value = value;
this.π = null;
this.d = Integer.MAX_VALUE;
this.t = Integer.MAX_VALUE;
this.color = COLOR.WHITE;
}

@Override
public String toString(){
String dString = d == Integer.MAX_VALUE ? "∞": d + "";
String tString = t == Integer.MAX_VALUE ? "∞": t + "";
String πString = π == null ? "null": π.value.toString();
return "Vertex: " + value + ", d:" + dString + ", π:" + πString;
return String.format("Vertex: %2s, π: %4s, d: %2s, t: %2s", value, πString, dString, tString);

}

Expand Down

0 comments on commit e1154cd

Please sign in to comment.