Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prim Algorithm Implementation #985

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions Prim Algorithm/Prim Algorithm.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import Foundation



func prims (_ matrix : [[Int]]) {
var selected = 0
var selectedSoFar = Set<Int>()
selectedSoFar.insert( (matrix[selected].enumerated().min{ $0.element < $1.element }?.offset)! )

while selectedSoFar.count < matrix.count {
var minValue = Int.max
var minIndex = selected
var initialRow = 0
for row in selectedSoFar {
let candidateMin = matrix[row].enumerated().filter{$0.element > 0 && !selectedSoFar.contains($0.offset) }.min{ $0.element < $1.element }
if (minValue > candidateMin?.element ?? Int.max ) {
minValue = candidateMin?.element ?? Int.max
minIndex = (candidateMin?.offset) ?? 0
initialRow = row
}
}
print ("edge value \(minValue) with \(initialRow) to \(minIndex)")
selectedSoFar.insert(minIndex)
selected = (minValue)
}
}

let input = [[0,9,75,0,0],[9,0,95,19,42],[75,95,0,51,66],[0,19,51,0,31],[0,42,66,31,0]]
prims(input)

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
<timeline fileName='timeline.xctimeline'/>
</playground>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions Prim Algorithm/README.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Prim's Algorithm

Prim's Algorithm is an algorithm that finds a minimum spanning tree for a weighted undirected graph. This means it finds a subset of the edges that forms a tree that includes every vertex, where the total weight of all the edges in the tree is minimized.


## How does it work

We start from one vertex and keep adding edges with the lowest weight until we reach our goal.
1. Initialize the minimum spanning tree with a vertex chosen at random
2. Find all the edges that connect the tree to new verticles, find the minimum and add it to the tree
3. Keep repeating step 2 until we get a minimum spanning tree

## Time complexity
Adjacency matrix, searching : O(|V|^{2})
binary heap and adjacency list : O((|V|+|E|)\log |V|)= O(|E|\log |V|)
Fibonacci heap and adjacency list : O(|E|+|V|\log |V|)

##Sources

[1]:
https://www.programiz.com/dsa/prim-algorithm
[2]:
https://www.raywenderlich.com/books/data-structures-algorithms-in-swift/v4.0/chapters/45-prim-s-algorithm-challenges
[3]: https://codereview.stackexchange.com/questions/215897/swift-prims-algorithm