1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TemplesOfCode\NodesAndEdges; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Edge |
9
|
|
|
* @package TemplesOfCode\NodesAndEdges |
10
|
|
|
*/ |
11
|
|
|
class Edge |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var int |
15
|
|
|
*/ |
16
|
|
|
private $v; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var int |
20
|
|
|
*/ |
21
|
|
|
private $w; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var float |
25
|
|
|
*/ |
26
|
|
|
private $weight; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Initializes an edge between vertices $v and $w of the given $weight |
30
|
|
|
* |
31
|
|
|
* @param int $v one vertex |
32
|
|
|
* @param int $w the other vertex |
33
|
|
|
* @param float $weight the weight of this edge |
34
|
|
|
* @throws InvalidArgumentException if either $v or $w is a negative integer or if $weight is NaN |
35
|
|
|
*/ |
36
|
|
|
public function __construct(int $v, int $w, float $weight = 0.0) |
37
|
|
|
{ |
38
|
|
|
// sanity check |
39
|
|
|
if ($v < 0) { |
40
|
|
|
// panic here |
41
|
|
|
throw new InvalidArgumentException("vertex index must be a non-negative integer"); |
42
|
|
|
} |
43
|
|
|
// sanity check |
44
|
|
|
if ($w < 0) { |
45
|
|
|
// panic here |
46
|
|
|
throw new InvalidArgumentException("vertex index must be a non-negative integer"); |
47
|
|
|
} |
48
|
|
|
// sanity check |
49
|
|
|
if (is_nan($weight)) { |
50
|
|
|
// panic here |
51
|
|
|
throw new InvalidArgumentException("Weight is NaN"); |
52
|
|
|
} |
53
|
|
|
// set |
54
|
|
|
$this->v = $v; |
55
|
|
|
// set |
56
|
|
|
$this->w = $w; |
57
|
|
|
// set |
58
|
|
|
$this->weight = $weight; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Returns the weight of this edge. |
63
|
|
|
* |
64
|
|
|
* @return float the weight of this edge |
65
|
|
|
*/ |
66
|
|
|
public function weight() |
67
|
|
|
{ |
68
|
|
|
return $this->weight; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Returns either endpoint of this edge. |
73
|
|
|
* |
74
|
|
|
* @return int either endpoint of this edge |
75
|
|
|
*/ |
76
|
|
|
public function either() |
77
|
|
|
{ |
78
|
|
|
return $this->v; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Returns the endpoint of this edge that is different from the given vertex. |
83
|
|
|
* |
84
|
|
|
* @param int $vertex one endpoint of this edge |
85
|
|
|
* @return int the other endpoint of this edge |
86
|
|
|
*/ |
87
|
|
|
public function other(int $vertex) |
88
|
|
|
{ |
89
|
|
|
// check what side are we on and return the other |
90
|
|
|
if ($vertex == $this->v) { |
91
|
|
|
// return it |
92
|
|
|
return $this->w; |
93
|
|
|
} else if ($vertex == $this->w) { |
94
|
|
|
// return it |
95
|
|
|
return $this->v; |
96
|
|
|
} else { |
97
|
|
|
// no go |
98
|
|
|
throw new InvalidArgumentException("Illegal endpoint"); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Compares two edges by weight. |
104
|
|
|
* |
105
|
|
|
* @param Edge $that the other edge |
106
|
|
|
* @return int a negative integer, zero, or positive integer depending on whether |
107
|
|
|
* the weight of this is less than, equal to, or greater than the |
108
|
|
|
* argument edge |
109
|
|
|
*/ |
110
|
|
|
public function compareTo(Edge $that) |
111
|
|
|
{ |
112
|
|
|
// get |
113
|
|
|
$weight = $this->weight(); |
114
|
|
|
// get |
115
|
|
|
$otherWeight = $that->weight(); |
116
|
|
|
// resolve |
117
|
|
|
if ($weight < $otherWeight) { |
118
|
|
|
// return less than |
119
|
|
|
return -1; |
120
|
|
|
} else if ($weight == $otherWeight){ |
121
|
|
|
// return equal |
122
|
|
|
return 0; |
123
|
|
|
} else { |
124
|
|
|
// return greater than |
125
|
|
|
return 1; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Returns a string representation of this edge. |
131
|
|
|
* |
132
|
|
|
* @return string a string representation of this edge |
133
|
|
|
*/ |
134
|
|
|
public function __toString() |
135
|
|
|
{ |
136
|
|
|
// return formatted |
137
|
|
|
return sprintf("%d-%d %.5f", $this->v, $this->w, $this->weight); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|