Data Structures and Algorithms (CS210A)
Lecture 6: β’ Design of O(π) time algorithm for Local Minima in a grid
β’ Data structure gem: Range minima Problem
1
Local minima in an array (Proof of correctness)
Theorem: A local minima in an array storing π distinct elements
can be found in O(log π) time.
2
Local minima in a grid (extending the solution from 1-D to 2-D)
Search for a local minima in the column M[β, πππ ]
Homework:
Use this idea to design an O(π log π) time algorithm for this problem.
β¦ and do not forget to prove its correctness . 3
πππ
7 9
Execute Explore() from M[π, πππ + π ]
A local minima exists in this region. Why ?
Smallest element of the column
What if there is no local minima in the
entire column.
π
Under what circumstances even this smallest element is not
a local minima ?
Local minima in a grid
Int Local-minima-in-grid(M) // returns the column containing a local minima
{ L 0;
R π β π;
found FALSE;
while(not found)
{ πππ (L + R)/2;
If (M[*, πππ ] has a local minima) found TRUE;
else {
let M[π, πππ ] be the smallest element in M[*, πππ ]
if(M[π, πππ + π] < M[π, πππ ]) ?? ;
else ??
}
}
return πππ ;
}
Running time of the algorithm = O(π log π)
4
L πππ + π
R πππ β π
O(π) time
O(π) time O(π) time
Proof of correctness ?
Local minima in a grid (Proof of Correctness)
P(π) :
βA local minima of grid M exists in M[π³,β¦, πΉ].β
5
π³ πΉ
β π such that M[π, π³] < M[β, π³ β π]β
π
πβ²
β πβ² such that M[πβ², πΉ] < M[β, πΉ + π]β and
Local minima in a grid
Theorem: A local minima in an πΓπ grid storing distinct elements can be found in O(π log π) time.
6
How to improve it to O(π) ?
Local minima in a grid in O(π) time
Let us carefully look at the calculations of the running time of the current algo.
ππ + ππ + ππ + β¦ β¦ + ππ = O(π π₯π¨π π)
What about the following series
ππ
π+ π
π
π+ π
π
π+ β¦ β¦ + ππ = ?
It is 2ππ = O(π) .
Get an !DEA from this series to modify our current algorithm
7
(log π terms)
(log π terms)
Local minima in a grid in O(π) time
Bisect alternatively along rows and column
8
INCORRECT
Local minima in a grid in O(π) time
9
50
60
98 97 99 96 95
94
93
92
91
90
89
Lessons learnt
β’ No hand-waving works for iterative algorithms
β’ We must be sure about β What is P(π)
β Proof of P(π).
10
Let us revisit the (π π₯π¨π π) algorithm
11
Local minima in a grid (Proof of Correctness)
P(π) :
βA local minima of grid M exists in M[π³,β¦, πΉ].β
12
π³ πΉ
β π such that M[π, π³] < M[β, π³ β π]β
π
πβ²
β πβ² such that M[πβ², πΉ] < M[β, πΉ + π]β and
Is there an alternate and simpler P(π) ?
At any stage, what guarantees the existence of local minima in the region ?
β’ At each stage, our algorithm may maintain a cell in the region
whose value is smaller than all elements lying at the boundary of the region ?
(Note the boundary lies outside the region) 13
Region
Boundary
Local minima in a grid Alternate O(π π₯π¨π π) algorithm)
14
π³ πΉ
7
9
smallest
5
11
7
What will be the cell in the
beginning of 1st iteration ?
How will we maintain it after each iteration ?
Local minima in a grid Alternate O(π π₯π¨π π) algorithm
15
π³ πΉ
7
5
Local minima in a grid Alternate O(π π₯π¨π π) algorithm
16
π³ πΉ
7
9
smallest
7
5
18
Local minima in a grid Alternate O(π π₯π¨π π) algorithm
β’ Write a neat pseudocode of this algorithm and prove its correctness.
17
π³ πΉ
7
5 Extending it to O(π) is easy now
Theorem:
Given an π Γ π grid storing ππ distinct elements, a local minima can be found in O(π) time.
Question:
On which algorithm paradigm, was this algorithm based on ?
β’ Greedy
β’ Divide and Conquer
β’ Dynamic Programming
Range-Minima Problem
A Motivating example
to realize the importance of data structures
19
i=4 j=11
3 5 1 8 19 0 -1 30 99 -6 10 2 40 27 44 67 A
Range-Minima Problem
Given: an array A storing π numbers,
Aim: a data structure to answer a sequence of queries of the following type
Range-minima(i,j) : report the smallest element from A[i],β¦,A[j]
Let A store one million numbers
Let the number of queries be 10 millions
Range-Minima(i,j) = -6
Range-Minima Problem
Question: Does there exist a data structure which is
β’ Compact
(O(π log π) size )
β’ Can answer each query efficiently ?
(O(π) time)
Homework : Ponder over the above question.
(we shall solve it in the next class)
21