-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample_test.go
More file actions
110 lines (96 loc) · 2.81 KB
/
Copy pathExample_test.go
File metadata and controls
110 lines (96 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 MaIII Themd
package dijkstra_test
import (
"fmt"
dijkstra "github.com/ultramcu/go-dijkstra"
)
func ExampleStGraph_DijkstraSearch() {
var g dijkstra.StGraph
// A directed graph:
//
// A --1--> B --2--> C
// | ^
// +---------5--------+ (direct A->C, but slower)
g.VertexAdd("A", 0, 0, 0,
dijkstra.StEdge{ToVertexName: "B", Weight: 1, IsOneWay: true},
dijkstra.StEdge{ToVertexName: "C", Weight: 5, IsOneWay: true},
)
g.VertexAdd("B", 1, 0, 0,
dijkstra.StEdge{ToVertexName: "C", Weight: 2, IsOneWay: true},
)
g.VertexAdd("C", 2, 0, 0)
ok, path := g.DijkstraSearch("A", "C")
if !ok {
fmt.Println("no path")
return
}
for _, p := range path {
fmt.Printf("%s (cost %.0f)\n", p.Name, p.Cost)
}
// Output:
// A (cost 0)
// B (cost 1)
// C (cost 3)
}
// ExampleStGraph_AStarSearch finds the same shortest path as
// DijkstraSearch but goal-directed, using the built-in straight-line
// heuristic over the vertices' (X, Y) coordinates.
func ExampleStGraph_AStarSearch() {
var g dijkstra.StGraph
// Vertices on a line at x = 0,1,2,3. Edge weights equal the
// straight-line distance, so the built-in heuristic is admissible.
// A direct S->T hop exists but is slower than going via A, B.
g.VertexAdd("S", 0, 0, 0,
dijkstra.StEdge{ToVertexName: "A", Weight: 1, IsOneWay: true},
dijkstra.StEdge{ToVertexName: "T", Weight: 5, IsOneWay: true},
)
g.VertexAdd("A", 1, 0, 0, dijkstra.StEdge{ToVertexName: "B", Weight: 1, IsOneWay: true})
g.VertexAdd("B", 2, 0, 0, dijkstra.StEdge{ToVertexName: "T", Weight: 1, IsOneWay: true})
g.VertexAdd("T", 3, 0, 0)
ok, path := g.AStarSearch("S", "T")
if !ok {
fmt.Println("no path")
return
}
for _, p := range path {
fmt.Printf("%s (cost %.0f)\n", p.Name, p.Cost)
}
// Output:
// S (cost 0)
// A (cost 1)
// B (cost 2)
// T (cost 3)
}
// ExampleStGraph_VertexBLock shows dynamic blocking: a vertex blocked at
// runtime makes the next search reroute around it.
func ExampleStGraph_VertexBLock() {
var g dijkstra.StGraph
g.VertexAdd("A", 0, 0, 0,
dijkstra.StEdge{ToVertexName: "C", Weight: 1, IsOneWay: true},
dijkstra.StEdge{ToVertexName: "B", Weight: 1, IsOneWay: true},
)
g.VertexAdd("B", 0, 0, 0, dijkstra.StEdge{ToVertexName: "D", Weight: 5, IsOneWay: true})
g.VertexAdd("C", 0, 0, 0, dijkstra.StEdge{ToVertexName: "D", Weight: 1, IsOneWay: true})
g.VertexAdd("D", 0, 0, 0)
show := func(path []dijkstra.StPath) {
out := ""
for i, p := range path {
if i > 0 {
out += " -> "
}
out += p.Name
}
fmt.Println(out)
}
// Cheapest route is via C.
_, path := g.DijkstraSearch("A", "D")
show(path)
// Block C at runtime; the next search reroutes via B.
g.VertexBLock("C")
_, path = g.DijkstraSearch("A", "D")
show(path)
// Output:
// A -> C -> D
// A -> B -> D
}