diff --git a/Depth-First Search/DepthFirstSearchIterative.swift b/Depth-First Search/DepthFirstSearchIterative.swift new file mode 100644 index 000000000..805425066 --- /dev/null +++ b/Depth-First Search/DepthFirstSearchIterative.swift @@ -0,0 +1,35 @@ +// +// DepthFirstIterative.swift +// DFS +// +// Created by Yash Jivani on 29/05/21. +// + +import Foundation + +func depthFirstSearchIterative(_ graph: Graph, source: Node) -> [String]{ + var nodesExplored = [source.label] + + source.visited = true + + var stack : [Node] = [] + + stack.append(source) + while(!stack.isEmpty){ + + let top = stack.removeFirst() + + if(!top.visited){ + nodesExplored.append(top.label) + top.visited = true + } + + for edge in top.neighbors{ + if(!edge.neighbor.visited){ + stack.insert(edge.neighbor, at: 0) + } + } + + } + return nodesExplored +}