Skip to content

Commit 12f54e0

Browse files
Write initial structs for Graph
1 parent 8d375e3 commit 12f54e0

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

Graph/Graph.playground/Contents.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
public struct GraphEdge<T> {
2+
let from: GraphVertex<T>
3+
let to: GraphVertex<T>
4+
let weight: Int
5+
}
6+
7+
public struct GraphVertex<T> {
8+
public var data: T
9+
private var edges: [GraphEdge<T>] // This is a simple adjacency list, rather than matrix
10+
11+
public mutating func connectTo(destinationVertex: GraphVertex<T>, withWeight weight: Int = 0) {
12+
edges.append(GraphEdge(from: self, to: destinationVertex, weight: weight))
13+
}
14+
15+
public mutating func connectBetween(inout otherVertex: GraphVertex<T>, withWeight weight: Int = 0) {
16+
edges.append(GraphEdge(from: self, to: otherVertex, weight: weight))
17+
otherVertex.edges.append(GraphEdge(from: otherVertex, to: self, weight: weight))
18+
}
19+
}
20+
21+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
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>

Graph/README.markdown

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Graph
2+
3+
YAY Graphs!
4+
TODO: Write stuff here.
5+
6+
*Written by Donald Pinckney*

0 commit comments

Comments
 (0)