| 1 |
package graph |
| 2 |
|
| 3 |
import ( |
| 4 |
"sort" |
| 5 |
"strconv" |
| 6 |
) |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
type Immutable struct { |
| 14 |
|
| 15 |
edges [][]neighbor |
| 16 |
stats Stats |
| 17 |
} |
| 18 |
|
| 19 |
type neighbor struct { |
| 20 |
vertex int |
| 21 |
cost int64 |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
func Sort(g Iterator) *Immutable { |
| 27 |
if g, ok := g.(*Immutable); ok { |
| 28 |
return g |
| 29 |
} |
| 30 |
return build(g, false) |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
func Transpose(g Iterator) *Immutable { |
| 38 |
return build(g, true) |
| 39 |
} |
| 40 |
|
| 41 |
func build(g Iterator, transpose bool) *Immutable { |
| 42 |
n := g.Order() |
| 43 |
h := &Immutable{edges: make([][]neighbor, n)} |
| 44 |
for v := range h.edges { |
| 45 |
g.Visit(v, func(w int, c int64) (skip bool) { |
| 46 |
if w < 0 || w >= n { |
| 47 |
panic("vertex out of range: " + strconv.Itoa(w)) |
| 48 |
} |
| 49 |
if transpose { |
| 50 |
h.edges[w] = append(h.edges[w], neighbor{v, c}) |
| 51 |
} else { |
| 52 |
h.edges[v] = append(h.edges[v], neighbor{w, c}) |
| 53 |
} |
| 54 |
return |
| 55 |
}) |
| 56 |
sort.Slice(h.edges[v], func(i, j int) bool { |
| 57 |
if e := h.edges[v]; e[i].vertex == e[j].vertex { |
| 58 |
return e[i].cost < e[j].cost |
| 59 |
} else { |
| 60 |
return e[i].vertex < e[j].vertex |
| 61 |
} |
| 62 |
}) |
| 63 |
} |
| 64 |
for v, neighbors := range h.edges { |
| 65 |
if len(neighbors) == 0 { |
| 66 |
h.stats.Isolated++ |
| 67 |
} |
| 68 |
prev := -1 |
| 69 |
for _, e := range neighbors { |
| 70 |
w, c := e.vertex, e.cost |
| 71 |
if v == w { |
| 72 |
h.stats.Loops++ |
| 73 |
} |
| 74 |
if c != 0 { |
| 75 |
h.stats.Weighted++ |
| 76 |
} |
| 77 |
if w == prev { |
| 78 |
h.stats.Multi++ |
| 79 |
} else { |
| 80 |
h.stats.Size++ |
| 81 |
prev = w |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
return h |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
func (g *Immutable) Visit(v int, do func(w int, c int64) bool) bool { |
| 94 |
for _, e := range g.edges[v] { |
| 95 |
if do(e.vertex, e.cost) { |
| 96 |
return true |
| 97 |
} |
| 98 |
} |
| 99 |
return false |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
w. |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
func (g *Immutable) VisitFrom(v int, a int, do func(w int, c int64) bool) bool { |
| 108 |
neighbors := g.edges[v] |
| 109 |
n := len(neighbors) |
| 110 |
i := sort.Search(n, func(i int) bool { return a <= neighbors[i].vertex }) |
| 111 |
for ; i < n; i++ { |
| 112 |
e := neighbors[i] |
| 113 |
if do(e.vertex, e.cost) { |
| 114 |
return true |
| 115 |
} |
| 116 |
} |
| 117 |
return false |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
func (g *Immutable) String() string { |
| 122 |
return String(g) |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
func (g *Immutable) Order() int { |
| 127 |
return len(g.edges) |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
func (g *Immutable) Edge(v, w int) bool { |
| 132 |
if v < 0 || v >= len(g.edges) { |
| 133 |
return false |
| 134 |
} |
| 135 |
edges := g.edges[v] |
| 136 |
n := len(edges) |
| 137 |
i := sort.Search(n, func(i int) bool { return w <= edges[i].vertex }) |
| 138 |
return i < n && w == edges[i].vertex |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
func (g *Immutable) Degree(v int) int { |
| 143 |
return len(g.edges[v]) |
| 144 |
} |