Skip to content

Commit 80978c9

Browse files
committed
[Function add][Bug fix]
1.Add a function of using Dijkstra algorithm of finding shortest path. 2. Fix the bug of weightedEdgeDigraph.
1 parent 6aef40e commit 80978c9

File tree

7 files changed

+383
-28
lines changed

7 files changed

+383
-28
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package ca.mcmaster.chapter.four.graph.spt;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.FileNotFoundException;
6+
import java.util.Stack;
7+
8+
import edu.princeton.cs.algs4.IndexMinPQ;
9+
10+
public class DijkstraSP implements SP {
11+
private DirectedEdge[] edgeTo;
12+
private double[] distTo;
13+
private IndexMinPQ<Double> pq; //优先级队列,存储了到每个顶点的dist
14+
public DijkstraSP(EdgeWeightedDigraph g, int s) {
15+
edgeTo = new DirectedEdge[g.V()];
16+
distTo = new double[g.V()];
17+
pq = new IndexMinPQ<>(g.V());
18+
for(int v = 0; v < g.V(); v++)
19+
distTo[v] = Double.POSITIVE_INFINITY;
20+
distTo[s] = 0D;
21+
pq.insert(s, 0D);
22+
while(!pq.isEmpty())
23+
relax(g, pq.delMin());
24+
}
25+
@Override
26+
public double distTo(int v) {
27+
return distTo[v];
28+
}
29+
@Override
30+
public boolean hasPathTo(int v) {
31+
return distTo[v] < Double.POSITIVE_INFINITY;
32+
}
33+
@Override
34+
public Iterable<DirectedEdge> pathTo(int v) {
35+
if(!hasPathTo(v)) return null;
36+
Stack<DirectedEdge> stack = new Stack<>();
37+
while(edgeTo[v] != null){
38+
DirectedEdge e = edgeTo[v];
39+
stack.push(e);
40+
v = e.from();
41+
}
42+
return stack;
43+
}
44+
/**
45+
* @Description: Relax the edge e.
46+
* @param e
47+
*/
48+
@SuppressWarnings("unused")
49+
private void relax(DirectedEdge e){
50+
int v = e.from(); int w = e.to();
51+
if(distTo(w) > distTo(v) + e.weight()){
52+
distTo[w] = distTo[v] + e.weight();
53+
edgeTo[w] = e;
54+
}
55+
}
56+
/**
57+
* @Description: G is digraph and v is a vertex.
58+
* @param g
59+
* @param v
60+
*/
61+
private void relax(EdgeWeightedDigraph g, int v){
62+
for(DirectedEdge e:g.adj(v)){
63+
int w = e.to(); //e is from v to w.
64+
if(distTo[w] > distTo[v] + e.weight()){
65+
distTo[w] = distTo[v] + e.weight();
66+
edgeTo[w] = e;
67+
if(pq.contains(w)) pq.changeKey(w, distTo[w]);
68+
else pq.insert(w, distTo[w]);
69+
}
70+
}
71+
}
72+
public static void main(String[] args) throws FileNotFoundException {
73+
FileInputStream is = new FileInputStream(new File("src/ca/mcmaster/chapter/four/graph/spt/tinyEWD.txt"));
74+
EdgeWeightedDigraph g = new EdgeWeightedDigraph(is);
75+
DijkstraSP dijkstraSP = new DijkstraSP(g, 0);
76+
Stack<DirectedEdge> pathTo = (Stack<DirectedEdge>) dijkstraSP.pathTo(6);
77+
StringBuilder sb = new StringBuilder();
78+
DirectedEdge edge = pathTo.pop();
79+
sb.append(edge.from() + "->");
80+
sb.append(edge.to() + "->");
81+
while(!pathTo.empty()){
82+
sb.append(pathTo.pop().to() + "->");
83+
}
84+
System.out.println(sb.toString());
85+
}
86+
}

Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/spt/EdgeWeightedDigraph.java

+23-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package ca.mcmaster.chapter.four.graph.spt;
22

3+
import java.io.File;
34
import java.io.FileInputStream;
5+
import java.io.FileNotFoundException;
46
import java.util.Scanner;
57

68
import ca.mcmaster.chapter.one.bag.Bag;
@@ -21,25 +23,27 @@ public EdgeWeightedDigraph(int V){
2123
@SuppressWarnings("unchecked")
2224
public EdgeWeightedDigraph(FileInputStream in) {
2325
Scanner s = null;
24-
s = new Scanner(in);
25-
this.V = s.nextInt();
26-
this.E = s.nextInt();
27-
adj = (Bag<DirectedEdge>[])new Bag[V];
28-
for (int i = 0; i < V; i++)
29-
adj[i] = new ListBag<DirectedEdge>();
30-
for(int i = 0; i < E; i ++){
31-
int v = s.nextInt();
32-
int w = s.nextInt();
33-
double weight = s.nextDouble();
34-
addEdge(v, w, weight);
26+
try{
27+
s = new Scanner(in);
28+
this.V = s.nextInt();
29+
this.adj = (Bag<DirectedEdge>[])new Bag[V];
30+
for (int i = 0; i < V; i++)
31+
adj[i] = new ListBag<DirectedEdge>();
32+
E = s.nextInt();
33+
for(int i = 0; i < E; i ++){
34+
int v = s.nextInt();
35+
int w = s.nextInt();
36+
double weight = s.nextDouble();
37+
addEdge(v, w, weight);
38+
}
39+
}finally{
40+
s.close();
3541
}
36-
s.close();
3742
}
3843
public int V() { return this.V; }
3944
public int E() { return this.E; }
4045
public void addEdge(int v, int w, double weight) {
41-
adj[v].add(new DirectedEdge(v, w, weight));
42-
this.E ++;
46+
this.adj[v].add(new DirectedEdge(v, w, weight));
4347
}
4448
public Iterable<DirectedEdge> adj(int v) {
4549
return adj[v];
@@ -53,4 +57,9 @@ public void display() {
5357
System.out.println(sb.toString());
5458
}
5559
}
60+
public static void main(String[] args) throws FileNotFoundException {
61+
FileInputStream is = new FileInputStream(new File("src/ca/mcmaster/chapter/four/graph/spt/tinyEWD.txt"));
62+
EdgeWeightedDigraph g = new EdgeWeightedDigraph(is);
63+
g.display();
64+
}
5665
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package ca.mcmaster.chapter.four.graph.spt;
2+
3+
public interface SP {
4+
/**
5+
* @Description: The distance between w and v, if not connected, dist is infinity.
6+
* @param v
7+
* @return
8+
*/
9+
public double distTo(int v);
10+
/**
11+
* @Description: If there is a path from s to v.
12+
* @param v
13+
* @return
14+
*/
15+
public boolean hasPathTo(int v);
16+
/**
17+
* @Description: Path from s to v.
18+
* @param v
19+
* @return
20+
*/
21+
public Iterable<DirectedEdge> pathTo(int v);
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
8
2+
15
3+
4 5 0.35
4+
5 4 0.35
5+
4 7 0.37
6+
5 7 0.28
7+
7 5 0.28
8+
5 1 0.32
9+
0 4 0.38
10+
0 2 0.26
11+
7 3 0.39
12+
1 3 0.29
13+
2 7 0.34
14+
6 2 0.40
15+
3 6 0.52
16+
6 0 0.58
17+
6 4 0.93

DataStructrue/Graph/DijkstraSP.java

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package ca.mcmaster.chapter.four.graph.spt;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.FileNotFoundException;
6+
import java.util.Stack;
7+
8+
import edu.princeton.cs.algs4.IndexMinPQ;
9+
10+
public class DijkstraSP implements SP {
11+
private DirectedEdge[] edgeTo;
12+
private double[] distTo;
13+
private IndexMinPQ<Double> pq; //优先级队列,存储了到每个顶点的dist
14+
public DijkstraSP(EdgeWeightedDigraph g, int s) {
15+
edgeTo = new DirectedEdge[g.V()];
16+
distTo = new double[g.V()];
17+
pq = new IndexMinPQ<>(g.V());
18+
for(int v = 0; v < g.V(); v++)
19+
distTo[v] = Double.POSITIVE_INFINITY;
20+
distTo[s] = 0D;
21+
pq.insert(s, 0D);
22+
while(!pq.isEmpty())
23+
relax(g, pq.delMin());
24+
}
25+
@Override
26+
public double distTo(int v) {
27+
return distTo[v];
28+
}
29+
@Override
30+
public boolean hasPathTo(int v) {
31+
return distTo[v] < Double.POSITIVE_INFINITY;
32+
}
33+
@Override
34+
public Iterable<DirectedEdge> pathTo(int v) {
35+
if(!hasPathTo(v)) return null;
36+
Stack<DirectedEdge> stack = new Stack<>();
37+
while(edgeTo[v] != null){
38+
DirectedEdge e = edgeTo[v];
39+
stack.push(e);
40+
v = e.from();
41+
}
42+
return stack;
43+
}
44+
/**
45+
* @Description: Relax the edge e.
46+
* @param e
47+
*/
48+
@SuppressWarnings("unused")
49+
private void relax(DirectedEdge e){
50+
int v = e.from(); int w = e.to();
51+
if(distTo(w) > distTo(v) + e.weight()){
52+
distTo[w] = distTo[v] + e.weight();
53+
edgeTo[w] = e;
54+
}
55+
}
56+
/**
57+
* @Description: G is digraph and v is a vertex.
58+
* @param g
59+
* @param v
60+
*/
61+
private void relax(EdgeWeightedDigraph g, int v){
62+
for(DirectedEdge e:g.adj(v)){
63+
int w = e.to(); //e is from v to w.
64+
if(distTo[w] > distTo[v] + e.weight()){
65+
distTo[w] = distTo[v] + e.weight();
66+
edgeTo[w] = e;
67+
if(pq.contains(w)) pq.changeKey(w, distTo[w]);
68+
else pq.insert(w, distTo[w]);
69+
}
70+
}
71+
}
72+
public static void main(String[] args) throws FileNotFoundException {
73+
FileInputStream is = new FileInputStream(new File("src/ca/mcmaster/chapter/four/graph/spt/tinyEWD.txt"));
74+
EdgeWeightedDigraph g = new EdgeWeightedDigraph(is);
75+
DijkstraSP dijkstraSP = new DijkstraSP(g, 0);
76+
Stack<DirectedEdge> pathTo = (Stack<DirectedEdge>) dijkstraSP.pathTo(6);
77+
StringBuilder sb = new StringBuilder();
78+
DirectedEdge edge = pathTo.pop();
79+
sb.append(edge.from() + "->");
80+
sb.append(edge.to() + "->");
81+
while(!pathTo.empty()){
82+
sb.append(pathTo.pop().to() + "->");
83+
}
84+
System.out.println(sb.toString());
85+
}
86+
}

DataStructrue/Graph/SP.java

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package ca.mcmaster.chapter.four.graph.spt;
2+
3+
public interface SP {
4+
/**
5+
* @Description: The distance between w and v, if not connected, dist is infinity.
6+
* @param v
7+
* @return
8+
*/
9+
public double distTo(int v);
10+
/**
11+
* @Description: If there is a path from s to v.
12+
* @param v
13+
* @return
14+
*/
15+
public boolean hasPathTo(int v);
16+
/**
17+
* @Description: Path from s to v.
18+
* @param v
19+
* @return
20+
*/
21+
public Iterable<DirectedEdge> pathTo(int v);
22+
}

0 commit comments

Comments
 (0)