Skip to content

Commit

Permalink
Fix iiitv#595: Add Prims [Go] (iiitv#600)
Browse files Browse the repository at this point in the history
  • Loading branch information
sergobot authored and singhpratyush committed Nov 2, 2017
1 parent 41ea7b4 commit 59f3727
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Community (college) maintained list of Algorithms and Data Structures implementa
| [Modular Exponential](http://www.geeksforgeeks.org/modular-exponentiation-power-in-modular-arithmetic/) | [:white_check_mark:](modular_exponential/modular_exponential.c) | | [:white_check_mark:](modular_exponential/ModularExponential.java) | [:white_check_mark:](modular_exponential/modular_exponential.py) | [:white_check_mark:](modular_exponential/modular_exponential.go) | [:white_check_mark:](modular_exponential/modularExponential.js) | |
| [N-Queen Problem](https://en.wikipedia.org/wiki/Eight_queens_puzzle) | | [:white_check_mark:](n_queen_problem/NQueenProblem.cpp) | [:white_check_mark:](n_queen_problem/NQueenProblem.java) | [:white_check_mark:](n_queen_problem/n_queen_problem.py) | [:white_check_mark:](n_queen_problem/n_queen_problem.go) | | |
| [Prime Factor](https://en.wikipedia.org/wiki/Prime_factor) | [:white_check_mark:](prime_factor/prime_factor.c) | | [:white_check_mark:](prime_factor/PrimeFactor.java) | [:white_check_mark:](prime_factor/prime_factor.py) | [:white_check_mark:](prime_factor/prime_factor.go) | [:white_check_mark:](prime_factor/primeFactor.js) | |
| [Prims](https://en.wikipedia.org/wiki/Prim%27s_algorithm) | [:white_check_mark:](prims/prims.c) | | [:white_check_mark:](prims/Prims.java) | | | [:white_check_mark:](prims/prims.js) | |
| [Prims](https://en.wikipedia.org/wiki/Prim%27s_algorithm) | [:white_check_mark:](prims/prims.c) | | [:white_check_mark:](prims/Prims.java) | | [:white_check_mark:](prims/prims.go) | [:white_check_mark:](prims/prims.js) | |
| [Quick Select](https://en.wikipedia.org/wiki/Quickselect) | [:white_check_mark:](quick_select/quick_select.c) | | [:white_check_mark:](quick_select/QuickSelect.java) | [:white_check_mark:](quick_select/quick_select.py) | | | |
| [Quicksort](https://en.wikipedia.org/wiki/Quicksort) | [:white_check_mark:](quick_sort/quicksort.c) | | [:white_check_mark:](quick_sort/QuickSort.java) | [:white_check_mark:](quick_sort/quick_sort.py) | [:white_check_mark:](quick_sort/quick_sort.go) | [:white_check_mark:](quick_sort/quickSort.js) | [:white_check_mark:](quick_sort/QuickSort.cs) |
| [Radix Sort](http://www.geeksforgeeks.org/radix-sort/) | | | [:white_check_mark:](radix_sort/RadixSort.java) | [:white_check_mark:](radix_sort/radix_sort.py) | | | |
Expand Down
71 changes: 71 additions & 0 deletions prims/prims.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package main

import (
"fmt"
"math"
)

// Prims finds a minimum spanning tree of given graph, starting at [0] and
// using Prim's algorithm.
func Prims(graph [][]int) []int {
length := len(graph)
// Stores the parent of each vertex
parents := make([]int, length)
// Stores key value of each vertex
keys := make([]int, length)
// true if already included in MST. Otherwise, false.
visited := make([]bool, length)
for i := 0; i < length; i++ {
keys[i] = math.MaxInt32
}

// Make the first node the root one (no parent)
keys[0] = 0
parents[0] = -1

for i := 1; i < length; i++ {
// Find the minimum key
u := minKey(keys, visited)
visited[u] = true

// Update the neighbours
for j := 0; j < length; j++ {
if graph[u][j] != 0 && !visited[j] && graph[u][j] < keys[j] {
parents[j] = u
keys[j] = graph[u][j]
}
}
}
return parents
}

func minKey(keys []int, visited []bool) int {
min := math.MaxInt32
minID := -1
length := len(keys)
for i := 0; i < length; i++ {
if !visited[i] && keys[i] < min {
min = keys[i]
minID = i
}
}
return minID
}

func main() {
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},
}
// Get parents of all the nodes
parents := Prims(graph)

// Print the Minimum Spanning Tree
fmt.Println("Edge\tWeight")
for i := 1; i < len(graph); i++ {
fmt.Printf("%d - %d\t%d\n", parents[i], i, graph[i][parents[i]])
}
}

0 comments on commit 59f3727

Please sign in to comment.