Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added depth first search in kotlin #218

Merged
merged 3 commits into from
Oct 8, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions searchingAlgo/DepthFirstSearch/depthFirstSearch.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Creates a Graph with [size] nodes
* @param init constructor for adding the edges of the nodes delegated to the instances
*/
class Graph(size: Int, init: Graph.() -> Unit) {
val adjacency: Array<MutableList<Int>> = Array(size) { ArrayList() }

init { init() }

fun addEdge(node: Int, to: Int) = adjacency[node].add(to)
}

/**
* Prints the route taken when exploring the [graph] starting from [current]
* @param searched an array of booleans relating to whether the relating nodes have been searched
*/
fun depthFirstSearch(graph: Graph, current: Int, searched: Array<Boolean> = Array(graph.adjacency.size) { false }) {
if(!searched[current]) {
searched[current] = true
print("$current ")
for (next in graph.adjacency[current]) {
depthFirstSearch(graph, next, searched)
}
}
}

fun main() {
val graph = Graph(4) {
addEdge(0, 1)
addEdge(0, 2)
addEdge(1, 2)
addEdge(2, 0)
addEdge(2, 3)
addEdge(3, 3)
}
println("Following is Depth First Traversal " + "(Starting from vertex 2)")
depthFirstSearch(graph, 2)
}