-
Notifications
You must be signed in to change notification settings - Fork 0
/
BellmanFord.go
61 lines (51 loc) · 1.42 KB
/
BellmanFord.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
package pathfinding
import (
"dsa-go/structures/graphs"
"errors"
"fmt"
"math"
)
func FindPathWithBellmanFord(g *graphs.WeightedGraph, src int) ([]float64, error) {
// Input validation
if src < 0 || len(g.Nodes) == 0 || g.Nodes[src] == nil {
return nil, errors.New("invalid graph or source node")
}
// Initialize required tables
V := len(g.Nodes)
distanceTable := make([]float64, V)
previousNodeTable := make([]*int, V)
for i := range distanceTable {
distanceTable[i] = math.Inf(1) // Initialize with infinity
previousNodeTable[i] = nil
}
distanceTable[src] = 0
// Step 1: Relax edges V-1 times
for i := 0; i < V-1; i++ {
for u := range g.Nodes {
edges := g.Nodes[u].GetItems()
for _, edge := range edges {
v := edge.Node
weight := edge.Weight
// Relax the edge (u -> v)
if distanceTable[u]+float64(weight) < distanceTable[v] {
distanceTable[v] = distanceTable[u] + float64(weight)
previousNodeTable[v] = &u
}
}
}
}
// Step 2: Check for negative-weight cycles
for u := range g.Nodes {
edges := g.Nodes[u].GetItems()
for _, edge := range edges {
v := edge.Node
weight := edge.Weight
// If an edge can still be relaxed, there is a negative-weight cycle
if distanceTable[u]+float64(weight) < distanceTable[v] {
return nil, errors.New("negative-weight cycle detected")
}
}
}
fmt.Println("Distance Table:", distanceTable)
return distanceTable, nil
}