Skip to content

Commit a72a812

Browse files
committed
Quickie cleanup of depth-first search. Needs more work.
1 parent 2e27f22 commit a72a812

File tree

4 files changed

+22
-25
lines changed

4 files changed

+22
-25
lines changed

Depth-First Search/DepthFirstSearch.playground/Pages/Simple Example.xcplaygroundpage/Contents.swift

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
func depthFirstSearch(graph: Graph, source: Node) -> [String] {
2-
var nodesExplored: [String] = [source.label]
2+
var nodesExplored = [source.label]
33
source.visited = true
44

5-
// Iterate through all neighbors, and for each one visit all of its neighbors
65
for edge in source.neighbors {
7-
let neighbor: Node = edge.neighbor
8-
9-
if (!neighbor.visited) {
10-
nodesExplored += depthFirstSearch(graph, source: neighbor)
6+
if !edge.neighbor.visited {
7+
nodesExplored += depthFirstSearch(graph, source: edge.neighbor)
118
}
129
}
1310
return nodesExplored
1411
}
1512

1613

14+
1715
let graph = Graph()
1816

1917
let nodeA = graph.addNode("a")
@@ -35,4 +33,4 @@ graph.addEdge(nodeE, neighbor: nodeH)
3533

3634

3735
let nodesExplored = depthFirstSearch(graph, source: nodeA)
38-
print(nodesExplored)
36+
print(nodesExplored)
Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
func depthFirstSearch(graph: Graph, source: Node) -> [String] {
2-
var nodesExplored: [String] = [source.label]
2+
var nodesExplored = [source.label]
33
source.visited = true
44

5-
// Iterate through all neighbors, and for each one visit all of its neighbors
65
for edge in source.neighbors {
7-
let neighbor: Node = edge.neighbor
8-
9-
if (!neighbor.visited) {
10-
nodesExplored += depthFirstSearch(graph, source: neighbor)
6+
if !edge.neighbor.visited {
7+
nodesExplored += depthFirstSearch(graph, source: edge.neighbor)
118
}
129
}
13-
1410
return nodesExplored
1511
}

Depth-First Search/README.markdown

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
11
# Depth-First Search
22

3-
Depth-first search (DFS) is an algorithm for traversing or searching [tree](../Tree/) or [graph](../Graph/) data structures. It starts at the tree source and explores as far as possible along each branch before backtracking.
3+
Depth-first search (DFS) is an algorithm for traversing or searching [tree](../Tree/) or [graph](../Graph/) data structures. It starts at a source node and explores as far as possible along each branch before backtracking.
4+
5+
[TODO: this is a work-in-progress]
46

57
## The code
68

7-
Simple implementation of breadth-first search using a queue:
9+
Simple recursive implementation of depth-first search:
810

911
```swift
1012
func depthFirstSearch(graph: Graph, source: Node) -> [String] {
11-
var nodesExplored: [String] = [source.label]
13+
var nodesExplored = [source.label]
1214
source.visited = true
1315

14-
// Iterate through all neighbors, and for each one visit all of its neighbors
1516
for edge in source.neighbors {
16-
let neighbor: Node = edge.neighbor
17-
18-
if (!neighbor.visited) {
19-
nodesExplored += depthFirstSearch(graph, source: neighbor)
17+
if !edge.neighbor.visited {
18+
nodesExplored += depthFirstSearch(graph, source: edge.neighbor)
2019
}
2120
}
2221
return nodesExplored
2322
}
2423
```
2524

25+
Where a [breadth-first search](../Breadth-First Search/) visits all immediate neighbors first, a depth-first search tries to go as deep down the tree or graph as it can.
26+
2627
Put this code in a playground and test it like so:
2728

2829
```swift
29-
let graph = Graph() // Representing the graph from the animated example
30+
let graph = Graph()
3031

3132
let nodeA = graph.addNode("a")
3233
let nodeB = graph.addNode("b")
@@ -58,4 +59,6 @@ Depth-first search can be used to solve many problems, for example:
5859
* Finding connected components of a sparse graph
5960
* Topological sorting of nodes in a graph
6061
* Finding bridges of a graph (see: [Bridges](https://en.wikipedia.org/wiki/Bridge_(graph_theory)#Bridge-finding_algorithm))
61-
* Among others
62+
* And lots of others!
63+
64+
*Written for Swift Algorithm Club by Paulo Tanaka*

README.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ Most of the time using just the built-in `Array`, `Dictionary`, and `Set` types
168168

169169
- Graph
170170
- [Breadth-First Search (BFS)](Breadth-First Search/)
171-
- Depth-First Search (DFS)
171+
- [Depth-First Search (DFS)](Depth-First Search/)
172172
- Shortest Path
173173
- Minimum Spanning Tree
174174
- All Paths

0 commit comments

Comments
 (0)