forked from jieli4970/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEdgeWeighGraph.java
More file actions
42 lines (30 loc) · 764 Bytes
/
EdgeWeighGraph.java
File metadata and controls
42 lines (30 loc) · 764 Bytes
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
import java.util.HashSet;
import java.util.Set;
public class EdgeWeighGraph {
private int V; // 顶点总数
private Set<Edge>[] adj; // 邻接表
public EdgeWeighGraph(int v) {
this.V = v;
adj = new Set[V];
for (int i = 0; i < V; i++) {
adj[i] = new HashSet<>();
}
}
public void addEdge(Edge edge) {
adj[edge.getV()].add(edge);
adj[edge.getW()].add(edge);
}
public int getV() {
return V;
}
public Set<Edge> adj(int v) {
return adj[v];
}
public Set<Edge> edges() {
Set<Edge> edges = new HashSet<>();
for (int i = 0; i < V; i++) {
edges.addAll(adj[i]);
}
return edges;
}
}