Skip to content

Instantly share code, notes, and snippets.

@jdriselvato
jdriselvato / adjacency-list.swift
Created March 6, 2022 04:21 — forked from larryfox/adjacency-list.swift
Adjacency list in Swift
// Found this lying around in my code folder from when I was
// first playing with Swift. Consider it public domain.
struct Graph<T: Hashable> {
let directed: Bool
var adj = Dictionary<T, [T]>()
init(directed d: Bool) {
directed = d
}
@jdriselvato
jdriselvato / GraphAM.swift
Created March 6, 2022 04:21 — forked from davidinga/GraphAM.swift
Graph data structure in Swift using an Adjacency Matrix.
public struct GraphAM {
private var adjMatrix = [[Int?]]()
private var numberOfVertices: Int {
return adjMatrix.count
}
init(numberOfVertices: Int) {
adjMatrix = Array(repeating: Array(repeating: nil, count: numberOfVertices), count: numberOfVertices)
}