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

added swift and some algorithm #868

Merged
merged 1 commit into from
Oct 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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.

Binary file not shown.
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Selection Sort

The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning. The algorithm maintains two subarrays in a given array.
1) The subarray which is already sorted.
2) Remaining subarray which is unsorted.
In every iteration of selection sort, the minimum element (considering ascending order) from the unsorted subarray is picked and moved to the sorted subarray.

## How does it work

arr[] = 64 25 12 22 11

// Find the minimum element in arr[0...4]
// and place it at beginning
11 25 12 22 64

// Find the minimum element in arr[1...4]
// and place it at beginning of arr[1...4]
11 12 25 22 64

// Find the minimum element in arr[2...4]
// and place it at beginning of arr[2...4]
11 12 22 25 64

// Find the minimum element in arr[3...4]
// and place it at beginning of arr[3...4]
11 12 22 25 64

## Time complexity
Best/Worst/Average Case: = 0(n^2)

##Sources

[1]:
Example and details:
https://www.geeksforgeeks.org/selection-sort/
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import UIKit

public func selectionSort<T: Comparable>(_ array: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
var a = array
guard array.count > 1 else { return array }

for x in 0 ..< a.count - 1 {

// print out the lowest value of the array
var lowest = x
for y in x + 1 ..< a.count {
if isOrderedBefore(a[y], a[lowest]) {
lowest = y
}
}

// swap lowest value with index
if x != lowest {
a.swapAt(x, lowest)
}
}
return a
}
let list = [ 2, -1, 11, 6, 5, 1, 0, 22, 1, 4, 8, 43 ]
selectionSort(list, <)
selectionSort(list, >)
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.

Binary file not shown.