Skip to content

Commit 06e354b

Browse files
committed
[Function add]
1.Add implementation of weighted undirected map.
1 parent a316c8b commit 06e354b

File tree

5 files changed

+218
-3
lines changed

5 files changed

+218
-3
lines changed

Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/mstree/Edge.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ public int either(){
2222
return v;
2323
}
2424
public int other(int v){
25-
if(v == this.v) return w;
26-
else return v;
25+
if(v == this.v) return this.w;
26+
else
27+
return this.v;
2728
}
2829
@Override
2930
public String toString() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package ca.mcmaster.chapter.four.graph.mstree;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.FileNotFoundException;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
import java.util.Scanner;
9+
10+
import ca.mcmaster.chapter.one.bag.Bag;
11+
import ca.mcmaster.chapter.one.bag.ListBag;
12+
13+
public class EdgeWeightedGraph {
14+
private final int V; //Number of vertex
15+
private int E; //Number of edge
16+
private final Bag<Edge>[] adj; //Create bag for saving edges of vertex
17+
@SuppressWarnings("unchecked")
18+
public EdgeWeightedGraph(int V){
19+
this.V = V;
20+
this.E = 0;
21+
adj = new Bag[V];
22+
for(int v = 0; v < V ; v++) adj[v] = new ListBag<>();
23+
}
24+
@SuppressWarnings("unchecked")
25+
public EdgeWeightedGraph(FileInputStream in){
26+
Scanner scanner = null;
27+
try {
28+
scanner = new Scanner(in);
29+
V = scanner.nextInt();
30+
E = 0;
31+
int edgeNum = scanner.nextInt();
32+
adj = new Bag[V];
33+
for(int v = 0; v < V ; v++) adj[v] = new ListBag<>();
34+
for(int e = 0; e < edgeNum; e++) addEdge(new Edge(scanner.nextInt(), scanner.nextInt(), scanner.nextDouble()));
35+
} finally{
36+
scanner.close();
37+
}
38+
}
39+
private void addEdge(Edge e){
40+
int v = e.either();
41+
int w = e.other(v);
42+
adj[v].add(e);
43+
adj[w].add(e);
44+
E++;
45+
}
46+
public int V(){
47+
return this.V;
48+
}
49+
public int E(){
50+
return this.E;
51+
}
52+
public Iterable<Edge> adj(int v){
53+
return adj[v];
54+
}
55+
public Iterable<Edge> edges(){
56+
List<Edge> res = new ArrayList<Edge>();
57+
for(int v = 0; v < V; v++)
58+
for(Edge e : adj[v]){
59+
if(v > e.other(v)) res.add(e);
60+
}
61+
return res;
62+
}
63+
public static void main(String[] args) throws FileNotFoundException {
64+
FileInputStream is = new FileInputStream(new File("src/ca/mcmaster/chapter/four/graph/mstree/tinyEWG.txt"));
65+
EdgeWeightedGraph graph = new EdgeWeightedGraph(is);
66+
Iterable<Edge> edges = graph.edges();
67+
for(Edge e : edges)
68+
System.out.println(e.toString());
69+
System.out.println(graph.E);
70+
}
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
8
2+
16
3+
4 5 0.35
4+
4 7 0.37
5+
5 7 0.28
6+
0 7 0.16
7+
1 5 0.32
8+
0 4 0.38
9+
2 3 0.17
10+
1 7 0.19
11+
0 2 0.26
12+
1 2 0.36
13+
1 3 0.29
14+
2 7 0.34
15+
6 2 0.40
16+
3 6 0.52
17+
6 0 0.58
18+
6 4 0.93
+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package ca.mcmaster.chapter.four.graph.mstree;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.FileNotFoundException;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
import java.util.Scanner;
9+
10+
import ca.mcmaster.chapter.one.bag.Bag;
11+
import ca.mcmaster.chapter.one.bag.ListBag;
12+
13+
public class EdgeWeightedGraph {
14+
private final int V; //Number of vertex
15+
private int E; //Number of edge
16+
private final Bag<Edge>[] adj; //Create bag for saving edges of vertex
17+
@SuppressWarnings("unchecked")
18+
public EdgeWeightedGraph(int V){
19+
this.V = V;
20+
this.E = 0;
21+
adj = new Bag[V];
22+
for(int v = 0; v < V ; v++) adj[v] = new ListBag<>();
23+
}
24+
@SuppressWarnings("unchecked")
25+
public EdgeWeightedGraph(FileInputStream in){
26+
Scanner scanner = null;
27+
try {
28+
scanner = new Scanner(in);
29+
V = scanner.nextInt();
30+
E = 0;
31+
int edgeNum = scanner.nextInt();
32+
adj = new Bag[V];
33+
for(int v = 0; v < V ; v++) adj[v] = new ListBag<>();
34+
for(int e = 0; e < edgeNum; e++) addEdge(new Edge(scanner.nextInt(), scanner.nextInt(), scanner.nextDouble()));
35+
} finally{
36+
scanner.close();
37+
}
38+
}
39+
private void addEdge(Edge e){
40+
int v = e.either();
41+
int w = e.other(v);
42+
adj[v].add(e);
43+
adj[w].add(e);
44+
E++;
45+
}
46+
public int V(){
47+
return this.V;
48+
}
49+
public int E(){
50+
return this.E;
51+
}
52+
public Iterable<Edge> adj(int v){
53+
return adj[v];
54+
}
55+
public Iterable<Edge> edges(){
56+
List<Edge> res = new ArrayList<Edge>();
57+
for(int v = 0; v < V; v++)
58+
for(Edge e : adj[v]){
59+
if(v > e.other(v)) res.add(e);
60+
}
61+
return res;
62+
}
63+
public static void main(String[] args) throws FileNotFoundException {
64+
FileInputStream is = new FileInputStream(new File("src/ca/mcmaster/chapter/four/graph/mstree/tinyEWG.txt"));
65+
EdgeWeightedGraph graph = new EdgeWeightedGraph(is);
66+
Iterable<Edge> edges = graph.edges();
67+
for(Edge e : edges)
68+
System.out.println(e.toString());
69+
System.out.println(graph.E);
70+
}
71+
}

DataStructrue/Graph/MinimumSpanningTrees.md

+55-1
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,65 @@ public class Edge implements Comparable<Edge> {
5757
}
5858
public int other(int v){
5959
if(v == this.v) return w;
60-
else return v;
60+
else return this.v;
6161
}
6262
@Override
6363
public String toString() {
6464
return String.format("%d-%d %.2f", v, w, weight);
6565
}
6666
}
67+
```
68+
69+
### 加权无向图
70+
```Java
71+
public class EdgeWeightedGraph {
72+
private final int V; //Number of vertex
73+
private int E; //Number of edge
74+
private final Bag<Edge>[] adj; //Create bag for saving edges of vertex
75+
@SuppressWarnings("unchecked")
76+
public EdgeWeightedGraph(int V){
77+
this.V = V;
78+
this.E = 0;
79+
adj = new Bag[V];
80+
for(int v = 0; v < V ; v++) adj[v] = new ListBag<>();
81+
}
82+
@SuppressWarnings("unchecked")
83+
public EdgeWeightedGraph(FileInputStream in){
84+
Scanner scanner = null;
85+
try {
86+
scanner = new Scanner(in);
87+
V = scanner.nextInt();
88+
E = 0;
89+
int edgeNum = scanner.nextInt();
90+
adj = new Bag[V];
91+
for(int v = 0; v < V ; v++) adj[v] = new ListBag<>();
92+
for(int e = 0; e < edgeNum; e++) addEdge(new Edge(scanner.nextInt(), scanner.nextInt(), scanner.nextDouble()));
93+
} finally{
94+
scanner.close();
95+
}
96+
}
97+
private void addEdge(Edge e){
98+
int v = e.either();
99+
int w = e.other(v);
100+
adj[v].add(e);
101+
adj[w].add(e);
102+
E++;
103+
}
104+
public int V(){
105+
return this.V;
106+
}
107+
public int E(){
108+
return this.E;
109+
}
110+
public Iterable<Edge> adj(int v){
111+
return adj[v];
112+
}
113+
public Iterable<Edge> edges(){
114+
List<Edge> res = new ArrayList<Edge>();
115+
for(int v = 0; v < V; v++)
116+
for(Edge e : adj[v])
117+
if(v > e.other(v)) res.add(e); //每条边均会被遍历两次,通过一次判断避免重复性。
118+
return res;
119+
}
120+
}
67121
```

0 commit comments

Comments
 (0)