-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathEdge.java
More file actions
44 lines (37 loc) · 1.04 KB
/
Copy pathEdge.java
File metadata and controls
44 lines (37 loc) · 1.04 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
public class Edge implements Comparable<Edge> {
/* A weighted edge */
/* Instance variables */
private int v, w; // vertices
private double weight; //weight of edge
/* Constructor */
public Edge(int v, int w, double weight) {
this.v = v;
this.w = w;
this.weight = weight;
}
/* API: Get a vertex from the edge */
public int either() {
return v;
}
/* API: Get the second vertex */
public int other(int v) {
if (v == this.v) return this.w;
else return this.v;
}
/* Used for comparing edges */
public int compareTo(Edge that) {
// compare based on weights
if (this.weight > that.weight) return +1;
else if (this.weight < that.weight) return -1;
else return 0;
}
/* API: Get the weight of the edge */
public double weight() {
return this.weight;
}
/* String representation of the edge */
public String toString() {
String edge = v + " <-> " + w;
return edge;
}
}