Skip to content

Commit ebc4508

Browse files
Combine playgrounds into 1 playground with pages
1 parent 10154dc commit ebc4508

File tree

6 files changed

+106
-11
lines changed

6 files changed

+106
-11
lines changed

Graph/Graph Adjacency List.playground/contents.xcplayground

Lines changed: 0 additions & 4 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,57 @@
1+
/*
2+
3+
A graph implementation, using an adjacency list.
4+
5+
In an adjacency list implementation, each vertex stores an array of edges, indicating to which vertices it has an edge (note the directionality). The edge stores the source and destination vertices, as well as a weight.
6+
7+
Connected vertices in this implementation is O(1).
8+
9+
*/
10+
111
public struct GraphEdge<T> {
2-
let from: GraphVertex<T>
3-
let to: GraphVertex<T>
4-
let weight: Int
12+
public let from: GraphVertex<T>
13+
public let to: GraphVertex<T>
14+
public let weight: Double
515
}
616

717
public struct GraphVertex<T> {
818
public var data: T
9-
private var edges: [GraphEdge<T>] = [] // This is a simple adjacency list, rather than matrix
19+
public private(set) var edges: [GraphEdge<T>] = [] // This is a simple adjacency list, rather than matrix
1020

1121
public init(data: T) {
1222
self.data = data
1323
}
1424

1525
// Creates a directed edge self -----> dest
16-
public mutating func connectTo(destinationVertex: GraphVertex<T>, withWeight weight: Int = 0) {
26+
public mutating func connectTo(destinationVertex: GraphVertex<T>, withWeight weight: Double = 0) {
1727
edges.append(GraphEdge(from: self, to: destinationVertex, weight: weight))
1828
}
1929

2030
// Creates an undirected edge by making 2 directed edges: self ----> other, and other ----> self
21-
public mutating func connectBetween(inout otherVertex: GraphVertex<T>, withWeight weight: Int = 0) {
31+
public mutating func connectBetween(inout otherVertex: GraphVertex<T>, withWeight weight: Double = 0) {
2232
edges.append(GraphEdge(from: self, to: otherVertex, weight: weight))
2333
otherVertex.edges.append(GraphEdge(from: otherVertex, to: self, weight: weight))
2434
}
2535
}
2636

2737

2838

29-
39+
// Create 4 separate vertices
3040
var v1 = GraphVertex(data: 1)
3141
var v2 = GraphVertex(data: 2)
3242
var v3 = GraphVertex(data: 3)
3343
var v4 = GraphVertex(data: 4)
3444

45+
// Setup a cycle like so:
3546
// v1 ---> v2 ---> v3 ---> v4
3647
// ^ |
3748
// | V
3849
// -----------<------------|
50+
3951
v1.connectTo(v2)
4052
v2.connectTo(v3)
4153
v3.connectTo(v4)
4254
v4.connectTo(v1)
55+
56+
57+
//: [Next](@next)
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//: [Previous](@previous)
2+
/*
3+
4+
A graph implementation, using an adjacency matrix.
5+
6+
In an adjacency matrix implementation, each vertex is associated with an index from 0..<V (V being the number of vertices). Then a matrix (a 2D array) is stored for the entire graph, with each entry indicating if the corresponding vertices are connected, and the weight. For example, if matrix[3][5] = 4.6, then vertex #3 is connected to vertex #5, with a weight of 4.6. Note that vertex #5 is not necessarily connected to vertex #3.
7+
8+
Creating a new vertex must expand the adjacency matrix in this implementation, so creating a new vertex is O(V).
9+
Connecting vertices os O(1).
10+
11+
*/
12+
13+
public struct GraphVertex<T> {
14+
public var data: T
15+
private let uniqueID: Int
16+
}
17+
18+
public struct Graph<T> {
19+
20+
// nil entries are used to mark that two vertices are NOT connected.
21+
private var adjacencyMatrix: [[Double?]] = []
22+
23+
public init() { }
24+
25+
public mutating func createVertex(data: T) -> GraphVertex<T> {
26+
let vertex = GraphVertex(data: data, uniqueID: adjacencyMatrix.count)
27+
28+
// Expand each existing row to the right one column
29+
for i in 0..<adjacencyMatrix.count {
30+
adjacencyMatrix[i].append(nil)
31+
}
32+
33+
// Add one new row at the bottom
34+
let newRow = [Double?](count: adjacencyMatrix.count + 1, repeatedValue: nil)
35+
adjacencyMatrix.append(newRow)
36+
37+
return vertex
38+
}
39+
40+
// Creates a directed edge source -----> dest. Represented by M[source][dest] = weight
41+
public mutating func connect(sourceVertex: GraphVertex<T>, toDestinationVertex: GraphVertex<T>, withWeight weight: Double = 0) {
42+
adjacencyMatrix[sourceVertex.uniqueID][toDestinationVertex.uniqueID] = weight
43+
}
44+
45+
// Creates an undirected edge by making 2 directed edges: some ----> other, and other ----> some
46+
public mutating func connect(someVertex: GraphVertex<T>, withVertex: GraphVertex<T>, withWeight weight: Double = 0) {
47+
adjacencyMatrix[someVertex.uniqueID][withVertex.uniqueID] = weight
48+
adjacencyMatrix[withVertex.uniqueID][someVertex.uniqueID] = weight
49+
50+
}
51+
}
52+
53+
54+
55+
// Create 4 separate vertices
56+
var graph = Graph<Int>()
57+
let v1 = graph.createVertex(1)
58+
let v2 = graph.createVertex(2)
59+
let v3 = graph.createVertex(3)
60+
let v4 = graph.createVertex(4)
61+
62+
// Setup a cycle like so:
63+
// v1 ---> v2 ---> v3 ---> v4
64+
// ^ |
65+
// | V
66+
// -----------<------------|
67+
68+
graph.connect(v1, toDestinationVertex: v2)
69+
graph.connect(v2, toDestinationVertex: v3)
70+
graph.connect(v3, toDestinationVertex: v4)
71+
graph.connect(v4, toDestinationVertex: v1)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Timeline
3+
version = "3.0">
4+
<TimelineItems>
5+
</TimelineItems>
6+
</Timeline>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='6.0' target-platform='ios' display-mode='rendered'>
3+
<pages>
4+
<page name='Adjacency List'/>
5+
<page name='Adjacency Matrix'/>
6+
</pages>
7+
</playground>

0 commit comments

Comments
 (0)