Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ extension Graph {
if let i = vertices.index(of: vertex) {
return i
}
return nil;
return nil
}

/// Find all of the neighbors of a vertex at a given index.
Expand Down Expand Up @@ -90,12 +90,11 @@ extension Graph {
}
return nil
}

/// Add a vertex to the graph.
///
/// - parameter v: The vertex to be added.
/// - returns: The index where the vertex was added.
public func addVertex(_ v: VertexType) -> Int {
@discardableResult public func addVertex(_ v: VertexType) -> Int {
vertices.append(v)
edges.append([EdgeType]())
return vertices.count - 1
Expand Down Expand Up @@ -133,24 +132,21 @@ open class UnweightedEdge: Edge {
open class UnweightedGraph<V: Equatable>: Graph {
var vertices: [V] = [V]()
var edges: [[UnweightedEdge]] = [[UnweightedEdge]]() //adjacency lists

public init() {
}


public init(vertices: [V]) {
for vertex in vertices {
_ = self.addVertex(vertex)
self.addVertex(vertex)
}
}

/// This is a convenience method that adds an unweighted edge.
///
/// - parameter from: The starting vertex's index.
/// - parameter to: The ending vertex's index.
public func addEdge(from: Int, to: Int) {
addEdge(UnweightedEdge(u: from, v: to))
}

/// This is a convenience method that adds an unweighted, undirected edge between the first occurence of two vertices.
///
/// - parameter from: The starting vertex.
Expand All @@ -162,7 +158,7 @@ open class UnweightedGraph<V: Equatable>: Graph {
}
}
}

/// MARK: Implement CustomStringConvertible
public var description: String {
var d: String = ""
Expand Down Expand Up @@ -324,26 +320,22 @@ open class WeightedEdge<W: Comparable & Summable>: Edge, Comparable {
open class WeightedGraph<V: Equatable & Hashable, W: Comparable & Summable>: Graph {
var vertices: [V] = [V]()
var edges: [[WeightedEdge<W>]] = [[WeightedEdge<W>]]() //adjacency lists

public init() {
}


public init(vertices: [V]) {
for vertex in vertices {
_ = self.addVertex(vertex)
self.addVertex(vertex)
}
}

/// Find all of the neighbors of a vertex at a given index.
///
/// - parameter index: The index for the vertex to find the neighbors of.
/// - returns: An array of tuples including the vertices as the first element and the weights as the second element.
public func neighborsForIndexWithWeights(_ index: Int) -> [(V, W)] {
var distanceTuples: [(V, W)] = [(V, W)]();
var distanceTuples: [(V, W)] = [(V, W)]()
for edge in edges[index] {
distanceTuples += [(vertices[edge.v], edge.weight)]
}
return distanceTuples;
return distanceTuples
}

/// This is a convenience method that adds a weighted edge.
Expand Down Expand Up @@ -520,7 +512,7 @@ public extension WeightedGraph {

return (distances, pathDict)
}


/// A convenience version of dijkstra() that allows the supply of the root
/// vertex instead of the index of the root vertex.
Expand All @@ -530,7 +522,7 @@ public extension WeightedGraph {
}
return ([], [:])
}

/// Helper function to get easier access to Dijkstra results.
public func distanceArrayToVertexDict(distances: [W?]) -> [V : W?] {
var distanceDict: [V: W?] = [V: W?]()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extension Array {
if count < 2 { return shuffledArray } // already shuffled
for i in (1..<count).reversed() { // count backwards
let position = Int(arc4random_uniform(UInt32(i + 1))) // random to swap
if i != position { // swap with the end, don't bother with selp swaps
if i != position { // swap with the end, don't bother with swap
shuffledArray.swapAt(i, position)
}
}
Expand Down Expand Up @@ -210,7 +210,6 @@ final class SimpleEquation: Chromosome {

let se = GeneticAlgorithm<SimpleEquation>(size: 10, threshold: 13.0, maxGenerations: 100, mutationChance: 0.1, crossoverChance: 0.7)
let result1 = se.run()
result1.fitness
result1.prettyPrint()


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ struct Item {
func knapsack(items: [Item], maxCapacity: Int) -> [Item] {
//build up dynamic programming table
var table: [[Float]] = [[Float]](repeating: [Float](repeating: 0.0, count: maxCapacity + 1), count: items.count + 1) //initialize table - overshooting in size
for i in 1...items.count {
for (i, item) in items.enumerated() {
for capacity in 1...maxCapacity {
if capacity - items[i - 1].weight >= 0 { // still room in knapsack
table[i][capacity] = max(table[i - 1][capacity - items[i - 1].weight] + items[i - 1].value, table[i - 1][capacity]) // only take if more valuable than previous combo
let previousItemsValue = table[i][capacity]
if capacity >= item.weight { // item fits in knapsack
let valueFreeingWeightForItem = table[i][capacity - item.weight]
table[i + 1][capacity] = max(valueFreeingWeightForItem + item.value, previousItemsValue) // only take if more valuable than previous combo
} else { // no room for this item
table[i][capacity] = table[i - 1][capacity] //use prior combo
table[i + 1][capacity] = previousItemsValue //use prior combo
}
}
}
Expand Down