Skip to content

Commit 22e1f5d

Browse files
committed
alg4 directed graph
1 parent 677074a commit 22e1f5d

7 files changed

+20
-226
lines changed

Ch_4_4/Ex_4_4_01.java

Lines changed: 0 additions & 8 deletions
This file was deleted.

Ch_4_4/_AcyclicSP.java

Lines changed: 0 additions & 8 deletions
This file was deleted.

Ch_4_4/_CPM.java

Lines changed: 0 additions & 8 deletions
This file was deleted.

Ch_4_4/_DijstraSP.java

Lines changed: 0 additions & 83 deletions
This file was deleted.

Ch_4_4/_DirectedEdge.java

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,34 @@
11
package Ch_4_4;
22

3-
import edu.princeton.cs.algs4.StdOut;
4-
53
/**
6-
* Created by HuGuodong on 2018/12/30.
4+
* Created by HuGuodong on 5/12/20.
75
*/
8-
96
public class _DirectedEdge {
107

118
private final int v;
129
private final int w;
1310
private final double weight;
1411

15-
public _DirectedEdge(int v, int w, double weight){
12+
public _DirectedEdge(int v, int w, double weight) {
1613
this.v = v;
1714
this.w = w;
1815
this.weight = weight;
1916
}
2017

21-
public double weight(){
22-
return weight;
23-
}
24-
25-
public int from(){
26-
return v;
18+
public double weight() {
19+
return this.weight;
2720
}
2821

29-
public int to(){
30-
return w;
22+
public int from() {
23+
return this.v;
3124
}
3225

33-
public String toString(){
34-
return String.format("%d->%d %5.2f", v, w, weight);
26+
public int to() {
27+
return this.w;
3528
}
3629

37-
public static void main(String[] args) {
38-
_DirectedEdge e = new _DirectedEdge(5,10, 2.22);
39-
StdOut.println(e);
30+
@Override
31+
public String toString() {
32+
return String.format("%d->%d %.2f", v, w, weight);
4033
}
4134
}

Ch_4_4/_EdgeWeightedDigraph.java

Lines changed: 9 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,39 @@
11
package Ch_4_4;
22

3-
import Ch_4_3._Edge;
43
import edu.princeton.cs.algs4.Bag;
5-
import edu.princeton.cs.algs4.In;
6-
import edu.princeton.cs.algs4.StdOut;
74

85
/**
9-
* Created by HuGuodong on 2018/12/30.
6+
* Created by HuGuodong on 5/12/20.
107
*/
11-
128
public class _EdgeWeightedDigraph {
139

1410
private final int V;
1511
private int E;
1612
private Bag<_DirectedEdge>[] adj;
1713

18-
private static final String NEWLINE = System.getProperty("line.separator");
19-
20-
public _EdgeWeightedDigraph(int V){
14+
public _EdgeWeightedDigraph(int V) {
2115
this.V = V;
2216
this.E = 0;
23-
adj = (Bag<_DirectedEdge>[]) new Bag[V];
17+
adj = new Bag[V];
2418
for (int v = 0; v < V; v++) {
2519
adj[v] = new Bag<>();
2620
}
2721
}
2822

29-
public _EdgeWeightedDigraph(In in){
30-
this(in.readInt());
31-
int E = in.readInt();
32-
for (int i = 0; i < E; i++) {
33-
int v = in.readInt();
34-
int w = in.readInt();
35-
double weight = in.readDouble();
36-
_DirectedEdge edge = new _DirectedEdge(v, w, weight);
37-
addEdge(edge);
38-
}
39-
}
40-
41-
public int V(){
42-
return V;
23+
public int V() {
24+
return this.V;
4325
}
4426

45-
public int E(){
46-
return E;
27+
public int E() {
28+
return this.E;
4729
}
4830

49-
public void addEdge(_DirectedEdge e){
31+
public void addEdge(_DirectedEdge e) {
5032
adj[e.from()].add(e);
5133
E++;
5234
}
5335

54-
public Iterable<_DirectedEdge> adj(int v){
36+
public Iterable<_DirectedEdge> adj(int v) {
5537
return adj[v];
5638
}
57-
58-
public Iterable<_DirectedEdge> edges(){
59-
Bag<_DirectedEdge> bag = new Bag<>();
60-
for (int v = 0; v < V; v++) {
61-
for(_DirectedEdge e : adj[v]){
62-
bag.add(e);
63-
}
64-
}
65-
return bag;
66-
}
67-
68-
@Override
69-
public String toString() {
70-
StringBuilder s = new StringBuilder();
71-
s.append(V + " " + E + NEWLINE);
72-
for (int v = 0; v < V; v++) {
73-
s.append(v + ": ");
74-
for (_DirectedEdge e : adj[v]) {
75-
s.append(e + ", ");
76-
}
77-
s.setLength(s.length()-2); // 删除最后的空格
78-
s.append(NEWLINE);
79-
}
80-
return s.toString();
81-
}
82-
83-
public static void main(String[] args) {
84-
_EdgeWeightedDigraph g = new _EdgeWeightedDigraph(new In("tinyEWD.txt"));
85-
StdOut.println(g);
86-
}
8739
}

Ch_4_4/_SP.java

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)