From 62a802f584123a997a2a30bb6078a4414de166f6 Mon Sep 17 00:00:00 2001 From: zhuxiuwei Date: Sat, 29 Oct 2016 22:06:07 +0800 Subject: [PATCH] update 22.2 --- Chapter22_ElementaryGraphAlgorithms.md | 43 +++++++- src/chap22_ElementaryGraphAlgo/BFS.java | 110 +++++++++++++++++++++ src/chap22_ElementaryGraphAlgo/Vertex.java | 49 +++++++++ 3 files changed, 201 insertions(+), 1 deletion(-) create mode 100644 src/chap22_ElementaryGraphAlgo/BFS.java create mode 100644 src/chap22_ElementaryGraphAlgo/Vertex.java diff --git a/Chapter22_ElementaryGraphAlgorithms.md b/Chapter22_ElementaryGraphAlgorithms.md index 08a0bdc..83cba60 100644 --- a/Chapter22_ElementaryGraphAlgorithms.md +++ b/Chapter22_ElementaryGraphAlgorithms.md @@ -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 + + + + + + diff --git a/src/chap22_ElementaryGraphAlgo/BFS.java b/src/chap22_ElementaryGraphAlgo/BFS.java new file mode 100644 index 0000000..1c294f5 --- /dev/null +++ b/src/chap22_ElementaryGraphAlgo/BFS.java @@ -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 v1 = new Vertex(1), v2 = new Vertex(2), v3 = new Vertex(3), v4 = new Vertex(4), v5 = new Vertex(5), v6 = new Vertex(6); + List> l1 = new LinkedList>(); l1.add(v2); l1.add(v4); + List> l2 = new LinkedList>(); l2.add(v5); + List> l3 = new LinkedList>(); l3.add(v6); l3.add(v5); + List> l4 = new LinkedList>(); l4.add(v2); + List> l5 = new LinkedList>(); l5.add(v4); + List> l6 = new LinkedList>(); l6.add(v6); + Map, List>> g = new HashMap, List>>(); + 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 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 r = new Vertex('r'), s = new Vertex('s'), t = new Vertex('t'), u = new Vertex('u'), v = new Vertex('v'), w = new Vertex('w'), x = new Vertex('x'), y = new Vertex('y'); + List> rl = new LinkedList>(); rl.add(s); rl.add(v); + List> sl = new LinkedList>(); sl.add(r); sl.add(w); + List> tl = new LinkedList>(); tl.add(u); tl.add(w); tl.add(x); + List> ul = new LinkedList>(); ul.add(t); ul.add(x); ul.add(y); + List> vl = new LinkedList>(); vl.add(r); + List> wl = new LinkedList>(); wl.add(s); wl.add(t); wl.add(x); + List> xl = new LinkedList>(); xl.add(t); xl.add(u); xl.add(y); + List> yl = new LinkedList>(); yl.add(u); yl.add(x); + Map, List>> g = new HashMap, List>>(); + 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 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 r = new Vertex('r'), s = new Vertex('s'), t = new Vertex('t'), u = new Vertex('u'), v = new Vertex('v'), w = new Vertex('w'), x = new Vertex('x'), y = new Vertex('y'); + List> rl = new LinkedList>(); rl.add(v); rl.add(s); + List> sl = new LinkedList>(); sl.add(w); sl.add(r); + List> tl = new LinkedList>(); tl.add(x); tl.add(w); tl.add(u); + List> ul = new LinkedList>(); ul.add(y); ul.add(x); ul.add(t); + List> vl = new LinkedList>(); vl.add(r); + List> wl = new LinkedList>(); wl.add(x); wl.add(t); wl.add(s); + List> xl = new LinkedList>(); xl.add(y); xl.add(u); xl.add(t); + List> yl = new LinkedList>(); yl.add(x); yl.add(u); + Map, List>> g = new HashMap, List>>(); + 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 key: g.keySet()) + System.out.println(key); + } + + /** + * BFS. p344. + * @param grahp The graph. + * @param s Source vertex. + */ + public static void bfs(Map, List>> grahp, Vertex s){ + Queue> q = new LinkedList>(); + s.color = COLOR.GREY; + q.offer(s); + while(!q.isEmpty()){ + Vertex v = q.poll(); + List> neighbors = grahp.get(v); + for(Vertex 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(); + } + +} diff --git a/src/chap22_ElementaryGraphAlgo/Vertex.java b/src/chap22_ElementaryGraphAlgo/Vertex.java new file mode 100644 index 0000000..fd6e536 --- /dev/null +++ b/src/chap22_ElementaryGraphAlgo/Vertex.java @@ -0,0 +1,49 @@ +package chap22_ElementaryGraphAlgo; +/** + * Vertex of graph. 160929 + * Used by BFS, DSF, etc. + */ +public class Vertex { + public T value; + public Vertex π; + 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}