forked from lightningnetwork/lnd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lnrpc+autopilot: add graph diameter calculation
* adds a brute force computation of the diameter * adds a more efficient calculation of the diameter
- Loading branch information
1 parent
4d9a05c
commit 52d56a8
Showing
4 changed files
with
273 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package autopilot | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var ( | ||
testShortestPathLengths = map[int]uint32{ | ||
0: 0, | ||
1: 1, | ||
2: 1, | ||
3: 1, | ||
4: 2, | ||
5: 2, | ||
6: 3, | ||
7: 3, | ||
8: 4, | ||
} | ||
testNodeEccentricities = map[int]uint32{ | ||
0: 4, | ||
1: 5, | ||
2: 4, | ||
3: 3, | ||
4: 3, | ||
5: 3, | ||
6: 4, | ||
7: 4, | ||
8: 5, | ||
} | ||
) | ||
|
||
// NewTestSimpleGraph is a helper that generates a SimpleGraph from a test | ||
// graph description. | ||
// Assumes that the graph description is internally consistent, i.e. edges are | ||
// not repeatedly defined. | ||
func NewTestSimpleGraph(graph testGraphDesc) SimpleGraph { | ||
// We convert the test graph description into an adjacency list. | ||
adjList := make([][]int, graph.nodes) | ||
for node, neighbors := range graph.edges { | ||
for _, neighbor := range neighbors { | ||
adjList[node] = append(adjList[node], neighbor) | ||
adjList[neighbor] = append(adjList[neighbor], node) | ||
} | ||
} | ||
|
||
return SimpleGraph{Adj: adjList} | ||
} | ||
|
||
func TestShortestPathLengths(t *testing.T) { | ||
simpleGraph := NewTestSimpleGraph(centralityTestGraph) | ||
|
||
// Test the shortest path lengths from node 0 to all other nodes. | ||
shortestPathLengths := simpleGraph.shortestPathLengths(0) | ||
require.Equal(t, shortestPathLengths, testShortestPathLengths) | ||
} | ||
|
||
func TestEccentricities(t *testing.T) { | ||
simpleGraph := NewTestSimpleGraph(centralityTestGraph) | ||
|
||
// Test the node eccentricities for all nodes. | ||
nodes := make([]int, len(simpleGraph.Adj)) | ||
for a := range nodes { | ||
nodes[a] = a | ||
} | ||
nodeEccentricities := simpleGraph.nodeEccentricities(nodes) | ||
require.Equal(t, nodeEccentricities, testNodeEccentricities) | ||
} | ||
|
||
func TestDiameterExact(t *testing.T) { | ||
simpleGraph := NewTestSimpleGraph(centralityTestGraph) | ||
|
||
// Test the diameter in a brute-force manner. | ||
diameter := simpleGraph.Diameter() | ||
require.Equal(t, uint32(5), diameter) | ||
} | ||
|
||
func TestDiameterCutoff(t *testing.T) { | ||
simpleGraph := NewTestSimpleGraph(centralityTestGraph) | ||
|
||
// Test the diameter by cutting out the inside of the graph. | ||
diameter := simpleGraph.DiameterRadialCutoff() | ||
require.Equal(t, uint32(5), diameter) | ||
} | ||
|
||
func BenchmarkShortestPathOpt(b *testing.B) { | ||
// TODO: a method that generates a huge graph is needed | ||
simpleGraph := NewTestSimpleGraph(centralityTestGraph) | ||
|
||
for n := 0; n < b.N; n++ { | ||
_ = simpleGraph.shortestPathLengths(0) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters