0% found this document useful (0 votes)
5 views

Dsa Class01

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

Dsa Class01

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

Data Structures & Applications:

ID2230

Lecture 01

30 Jul 2024
Course Logistics

Instructors: N.R.Aravind, Rakesh Venkat


Web-page:
http://www.iith.ac.in/ aravind/id2230
Course platform: Canvas
Syllabus
Announcements
Assignment submission
Grading Policy

Programming Assignments: 25%


Three Quizzes: 20+20+35=75%
Recommended Attendance: 70%
References

Introduction to Algorithms by Cormen,


Leicerson, Rivest, Stein (CLRS)
Fundamentals of Data Structures in C by
Horowitz, Sahni, Anderson-Freed
Online resources on Canvas, webpage
Data Structures

Organize information for


efficient retrieval & processing*

* for computational problems


DSA in computing

Databases
Operating Systems
Computer Networks
Image processing
Computer Graphics
Cybersecurity
Data Science & Machine Learning
Goals

Learn and implement classical data structures.


Apply to solve computational problems.
Computational & Algorithmic Problems
Searching for text in a file
Searching for substrings

Input: T = T [1]...T [n], S = S[1] . . . S[k]


Output: All indices i where
T [i] . . . T [i + k − 1] = S[1] . . . S[k].
Searching for substrings

Algorithm Naive Substring Search


1: for i=1 to n-k+1 do
2: if T [i] . . . T [i + k − 1] = S[1] . . . S[k] then
3: Report i
4: end if
5: end for
Finding nearby restaurants
Finding near points
Finding near points

Input: P = {P1 , . . . , Pn } ⊂ R2 , a query point q,


distance d ∈ R+ .
Output: All points in S within distance of d from p.
Solving equations

Input: A polynomial f (x)


Output: All real roots of f
Finding the smallest element

Input: A sequence a1 , a2 , . . . , an of real numbers


Output: The smallest number in the sequence
Sorting numbers

Input: A sequence a1 , . . . , an of numbers


Output: A permutation b1 , . . . , bn of the input such
that b1 ≤ b2 ≤ . . . ≤ bn .
Sorting numbers

Input: 12, 30, -20, 5, 18, 42, 17


Output: -20, 5, 12, 17, 18, 30, 42
Algorithm: Insertion Sort

Input: 12, 30, -20, 5, 18, 42, 17


1 12, 30
Algorithm: Insertion Sort

Input: 12, 30, -20, 5, 18, 42, 17


1 12, 30
2 12, 30, -20
Algorithm: Insertion Sort

Input: 12, 30, -20, 5, 18, 42, 17


1 12, 30
2 12, 30, -20
3 -20, 12, 30
Algorithm: Insertion Sort

Input: 12, 30, -20, 5, 18, 42, 17


1 12, 30
2 12, 30, -20
3 -20, 12, 30, 5
Algorithm: Insertion Sort

Input: 12, 30, -20, 5, 18, 42, 17


1 12, 30
2 12, 30, -20
3 -20, 12, 30, 5
4 -20, 5, 12, 30
Algorithm: Insertion Sort

Input: 12, 30, -20, 5, 18, 42, 17


1 12, 30
2 12, 30, -20
3 -20, 12, 30, 5
4 -20, 5, 12, 30, 18
Algorithm: Insertion Sort

Input: 12, 30, -20, 5, 18, 42, 17


1 12, 30
2 12, 30, -20
3 -20, 12, 30, 5
4 -20, 5, 12, 30, 18
5 -20, 5, 12, 18, 30
Algorithm: Insertion Sort

Input: 12, 30, -20, 5, 18, 42, 17


1 12, 30
2 12, 30, -20
3 -20, 12, 30, 5
4 -20, 5, 12, 30, 18
5 -20, 5, 12, 18, 30, 42
Algorithm: Insertion Sort

Input: 12, 30, -20, 5, 18, 42, 17


1 12, 30
2 12, 30, -20
3 -20, 12, 30, 5
4 -20, 5, 12, 30, 18
5 -20, 5, 12, 18, 30, 42
6 -20, 5, 12, 18, 30, 42, 17
Algorithm: Insertion Sort

Input: 12, 30, -20, 5, 18, 42, 17


1 12, 30
2 12, 30, -20
3 -20, 12, 30, 5
4 -20, 5, 12, 30, 18
5 -20, 5, 12, 18, 30, 42
6 -20, 5, 12, 18, 30, 42, 17
7 -20, 5, 12, 17, 18, 30, 42
Algorithm: Insertion Sort

Algorithm
1: procedure Insertion Sort(a1 , a2 , . . . , an )
2: for i=2 to n do
3: Insert ai at j + 1: aj < ai < aj+1 .
4: end for
5: end procedure
Algorithm: The Insertion Step

-20, 5, 12, 18, 30, 42, 17


Algorithm: The Insertion Step

-20, 5, 12, 18, 30, 42, 17


-20, 5, 12, 18, 30, 17, 42
Algorithm: The Insertion Step

-20, 5, 12, 18, 30, 42, 17


-20, 5, 12, 18, 30, 17, 42
-20, 5, 12, 18, 17, 30, 42
Algorithm: The Insertion Step

-20, 5, 12, 18, 30, 42, 17


-20, 5, 12, 18, 30, 17, 42
-20, 5, 12, 18, 17, 30, 42
-20, 5, 12, 17, 18, 30, 42
Algorithm: The Insertion Step

-20, 5, 12, 18, 30, 42, 17


-20, 5, 12, 18, 30, 17, 42
-20, 5, 12, 18, 17, 30, 42
-20, 5, 12, 17, 18, 30, 42
-20, 5, 12, 17, 18, 30, 42
Algorithm: Insertion Step

Algorithm
1: procedure Insertion Step(a1 , a2 , . . . , an , i)
2: for j=i-1 to 1 do
3: If aj+1 < aj , swap aj+1 and aj .
4: end for
5: end procedure
Algorithm: Insertion Sort

Algorithm
1: procedure Insertion Sort(a1 , a2 , . . . , an )
2: for i=2 to n do
3: for j=i-1 to 1 do
4: If aj+1 < aj , swap aj+1 and aj .
5: end for
6: end for
7: end procedure
Correctness and efficiency

Why is Insertion Sort correct?


How fast is Insertion Sort?
Correctness of Insertion Sort

At the end of iteration i:


1 The first i elements of a[] are in sorted order.
2 The first i elements of a[] are unchanged.
3 The remaining elements of a[] are unchanged.
Measuring efficiency

How fast is Insertion Sort? [3 GHz]


n Time (best) Time (Worst) Time(Average)
104 0.0001 sec 0.05 sec 0.025 sec
5
10
Measuring efficiency

How fast is Insertion Sort? [3 GHz]


n Time (best) Time (Worst) Time(Average)
104 0.0001 sec 0.05 sec 0.025 sec
5
10 0.001 sec 5 sec 2.5 sec
6
10
Measuring efficiency

How fast is Insertion Sort? [3 GHz]


n Time (best) Time (Worst) Time(Average)
104 0.0001 sec 0.05 sec 0.025 sec
5
10 0.001 sec 5 sec 2.5 sec
6
10 0.01 sec 8 min 4 min
Measuring efficiency

How fast is Insertion Sort? [3 GHz]


n Time (best) Time (Worst) Time(Average)
104 0.0001 sec 0.05 sec 0.025 sec
5
10 0.001 sec 5 sec 2.5 sec
6
10 0.01 sec 8 min 4 min
7
10 0.1 sec 13 hrs 6.5 hrs
8
10 1 sec 54 days 27 days
Measuring Efficiency

Problems with experimental measures


Processor speed, hardware
Programming language
Sample inputs may not be representative.
Time complexity analysis

Measure: No. of unit cost


operations/instructions
Function of input size
Worst-case inputs
Time complexity: Insertion Step

Inserting ai into a1 < a2 < . . . < ai−1 :


Comparison + Swap:
Time complexity: Insertion Step

Inserting ai into a1 < a2 < . . . < ai−1 :


Comparison + Swap: 4 operations
Worst-case number of times: i − 1
Cost of insertion step for ai : ≤ 4(i − 1)
Time complexity: Insertion Sort

Cost of insertion step for ai : ≤ 4(i − 1)


Total cost of insertion sort:
n
X
4(i − 1) = 2n(n − 1)
i=2
= 2n2 − 2n
RAM model

Operations taking constant time


Memory access in arrays
Assignment
Arithmetic operations
Logical operations
...for items that fit into one word of memory.
What’s the Input Size?

Add a m-bit integer and a n-bit integer.


Sort n numbers.
Multiply two n by n matrices.
Summary

Data structures: organize information for


efficient processing
Computational problems → Algorithms →
Data structures → Programs
Describe algorithms using English plus
pseudocode.
Efficiency ∼ time complexity: # of unit
operations

You might also like