Skip to content

Commit 4681f47

Browse files
committed
[Function add]
1.Add degree to graph.
1 parent dbaf3e3 commit 4681f47

File tree

5 files changed

+39
-8
lines changed

5 files changed

+39
-8
lines changed

Diff for: Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/DFSCC.java

+1
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,6 @@ public static void main(String[] args) throws FileNotFoundException {
5858
sb.append(v + " ");
5959
System.out.println(sb.toString());
6060
}
61+
6162
}
6263
}

Diff for: Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/Graph.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package ca.mcmaster.chapter.four.graph;
22

3-
import java.awt.DisplayMode;
4-
53
public interface Graph {
64
/**
75
* @Description: Get the vertex number.
@@ -35,7 +33,7 @@ public interface Graph {
3533
*/
3634
static Integer degree(Graph G, int V){
3735
Integer degree = new Integer(0);
38-
for(int w : G.adj(V)) degree++;
36+
for(@SuppressWarnings("unused") int w : G.adj(V)) degree++;
3937
return degree;
4038
}
4139

Diff for: Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/SymbolGraphImpl.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,13 @@ public static void main(String[] args) throws FileNotFoundException {
6969
for(int v = 0; v < vertexNum; v++){
7070
StringBuilder sb = new StringBuilder(symbolGraphImpl.name(v) + " -> ");
7171
for(int w : graph.adj(v))
72-
sb.append(symbolGraphImpl.name(w) + " ");
73-
System.out.println(sb.toString());
72+
sb.append(symbolGraphImpl.name(w) + "|");
73+
}
74+
BreadthFirstPath bfs = new BreadthFirstPath(graph, symbolGraphImpl.index("Bacon, Kevin"));
75+
Iterable<Integer> pathTo = bfs.pathTo(symbolGraphImpl.index("Kidman, Nicole"));
76+
System.out.println("Bacon, Kevin");
77+
for(int w : pathTo){
78+
System.out.println(symbolGraphImpl.name(w));
7479
}
7580
}
7681
}

Diff for: DataStructrue/Graph/SymbolGraphImpl.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,13 @@ public static void main(String[] args) throws FileNotFoundException {
6969
for(int v = 0; v < vertexNum; v++){
7070
StringBuilder sb = new StringBuilder(symbolGraphImpl.name(v) + " -> ");
7171
for(int w : graph.adj(v))
72-
sb.append(symbolGraphImpl.name(w) + " ");
73-
System.out.println(sb.toString());
72+
sb.append(symbolGraphImpl.name(w) + "|");
73+
}
74+
BreadthFirstPath bfs = new BreadthFirstPath(graph, symbolGraphImpl.index("Bacon, Kevin"));
75+
Iterable<Integer> pathTo = bfs.pathTo(symbolGraphImpl.index("Kidman, Nicole"));
76+
System.out.println("Bacon, Kevin");
77+
for(int w : pathTo){
78+
System.out.println(symbolGraphImpl.name(w));
7479
}
7580
}
7681
}

Diff for: DataStructrue/Graph/UndirectedGraph.md

+23-1
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ public class SymbolGraphImpl implements SymbolGraph {
626626

627627
@Override
628628
public Graph G() { return G; }
629-
629+
630630
public static void main(String[] args) throws FileNotFoundException {
631631
SymbolGraphImpl symbolGraphImpl = new SymbolGraphImpl("src/ca/mcmaster/chapter/four/graph/movies.txt", "/");
632632
Graph graph = symbolGraphImpl.G();
@@ -639,4 +639,26 @@ public class SymbolGraphImpl implements SymbolGraph {
639639
}
640640
}
641641
}
642+
```
643+
644+
#### 间隔的度数
645+
>间隔的度数实际上就是最短路径,即两个顶点之间的最短距离。
646+
```Java
647+
public static void main(String[] args) throws FileNotFoundException {
648+
SymbolGraphImpl symbolGraphImpl = new SymbolGraphImpl("src/ca/mcmaster/chapter/four/graph/movies.txt", "/");
649+
Graph graph = symbolGraphImpl.G();
650+
int vertexNum = graph.V();
651+
for(int v = 0; v < vertexNum; v++){
652+
StringBuilder sb = new StringBuilder(symbolGraphImpl.name(v) + " -> ");
653+
for(int w : graph.adj(v))
654+
sb.append(symbolGraphImpl.name(w) + "|");
655+
}
656+
//Use BFS to find the shorted path.
657+
BreadthFirstPath bfs = new BreadthFirstPath(graph, symbolGraphImpl.index("Bacon, Kevin"));
658+
Iterable<Integer> pathTo = bfs.pathTo(symbolGraphImpl.index("Kidman, Nicole"));
659+
System.out.println("Bacon, Kevin");
660+
for(int w : pathTo){
661+
System.out.println(symbolGraphImpl.name(w));
662+
}
663+
}
642664
```

0 commit comments

Comments
 (0)