-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathedge.go
52 lines (44 loc) · 812 Bytes
/
edge.go
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
package algo
import "fmt"
// Edge is a ADT for EdgeWeightedGraph
type Edge struct {
v, w int
weight float32
}
// NewEdge ...
func NewEdge(v, w int, weight float32) *Edge {
return &Edge{v, w, weight}
}
// Weight ...
func (e *Edge) Weight() float32 {
return e.weight
}
// Either ...
func (e *Edge) Either() int {
return e.v
}
// Other ...
func (e *Edge) Other(v int) int {
if v == e.v {
return e.w
} else if v == e.w {
return e.v
} else {
panic("invalid vertex")
}
}
// String ...
func (e *Edge) String() string {
return fmt.Sprintf("%d-%d %.5f", e.v, e.w, e.weight)
}
// CompareTo implements PQItem interface
func (e *Edge)CompareTo(other interface{})int{
ee := other.(*Edge)
if e.weight > ee.weight {
return 1
} else if e.weight < ee.weight {
return -1
} else {
return 0
}
}