Tuesday, 30 August 2022
toastKing.js : A new JavaScript library!
Monday, 8 August 2022
Optimal String Search Using Genetic Algorithm
Optimal String Search Using Genetic Algorithm
Wednesday, 18 May 2022
Flood Fill Algorithm
Flood Fill Algorithm
Flood fill algorithm helps in visiting each and every point in a given area. It determines the area connected to a given cell in a multi-dimensional array. Following are some famous implementations of flood fill algorithm:
Bucket Fill in Paint:
Clicking in an area with this tool selected fills that area with the selected color.
Solving a Maze:
Given a matrix with some starting point, and some destination with some obstacles in between, this algorithm helps to find out the path from source to destination
Minesweeper:
When a blank cell is discovered, this algorithm helps in revealing neighboring cells. This step is done recursively till cells having numbers are discovered.
Flood fill algorithm can be simply modeled as graph traversal problem, representing the given area as a matrix and considering every cell of that matrix as a vertex that is connected to points above it, below it, to right of it, and to left of it and in case of 8-connections, to the points at both diagonals also. For example, consider the image given below.
function DFS(x, y, visited, n, m)
if (x ≥ n OR y ≥ m)
return
if(x < 0 OR y < 0)
return
if(visisted[x][y] == True)
return
visited[x][y] = True
DFS(x-1, y-1, visited, n, m)
DFS(x-1, y, visited, n, m)
DFS(x-1, y+1, visited, n, m)
DFS(x, y-1, visited, n, m)
DFS(x, y+1, visited, n, m)
DFS(x+1, y-1, visited, n, m)
DFS(x+1, y, visited, n, m)
DFS(x+1, y+1, visited, n, m)Friday, 15 April 2022
HeapSort as a combine subroutine of MergeSort
This article is taken from my ResearchGate profile. Link . This article is under Creative Commons Licence By (cc-by).
Wednesday, 13 April 2022
Divide and Conquer Algorithm for Linear Search
Saturday, 2 April 2022
Sum of an array using Divide and Conquer algorithm
Sum of an array using Divide and Conquer algorithm (DAC)
Finding sum of an array is a very easy task, just add all the elements in a single pass! Time and space complexity associated with this approach is linear and constant respectively.
In this post, I will show how we can find the sum of an array using Divide and Conquer algorithm.
First of all I would like you to have a glance at What a Divide and Conquer algorithm is?
Divide and Conquer is an algorithmic strategy used to find solution of a particular problem by recursively breaking down the problem into sub-problems of same type, and when the base condition of the recursion is hit, assemble the results of those sub-problems to form the solution of the original problem.
This strategy often brings considerable optimizations in some while solving some problems. Some popular algorithms that implements Divide and Conquer are:
1. Quicksort
2. Mergesort
3. Binary Exponentiation
4. Quickselect
5. Straseen's algorithm (Matrix Multiplication)
6. Binary Search
The Divide and Conquer strategy can be divided into three parts:
1. Divide: Divide the larger problem into smaller sub-problems of same type.
2. Conquer: Solve the sub-problems by recursively calling until solved.
3. Combine: Combine the sub-problems to get the final solution of the whole problem.
It becomes very easy to find the time complexity of DAC algorithms using Master Theorem.
Image given below shows how to solve recurrence relations using Master Method.
Now, coming back to the question. We have to find the sum of an array using DAC.
Let's formulate the solution:
Sum of an array can be found using breaking the array into two halves, add them and return the sum. Recursively break down the problem into two halves, we the array size becomes 1, return the value of that single element. So simple!
output: 27
I have traced the algorithm:
Now, let's find the time complexity of this algorithm.
Hence, the time complexity is .








