Data Structures and Algorithms: CS210/CS210A
Data Structures and Algorithms: CS210/CS210A
(CS210/CS210A)
Lecture 1:
• An overview and motivation for the course
• some concrete examples.
1
The website of the course
moodle.cse.iitk.ac.in
CSE
CS210: Data Structures and Algorithms
(guest login allowed)
2
Those were the golden moments…
3
Prerequisite of this course
4
Salient features of the course
A processor (CPU)
speed = few GHz
(a few nanoseconds to execute an instruction)
6
A simplifying assumption
(for the rest of the lecture)
7
8
What is an algorithm ?
Definition:
A finite sequence of well defined instructions
required to solve a given computational problem.
9
10
Revisiting problems from ESC101
11
Problem 1:
Fibonacci numbers
Fibonacci numbers
𝑭(0) = 0;
𝑭(1) = 1;
𝑭(𝑛) = 𝑭(𝑛 − 1) + 𝑭(𝑛 − 2) for all 𝑛 > 1;
𝑭 𝑛 ≈ 𝑎 ⋅ 𝑏𝑛
13
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;
14
Recursive algorithm for 𝑭(𝒏)
Rfib(n)
{ if n=0 return 0;
else if n=1 return 1;
else return(Rfib(n-1) + Rfib(n-2))
}
15
Homework 1
(compulsory)
Input: a number 𝒏
𝒏 : long long int (64 bit integer).
Output: F(𝒏) mod 𝟐𝟎𝟏𝟒
1 minute
10 minutes
60 minutes
16
Problem 2:
Subset-sum problem
𝒏/𝟐
The fastest existing algorithm till date : 𝟐 instructions
17
Problem 3:
Sorting
A fact:
A significant fraction of the code of all the software is for sorting or searching only.
18
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)
19
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 ?)
20
21
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
22
Aim of a data structure ?
23
Range-Minima Problem
A Motivating example
to realize the importance of data structures
24
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[𝑗]
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
(we shall discuss these problems sometime in this course or the next level
course CS345)
26
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
27
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
28
Range-Minima Problem
• Compact
(nearly the same size as the input array A)
• Can answer each query efficiently ?
(a few nanoseconds per query)
29
Range-1-Query
Determining if a rectangle has at least one 1 ?
• Data structure: a few tables.
• Query time: a few nanoseconds.
0 0 0 1 1 0 1 0
0 1 1 0 1 0 0 1
1 0 1 0 0 1 1 1
1 0 0 0 0 1 0 0
0 1 0 1 0 0 0 0
0 0 1 0 1 0 0 1
1 0 0 0 0 0 0 1
0 0 1 1 1 0 0 1
Any idea about
when this result in a Conference in
was derived ? 2015
Data structures to be covered in this
course
Elementary Data Structures
– Array
– List
– Stack
– Queue
31
• Look forward to working with all of you to make this course
enjoyable.
32