Debian Codegraph @

mutable.go

raw symbols SCIP
1 package graph
2
3 import (
4 "strconv"
5 )
6
7 const initialMapSize = 4
8
9 // Mutable represents a directed graph with a fixed number
10 // of vertices and weighted edges that can be added or removed.
11 // The implementation uses hash maps to associate each vertex in the graph with
12 // its adjacent vertices. This gives constant time performance for
13 // all basic operations.
14 //
15 type Mutable struct {
16 // The map edges[v] contains the mapping {w:c} if there is an edge
17 // from v to w, and c is the cost assigned to this edge.
18 // The maps may be nil and are allocated as needed.
19 edges []map[int]int64
20 }
21
22 // New constructs a new graph with n vertices, numbered from 0 to n-1, and no edges.
23 func New(n int) *Mutable {
24 return &Mutable{edges: make([]map[int]int64, n)}
25 }
26
27 // Copy returns a copy of g.
28 // If g is a multigraph, any duplicate edges in g will be lost.
29 func Copy(g Iterator) *Mutable {
30 switch g := g.(type) {
31 case *Mutable:
32 return copyMutable(g)
33 case *Immutable:
34 return copyImmutable(g)
35 }
36 n := g.Order()
37 h := New(n)
38 for v := 0; v < n; v++ {
39 g.Visit(v, func(w int, c int64) (skip bool) {
40 h.AddCost(v, w, c)
41 return
42 })
43 }
44 return h
45 }
46
47 func copyMutable(g *Mutable) *Mutable {
48 h := New(g.Order())
49 for v, neighbors := range g.edges {
50 if deg := len(neighbors); deg > 0 {
51 h.edges[v] = make(map[int]int64, deg)
52 for w, c := range neighbors {
53 h.edges[v][w] = c
54 }
55 }
56 }
57 return h
58 }
59
60 func copyImmutable(g *Immutable) *Mutable {
61 h := New(g.Order())
62 for v, neighbors := range g.edges {
63 if deg := len(neighbors); deg > 0 {
64 h.edges[v] = make(map[int]int64, deg)
65 for _, edge := range neighbors {
66 h.edges[v][edge.vertex] = edge.cost
67 }
68 }
69 }
70 return h
71 }
72
73 // String returns a string representation of the graph.
74 func (g *Mutable) String() string {
75 return String(g)
76 }
77
78 // Order returns the number of vertices in the graph.
79 func (g *Mutable) Order() int {
80 return len(g.edges)
81 }
82
83 // Visit calls the do function for each neighbor w of v,
84 // with c equal to the cost of the edge from v to w.
85 // If do returns true, Visit returns immediately,
86 // skipping any remaining neighbors, and returns true.
87 //
88 // The iteration order is not specified and is not guaranteed
89 // to be the same every time.
90 // It is safe to delete, but not to add, edges adjacent to v
91 // during a call to this method.
92 func (g *Mutable) Visit(v int, do func(w int, c int64) bool) bool {
93 for w, c := range g.edges[v] {
94 if do(w, c) {
95 return true
96 }
97 }
98 return false
99 }
100
101 // Degree returns the number of outward directed edges from v.
102 func (g *Mutable) Degree(v int) int {
103 return len(g.edges[v])
104 }
105
106 // Edge tells if there is an edge from v to w.
107 func (g *Mutable) Edge(v, w int) bool {
108 if v < 0 || v >= g.Order() {
109 return false
110 }
111 _, ok := g.edges[v][w]
112 return ok
113 }
114
115 // Cost returns the cost of an edge from v to w, or 0 if no such edge exists.
116 func (g *Mutable) Cost(v, w int) int64 {
117 if v < 0 || v >= g.Order() {
118 return 0
119 }
120 return g.edges[v][w]
121 }
122
123 // Add inserts a directed edge from v to w with zero cost.
124 // It removes the previous cost if this edge already exists.
125 func (g *Mutable) Add(v, w int) {
126 g.AddCost(v, w, 0)
127 }
128
129 // AddCost inserts a directed edge from v to w with cost c.
130 // It overwrites the previous cost if this edge already exists.
131 func (g *Mutable) AddCost(v, w int, c int64) {
132 // Make sure not to break internal state.
133 if w < 0 || w >= len(g.edges) {
134 panic("vertex out of range: " + strconv.Itoa(w))
135 }
136 if g.edges[v] == nil {
137 g.edges[v] = make(map[int]int64, initialMapSize)
138 }
139 g.edges[v][w] = c
140 }
141
142 // AddBoth inserts edges with zero cost between v and w.
143 // It removes the previous costs if these edges already exist.
144 func (g *Mutable) AddBoth(v, w int) {
145 g.AddCost(v, w, 0)
146 if v != w {
147 g.AddCost(w, v, 0)
148 }
149 }
150
151 // AddBothCost inserts edges with cost c between v and w.
152 // It overwrites the previous costs if these edges already exist.
153 func (g *Mutable) AddBothCost(v, w int, c int64) {
154 g.AddCost(v, w, c)
155 if v != w {
156 g.AddCost(w, v, c)
157 }
158 }
159
160 // Delete removes an edge from v to w.
161 func (g *Mutable) Delete(v, w int) {
162 delete(g.edges[v], w)
163 }
164
165 // DeleteBoth removes all edges between v and w.
166 func (g *Mutable) DeleteBoth(v, w int) {
167 g.Delete(v, w)
168 if v != w {
169 g.Delete(w, v)
170 }
171 }