Number of islands
We are given a grid map of '1's and '0's(water). We
are to count the number of islands on the grid.
Algorithm
1. Mark the current node as visited(initially current node is the root node).
2. Check if current node is the goal, If so, then return it.
1. Iterate over children nodes of current node, and do the following:
2. Check if a child node is not visited.
1.If so, then, mark it as visited.
3. Go to it's sub tree recursively until you find the goal node(In other words, do the same steps here passing the child node as the current node in the next recursive call).
4. If the child node has the goal node in this sub tree, then, return it.
3. If goal node is not found, then goal node is not in the tree!
Computational complexity
This algorithm takes O(M + N) where M is the number of rows and E is the number of columns.Applications
- topological sorting.
- bipartite graph testing.
- finding strongly connected components.
- solving puzzles that can only have one solution.
- detecting cycles in a graph.
References
number of islands