-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWEdge.java
125 lines (111 loc) · 3.31 KB
/
WEdge.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
/** Implementation of an edge class (for graphs), could be directed or not.
*/
/**Makes an edge wieh comparable.
* @param <VT> an input object
*/
public class WEdge<VT> implements Comparable<WEdge> {
/** Starting vertex of an WEdge. */
private GVertex<VT> source;
/** Ending vertex of an edge. */
private GVertex<VT> end;
/** Whether or not the edge is directed. */
private boolean directed;
/**The weight of the edge. */
private double weight;
/** Create an undirected edge.
* @param u the start
* @param v the end
* @param inweight the input weight
*/
public WEdge(GVertex<VT> u, GVertex<VT> v, double inweight) {
this.source = u;
this.end = v;
this.directed = false;
this.weight = inweight;
}
/** Create an edge.
* @param u the start
* @param v the end
* @param dir true if directed, false otherwise
* @param inweight the input weight
*/
public WEdge(GVertex<VT> u, GVertex<VT> v, boolean dir, double inweight) {
this.source = u;
this.end = v;
this.directed = dir;
this.weight = inweight;
}
/** Is the edge directed.
* @return true if yes, false otherwise
*/
public boolean isDirected() {
return this.directed;
}
/** Is a vertex incident to this edge.
* @param v the vertex
* @return true if source or end, false otherwise
*/
public boolean isIncident(GVertex<VT> v) {
return this.source.equals(v) || this.end.equals(v);
}
/** Get the starting endpoint vertex.
* @return the vertex
*/
public GVertex<VT> source() {
return this.source;
}
/** Get the ending endpoint vertex.
* @return the vertex
*/
public GVertex<VT> end() {
return this.end;
}
/** Get the weight of an edge.
* @return the edge weight;
*/
public double weight() {
return this.weight;
}
/** Create a string representation of the edge.
* @return the string as (source,end)
*/
public String toString() {
return "(" + this.source + "," + this.end + "," + this.weight + ")";
}
/** Check if two edges are the same.
* @param other the edge to compare to this
* @return true if directedness and endpoints match, false otherwise
*/
public boolean equals(Object other) {
if (other instanceof WEdge) {
WEdge e = (WEdge) other;
if (this.directed != e.directed) {
return false;
}
if (this.directed) {
return this.source.equals(e.source)
&& this.end.equals(e.end);
} else {
return this.source.equals(e.source)
&& this.end.equals(e.end)
|| this.source.equals(e.end)
&& this.end.equals(e.source);
}
}
return false;
}
/** Make a hashCode based on the toString.
* @return the hashCode
*/
public int hashCode() {
return this.toString().hashCode();
}
@Override // compares two edges based on their weights
public int compareTo(WEdge other) {
if (this.weight < other.weight) {
return -1;
} else {
return 1;
}
}
}