|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | +"fmt" |
| 5 | +"math" |
| 6 | + |
| 7 | +"github.com/Data-Structures-and-Algorithms/Go/Algorithms/Graph-Algorithms/djikstras/minheapmap" |
| 8 | +) |
| 9 | + |
| 10 | +//Using a MinHeap with O(1) Lookup for an implementation of the Djikstras algo for minimum cost spanning path. |
| 11 | +type node struct { |
| 12 | +name string //ID of the node |
| 13 | +neighbours map[*node]int // indicates a directed edge from the node to the neighbour |
| 14 | +} |
| 15 | + |
| 16 | +func main() { |
| 17 | +//Build a graph representation.. |
| 18 | +n1 := &node{ |
| 19 | +name: "guitar", |
| 20 | +} |
| 21 | +n2 := &node{ |
| 22 | +name: "book", |
| 23 | +} |
| 24 | +n3 := &node{ |
| 25 | +name: "LP", |
| 26 | +} |
| 27 | +n4 := &node{ |
| 28 | +name: "poster", |
| 29 | +} |
| 30 | +n5 := &node{ |
| 31 | +name: "drums", |
| 32 | +} |
| 33 | +n6 := &node{ |
| 34 | +name: "piano", |
| 35 | +} |
| 36 | +n1.neighbours = make(map[*node]int) |
| 37 | +n1.neighbours[n6] = 20 |
| 38 | +n2.neighbours = make(map[*node]int) |
| 39 | +n2.neighbours[n4] = 0 |
| 40 | +n2.neighbours[n3] = 5 |
| 41 | +n3.neighbours = make(map[*node]int) |
| 42 | +n3.neighbours[n1] = 15 |
| 43 | +n3.neighbours[n5] = 20 |
| 44 | +n4.neighbours = make(map[*node]int) |
| 45 | +n4.neighbours[n5] = 10 |
| 46 | +n4.neighbours[n1] = 30 |
| 47 | +n5.neighbours = make(map[*node]int) |
| 48 | +n5.neighbours[n6] = 10 |
| 49 | +src := n2 |
| 50 | +// src: ok , adjList: ok |
| 51 | +mheap := minHeapMap{ |
| 52 | +minHeap: make([]entry, 0), |
| 53 | +mapping: make(map[*node]int), |
| 54 | +} |
| 55 | +minheapmap.Push(&mheap, entry{ |
| 56 | +n: n1, |
| 57 | +cost: math.MaxInt32, |
| 58 | +}) |
| 59 | +minheapmap.Push(&mheap, entry{ |
| 60 | +n: n3, |
| 61 | +cost: math.MaxInt32, |
| 62 | +}) |
| 63 | +minheapmap.Push(&mheap, entry{ |
| 64 | +n: n4, |
| 65 | +cost: math.MaxInt32, |
| 66 | +}) |
| 67 | +minheapmap.Push(&mheap, entry{ |
| 68 | +n: n5, |
| 69 | +cost: math.MaxInt32, |
| 70 | +}) |
| 71 | +minheapmap.Push(&mheap, entry{ |
| 72 | +n: n6, |
| 73 | +cost: math.MaxInt32, |
| 74 | +}) |
| 75 | +minheapmap.Push(&mheap, entry{ |
| 76 | +n: n2, |
| 77 | +cost: 0, |
| 78 | +}) |
| 79 | +fmt.Println("created heap:", mheap.minHeap) |
| 80 | +srcMap := make(map[*node]*node) |
| 81 | +srcMap[src] = src |
| 82 | +costMap := make(map[*node]int) |
| 83 | +for len(mheap.minHeap) > 0 { |
| 84 | +top := minheapmap.Pop(&mheap).(entry) |
| 85 | +n := top.n |
| 86 | +fmt.Println("taking out", n.name) |
| 87 | +delete(mheap.mapping, n) |
| 88 | +c := top.cost |
| 89 | +fmt.Println("to reach", n.name, "cost:", c) |
| 90 | +costMap[n] = c |
| 91 | +for ngbr, cost := range n.neighbours { |
| 92 | +fmt.Println("at neighbr", ngbr.name, "edge cost:", cost) |
| 93 | +if mheap.contains(ngbr) { |
| 94 | +//current cost |
| 95 | +k := mheap.GetKey(ngbr) |
| 96 | +currCost := mheap.minHeap[k].cost |
| 97 | +fmt.Println("curr cost of reaching", ngbr.name, "-", currCost) |
| 98 | +//if current cost < c+cost |
| 99 | +if c+cost < currCost { |
| 100 | +mheap.minHeap[k].cost = c + cost |
| 101 | +fmt.Println("upgating", ngbr.name, "to", c+cost) |
| 102 | +minheapmap.Decrease(&mheap, ngbr) |
| 103 | +srcMap[ngbr] = n |
| 104 | +} |
| 105 | +// update cost of the ngbr in the heap |
| 106 | +} |
| 107 | +} |
| 108 | + |
| 109 | +} |
| 110 | +for n, c := range costMap { |
| 111 | +fmt.Println("node:", n.name, " cost:", c) |
| 112 | +} |
| 113 | + |
| 114 | +} |
| 115 | + |
| 116 | +//need to implement decrement() in log(n) and contains in O(1) |
| 117 | +type minHeapMap struct { |
| 118 | +minHeap []entry |
| 119 | +mapping map[(*node)]int |
| 120 | +} |
| 121 | + |
| 122 | +type entry struct { |
| 123 | +n *node |
| 124 | +cost int |
| 125 | +} |
| 126 | + |
| 127 | +func (m *minHeapMap) contains(n *node) bool { |
| 128 | +_, ok := m.mapping[n] |
| 129 | +return ok |
| 130 | +} |
| 131 | + |
| 132 | +func (m *minHeapMap) Remove(x interface{}) { |
| 133 | +delete((*m).mapping, x.(entry).n) |
| 134 | +} |
| 135 | + |
| 136 | +func (m *minHeapMap) Update(k, v interface{}) { |
| 137 | +(*m).mapping[k.(entry).n] = v.(int) |
| 138 | +} |
| 139 | + |
| 140 | +func (m minHeapMap) GetElem(x interface{}) interface{} { |
| 141 | +return (m).minHeap[x.(int)] |
| 142 | +} |
| 143 | + |
| 144 | +func (m *minHeapMap) GetKey(x interface{}) int { |
| 145 | +return (*m).mapping[x.(*node)] |
| 146 | +} |
| 147 | + |
| 148 | +func (m *minHeapMap) Len() int { |
| 149 | +return len((*m).minHeap) |
| 150 | +} |
| 151 | + |
| 152 | +func (m *minHeapMap) Less(i, j int) bool { |
| 153 | +return ((*m).minHeap)[i].cost < ((*m).minHeap)[j].cost |
| 154 | +} |
| 155 | + |
| 156 | +func (m *minHeapMap) Swap(i, j int) { |
| 157 | +(*m).minHeap[i], (*m).minHeap[j] = (*m).minHeap[j], (*m).minHeap[i] |
| 158 | +} |
| 159 | + |
| 160 | +func (m *minHeapMap) Push(x interface{}) { |
| 161 | +(*m).minHeap = append((*m).minHeap, x.(entry)) |
| 162 | +(*m).mapping[x.(entry).n] = len((*m).minHeap) - 1 |
| 163 | +} |
| 164 | + |
| 165 | +func (m *minHeapMap) Pop() interface{} { |
| 166 | +v := (*m).minHeap |
| 167 | +l := len(v) |
| 168 | +top := v[l-1] |
| 169 | +v = v[:l-1] |
| 170 | +(*m).minHeap = v |
| 171 | +return top |
| 172 | +} |
0 commit comments