Skip to content

Commit 7f288a2

Browse files
committed
Added depth first search in kotlin
1 parent 2b795d5 commit 7f288a2

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

searchingAlgo/DepthFirstSearch/depthFirstSearch.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* Creates a Graph with [size] nodes
3+
* @param init constructor for adding the edges of the nodes delegated to the instances
4+
*/
15
class Graph(size: Int, init: Graph.() -> Unit) {
26
val adjacency: Array<MutableList<Int>> = Array(size) { ArrayList() }
37

@@ -6,6 +10,10 @@ class Graph(size: Int, init: Graph.() -> Unit) {
610
fun addEdge(node: Int, to: Int) = adjacency[node].add(to)
711
}
812

13+
/**
14+
* Prints the route taken when exploring the [graph] starting from [current]
15+
* @param searched an array of booleans relating to whether the relating nodes have been searched
16+
*/
917
fun depthFirstSearch(graph: Graph, current: Int, searched: Array<Boolean> = Array(graph.adjacency.size) { false }) {
1018
if(!searched[current]) {
1119
searched[current] = true

0 commit comments

Comments
 (0)