+ All Categories
Transcript
Page 1: Data Structures and Algorithms - E & ICT Academy β€Ί wp-content β€Ί uploads β€Ί CS210-Data...Data Structures and Algorithms (CS210A) Lecture 6: β€’ Design of O(𝑛) time algorithm

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

Page 2: Data Structures and Algorithms - E & ICT Academy β€Ί wp-content β€Ί uploads β€Ί CS210-Data...Data Structures and Algorithms (CS210A) Lecture 6: β€’ Design of O(𝑛) time algorithm

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

Page 3: Data Structures and Algorithms - E & ICT Academy β€Ί wp-content β€Ί uploads β€Ί CS210-Data...Data Structures and Algorithms (CS210A) Lecture 6: β€’ Design of O(𝑛) time algorithm

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 ?

Page 4: Data Structures and Algorithms - E & ICT Academy β€Ί wp-content β€Ί uploads β€Ί CS210-Data...Data Structures and Algorithms (CS210A) Lecture 6: β€’ Design of O(𝑛) time algorithm

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 ?

Page 5: Data Structures and Algorithms - E & ICT Academy β€Ί wp-content β€Ί uploads β€Ί CS210-Data...Data Structures and Algorithms (CS210A) Lecture 6: β€’ Design of O(𝑛) time algorithm

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

Page 6: Data Structures and Algorithms - E & ICT Academy β€Ί wp-content β€Ί uploads β€Ί CS210-Data...Data Structures and Algorithms (CS210A) Lecture 6: β€’ Design of O(𝑛) time algorithm

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(𝒏) ?

Page 7: Data Structures and Algorithms - E & ICT Academy β€Ί wp-content β€Ί uploads β€Ί CS210-Data...Data Structures and Algorithms (CS210A) Lecture 6: β€’ Design of O(𝑛) time algorithm

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)

Page 8: Data Structures and Algorithms - E & ICT Academy β€Ί wp-content β€Ί uploads β€Ί CS210-Data...Data Structures and Algorithms (CS210A) Lecture 6: β€’ Design of O(𝑛) time algorithm

Local minima in a grid in O(𝒏) time

Bisect alternatively along rows and column

8

INCORRECT

Page 9: Data Structures and Algorithms - E & ICT Academy β€Ί wp-content β€Ί uploads β€Ί CS210-Data...Data Structures and Algorithms (CS210A) Lecture 6: β€’ Design of O(𝑛) time algorithm

Local minima in a grid in O(𝒏) time

9

50

60

98 97 99 96 95

94

93

92

91

90

89

Page 10: Data Structures and Algorithms - E & ICT Academy β€Ί wp-content β€Ί uploads β€Ί CS210-Data...Data Structures and Algorithms (CS210A) Lecture 6: β€’ Design of O(𝑛) time algorithm

Lessons learnt

β€’ No hand-waving works for iterative algorithms

β€’ We must be sure about – What is P(𝑖)

– Proof of P(𝑖).

10

Page 11: Data Structures and Algorithms - E & ICT Academy β€Ί wp-content β€Ί uploads β€Ί CS210-Data...Data Structures and Algorithms (CS210A) Lecture 6: β€’ Design of O(𝑛) time algorithm

Let us revisit the (𝒏 π₯𝐨𝐠 𝒏) algorithm

11

Page 12: Data Structures and Algorithms - E & ICT Academy β€Ί wp-content β€Ί uploads β€Ί CS210-Data...Data Structures and Algorithms (CS210A) Lecture 6: β€’ Design of O(𝑛) time algorithm

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(𝑖) ?

Page 13: Data Structures and Algorithms - E & ICT Academy β€Ί wp-content β€Ί uploads β€Ί CS210-Data...Data Structures and Algorithms (CS210A) Lecture 6: β€’ Design of O(𝑛) time algorithm

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

Page 14: Data Structures and Algorithms - E & ICT Academy β€Ί wp-content β€Ί uploads β€Ί CS210-Data...Data Structures and Algorithms (CS210A) Lecture 6: β€’ Design of O(𝑛) time algorithm

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 ?

Page 15: Data Structures and Algorithms - E & ICT Academy β€Ί wp-content β€Ί uploads β€Ί CS210-Data...Data Structures and Algorithms (CS210A) Lecture 6: β€’ Design of O(𝑛) time algorithm

Local minima in a grid Alternate O(𝒏 π₯𝐨𝐠 𝒏) algorithm

15

𝑳 𝑹

7

5

Page 16: Data Structures and Algorithms - E & ICT Academy β€Ί wp-content β€Ί uploads β€Ί CS210-Data...Data Structures and Algorithms (CS210A) Lecture 6: β€’ Design of O(𝑛) time algorithm

Local minima in a grid Alternate O(𝒏 π₯𝐨𝐠 𝒏) algorithm

16

𝑳 𝑹

7

9

smallest

7

5

18

Page 17: Data Structures and Algorithms - E & ICT Academy β€Ί wp-content β€Ί uploads β€Ί CS210-Data...Data Structures and Algorithms (CS210A) Lecture 6: β€’ Design of O(𝑛) time algorithm

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

Page 18: Data Structures and Algorithms - E & ICT Academy β€Ί wp-content β€Ί uploads β€Ί CS210-Data...Data Structures and Algorithms (CS210A) Lecture 6: β€’ Design of O(𝑛) time algorithm

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

Page 19: Data Structures and Algorithms - E & ICT Academy β€Ί wp-content β€Ί uploads β€Ί CS210-Data...Data Structures and Algorithms (CS210A) Lecture 6: β€’ Design of O(𝑛) time algorithm

Range-Minima Problem

A Motivating example

to realize the importance of data structures

19

Page 20: Data Structures and Algorithms - E & ICT Academy β€Ί wp-content β€Ί uploads β€Ί CS210-Data...Data Structures and Algorithms (CS210A) Lecture 6: β€’ Design of O(𝑛) time algorithm

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

Page 21: Data Structures and Algorithms - E & ICT Academy β€Ί wp-content β€Ί uploads β€Ί CS210-Data...Data Structures and Algorithms (CS210A) Lecture 6: β€’ Design of O(𝑛) time algorithm

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


Top Related