Skip to content

Commit 6aef40e

Browse files
committed
[Function add]
1.Add the function of DirectEdge and EdgeWeightDigraph.
1 parent 8b6a1d6 commit 6aef40e

File tree

6 files changed

+239
-0
lines changed

6 files changed

+239
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package ca.mcmaster.chapter.four.graph.spt;
2+
3+
public class DirectedEdge {
4+
private final int v; //边的起始点
5+
private final int w; //边的指向
6+
private final double weight; //有向边的权重
7+
public DirectedEdge(int v, int w, double weight){
8+
this.v = v;
9+
this.w = w;
10+
this.weight = weight;
11+
}
12+
public double weight(){ return weight; }
13+
public int from(){ return this.v; }
14+
public int to(){ return this.w; }
15+
@Override
16+
public String toString() {
17+
return String.format("%d -> %d %.2f", v, w, weight);
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package ca.mcmaster.chapter.four.graph.spt;
2+
3+
import java.io.FileInputStream;
4+
import java.util.Scanner;
5+
6+
import ca.mcmaster.chapter.one.bag.Bag;
7+
import ca.mcmaster.chapter.one.bag.ListBag;
8+
9+
public class EdgeWeightedDigraph {
10+
private final int V; //Number of vertex.
11+
private int E; //Number of edges.
12+
private Bag<DirectedEdge>[] adj; //Adjacency list
13+
@SuppressWarnings("unchecked")
14+
public EdgeWeightedDigraph(int V){
15+
this.V = V;
16+
this.E = 0;
17+
adj = (Bag<DirectedEdge>[])new Bag[V];
18+
for(int i = 0; i < V; i++)
19+
adj[i] = new ListBag<>();
20+
}
21+
@SuppressWarnings("unchecked")
22+
public EdgeWeightedDigraph(FileInputStream in) {
23+
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);
35+
}
36+
s.close();
37+
}
38+
public int V() { return this.V; }
39+
public int E() { return this.E; }
40+
public void addEdge(int v, int w, double weight) {
41+
adj[v].add(new DirectedEdge(v, w, weight));
42+
this.E ++;
43+
}
44+
public Iterable<DirectedEdge> adj(int v) {
45+
return adj[v];
46+
}
47+
public void display() {
48+
for(int i = 0; i < V; i++){
49+
StringBuilder sb = new StringBuilder(i + ": ");
50+
for(DirectedEdge e : adj[i]){
51+
sb.append(e.to() + "|");
52+
}
53+
System.out.println(sb.toString());
54+
}
55+
}
56+
}

Diff for: DataStructrue/Graph/DirectedEdge.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package ca.mcmaster.chapter.four.graph.spt;
2+
3+
public class DirectedEdge {
4+
private final int v; //边的起始点
5+
private final int w; //边的指向
6+
private final double weight; //有向边的权重
7+
public DirectedEdge(int v, int w, double weight){
8+
this.v = v;
9+
this.w = w;
10+
this.weight = weight;
11+
}
12+
public double weight(){ return weight; }
13+
public int from(){ return this.v; }
14+
public int to(){ return this.w; }
15+
@Override
16+
public String toString() {
17+
return String.format("%d -> %d %.2f", v, w, weight);
18+
}
19+
}

Diff for: DataStructrue/Graph/EdgeWeightedDigraph.java

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package ca.mcmaster.chapter.four.graph.spt;
2+
3+
import java.io.FileInputStream;
4+
import java.util.Scanner;
5+
6+
import ca.mcmaster.chapter.one.bag.Bag;
7+
import ca.mcmaster.chapter.one.bag.ListBag;
8+
9+
public class EdgeWeightedDigraph {
10+
private final int V; //Number of vertex.
11+
private int E; //Number of edges.
12+
private Bag<DirectedEdge>[] adj; //Adjacency list
13+
@SuppressWarnings("unchecked")
14+
public EdgeWeightedDigraph(int V){
15+
this.V = V;
16+
this.E = 0;
17+
adj = (Bag<DirectedEdge>[])new Bag[V];
18+
for(int i = 0; i < V; i++)
19+
adj[i] = new ListBag<>();
20+
}
21+
@SuppressWarnings("unchecked")
22+
public EdgeWeightedDigraph(FileInputStream in) {
23+
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);
35+
}
36+
s.close();
37+
}
38+
public int V() { return this.V; }
39+
public int E() { return this.E; }
40+
public void addEdge(int v, int w, double weight) {
41+
adj[v].add(new DirectedEdge(v, w, weight));
42+
this.E ++;
43+
}
44+
public Iterable<DirectedEdge> adj(int v) {
45+
return adj[v];
46+
}
47+
public void display() {
48+
for(int i = 0; i < V; i++){
49+
StringBuilder sb = new StringBuilder(i + ": ");
50+
for(DirectedEdge e : adj[i]){
51+
sb.append(e.to() + "|");
52+
}
53+
System.out.println(sb.toString());
54+
}
55+
}
56+
}

Diff for: DataStructrue/Graph/ShortestPath.md

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Shortest Path
2+
> 找到从一个顶点到达另一个顶点的成本的最小路径。
3+
> 在一幅加权有向图中,从顶点s到顶点t的最短路径是从所有从s到t的路径中最小权重者。
4+
5+
### 最短路径的性质
6+
1. 路径是有向的。最短路径需要考虑到各边的方向。
7+
2. 权重不一定等价距离,和最小树相似,可以理解为成本的最小值。
8+
3. 并不是所有顶点都是可达的。为了简化问题,我们的样图都是强连通的(从每个顶点到另一个顶点都是可达的)。
9+
4. 负权重会使问题更复杂,我们假设边的权重都是正的。
10+
5. 最短路径一般都是简单的。忽略构成环的零权重边。
11+
6. 最短路径不一定是唯一的。
12+
7. 可能存在平行边或自环。
13+
14+
### 最短路径树(Shortest Path Tree)
15+
> 给定一幅加权有向图和一个顶点s,以s为起点的一棵最短路径树是图的一幅子图,它包含s和从s可到达的所有顶点。这棵树的根节点为s,树的每条路径都是有向图中的一条最短路径。
16+
17+
#### Directed Edge 有向边对象
18+
```Java
19+
public class DirectedEdge {
20+
private final int v; //边的起始点
21+
private final int w; //边的指向
22+
private final double weight; //有向边的权重
23+
public DirectedEdge(int v, int w, double weight){
24+
this.v = v;
25+
this.w = w;
26+
this.weight = weight;
27+
}
28+
public double weight(){ return weight; }
29+
public int from(){ return this.v; }
30+
public int to(){ return this.w; }
31+
@Override
32+
public String toString() {
33+
return String.format("%d -> %d %.2f", v, w, weight);
34+
}
35+
}
36+
```
37+
38+
#### EdgeWeightDigraph 加权有向图
39+
```Java
40+
public class EdgeWeightedDigraph {
41+
private final int V; //Number of vertex.
42+
private int E; //Number of edges.
43+
private Bag<DirectedEdge>[] adj; //Adjacency list
44+
@SuppressWarnings("unchecked")
45+
public EdgeWeightedDigraph(int V){
46+
this.V = V;
47+
this.E = 0;
48+
adj = (Bag<DirectedEdge>[])new Bag[V];
49+
for(int i = 0; i < V; i++)
50+
adj[i] = new ListBag<>();
51+
}
52+
@SuppressWarnings("unchecked")
53+
public EdgeWeightedDigraph(FileInputStream in) {
54+
Scanner s = null;
55+
s = new Scanner(in);
56+
this.V = s.nextInt();
57+
this.E = s.nextInt();
58+
adj = (Bag<DirectedEdge>[])new Bag[V];
59+
for (int i = 0; i < V; i++)
60+
adj[i] = new ListBag<DirectedEdge>();
61+
for(int i = 0; i < E; i ++){
62+
int v = s.nextInt();
63+
int w = s.nextInt();
64+
double weight = s.nextDouble();
65+
addEdge(v, w, weight);
66+
}
67+
s.close();
68+
}
69+
public int V() { return this.V; }
70+
public int E() { return this.E; }
71+
public void addEdge(int v, int w, double weight) {
72+
adj[v].add(new DirectedEdge(v, w, weight));
73+
this.E ++;
74+
}
75+
public Iterable<DirectedEdge> adj(int v) {
76+
return adj[v];
77+
}
78+
public void display() {
79+
for(int i = 0; i < V; i++){
80+
StringBuilder sb = new StringBuilder(i + ": ");
81+
for(DirectedEdge e : adj[i]){
82+
sb.append(e.to() + "|");
83+
}
84+
System.out.println(sb.toString());
85+
}
86+
}
87+
}
88+
```

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ I started learning the data struture systematically, I will list my notes in the
3838
1. [Undirected Graph](https://github.com/Seanforfun/Algorithm/blob/master/DataStructrue/Graph/UndirectedGraph.md)
3939
2. [Directed Graph](https://github.com/Seanforfun/Algorithm/blob/master/DataStructrue/Graph/DirectedGraph.md)
4040
3. [Minimun Spanning Trees](https://github.com/Seanforfun/Algorithm/blob/master/DataStructrue/Graph/MinimumSpanningTrees.md)
41+
4. [Shortest Path](https://github.com/Seanforfun/Algorithm/blob/master/DataStructrue/Graph/ShortestPath.md)
4142

4243
## LeetCode
4344
This part I listed my [leetcode solutions](https://github.com/Seanforfun/Algorithm/tree/master/leetcode).

0 commit comments

Comments
 (0)