• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 15561477203

10 Jun 2025 01:54PM UTC coverage: 58.351% (-10.1%) from 68.487%
15561477203

Pull #9356

github

web-flow
Merge 6440b25db into c6d6d4c0b
Pull Request #9356: lnrpc: add incoming/outgoing channel ids filter to forwarding history request

33 of 36 new or added lines in 2 files covered. (91.67%)

28366 existing lines in 455 files now uncovered.

97715 of 167461 relevant lines covered (58.35%)

1.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

3.64
/autopilot/betweenness_centrality.go
1
package autopilot
2

3
import (
4
        "context"
5
        "fmt"
6
        "sync"
7
)
8

9
// stack is a simple int stack to help with readability of Brandes'
10
// betweenness centrality implementation below.
11
type stack struct {
12
        stack []int
13
}
14

UNCOV
15
func (s *stack) push(v int) {
×
UNCOV
16
        s.stack = append(s.stack, v)
×
UNCOV
17
}
×
18

UNCOV
19
func (s *stack) top() int {
×
UNCOV
20
        return s.stack[len(s.stack)-1]
×
UNCOV
21
}
×
22

UNCOV
23
func (s *stack) pop() {
×
UNCOV
24
        s.stack = s.stack[:len(s.stack)-1]
×
UNCOV
25
}
×
26

UNCOV
27
func (s *stack) empty() bool {
×
UNCOV
28
        return len(s.stack) == 0
×
UNCOV
29
}
×
30

31
// queue is a simple int queue to help with readability of Brandes'
32
// betweenness centrality implementation below.
33
type queue struct {
34
        queue []int
35
}
36

UNCOV
37
func (q *queue) push(v int) {
×
UNCOV
38
        q.queue = append(q.queue, v)
×
UNCOV
39
}
×
40

UNCOV
41
func (q *queue) front() int {
×
UNCOV
42
        return q.queue[0]
×
UNCOV
43
}
×
44

UNCOV
45
func (q *queue) pop() {
×
UNCOV
46
        q.queue = q.queue[1:]
×
UNCOV
47
}
×
48

UNCOV
49
func (q *queue) empty() bool {
×
UNCOV
50
        return len(q.queue) == 0
×
UNCOV
51
}
×
52

53
// BetweennessCentrality is a NodeMetric that calculates node betweenness
54
// centrality using Brandes' algorithm. Betweenness centrality for each node
55
// is the number of shortest paths passing through that node, not counting
56
// shortest paths starting or ending at that node. This is a useful metric
57
// to measure control of individual nodes over the whole network.
58
type BetweennessCentrality struct {
59
        // workers number of goroutines are used to parallelize
60
        // centrality calculation.
61
        workers int
62

63
        // centrality stores original (not normalized) centrality values for
64
        // each node in the graph.
65
        centrality map[NodeID]float64
66

67
        // min is the minimum centrality in the graph.
68
        min float64
69

70
        // max is the maximum centrality in the graph.
71
        max float64
72
}
73

74
// NewBetweennessCentralityMetric creates a new BetweennessCentrality instance.
75
// Users can specify the number of workers to use for calculating centrality.
76
func NewBetweennessCentralityMetric(workers int) (*BetweennessCentrality, error) {
3✔
77
        // There should be at least one worker.
3✔
78
        if workers < 1 {
3✔
UNCOV
79
                return nil, fmt.Errorf("workers must be positive")
×
UNCOV
80
        }
×
81
        return &BetweennessCentrality{
3✔
82
                workers: workers,
3✔
83
        }, nil
3✔
84
}
85

86
// Name returns the name of the metric.
87
func (bc *BetweennessCentrality) Name() string {
×
88
        return "betweenness_centrality"
×
89
}
×
90

91
// betweennessCentrality is the core of Brandes' algorithm.
92
// We first calculate the shortest paths from the start node s to all other
93
// nodes with BFS, then update the betweenness centrality values by using
94
// Brandes' dependency trick.
95
// For detailed explanation please read:
96
// https://www.cl.cam.ac.uk/teaching/1617/MLRD/handbook/brandes.html
UNCOV
97
func betweennessCentrality(g *SimpleGraph, s int, centrality []float64) {
×
UNCOV
98
        // pred[w] is the list of nodes that immediately precede w on a
×
UNCOV
99
        // shortest path from s to t for each node t.
×
UNCOV
100
        pred := make([][]int, len(g.Nodes))
×
UNCOV
101

×
UNCOV
102
        // sigma[t] is the number of shortest paths between nodes s and t
×
UNCOV
103
        // for each node t.
×
UNCOV
104
        sigma := make([]int, len(g.Nodes))
×
UNCOV
105
        sigma[s] = 1
×
UNCOV
106

×
UNCOV
107
        // dist[t] holds the distance between s and t for each node t.
×
UNCOV
108
        // We initialize this to -1 (meaning infinity) for each t != s.
×
UNCOV
109
        dist := make([]int, len(g.Nodes))
×
UNCOV
110
        for i := range dist {
×
UNCOV
111
                dist[i] = -1
×
UNCOV
112
        }
×
113

UNCOV
114
        dist[s] = 0
×
UNCOV
115

×
UNCOV
116
        var (
×
UNCOV
117
                st stack
×
UNCOV
118
                q  queue
×
UNCOV
119
        )
×
UNCOV
120
        q.push(s)
×
UNCOV
121

×
UNCOV
122
        // BFS to calculate the shortest paths (sigma and pred)
×
UNCOV
123
        // from s to t for each node t.
×
UNCOV
124
        for !q.empty() {
×
UNCOV
125
                v := q.front()
×
UNCOV
126
                q.pop()
×
UNCOV
127
                st.push(v)
×
UNCOV
128

×
UNCOV
129
                for _, w := range g.Adj[v] {
×
UNCOV
130
                        // If distance from s to w is infinity (-1)
×
UNCOV
131
                        // then set it and enqueue w.
×
UNCOV
132
                        if dist[w] < 0 {
×
UNCOV
133
                                dist[w] = dist[v] + 1
×
UNCOV
134
                                q.push(w)
×
UNCOV
135
                        }
×
136

137
                        // If w is on a shortest path the update
138
                        // sigma and add v to w's predecessor list.
UNCOV
139
                        if dist[w] == dist[v]+1 {
×
UNCOV
140
                                sigma[w] += sigma[v]
×
UNCOV
141
                                pred[w] = append(pred[w], v)
×
UNCOV
142
                        }
×
143
                }
144
        }
145

146
        // delta[v] is the ratio of the shortest paths between s and t that go
147
        // through v and the total number of shortest paths between s and t.
148
        // If we have delta then the betweenness centrality is simply the sum
149
        // of delta[w] for each w != s.
UNCOV
150
        delta := make([]float64, len(g.Nodes))
×
UNCOV
151

×
UNCOV
152
        for !st.empty() {
×
UNCOV
153
                w := st.top()
×
UNCOV
154
                st.pop()
×
UNCOV
155

×
UNCOV
156
                // pred[w] is the list of nodes that immediately precede w on a
×
UNCOV
157
                // shortest path from s.
×
UNCOV
158
                for _, v := range pred[w] {
×
UNCOV
159
                        // Update delta using Brandes' equation.
×
UNCOV
160
                        delta[v] += (float64(sigma[v]) / float64(sigma[w])) * (1.0 + delta[w])
×
UNCOV
161
                }
×
162

UNCOV
163
                if w != s {
×
UNCOV
164
                        // As noted above centrality is simply the sum
×
UNCOV
165
                        // of delta[w] for each w != s.
×
UNCOV
166
                        centrality[w] += delta[w]
×
UNCOV
167
                }
×
168
        }
169
}
170

171
// Refresh recalculates and stores centrality values.
172
func (bc *BetweennessCentrality) Refresh(ctx context.Context,
UNCOV
173
        graph ChannelGraph) error {
×
UNCOV
174

×
UNCOV
175
        cache, err := NewSimpleGraph(ctx, graph)
×
UNCOV
176
        if err != nil {
×
177
                return err
×
178
        }
×
179

UNCOV
180
        var wg sync.WaitGroup
×
UNCOV
181
        work := make(chan int)
×
UNCOV
182
        partials := make(chan []float64, bc.workers)
×
UNCOV
183

×
UNCOV
184
        // Each worker will compute a partial result.
×
UNCOV
185
        // This partial result is a sum of centrality updates
×
UNCOV
186
        // on roughly N / workers nodes.
×
UNCOV
187
        worker := func() {
×
UNCOV
188
                defer wg.Done()
×
UNCOV
189
                partial := make([]float64, len(cache.Nodes))
×
UNCOV
190

×
UNCOV
191
                // Consume the next node, update centrality
×
UNCOV
192
                // parital to avoid unnecessary synchronization.
×
UNCOV
193
                for node := range work {
×
UNCOV
194
                        betweennessCentrality(cache, node, partial)
×
UNCOV
195
                }
×
UNCOV
196
                partials <- partial
×
197
        }
198

199
        // Now start the N workers.
UNCOV
200
        wg.Add(bc.workers)
×
UNCOV
201
        for i := 0; i < bc.workers; i++ {
×
UNCOV
202
                go worker()
×
UNCOV
203
        }
×
204

205
        // Distribute work amongst workers.
206
        // Should be fair when the graph is sufficiently large.
UNCOV
207
        for node := range cache.Nodes {
×
UNCOV
208
                work <- node
×
UNCOV
209
        }
×
210

UNCOV
211
        close(work)
×
UNCOV
212
        wg.Wait()
×
UNCOV
213
        close(partials)
×
UNCOV
214

×
UNCOV
215
        // Collect and sum partials for final result.
×
UNCOV
216
        centrality := make([]float64, len(cache.Nodes))
×
UNCOV
217
        for partial := range partials {
×
UNCOV
218
                for i := 0; i < len(partial); i++ {
×
UNCOV
219
                        centrality[i] += partial[i]
×
UNCOV
220
                }
×
221
        }
222

223
        // Get min/max to be able to normalize
224
        // centrality values between 0 and 1.
UNCOV
225
        bc.min = 0
×
UNCOV
226
        bc.max = 0
×
UNCOV
227
        if len(centrality) > 0 {
×
UNCOV
228
                for _, v := range centrality {
×
UNCOV
229
                        if v < bc.min {
×
230
                                bc.min = v
×
UNCOV
231
                        } else if v > bc.max {
×
UNCOV
232
                                bc.max = v
×
UNCOV
233
                        }
×
234
                }
235
        }
236

237
        // Divide by two as this is an undirected graph.
UNCOV
238
        bc.min /= 2.0
×
UNCOV
239
        bc.max /= 2.0
×
UNCOV
240

×
UNCOV
241
        bc.centrality = make(map[NodeID]float64)
×
UNCOV
242
        for u, value := range centrality {
×
UNCOV
243
                // Divide by two as this is an undirected graph.
×
UNCOV
244
                bc.centrality[cache.Nodes[u]] = value / 2.0
×
UNCOV
245
        }
×
246

UNCOV
247
        return nil
×
248
}
249

250
// GetMetric returns the current centrality values for each node indexed
251
// by node id.
UNCOV
252
func (bc *BetweennessCentrality) GetMetric(normalize bool) map[NodeID]float64 {
×
UNCOV
253
        // Normalization factor.
×
UNCOV
254
        var z float64
×
UNCOV
255
        if (bc.max - bc.min) > 0 {
×
UNCOV
256
                z = 1.0 / (bc.max - bc.min)
×
UNCOV
257
        }
×
258

UNCOV
259
        centrality := make(map[NodeID]float64)
×
UNCOV
260
        for k, v := range bc.centrality {
×
UNCOV
261
                if normalize {
×
UNCOV
262
                        v = (v - bc.min) * z
×
UNCOV
263
                }
×
UNCOV
264
                centrality[k] = v
×
265
        }
266

UNCOV
267
        return centrality
×
268
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc