-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathWeightedGraph.java
131 lines (105 loc) · 3.51 KB
/
WeightedGraph.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import java.util.TreeMap;
import java.util.Scanner;
/// 暂时只支持无向带权图
public class WeightedGraph implements Cloneable{
private int V;
private int E;
private TreeMap<Integer, Integer>[] adj;
public WeightedGraph(String filename){
File file = new File(filename);
try(Scanner scanner = new Scanner(file)){
V = scanner.nextInt();
if(V < 0) throw new IllegalArgumentException("V must be non-negative");
adj = new TreeMap[V];
for(int i = 0; i < V; i ++)
adj[i] = new TreeMap<Integer, Integer>();
E = scanner.nextInt();
if(E < 0) throw new IllegalArgumentException("E must be non-negative");
for(int i = 0; i < E; i ++){
int a = scanner.nextInt();
validateVertex(a);
int b = scanner.nextInt();
validateVertex(b);
int v = scanner.nextInt();
if(a == b) throw new IllegalArgumentException("Self Loop is Detected!");
if(adj[a].containsKey(b)) throw new IllegalArgumentException("Parallel Edges are Detected!");
adj[a].put(b, v);
adj[b].put(a, v);
}
}
catch(IOException e){
e.printStackTrace();
}
}
public void validateVertex(int v){
if(v < 0 || v >= V)
throw new IllegalArgumentException("vertex " + v + "is invalid");
}
public int V(){
return V;
}
public int E(){
return E;
}
public boolean hasEdge(int v, int w){
validateVertex(v);
validateVertex(w);
return adj[v].containsKey(w);
}
public int getWeight(int v, int w){
if(hasEdge(v, w)) return adj[v].get(w);
throw new IllegalArgumentException(String.format("No edge %d-%d", v, w));
}
public Iterable<Integer> adj(int v){
validateVertex(v);
return adj[v].keySet();
}
public int degree(int v){
validateVertex(v);
return adj[v].size();
}
public void removeEdge(int v, int w){
validateVertex(v);
validateVertex(w);
if(adj[v].containsKey(w)) E --;
adj[v].remove(w);
adj[w].remove(v);
}
@Override
public Object clone(){
try{
WeightedGraph cloned = (WeightedGraph) super.clone();
cloned.adj = new TreeMap[V];
for(int v = 0; v < V; v ++){
cloned.adj[v] = new TreeMap<Integer, Integer>();
for(Map.Entry<Integer, Integer> entry: adj[v].entrySet())
cloned.adj[v].put(entry.getKey(), entry.getValue());
}
return cloned;
}
catch (CloneNotSupportedException e){
e.printStackTrace();
}
return null;
}
@Override
public String toString(){
StringBuilder sb = new StringBuilder();
sb.append(String.format("V = %d, E = %d\n", V, E));
for(int v = 0; v < V; v ++){
sb.append(String.format("%d : ", v));
for(Map.Entry<Integer, Integer> entry: adj[v].entrySet())
sb.append(String.format("(%d: %d) ", entry.getKey(), entry.getValue()));
sb.append('\n');
}
return sb.toString();
}
public static void main(String[] args){
WeightedGraph g = new WeightedGraph("g.txt");
System.out.print(g);
}
}