Debian Codegraph @

immutable.go

raw symbols SCIP
1 package graph
2
3 import (
4 "sort"
5 "strconv"
6 )
7
8 // Immutable is a compact representation of an immutable graph.
9 // The implementation uses lists to associate each vertex in the graph
10 // with its adjacent vertices. This makes for fast and predictable
11 // iteration: the Visit method produces its elements by reading
12 // from a fixed sorted precomputed list. This type supports multigraphs.
13 type Immutable struct {
14 // edges[v] is a sorted list of v's neighbors.
15 edges [][]neighbor
16 stats Stats
17 }
18
19 type neighbor struct {
20 vertex int
21 cost int64
22 }
23
24 // Sort returns an immutable copy of g with a Visit method
25 // that returns its neighbors in increasing numerical order.
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 // Transpose returns the transpose graph of g.
34 // The transpose graph has the same set of vertices as g,
35 // but all of the edges are reversed compared to the orientation
36 // of the corresponding edges in g.
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 // Visit calls the do function for each neighbor w of v,
89 // with c equal to the cost of the edge from v to w.
90 // The neighbors are visited in increasing numerical order.
91 // If do returns true, Visit returns immediately,
92 // skipping any remaining neighbors, and returns true.
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 // VisitFrom calls the do function starting from the first neighbor w
103 // for which w ≥ a, with c equal to the cost of the edge from v to w.
104 // The neighbors are then visited in increasing numerical order.
105 // If do returns true, VisitFrom returns immediately,
106 // skipping any remaining neighbors, and returns true.
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 // String returns a string representation of the graph.
121 func (g *Immutable) String() string {
122 return String(g)
123 }
124
125 // Order returns the number of vertices in the graph.
126 func (g *Immutable) Order() int {
127 return len(g.edges)
128 }
129
130 // Edge tells if there is an edge from v to w.
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 // Degree returns the number of outward directed edges from v.
142 func (g *Immutable) Degree(v int) int {
143 return len(g.edges[v])
144 }