-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
211 lines (176 loc) · 4.14 KB
/
main.go
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package main
import (
"bufio"
"fmt"
"os"
"slices"
"sort"
"strconv"
"strings"
"time"
)
type Item struct {
node *Node
prevNode *Node
dist int64
}
type PriorityQueue []Item
var _ sort.Interface = PriorityQueue{}
func (pq *PriorityQueue) Push(item Item) {
*pq = append(*pq, item)
}
func (pq *PriorityQueue) Pop() Item {
item := (*pq)[len(*pq)-1]
*pq = (*pq)[:len(*pq)-1]
return item
}
func (pq PriorityQueue) Len() int { return len(pq) }
func (pq PriorityQueue) Less(i, j int) bool { return pq[i].dist < pq[j].dist }
func (pq PriorityQueue) Swap(i, j int) { pq[i], pq[j] = pq[j], pq[i] }
func NewNode(id string) *Node {
return &Node{
ID: id,
OutEdges: make([]Edge, 0),
}
}
type Node struct {
ID string
OutEdges []Edge
}
func (from *Node) AddEdgeTo(to *Node, weight int64) {
for _, edge := range from.OutEdges {
if edge.To == to {
return
}
}
from.OutEdges = append(from.OutEdges, Edge{from, to, weight})
// to.OutEdges = append(to.OutEdges, Edge{to, from, weight})
}
type Edge struct {
From *Node
To *Node
Weight int64
}
func CreatePriorityQueue(pathMap map[string]Item, seenNodes map[string]struct{}) PriorityQueue {
pq := PriorityQueue{}
for _, item := range pathMap {
if _, ok := seenNodes[item.node.ID]; !ok {
pq.Push(item)
}
}
sort.Sort(pq)
slices.Reverse(pq)
return pq
}
func Dijkstra(start *Node) map[string]Item {
pathMap := map[string]Item{}
seenNodes := map[string]struct{}{}
newItem := Item{
node: start,
prevNode: nil,
dist: 0,
}
pathMap[start.ID] = newItem
frontier := CreatePriorityQueue(pathMap, seenNodes)
for len(frontier) != 0 {
currentItem := frontier.Pop()
for _, edge := range currentItem.node.OutEdges {
nodeDist, seen := pathMap[edge.To.ID]
if seen {
currentDist := currentItem.dist + edge.Weight
if currentDist < nodeDist.dist {
newItem := Item{
node: edge.To,
prevNode: currentItem.node,
dist: currentDist,
}
pathMap[newItem.node.ID] = newItem
}
} else {
newItem := Item{
node: edge.To,
prevNode: currentItem.node,
dist: currentItem.dist + int64(edge.Weight),
}
pathMap[newItem.node.ID] = newItem
}
}
seenNodes[currentItem.node.ID] = struct{}{}
frontier = CreatePriorityQueue(pathMap, seenNodes)
}
return pathMap
}
func main() {
file, err := os.Open("./0081_matrix.txt")
if err != nil {
panic(err)
}
defer file.Close()
start := time.Now()
scanner := bufio.NewScanner(file)
matrix := [][]int64{}
for scanner.Scan() {
line := scanner.Text()
values := strings.Split(line, ",")
row := []int64{}
for _, value := range values {
num, err := strconv.ParseInt(value, 10, 64)
if err != nil {
panic(err)
}
row = append(row, num)
}
matrix = append(matrix, row)
}
// Create nodes:
nodes := map[string]*Node{}
for i, row := range matrix {
for j := range row {
id := fmt.Sprintf("%d-%d", i, j)
node := NewNode(id)
nodes[id] = node
}
}
// Create edges:
for i, row := range matrix {
for j := range row {
id := fmt.Sprintf("%d-%d", i, j)
node := nodes[id]
// Add right edge
rightID := fmt.Sprintf("%d-%d", i, j+1)
rightNode, ok := nodes[rightID]
if ok {
node.AddEdgeTo(rightNode, matrix[i][j+1])
}
// Add down edge
downID := fmt.Sprintf("%d-%d", i+1, j)
downNode, ok := nodes[downID]
if ok {
node.AddEdgeTo(downNode, matrix[i+1][j])
}
}
}
// Add nodeZero
startNode := NewNode("startNode")
firstNode := nodes["0-0"]
startNode.AddEdgeTo(firstNode, matrix[0][0])
target := fmt.Sprintf("%d-%d", len(matrix)-1, len(matrix[0])-1)
pathMat := Dijkstra(startNode)
targetCost := pathMat[target].dist
elapsed := time.Since(start)
fmt.Printf("minimal path sum from the top left to the bottom right\n")
fmt.Printf("by only moving right and down = %v (elapsed = %v)\n", targetCost, elapsed)
}
func GetShortestPath(pathMap map[string]Item, target string) []*Node {
targetItem := pathMap[target]
path := []*Node{}
for {
path = append(path, targetItem.node)
if targetItem.prevNode == nil {
break
}
targetItem = pathMap[targetItem.prevNode.ID]
}
slices.Reverse(path)
return path
}