Skip to content

Commit

Permalink
imporve 22.4-2, and update Vertex toString()
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuxiuwei committed Nov 8, 2016
1 parent 5c97241 commit 4a0d7a2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/chap22_ElementaryGraphAlgo/BFS.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static void prac_22_2_1(){
System.out.println("=========== prac_22_2_1: After BFS: ==============");
bfs(g, v3);
for(Vertex<Integer> key: g.keySet())
System.out.println(key);
System.out.println(key.formatString());
}

/**
Expand All @@ -53,7 +53,7 @@ public static void prac_22_2_2(){
System.out.println("=========== prac_22_2_2: After BFS: ==============");
bfs(g, s);
for(Vertex<Character> key: g.keySet())
System.out.println(key);
System.out.println(key.formatString());
}

/**
Expand All @@ -75,7 +75,7 @@ public static void prac_22_2_5(){
System.out.println("=========== prac_22_2_5: After BFS: ==============");
bfs(g, s);
for(Vertex<Character> key: g.keySet())
System.out.println(key);
System.out.println(key.formatString());
}

/**
Expand Down
27 changes: 15 additions & 12 deletions src/chap22_ElementaryGraphAlgo/CountOfPaths.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import java.util.Stack;

/**
* 161107 Practice 22.4-2 两个结点之间简单路径的数量。
* 161107 Practice 22.4-2 有向无环图,两个结点之间简单路径的数量。
* @author xiuzhu
* !!!! Note: 花了比较多的时间。关键是在扫描邻居的时候,为了不要扫描已经扫描过的,需要加一个map neighborsHasChecked 来记录。
* !!!! Note: 花了比较多的时间。关键是在扫描邻居的时候,为了不要扫描已经扫描过的,需要加一个map neighborsHasCheckedIndex 来记录。
*/
public class CountOfPaths {

Expand All @@ -30,32 +30,35 @@ public static <E> int countPathsBetweenVertexs(Map<Vertex<E>, List<Vertex<E>>> g
Stack<Vertex<E>> stack = new Stack<Vertex<E>>();
Set<Vertex<E>> visiting = new HashSet<Vertex<E>>();
Set<Vertex<E>> hasPathToT = new HashSet<Vertex<E>>();
Map<Vertex<E>, Set<Vertex<E>>> neighborsHasChecked = new HashMap<Vertex<E>, Set<Vertex<E>>>(); //Note, need add this to avoid go back and double counted.
Map<Vertex<E>, Integer> neighborsHasCheckedIndex = new HashMap<Vertex<E>, Integer>(); //Note, need add this to avoid go back and double counted.
//initialize neighborsHasChecked map
for (Vertex<E> ver: graph.keySet()) {
ver.reset();
neighborsHasChecked.put(ver, new HashSet<Vertex<E>>());
neighborsHasCheckedIndex.put(ver, 0);
}
s.color = COLOR.GREY;
stack.push(s);
while(s.color != COLOR.BLACK){
Vertex<E> next = null;
for (Vertex<E> neighbor: graph.get(stack.peek())){

int neighborIndex = neighborsHasCheckedIndex.get(stack.peek());
while(neighborIndex < graph.get(stack.peek()).size()){
Vertex<E> neighbor = graph.get(stack.peek()).get(neighborIndex);
if(neighbor.color == COLOR.WHITE){
neighborsHasChecked.get(stack.peek()).add(neighbor); //!!!Note when to add to neighborsHasChecked
neighborsHasCheckedIndex.put(stack.peek(), ++ neighborIndex);
next = neighbor;
break;
}
else{
if(!neighborsHasChecked.get(stack.peek()).contains(neighbor)){ //Each neighbor just check once, to avoid go back and double counted.
if(neighbor.equals(t) || hasPathToT.contains(neighbor)){
hasPathToT.addAll(visiting);
count ++; //when T is already grey.
}
if(neighbor.equals(t) || hasPathToT.contains(neighbor)){
hasPathToT.addAll(visiting);
count ++; //when T is already grey.
}
neighborsHasChecked.get(stack.peek()).add(neighbor); //!!!Note when to add to neighborsHasChecked
}
neighborsHasCheckedIndex.put(stack.peek(), ++ neighborIndex);
}


if(next != null && !next.equals(t)){
next.π = stack.peek();
next.color = COLOR.GREY;
Expand Down
4 changes: 2 additions & 2 deletions src/chap22_ElementaryGraphAlgo/DFS.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static void dfs_Image22_6(){

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

//print edge types
Expand Down Expand Up @@ -125,7 +125,7 @@ public static void dfs_Image22_3(){
dfs(g);
//print d and t.
for (Vertex<Character> ver: g.keySet()) {
System.out.println(ver);
System.out.println(ver.formatString());
}
//print edge types
for (Vertex<Character> uu: g.keySet()) {
Expand Down
6 changes: 5 additions & 1 deletion src/chap22_ElementaryGraphAlgo/Vertex.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@ public Vertex(T value){

@Override
public String toString(){
return value.toString();

}

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

}

@Override
Expand Down

0 comments on commit 4a0d7a2

Please sign in to comment.