From 63c516cbb46c090219fd9f058f3eff059fde30a8 Mon Sep 17 00:00:00 2001 From: alvredaivena Date: Sun, 14 Dec 2014 06:19:54 +0700 Subject: [PATCH] added Dijkstra --- Dijkstra.go | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 3 ++- 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 Dijkstra.go diff --git a/Dijkstra.go b/Dijkstra.go new file mode 100644 index 0000000..6e2a361 --- /dev/null +++ b/Dijkstra.go @@ -0,0 +1,70 @@ +package main + +import "fmt" +import "sort" + +// O(n) +func MinCost(cost []int, dist []int) (int, int) { + minn := 100000000 + to := 100000000 + for j:=0; j"+string(b+'A') + path[path_temp] = a + min += a + next = b + count += 1 + } + return path, min +} + +func main(){ + vertex := 5 + /* + 2 3 + A----B----C + | / \ | + 6| 8/ \5 |7 + | / \ | + D---------E + 9 + */ + graph := [][]int{ + {0, 2, 0, 6, 0}, + {2, 0, 3, 8, 5}, + {0, 3, 0, 0, 7}, + {6, 8, 0, 0, 9}, + {0, 5, 7, 9, 0}, + } + path, min := Dijkstra(graph, vertex) + fmt.Println("Shortest path : ") + var keys []string + for k := range path { + keys = append(keys, k) + } + sort.Strings(keys) + for _, k := range keys { + fmt.Println(k, ":", path[k]) + } + fmt.Println("Total cost :", min) +} diff --git a/README.md b/README.md index e996964..a2c39ba 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ ChangeLog: * 2014-12-11 added QuickSort, CombSort, Heapsort (Agung) * 2014-12-11 added CocktailSort, added Non-recursive MergeSort (Alvreda) * 2014-12-13 added Shellsort( versi biasa, Ciura, Knuth) (Marcelinus Devin) +* 2014-12-14 added Dijkstra (Alvreda) TODO: ===== @@ -44,4 +45,4 @@ BOUNTY: * implement Non-recursive Inplace MergeSort +1800, other sorting algorithms, that allow Less/Equal/Swap/Len interface * implement BinarySearch, JumpSearch, InterpolationSearch +400 that Allow Less/Equal interface * implement R-Tree +5000, k-d Tree +8000 (using reflection) -* implement any other data structure or algorithm (bounty may vary, contact me for details) \ No newline at end of file +* implement any other data structure or algorithm (bounty may vary, contact me for details)