Skip to content

Commit 92c90aa

Browse files
committed
sp
1 parent 22e1f5d commit 92c90aa

File tree

4 files changed

+136
-0
lines changed

4 files changed

+136
-0
lines changed

Ch_4_4/TestDijkstraSP.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package Ch_4_4;
2+
3+
import edu.princeton.cs.algs4.In;
4+
import edu.princeton.cs.algs4.StdOut;
5+
6+
/**
7+
* Created by HuGuodong on 5/12/20.
8+
*/
9+
public class TestDijkstraSP {
10+
11+
public static void main(String[] args) {
12+
_EdgeWeightedDigraph G = new _EdgeWeightedDigraph(new In("algdata/tinyEWD.txt"));
13+
int s = 0;
14+
_DijkstraSP sp = new _DijkstraSP(G, s);
15+
for (int t = 0; t < G.V(); t++) {
16+
StdOut.print(s + " to " + t);
17+
StdOut.printf(" (%4.2f): ", sp.distTo(t));
18+
if (sp.hasPathTo(t)) {
19+
for (_DirectedEdge e : sp.pathTo(t)) {
20+
StdOut.print(e + " ");
21+
}
22+
}
23+
StdOut.println();
24+
}
25+
// 0 to 0 (0.00):
26+
// 0 to 1 (1.05): 0->4 0.38 4->5 0.35 5->1 0.32
27+
// 0 to 2 (0.26): 0->2 0.26
28+
// 0 to 3 (0.99): 0->2 0.26 2->7 0.34 7->3 0.39
29+
// 0 to 4 (0.38): 0->4 0.38
30+
// 0 to 5 (0.73): 0->4 0.38 4->5 0.35
31+
// 0 to 6 (1.51): 0->2 0.26 2->7 0.34 7->3 0.39 3->6 0.52
32+
// 0 to 7 (0.60): 0->2 0.26 2->7 0.34
33+
}
34+
}

Ch_4_4/_DijkstraSP.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package Ch_4_4;
2+
3+
import edu.princeton.cs.algs4.IndexMinPQ;
4+
import edu.princeton.cs.algs4.Stack;
5+
6+
/**
7+
* Created by HuGuodong on 5/12/20.
8+
*/
9+
public class _DijkstraSP {
10+
11+
private _DirectedEdge[] edgeTo;
12+
private double[] distTo;
13+
private IndexMinPQ<Double> pq;
14+
15+
public _DijkstraSP(_EdgeWeightedDigraph G, int s) {
16+
edgeTo = new _DirectedEdge[G.V()];
17+
distTo = new double[G.V()];
18+
pq = new IndexMinPQ<>(G.V());
19+
for (int v = 0; v < G.V(); v++) {
20+
distTo[v] = Double.POSITIVE_INFINITY;
21+
}
22+
distTo[s] = 0.0;
23+
24+
pq.insert(s, 0.0);
25+
while (!pq.isEmpty()) {
26+
relax(G, pq.delMin());
27+
}
28+
}
29+
30+
private void relax(_EdgeWeightedDigraph G, int v) {
31+
for (_DirectedEdge e : G.adj(v)) {
32+
int w = e.to();
33+
if (distTo[w] > distTo[v] + e.weight()) {
34+
distTo[w] = distTo[v] + e.weight();
35+
edgeTo[w] = e;
36+
if (!pq.contains(w)) pq.insert(w, distTo[w]);
37+
else pq.changeKey(w, distTo[w]);
38+
}
39+
}
40+
}
41+
42+
public double distTo(int v) {
43+
return distTo[v];
44+
}
45+
46+
public boolean hasPathTo(int v) {
47+
return distTo[v] < Double.POSITIVE_INFINITY;
48+
}
49+
50+
public Iterable<_DirectedEdge> pathTo(int v) {
51+
if (!hasPathTo(v)) return null;
52+
Stack<_DirectedEdge> path = new Stack<>();
53+
for (_DirectedEdge e = edgeTo[v]; e != null; e = edgeTo[e.from()]) {
54+
path.push(e);
55+
}
56+
return path;
57+
}
58+
}

Ch_4_4/_EdgeWeightedDigraph.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package Ch_4_4;
22

33
import edu.princeton.cs.algs4.Bag;
4+
import edu.princeton.cs.algs4.In;
45

56
/**
67
* Created by HuGuodong on 5/12/20.
@@ -20,6 +21,18 @@ public _EdgeWeightedDigraph(int V) {
2021
}
2122
}
2223

24+
public _EdgeWeightedDigraph(In in) {
25+
this(in.readInt());
26+
int E = in.readInt();
27+
for (int i = 0; i < E; i++) {
28+
int v = in.readInt();
29+
int w = in.readInt();
30+
double weight = in.readDouble();
31+
_DirectedEdge e = new _DirectedEdge(v, w, weight);
32+
addEdge(e);
33+
}
34+
}
35+
2336
public int V() {
2437
return this.V;
2538
}
@@ -36,4 +49,14 @@ public void addEdge(_DirectedEdge e) {
3649
public Iterable<_DirectedEdge> adj(int v) {
3750
return adj[v];
3851
}
52+
53+
public Iterable<_DirectedEdge> edges() {
54+
Bag<_DirectedEdge> bag = new Bag<>();
55+
for (int v = 0; v < V; v++) {
56+
for (_DirectedEdge e : adj[v]) {
57+
bag.add(e);
58+
}
59+
}
60+
return bag;
61+
}
3962
}

Ch_4_4/note.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
data structures for shortest path
3+
4+
- parent-edge `edgeTo[v]`
5+
- distance to source `distTo[v]`, represent the length of sp from `s` to `v`
6+
7+
![2020-05-12_000.jpg](https://gitee.com/gdhu/testtingop/raw/master/2020-05-12_000.jpg)
8+
9+
edge relaxation
10+
11+
![2020-05-12_001.jpg](https://gitee.com/gdhu/testtingop/raw/master/2020-05-12_001.jpg)
12+
13+
if the length to w is larger than the sum of length to `v` plus weight of `v` to `w`,
14+
then `relax`: change `distTo[w] = distTo[v] + e.weight()` and `edgeTo[w] = e`.
15+
16+
17+
18+
19+
20+
21+

0 commit comments

Comments
 (0)