You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Graph/README.markdown
+46-48
Original file line number
Diff line number
Diff line change
@@ -42,7 +42,7 @@ Like a tree this does not have any cycles in it (no matter where you start, ther
42
42
43
43
Maybe you're shrugging your shoulders and thinking, what's the big deal? Well, it turns out that graphs are an extremely useful data structure.
44
44
45
-
If you have some programming problem where you can represent some of your data as vertices and some of it as edges between those vertices, then you can draw your problem as a graph and use well-known graph algorithms such as [breadth-first search](../Breadth-First Search/) or [depth-first search](../Depth-First Search) to find a solution.
45
+
If you have some programming problem where you can represent some of your data as vertices and some of it as edges between those vertices, then you can draw your problem as a graph and use well-known graph algorithms such as [breadth-first search](../Breadth-First Search/) or [depth-first search](../Depth-First Search) to find a solution.
46
46
47
47
For example, let's say you have a list of tasks where some tasks have to wait on others before they can begin. You can model this using an acyclic directed graph:
48
48
@@ -110,12 +110,13 @@ We'll show you sample implementations of both adjacency list and adjacency matri
110
110
The adjacency list for each vertex consists of `Edge` objects:
@@ -147,10 +148,7 @@ We can represent it as an adjacency matrix or adjacency list. The classes implem
147
148
Let's create some directed, weighted graphs, using each representation, to store the example:
148
149
149
150
```swift
150
-
var adjacencyMatrixGraph = AdjacencyMatrixGraph<Int>()
151
-
var adjacencyListGraph = AdjacencyListGraph<Int>()
152
-
153
-
for graph in [ adjacencyMatrixGraph, adjacencyListGraph ] {
151
+
for graph in [AdjacencyMatrixGraph<Int>(), AdjacencyListGraph<Int>()] {
154
152
155
153
let v1 = graph.createVertex(1)
156
154
let v2 = graph.createVertex(2)
@@ -163,8 +161,8 @@ for graph in [ adjacencyMatrixGraph, adjacencyListGraph ] {
163
161
graph.addDirectedEdge(v3, to: v4, withWeight: 4.5)
164
162
graph.addDirectedEdge(v4, to: v1, withWeight: 2.8)
165
163
graph.addDirectedEdge(v2, to: v5, withWeight: 3.2)
166
-
}
167
164
165
+
}
168
166
```
169
167
170
168
As mentioned earlier, to create an undirected edge you need to make two directed edges. If we wanted undirected graphs, we'd call this method instead, which takes care of that work for us:
@@ -184,7 +182,7 @@ We could provide `nil` as the values for the `withWeight` parameter in either ca
184
182
To maintain the adjacency list, there is a class that maps a list of edges to a vertex. The graph simply maintains an array of such objects and modifies them as necessary.
@@ -193,34 +191,34 @@ private class EdgeList<T where T: Equatable, T: Hashable> {
193
191
self.vertex= vertex
194
192
}
195
193
196
-
funcaddEdge(edge: Edge<T>) {
194
+
funcaddEdge(_edge: Edge<T>) {
197
195
edges?.append(edge)
198
196
}
199
197
200
198
}
201
-
```
199
+
```
202
200
203
201
They are implemented as a class as opposed to structs so we can modify them by reference, in place, like when adding an edge to a new vertex, where the source vertex already has an edge list:
let matchingVertices = vertices.filter() { vertex in
207
+
return vertex.data== data
208
+
}
209
+
210
+
if matchingVertices.count>0 {
211
+
return matchingVertices.last!
220
212
}
213
+
214
+
// if the vertex doesn't exist, create a new one
215
+
let vertex =Vertex(data: data, index: adjacencyList.count)
216
+
adjacencyList.append(EdgeList(vertex: vertex))
217
+
return vertex
218
+
}
221
219
```
222
220
223
-
The adjacency list for the example looks like this:
221
+
The adjacency list for the example looks like this:
224
222
225
223
```
226
224
v1 -> [(v2: 1.0)]
@@ -238,32 +236,32 @@ We'll keep track of the adjacency matrix in a two-dimensional `[[Double?]]` arra
238
236
To index into the matrix using vertices, we use the `index` property in `Vertex`, which is assigned when creating the vertex through the graph object. When creating a new vertex, the graph must resize the matrix:
0 commit comments