-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjohnson.go
207 lines (175 loc) · 5.15 KB
/
johnson.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
package main
import (
"container/heap"
"fmt"
"math"
)
// Edge represents a directed edge in the graph.
type Edge struct {
From, To int
Weight float64
}
// Graph represents a directed graph with a list of edges and vertices.
type Graph struct {
Vertices int
Edges []Edge
}
// NewGraph creates a new graph with the specified number of vertices.
func NewGraph(vertices int) *Graph {
return &Graph{
Vertices: vertices,
Edges: []Edge{},
}
}
// AddEdge adds a directed edge to the graph.
func (g *Graph) AddEdge(from, to int, weight float64) {
g.Edges = append(g.Edges, Edge{From: from, To: to, Weight: weight})
}
// BellmanFord computes shortest paths from a source vertex using the Bellman-Ford algorithm.
// Returns a slice of distances or nil if a negative weight cycle is detected.
func (g *Graph) BellmanFord(source int) []float64 {
distances := make([]float64, g.Vertices)
for i := range distances {
distances[i] = math.Inf(1)
}
distances[source] = 0
// Relax edges |V|-1 times.
for i := 0; i < g.Vertices-1; i++ {
for _, edge := range g.Edges {
if distances[edge.From]+edge.Weight < distances[edge.To] {
distances[edge.To] = distances[edge.From] + edge.Weight
}
}
}
// Check for negative weight cycles.
for _, edge := range g.Edges {
if distances[edge.From]+edge.Weight < distances[edge.To] {
return nil
}
}
return distances
}
// Dijkstra computes shortest paths from a source vertex using Dijkstra's algorithm.
// Returns a slice of distances.
func (g *Graph) Dijkstra(source int, adjustedWeights map[int]map[int]float64) []float64 {
distances := make([]float64, g.Vertices)
for i := range distances {
distances[i] = math.Inf(1)
}
distances[source] = 0
pq := &PriorityQueue{}
heap.Init(pq)
heap.Push(pq, &Item{vertex: source, priority: 0})
for pq.Len() > 0 {
current := heap.Pop(pq).(*Item)
u := current.vertex
// Skip if this distance is outdated.
if current.priority > distances[u] {
continue
}
// Relax neighbors.
for v, weight := range adjustedWeights[u] {
if distances[u]+weight < distances[v] {
distances[v] = distances[u] + weight
heap.Push(pq, &Item{vertex: v, priority: distances[v]})
}
}
}
return distances
}
// Johnson computes shortest paths between all pairs of vertices using Johnson's Algorithm.
// Returns a 2D slice of distances or nil if a negative weight cycle is detected.
func (g *Graph) Johnson() [][]float64 {
// Step 1: Add a new vertex connected to all other vertices with zero-weight edges.
extendedGraph := &Graph{
Vertices: g.Vertices + 1,
Edges: append(g.Edges, createZeroWeightEdges(g.Vertices)...),
}
// Step 2: Run Bellman-Ford from the new vertex.
h := extendedGraph.BellmanFord(g.Vertices)
if h == nil {
return nil // Negative weight cycle detected.
}
// Step 3: Reweight the edges to eliminate negative weights.
adjustedWeights := make(map[int]map[int]float64)
for _, edge := range g.Edges {
if _, exists := adjustedWeights[edge.From]; !exists {
adjustedWeights[edge.From] = make(map[int]float64)
}
adjustedWeights[edge.From][edge.To] = edge.Weight + h[edge.From] - h[edge.To]
}
// Step 4: Run Dijkstra for each vertex.
allPairsDistances := make([][]float64, g.Vertices)
for u := 0; u < g.Vertices; u++ {
shortestFromU := g.Dijkstra(u, adjustedWeights)
allPairsDistances[u] = make([]float64, g.Vertices)
for v := 0; v < g.Vertices; v++ {
// Adjust back to original weights.
if shortestFromU[v] != math.Inf(1) {
allPairsDistances[u][v] = shortestFromU[v] - h[u] + h[v]
} else {
allPairsDistances[u][v] = math.Inf(1)
}
}
}
return allPairsDistances
}
// createZeroWeightEdges creates zero-weight edges from a new vertex to all existing vertices.
func createZeroWeightEdges(vertices int) []Edge {
edges := make([]Edge, vertices)
for i := 0; i < vertices; i++ {
edges[i] = Edge{From: vertices, To: i, Weight: 0}
}
return edges
}
// Priority Queue Implementation for Dijkstra's Algorithm.
type Item struct {
vertex int
priority float64
index int
}
// PriorityQueue is a priority queue for Dijkstra's algorithm.
type PriorityQueue []*Item
func (pq PriorityQueue) Len() int { return len(pq) }
func (pq PriorityQueue) Less(i, j int) bool {
return pq[i].priority < pq[j].priority
}
func (pq PriorityQueue) Swap(i, j int) {
pq[i], pq[j] = pq[j], pq[i]
pq[i].index = i
pq[j].index = j
}
func (pq *PriorityQueue) Push(x interface{}) {
item := x.(*Item)
item.index = len(*pq)
*pq = append(*pq, item)
}
func (pq *PriorityQueue) Pop() interface{} {
old := *pq
n := len(old)
item := old[n-1]
item.index = -1 // For safety.
*pq = old[0 : n-1]
return item
}
// Main function demonstrating Johnson's Algorithm.
func main() {
graph := NewGraph(5)
// Add edges with their weights.
graph.AddEdge(0, 1, 3)
graph.AddEdge(0, 2, 8)
graph.AddEdge(1, 3, 1)
graph.AddEdge(2, 3, -4)
graph.AddEdge(3, 4, 2)
graph.AddEdge(4, 0, -1)
// Run Johnson's Algorithm.
distances := graph.Johnson()
if distances == nil {
fmt.Println("The graph contains a negative weight cycle.")
} else {
fmt.Println("Shortest distances between all pairs of vertices:")
for i, row := range distances {
fmt.Printf("From vertex %d: %v\n", i, row)
}
}
}