327768f4ad
Use [33]byte for graph vertex representation. Delete unneeded stuff: 1. DeepEqual for graph comparison 2. EdgePath 3. 2-thread BFS 4. Table transfer messages and neighborhood radius 5. Beacons Refactor: 1. Change ID to Vertex 2. Test use table driven approach 3. Add comments 4. Make graph internal representation private 5. Use wire.OutPoint as EdgeId 6. Decouple routing messages from routing implementation 7. Delete Async methods 8. Delete unneeded channels and priority buffer from manager 9. Delete unneeded interfaces in internal graph realisation 10. Renamed ID to Vertex
40 lines
864 B
Go
40 lines
864 B
Go
// Copyright (c) 2016 Bitfury Group Limited
|
|
// Distributed under the MIT software license, see the accompanying
|
|
// file LICENSE or http://www.opensource.org/licenses/mit-license.php
|
|
|
|
package graph
|
|
|
|
import "errors"
|
|
|
|
func Path(v1, v2 Vertex, parent map[Vertex]Vertex) ([]Vertex, error) {
|
|
path, err := ReversePath(v1, v2, parent)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
Reverse(path)
|
|
return path, nil
|
|
}
|
|
|
|
func ReversePath(v1, v2 Vertex, parent map[Vertex]Vertex) ([]Vertex, error) {
|
|
path := []Vertex{v2}
|
|
for v2 != v1 {
|
|
if v2 == parent[v2] {
|
|
return nil, CyclicDataError
|
|
}
|
|
var ok bool
|
|
v2, ok = parent[v2]
|
|
if !ok {
|
|
return nil, errors.New("Invalid key")
|
|
}
|
|
path = append(path, v2)
|
|
}
|
|
return path, nil
|
|
}
|
|
|
|
func Reverse(path []Vertex) {
|
|
length := len(path)
|
|
for i := 0; i < length/2; i++ {
|
|
path[i], path[length-1-i] = path[length-1-i], path[i]
|
|
}
|
|
}
|