0% found this document useful (0 votes)
65 views30 pages

Lec01 Motivation

This document discusses the range-minima problem and motivates the need for efficient data structures. The range-minima problem involves preprocessing an array to answer queries about the minimum element between two indices. Naively solving each query takes milliseconds, while precomputing all answers uses too much space. The goal is a compact data structure that answers queries in nanoseconds. This problem illustrates how data structures can optimize operations like queries.

Uploaded by

Atharv Agarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views30 pages

Lec01 Motivation

This document discusses the range-minima problem and motivates the need for efficient data structures. The range-minima problem involves preprocessing an array to answer queries about the minimum element between two indices. Naively solving each query takes milliseconds, while precomputing all answers uses too much space. The goal is a compact data structure that answers queries in nanoseconds. This problem illustrates how data structures can optimize operations like queries.

Uploaded by

Atharv Agarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Data Structures and Algorithms

(ESO207)

Lecture 1:
• An overview and motivation for the course
• some concrete examples.

1
Some Information
ESO207/ESO207A: Data Structures and Algorithms

Course Website: moodle.cse.iitk.ac.in


Instructor email: rtewari@cse.iitk.ac.in
TA in-charge: Ronak Bhadra (ronakb@cse.iitk.ac.in)

2
Acknowledgment

Thanks to Prof Surender Baswana for allowing


me to use and modify his lecture slides.

3
Prerequisite of this course

• A good command on Programming in C


– Programs involving arrays
– Recursion
– Linked lists (preferred)

• Fascination for solving Puzzles

4
Salient features of the course

• Every concept We shall re-invent in the class itself.

• Solving each problem Through discussion in the class.

solution will emerge naturally if we ask


right set of questions
and then try to find their answers.

… so that finally it is a concept/solution derived by you


and not a concept from some scientist/book/teacher.

Isn’t that nice 


5
Let us open a desktop/laptop

A processor (CPU)
speed = few GHz
(a few nanoseconds to execute an instruction)

Internal memory (RAM)


size = a few GB (Stores a billion bytes/words)
speed = a few GHz( a few nanoseconds to read a byte/word)

External Memory (Hard Disk Drive)


size = a few tera bytes
speed : seek time = miliseconds
transfer rate= around billion bits per second

6
A simplifying assumption
(for the rest of the lecture)

It takes around a few nanoseconds to execute an instruction.

(This assumption is well supported by the modern day computers)

7
EFFICIENT ALGORITHMS

8
What is an algorithm ?

Definition:
A finite sequence of well defined instructions
required to solve a given computational problem.

A prime objective of the course:


Design of efficient algorithms

9
WHY SHOULD WE CARE FOR
EFFICIENT ALGORITHMS

WE HAVE PROCESSORS RUNNING AT GIGAHERTZ?

10
Revisiting problems from ESC101

11
Problem 1:
Fibonacci numbers

Fibonacci numbers
;
;
for all ;

An easy exercise : Using induction or otherwise, show that


>

Algorithms you must have implemented for computing (n) :


• Iterative
• recursive
12
Iterative Algorithm for ()

IFib(n)
if n=0 return 0;
else if n=1 return 1;
else {
a  0; b 1;
For(i=2 to n) do
{ temp  b;
b a+b;
a temp;
}
}
return b;

13
Recursive algorithm for ()

Rfib(n)
{ if n=0 return 0;
else if n=1 return 1;
else return(Rfib(n-1) + Rfib(n-2))
}

14
Homework 1
(compulsory)

Write a C program for the following problem:

Input: a number
: long long int (64 bit integer).
Output: F() mod

Time Taken Largest for Rfib Largest for IFib

1 minute
10 minutes
60 minutes

15
Problem 2:
Subset-sum problem

Input: An array A storing numbers, and a number

A 21 2123
20

Output: Determine if there is a subset of numbers from A whose sum is .

The fastest existing algorithm till date : instructions

• Time for At least an year


• Time for At least 1000 years
on the fastest existing computer.

16
Problem 3:
Sorting

Input: An array A storing n numbers.


Output: Sorted A

A fact:
A significant fraction of the code of all the software is for sorting or searching only.

To sort 10 million numbers on the present day computers


• Selection sort will take at least a few hours.
• Merge sort will take only a few seconds.
• Quick sort will take ??? .

17
How to design efficient algorithm for a problem ?
Design of algorithms and data structures is also
an Art

Requires:
• Creativity
• Hard work
• Practice
• Perseverance (most important) 18
Summary of Algorithms
• There are many practically relevant problems for which there does not
exist any efficient algorithm till date . (How to deal with them ?)

• Efficient algorithms are important for theoretical as well as practical


purposes.

• Algorithm design is an art which demands a lot of creativity, intuition, and


perseverance.

• More and more applications in real life require efficient algorithms


– Search engines like Google exploits many clever algorithms.

19
THE DATA STRUCTURES

20
An Example
Given: a telephone directory storing telephone no. of hundred million persons.
Aim: to answer a sequence of queries of the form
“what is the phone number of a given person ?”.

Solution 1 :
Keep the directory in an array.
do sequential search for each query.
Time per query: around 1/10th of a second

Solution 2:
Keep the directory in an array, and sort it according to names,
do binary search for each query.

Time per query: less than 100 nanoseconds

21
Aim of a data structure ?

To store/organize a given data in the memory of computer so that


each subsequent operation (query/update) can be performed quickly ?

22
Range-Minima Problem

A Motivating example
to realize the importance of data structures

23
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() : report the smallest element from A[],…,A[]

Let = one million.


No. of queries = 10 millions
Range-Minima() = -6

A 3 5 1 8 19 0 -1 30 99 -6 10 2 40 27 44 67

𝑖=4 𝑗=11
Range-Minima Problem

Applications:

• Computational geometry

• String matching

• As an efficient subroutine in a variety of algorithms

25
Range-Minima Problem
Solution 1:
Answer each query in a brute force manner using A itself.
Range-minima-trivial(i,j)
{ temp  i+1; Time for answering all queries:
min  A[i]; a few hours
While(temp <= j)
{ if (min > A[temp])
min  A[temp];
temp temp+1;
}
return min
}
Time taken to answer a query: few milliseconds

26
Range-Minima Problem
Solution 2:
Compute and store answer for each possible query in a × matrix B.
𝑗
Solution 2 is
Theoretically efficient but
𝑖 3
practically impossible

Size of B is too large to be


B kept in RAM. So we shall have
to keep most of it in the Hard
disk drive. Hence it will take
a few milliseconds per query.

B[][] stores the smallest element from A[],…,A[]


Space : roughly words.
27
Range-Minima Problem

Question: Does there exist a data structure for Range-minima which is

• Compact
(nearly the same size as the input array A)
• Can answer each query efficiently ?
(a few nanoseconds per query)

Homework 2: Ponder over the above question.


(we shall solve it soon)

28
Data structures to be covered in this
course
Elementary Data Structures
– Array
– List
– Stack
– Queue

Hierarchical Data Structures


– Binary Heap
– Binary Search Trees
Most fascinating and
powerful data structures
Augmented Data Structures
29
• Look forward to working with all of you to make this course
enjoyable.

• This course will be light in contents (no formulas)


But it will be very demanding too.

• In case of any difficulty during the course,


just drop me an email without any delay.
I shall be happy to help 

30

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy