-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeightedEdgeGraph.java
More file actions
57 lines (47 loc) · 1.34 KB
/
WeightedEdgeGraph.java
File metadata and controls
57 lines (47 loc) · 1.34 KB
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
package Graph;
import java.util.*;
public class WeightedEdgeGraph {
private int V;
private int E;
private ArrayList<WeightedEdge>[] adj;
private double[][] matrix;
public WeightedEdgeGraph(int v) {
this.V = v;
this.E = 0;
adj = (ArrayList<WeightedEdge>[])new ArrayList[v];
for (int i = 0; i < v; i++) {
adj[i] = new ArrayList<WeightedEdge>();
}
matrix = new double[v][v];
for (int i = 0; i < v; i++) {
for (int j = 0; j < v; j++) {
if (i != j) matrix[i][j] = Double.MAX_VALUE;
}
}
}
public int V() {return V;}
public int E() {return E;}
public void addEdge(WeightedEdge e) {
int v = e.either(), w = e.other(v);
adj[v].add(e);
adj[w].add(e);
matrix[v][w] = e.weight();
matrix[w][v] = e.weight();
E++;
}
public Iterable<WeightedEdge> adj(int v) {
return adj[v];
}
public double[][] matrix() {
return matrix;
}
public Iterable<WeightedEdge> edges() {
List<WeightedEdge> list = new ArrayList<WeightedEdge>();
for (int v = 0; v < V; v++) {
for (WeightedEdge e: adj[v]) {
if (e.other(v) > v) list.add(e);
}
}
return list;
}
}