forked from zhuxiuwei/CLRS
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
201 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |