Skip to content

Commit

Permalink
update 22.2
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuxiuwei committed Oct 29, 2016
1 parent 6f03c55 commit 62a802f
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 1 deletion.
43 changes: 42 additions & 1 deletion Chapter22_ElementaryGraphAlgorithms.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,45 @@ in-degree:O(V+E)
2. 缺点是HashTable更占空间?以及需要需要解决散列冲突问题?
3. 用搜索树替代,缺点是搜索时间效率降低。

##22.1 广度优先搜索
##22.1 广度优先搜索(BFS)
#####22.2-1 Show the d and π values that result from running breadth-first search on the directed graph of Figure 22.2(a), using vertex 3 as the source.
Vertex: 1, π: null, d:0
Vertex: 2, π:4, d:3
Vertex: 3, π: null, d:0
Vertex: 4, π:5, d:2
Vertex: 5, π:3, d:1
Vertex: 6, π:3, d:1
[代码](https://github.com/zhuxiuwei/CLRS/blob/master/src/chap22_ElementaryGraphAlgo/BFS.java#L19)

#####22.2-2 Show the d and π values that result from running breadth-first search on the undirected graph of Figure 22.3, using vertex u as the source.
Vertex: r, d:1, π:s
Vertex: s, d:0, π: null
Vertex: t, d:2, π:w
Vertex: u, d:3, π:t
Vertex: v, d:2, π:r
Vertex: w, d:1, π:s
Vertex: x, d:2, π:w
Vertex: y, d:3, π:x
[代码](https://github.com/zhuxiuwei/CLRS/blob/master/src/chap22_ElementaryGraphAlgo/BFS.java#L39)

#####22.2-4 What is the running time of BFS if its input graph is represented by an adjacency matrix and the algorithm is modified to handle this form of input?
从邻接表的O(V+E)变成O(v + v^2),因为扫描所有邻居的时间从O(E)变成O(V^2)。

#####22.2-5 Argue that in a breadth-first search, the value d[u] assigned to a vertex u is independent of the order in which the vertices in each adjacency list are given. Using Figure 22.3 as an example, show that the breadth-first tree computed by BFS can depend on the ordering within adjacency lists.
[代码](https://github.com/zhuxiuwei/CLRS/blob/master/src/chap22_ElementaryGraphAlgo/BFS.java#L61)
The order in adjacency list is different from [practice 22.2-2](https://github.com/zhuxiuwei/CLRS/blob/master/src/chap22_ElementaryGraphAlgo/BFS.java#L39), but the d in output are the same. π can be changed.
Output:
Vertex: r, d:1, π:s
Vertex: s, d:0, π: null
Vertex: t, d:2, π:w
Vertex: u, d:3, π:x <- π here is different from 22.2-2 output, but d not remains the same.
Vertex: v, d:2, π:r
Vertex: w, d:1, π:s
Vertex: x, d:2, π:w
Vertex: y, d:3, π:x






110 changes: 110 additions & 0 deletions src/chap22_ElementaryGraphAlgo/BFS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package chap22_ElementaryGraphAlgo;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;


/**
* BFS。adjacent list.
* 161029
*/
public class BFS {

/**
* practice 22.2-1
*/
public static void prac_22_2_1(){
//Graph of image 22-2(a).
Vertex<Integer> v1 = new Vertex<Integer>(1), v2 = new Vertex<Integer>(2), v3 = new Vertex<Integer>(3), v4 = new Vertex<Integer>(4), v5 = new Vertex<Integer>(5), v6 = new Vertex<Integer>(6);
List<Vertex<Integer>> l1 = new LinkedList<Vertex<Integer>>(); l1.add(v2); l1.add(v4);
List<Vertex<Integer>> l2 = new LinkedList<Vertex<Integer>>(); l2.add(v5);
List<Vertex<Integer>> l3 = new LinkedList<Vertex<Integer>>(); l3.add(v6); l3.add(v5);
List<Vertex<Integer>> l4 = new LinkedList<Vertex<Integer>>(); l4.add(v2);
List<Vertex<Integer>> l5 = new LinkedList<Vertex<Integer>>(); l5.add(v4);
List<Vertex<Integer>> l6 = new LinkedList<Vertex<Integer>>(); l6.add(v6);
Map<Vertex<Integer>, List<Vertex<Integer>>> g = new HashMap<Vertex<Integer>, List<Vertex<Integer>>>();
g.put(v1, l1); g.put(v2, l2); g.put(v3, l3); g.put(v4, l4); g.put(v5, l5); g.put(v6, l6);
System.out.println("=========== prac_22_2_1: After BFS: ==============");
bfs(g, v3);
for(Vertex<Integer> key: g.keySet())
System.out.println(key);
}

/**
* practice 22.2-2
*/
public static void prac_22_2_2(){
//Graph of image 22-3(a).
Vertex<Character> 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');
List<Vertex<Character>> rl = new LinkedList<Vertex<Character>>(); rl.add(s); rl.add(v);
List<Vertex<Character>> sl = new LinkedList<Vertex<Character>>(); sl.add(r); sl.add(w);
List<Vertex<Character>> tl = new LinkedList<Vertex<Character>>(); tl.add(u); tl.add(w); tl.add(x);
List<Vertex<Character>> ul = new LinkedList<Vertex<Character>>(); ul.add(t); ul.add(x); ul.add(y);
List<Vertex<Character>> vl = new LinkedList<Vertex<Character>>(); vl.add(r);
List<Vertex<Character>> wl = new LinkedList<Vertex<Character>>(); wl.add(s); wl.add(t); wl.add(x);
List<Vertex<Character>> xl = new LinkedList<Vertex<Character>>(); xl.add(t); xl.add(u); xl.add(y);
List<Vertex<Character>> yl = new LinkedList<Vertex<Character>>(); yl.add(u); yl.add(x);
Map<Vertex<Character>, List<Vertex<Character>>> g = new HashMap<Vertex<Character>, List<Vertex<Character>>>();
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);
System.out.println("=========== prac_22_2_2: After BFS: ==============");
bfs(g, s);
for(Vertex<Character> key: g.keySet())
System.out.println(key);
}

/**
* practice 22.2-5
*/
public static void prac_22_2_5(){
//Graph of image 22-3(a).
Vertex<Character> 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');
List<Vertex<Character>> rl = new LinkedList<Vertex<Character>>(); rl.add(v); rl.add(s);
List<Vertex<Character>> sl = new LinkedList<Vertex<Character>>(); sl.add(w); sl.add(r);
List<Vertex<Character>> tl = new LinkedList<Vertex<Character>>(); tl.add(x); tl.add(w); tl.add(u);
List<Vertex<Character>> ul = new LinkedList<Vertex<Character>>(); ul.add(y); ul.add(x); ul.add(t);
List<Vertex<Character>> vl = new LinkedList<Vertex<Character>>(); vl.add(r);
List<Vertex<Character>> wl = new LinkedList<Vertex<Character>>(); wl.add(x); wl.add(t); wl.add(s);
List<Vertex<Character>> xl = new LinkedList<Vertex<Character>>(); xl.add(y); xl.add(u); xl.add(t);
List<Vertex<Character>> yl = new LinkedList<Vertex<Character>>(); yl.add(x); yl.add(u);
Map<Vertex<Character>, List<Vertex<Character>>> g = new HashMap<Vertex<Character>, List<Vertex<Character>>>();
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);
System.out.println("=========== prac_22_2_5: After BFS: ==============");
bfs(g, s);
for(Vertex<Character> key: g.keySet())
System.out.println(key);
}

/**
* BFS. p344.
* @param grahp The graph.
* @param s Source vertex.
*/
public static <E> void bfs(Map<Vertex<E>, List<Vertex<E>>> grahp, Vertex<E> s){
Queue<Vertex<E>> q = new LinkedList<Vertex<E>>();
s.color = COLOR.GREY;
q.offer(s);
while(!q.isEmpty()){
Vertex<E> v = q.poll();
List<Vertex<E>> neighbors = grahp.get(v);
for(Vertex<E> neighbor: neighbors){
if(neighbor.color == COLOR.WHITE){
q.offer(neighbor);
neighbor.π = v;
neighbor.d = v.d + 1;
neighbor.color = COLOR.GREY;
}
}
v.color = COLOR.BLACK;
}
}

public static void main(String[] args) {
prac_22_2_1();
prac_22_2_2();
prac_22_2_5();
}

}
49 changes: 49 additions & 0 deletions src/chap22_ElementaryGraphAlgo/Vertex.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package chap22_ElementaryGraphAlgo;
/**
* Vertex of graph. 160929
* Used by BFS, DSF, etc.
*/
public class Vertex<T> {
public T value;
public Vertex<T> π;
public int d; //
public COLOR color;

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

@Override
public String toString(){
if(π != null)
return "Vertex: " + value + ", d:" + d + ", π:" + π.value;
else
return "Vertex: " + value + ", d:" + d + ", π: null";

}

@Override
public boolean equals(Object obj){
if(obj == null)
return false;

if(!(obj instanceof Vertex))
return false;

@SuppressWarnings({ "rawtypes" })
Vertex o = (Vertex)obj;
return o.value == this.value; //Assume value is unique in a Graph.
}

@Override
public int hashCode(){
int res = 17;
res = res * 31 + value.hashCode();
return res;
}
}

enum COLOR{WHITE, GREY, BLACK}

0 comments on commit 62a802f

Please sign in to comment.