SlideShare a Scribd company logo
1 of 45
Download to read offline
Data Structures and Algorithms 
… 
Algorithm: Outline, the essence of a computational procedure, step-by-step instructions 
… 
Program: an implementation of an algorithm in some programming language 
… 
Data structure: Organizationof data needed to solve the problem
Algorithmic problem 
…Infinite number of input instancessatisfying the specification. For eg: A sorted, non-decreasing sequence of natural numbers of non-zero, finite length: 
… 
1, 20, 908, 909, 100000, 1000000000. 
… 
3. 
Specification of input 
? 
Specification of output as a function of input
Algorithmic Solution 
…Algorithm describes actions on the input instance 
… 
Infinitely many correct algorithms for the same algorithmic problem 
Input instance, adhering to the specification 
Algorithm 
Output related to the input as required
What is a Good Algorithm? 
…Efficient: 
… 
Running time 
… 
Space used 
… 
Efficiency as a function of input size: 
… 
The number of bits in an input number 
… 
Number of data elements (numbers, points)
Measuring the Running Time 
Experimental Study 
… 
Write a programthat implements the algorithm 
… 
Run the program with data sets of varying size and composition. 
… 
Use a method like System.currentTimeMillis()to get an accurate measure of the actual running time. 
How should we measure the running time of an algorithm?
Limitations of Experimental Studies 
… 
It is necessary to implementand test the algorithm in order to determine its running time. 
… 
Experiments can be done only on a limited set of inputs, and may not be indicative of the running time on other inputs not included in the experiment. 
… 
In order to compare two algorithms, the same hardware and software environmentsshould be used.
Beyond Experimental Studies 
We will develop a general methodologyfor analyzing running time of algorithms. This approach 
… 
Uses a high-level descriptionof the algorithm instead of testing one of its implementations. 
… 
Takes into account all possible inputs. 
… 
Allows one to evaluate the efficiency of any algorithm in a way that is independent of the hardware and software environment.
Pseudo-Code 
…A mixture of natural language and high-level programming concepts that describes the main ideas behind a generic implementation of a data structure or algorithm. 
… 
Eg: AlgorithmarrayMax(A, n): 
Input:An array A storing n integers. 
Output:The maximum element in A. 
currentMax ←A[0] 
for i ←1to n-1do 
ifcurrentMax< A[i] thencurrentMax ←A[i] 
returncurrentMax
Pseudo-Code 
It is more structured than usual prose but 
less formal than a programming language 
… 
Expressions: 
… 
use standard mathematical symbols to describe numeric and boolean expressions 
… 
use ←for assignment (“=” in Java) 
… 
use = for the equality relationship (“==” in Java) 
… 
Method Declarations: 
… 
Algorithmname(param1, param2)
Pseudo Code 
…Programming Constructs: 
… 
decision structures: if ... then ... [else ... ] 
… 
while-loops: while ... do 
… 
repeat-loops: repeat ... until ... 
… 
for-loop: for ... do 
… 
array indexing: A[i], A[i,j] 
… 
Methods: 
… 
calls: object method(args) 
… 
returns: return value
Analysis of Algorithms 
…Primitive Operation:Low-level operation independent of programming language. Can be identified in pseudo-code. For eg: 
… 
Data movement (assign) 
… 
Control (branch, subroutine call, return) 
… 
arithmetic an logical operations (e.g. addition, comparison) 
… 
By inspecting the pseudo-code, we can count the number of primitive operations executed by an algorithm.
Sort 
Example: Sorting 
INPUT 
sequence of numbers 
a1, a2, a3,….,an 
b1,b2,b3,….,bn 
OUTPUT 
a permutation of the sequence of numbers 
2 5 4 10 7 
2 45 7 10 
Correctness (requirements for the output) 
For any given input the algorithm halts with the output: 
• 
b1< b2< b3< …. < bn 
•b1, b2, b3, …., bnis a permutation ofa1, a2, a3,….,an 
Running time 
Depends on 
• 
number of elements(n) 
•how (partially) sortedthey are 
• 
algorithm
Insertion Sort 
A 
1 
n 
j 
3 
6 
8 
4 
9 
7 
2 
5 
1 
i 
Strategy 
• 
Start “empty handed” 
•Insert a card in the rightposition of the already sortedhand 
•Continue until all cards areinserted/sorted 
INPUT: A[1..n] –an array of integers 
OUTPUT: a permutation of A such that A[1]≤ A[2]≤ …≤A[n] 
forj←2 to n do 
key ← A[j] 
Insert A[j]into the sorted sequence 
A[1..j-1] 
i←j-1 
while i>0 and A[i]>key 
do A[i+1]←A[i] 
i-- 
A[i+1]←key
Analysis of Insertion Sort 
forj←2 to ndo 
key←A[j] 
Insert A[j]into the sorted 
sequence A[1..j-1] 
i←j-1 
while i>0 and A[i]>key 
do A[i+1]←A[i] 
i-- 
A[i+1] ←key 
costc1 
c2 
0 
c3 
c4 
c5 
c6 
c7 
timesnn-1n-1n-1n-1 
2njjt=Σ2(1)njjt=−Σ2(1)njjt=−Σ 
Total time = n(c1+c2+c3+c7) + Σnj=2tj(c4+c5+c6) 
–(c2+c3+c5+c6+c7)
Best/Worst/Average Case 
…Best case: elements already sorted; tj=1, running time = f(n),i.e., lineartime. 
… 
Worst case: elements are sorted in inverse order; tj=j, running time = f(n2),i.e., quadratictime 
… 
Average case: tj=j/2, running time = f(n2), i.e.,quadratictime 
Total time = n(c1+c2+c3+c7) + Σnj=2tj(c4+c5+c6) 
–(c2+c3+c5+c6+c7)
Best/Worst/Average Case (2) 
…For a specific size of input n, investigate running times for different input instances:
Best/Worst/Average Case (3) 
For inputs of all sizes: 
1n 
2n 
3n 
4n 
5n 
6n 
Input instance size 
Running time 
1 2 3 4 5 6 7 8 9 10 11 12 ….. 
best-case 
average-case 
worst-case
Best/Worst/Average Case (4) 
…Worst case is usually used: It is an upper- bound and in certain application domains (e.g., air traffic control, surgery) knowing the worst- casetime complexity is of crucial importance 
… 
For some algorithms worst caseoccurs fairly often 
… 
Average caseis often as bad as the worst case 
… 
Finding average case can be very difficult
Asymptotic Analysis 
…Goal: to simplify analysis of running time by getting rid of”details”, which may be affected by specific implementation and hardware 
… 
like “rounding”: 1,000,001≈1,000,000 
… 
3n2≈n2 
… 
Capturing the essence: how the running time of an algorithm increases with the size of the input in the limit. 
… 
Asymptotically more efficient algorithms are best for all but small inputs
Asymptotic Notation 
…The “big-Oh” O-Notation 
… 
asymptotic upper bound 
… 
f(n) is O(g(n)), if there exists constants cand n0, s.t. f(n) ≤c g(n)for n≥n0 
… 
f(n) and g(n) are functionsover non- negative integers 
… 
Used for worst-caseanalysis)(nf ()cgn⋅ 0n 
Input Size 
Running Time
Example 
For functions f(n)andg(n) there are positive constants cand n0such that: f(n) ≤ c g(n)for n ≥ n0 
conclusion: 
2n+6 is O(n).
Another Example 
On the other hand… 
n2is not O(n) because there is no cand n0such that: n2≤ cnfor n ≥ n0 
The graph to the right illustrates that no matter how large a cis chosen there is an nbig enough that n2 > cn) .
Asymptotic Notation 
…Simple Rule: Drop lower order terms and constant factors. 
… 
50 n log n is O(n log n) 
… 
7n -3 is O(n) 
… 
8n2log n + 5n2+ n is O(n2log n) 
… 
Note: Even though (50 n log n) isO(n5), it is expected that such an approximation be of as small an order as possible
Asymptotic Analysis of Running Time 
…Use O-notation to express number of primitive operations executed as function of input size. 
… 
Comparing asymptotic running times 
… 
an algorithm that runs in O(n) time is better than one that runs in O(n2) time 
… 
similarly, O(log n) is better than O(n) 
… 
hierarchy of functions: log n < n < n2< n3< 2n 
… 
Caution!Beware of very large constant factors. An algorithm running in time 1,000,000 n is still O(n) but might be less efficient than one running in time 2n2, which is O(n2)
Example of Asymptotic Analysis 
AlgorithmprefixAverages1(X): 
Input: An n-element array X of numbers. 
Output:An n-element array A of numbers such that A[i] is the average of elements X[0], ... , X[i]. 
fori ←0 ton-1 do 
a ←0 
forj ←0 toi do 
a ←a + X[j] 
A[i] ←a/(i+1) 
returnarray A 
Analysis: running time is O(n2) 
1 
step 
i iterations with i=0,1,2...n- 
1 
n iterations
A Better Algorithm 
AlgorithmprefixAverages2(X): 
Input: An n-element array X of numbers. 
Output:An n-element array A of numbers such 
that A[i] is the average of elements X[0], ... , X[i]. 
s ←0 
fori ←0 ton do 
s ←s + X[i] 
A[i] ←s/(i+1) 
returnarray A 
Analysis: Running time is O(n)
Asymptotic Notation (terminology) 
…Special classes of algorithms: 
… 
Logarithmic: O(log n) 
… 
Linear: O(n) 
… 
Quadratic: O(n2) 
… 
Polynomial:O(nk), k ≥ 1 
… 
Exponential: O(an), a > 1 
… 
“Relatives” of the Big-Oh 
… 
Ω(f(n)): Big Omega-asymptotic lowerbound 
… 
Θ(f(n)): Big Theta-asymptotic tightbound
…The “big-Omega” Ω− Notation 
… 
asymptotic lower bound 
… 
f(n) isΩ(g(n))if there exists constants cand n0, s.t. c g(n) ≤f(n) for n ≥n0 
… 
Used to describe best- case running times or lower bounds for algorithmic problems 
… 
E.g., lower-bound for searching in an unsorted array is Ω(n). 
Input Size 
Running Time )(nf ()cgn⋅ 0n 
Asymptotic Notation
…The “big-Theta” Θ−Notation 
… 
asymptotically tight bound 
… 
f(n) =Θ(g(n))if there exists constants c1, c2,and n0, s.t. c1g(n) ≤f(n) ≤c2g(n) for n ≥ n0 
… 
f(n)is Θ(g(n)) if and only if f(n)is Ο(g(n)) and f(n)is Ω(g(n)) 
… 
O(f(n)) is often misused instead of Θ(f(n)) 
Asymptotic Notation 
Input Size 
Running Time )(nf 0n )(ngc⋅2)(ngc⋅1
Asymptotic Notation 
Two more asymptotic notations 
… 
"Little-Oh" notation f(n) iso(g(n)) non-tight analogue of Big-Oh 
… 
For every c, there should exist n0 ,s.t. f(n) ≤c g(n)for n ≥n0 
… 
Used for comparisonsof running times. If f(n)=o(g(n)), it is said that g(n) dominates f(n). 
… 
"Little-omega" notation f(n) isω(g(n)) non-tight analogue of Big-Omega
Asymptotic Notation 
…Analogy with real numbers 
… 
f(n) = O(g(n))≅f ≤ g 
… 
f(n) = Ω(g(n))≅f ≥g 
… 
f(n) = Θ(g(n))≅f = g 
… 
f(n) = o(g(n))≅f < g 
… 
f(n) = ω(g(n))≅f > g 
… 
Abuse of notation: f(n) = O(g(n)) actually means f(n) ∈O(g(n))
Comparison of Running Times 
Running 
Time 
Maximum problem size (n) 
1 second 
1 minute 
1 hour 
400n 
2500 
150000 
9000000 
20nlog n 
4096 
166666 
7826087 
2n2 
707 
5477 
42426 
n4 
31 
88 
244 
2n 
19 
25 
31
Correctness of Algorithms 
…The algorithm is correctif for any legal input it terminates and produces the desired output. 
… 
Automatic proof of correctness is not possible 
… 
But there are practical techniques and rigorous formalisms that help to reason about the correctness of algorithms
Partial and Total Correctness 
…Partial correctness 
Any legal input 
Algorithm 
Output 
IFthis point is reached, 
THENthis is the desired output 
…Totalcorrectness 
Any legal input 
Algorithm 
Output 
INDEEDthis point is reached, 
ANDthis is the desired output
Assertions 
…To prove correctness we associate a number of assertions(statements about the state of the execution) with specific checkpoints in the algorithm. 
… 
E.g., A[1], …, A[k]form an increasing sequence 
… 
Preconditions–assertions that must be valid beforethe execution of an algorithm or a subroutine 
… 
Postconditions–assertions that must be valid afterthe execution of an algorithm or a subroutine
Loop Invariants 
…Invariants–assertions that are valid any time they are reached (many times during the execution of an algorithm, e.g., in loops) 
… 
We must show three things about loop invariants: 
… 
Initialization –it is true prior to the first iteration 
… 
Maintenance –if it is true before an iteration, it remains true before the next iteration 
… 
Termination –when loop terminates the invariant gives a useful property to show the correctness of the algorithm
Example of Loop Invariants (1) 
…Invariant: at the start of each forloop, A[1…j-1] consists of elements originally in A[1…j-1] but in sorted order 
forj ←2 to length(A) 
dokey ←A[j] 
i ←j-1 
while i>0 and A[i]>key 
do A[i+1] ←A[i] 
i-- 
A[i+1] ←key
Example of Loop Invariants (2) 
…Invariant: at the start of each for loop, A[1…j-1] consists of elements originally in A[1…j-1] but in sorted order 
forj ←2 to length(A) 
dokey ←A[j] 
i ←j-1 
while i>0 and A[i]>key 
do A[i+1]←A[i] 
i-- 
A[i+1] ←key 
…Initialization: j = 2, the invariant trivially holds because A[1] is a sorted array ☺
Example of Loop Invariants (3) 
…Invariant: at the start of each for loop, A[1…j-1] consists of elements originally in A[1…j-1] but in sorted order 
forj ←2 to length(A) 
dokey ←A[j] 
i ←j-1 
while i>0 and A[i]>key 
do A[i+1] ←A[i] 
i-- 
A[i+1] ←key 
…Maintenance: the inner while loop moves elements 
A[j-1], A[j-2], …, A[j-k] one position right without changing their order. Then the former A[j] element is inserted into k-th position so that A[k-1] ≤ A[k] ≤ 
A[k+1]. 
A 
[1…j-1] sorted + A[j] → A[1…j] sorted
Example of Loop Invariants (4) 
…Invariant: at the start of each for loop, A[1…j-1] consists of elements originally in A[1…j-1] but in sorted order 
forj ←2 to length(A) 
dokey ←A[j] 
i ←j-1 
while i>0 and A[i]>key 
do A[i+1] ←A[i] 
i-- 
A[i+1] ←key 
…Termination: the loop terminates, when j=n+1. Then the invariant states: “A[1…n] consists of elements originally in A[1…n] but in sorted order” ☺
Math You Need to Review 
…Properties of logarithms: 
logb(xy) = logbx + logby 
logb(x/y) = logbx -logby 
logbxa= a logbx 
logba = logxa/logxb 
… 
Properties of exponentials: 
a(b+c)= abac; abc= (ab)c 
ab/ac= a(b-c); b = alogab 
… 
Floor:⎣x⎦= the largest integer ≤ x 
… 
Ceiling:⎡x⎤= the smallest integer ≥ x
Math Review 
…Geometric progression 
… 
given an integer n0and a real number 0< a ≠1 
… 
geometric progressions exhibit exponential growth 
… 
Arithmetic progression 
12011... 1nniniaaaaaa+ = − =++++= −Σ20123... 2ninnin= + =++++=Σ
Summations 
…The running time of insertion sort is determined by a nested loop 
… 
Nested loops correspond to summations 
forj←2 to length(A) 
key←A[j] 
i←j-1 
while i>0 and A[i]>key 
A[i+1]←A[i] 
i←i-1 
A[i+1]←key22(1)()njjOn=−=Σ
Proof by Induction 
…We want to show that property P is true for all integers n ≥n0 
… 
Basis: prove that Pis true for n0 
… 
Inductive step: prove that if Pis true for all ksuch that n0 ≤k ≤n –1 then Pis also true for n 
… 
Example 
… 
Basis 
0(1)() for 12ninnSnin= + ==≥Σ101(11)(1) 2iSi= + ==Σ
Proof by Induction (2) 
01002(1)() for 1k12()(1) (11)(2)(1) 22(1) 2kinniikkSkinSniinSnnnnnnnnnn= − == + ==≤≤− ==+=−+= −+−+ =−+== + = ΣΣΣ 
…Inductive Step

More Related Content

What's hot

Столпы функционального программирования для адептов ООП, Николай Мозговой
Столпы функционального программирования для адептов ООП, Николай МозговойСтолпы функционального программирования для адептов ООП, Николай Мозговой
Столпы функционального программирования для адептов ООП, Николай Мозговой
Sigma Software
 
TI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class FunctionsTI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class Functions
Eelco Visser
 
Monad presentation scala as a category
Monad presentation   scala as a categoryMonad presentation   scala as a category
Monad presentation scala as a category
samthemonad
 

What's hot (20)

Dive Into PyTorch
Dive Into PyTorchDive Into PyTorch
Dive Into PyTorch
 
Introduction to functional programming using Ocaml
Introduction to functional programming using OcamlIntroduction to functional programming using Ocaml
Introduction to functional programming using Ocaml
 
[Codemotion 2015] patrones de diseño con java8
[Codemotion 2015] patrones de diseño con java8[Codemotion 2015] patrones de diseño con java8
[Codemotion 2015] patrones de diseño con java8
 
Futures e abstração - QCon São Paulo 2015
Futures e abstração - QCon São Paulo 2015Futures e abstração - QCon São Paulo 2015
Futures e abstração - QCon São Paulo 2015
 
A taste of Functional Programming
A taste of Functional ProgrammingA taste of Functional Programming
A taste of Functional Programming
 
presentation
presentationpresentation
presentation
 
Coding in Style
Coding in StyleCoding in Style
Coding in Style
 
Clojure basics
Clojure basicsClojure basics
Clojure basics
 
Столпы функционального программирования для адептов ООП, Николай Мозговой
Столпы функционального программирования для адептов ООП, Николай МозговойСтолпы функционального программирования для адептов ООП, Николай Мозговой
Столпы функционального программирования для адептов ООП, Николай Мозговой
 
Lec5
Lec5Lec5
Lec5
 
The Ring programming language version 1.3 book - Part 25 of 88
The Ring programming language version 1.3 book - Part 25 of 88The Ring programming language version 1.3 book - Part 25 of 88
The Ring programming language version 1.3 book - Part 25 of 88
 
"PyTorch Deep Learning Framework: Status and Directions," a Presentation from...
"PyTorch Deep Learning Framework: Status and Directions," a Presentation from..."PyTorch Deep Learning Framework: Status and Directions," a Presentation from...
"PyTorch Deep Learning Framework: Status and Directions," a Presentation from...
 
NUMPY
NUMPY NUMPY
NUMPY
 
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
 
TI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class FunctionsTI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class Functions
 
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
 
Monad presentation scala as a category
Monad presentation   scala as a categoryMonad presentation   scala as a category
Monad presentation scala as a category
 
Numpy Talk at SIAM
Numpy Talk at SIAMNumpy Talk at SIAM
Numpy Talk at SIAM
 
Beauty and the beast - Haskell on JVM
Beauty and the beast  - Haskell on JVMBeauty and the beast  - Haskell on JVM
Beauty and the beast - Haskell on JVM
 
Python Scipy Numpy
Python Scipy NumpyPython Scipy Numpy
Python Scipy Numpy
 

Viewers also liked

Viewers also liked (12)

Holy Books
Holy BooksHoly Books
Holy Books
 
Lec10
Lec10Lec10
Lec10
 
Lec23
Lec23Lec23
Lec23
 
I 7
I 7I 7
I 7
 
C notes.pdf
C notes.pdfC notes.pdf
C notes.pdf
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
 
C language ppt
C language pptC language ppt
C language ppt
 
Basics of C programming
Basics of C programmingBasics of C programming
Basics of C programming
 
What's Trending in Talent and Learning for 2016?
What's Trending in Talent and Learning for 2016?What's Trending in Talent and Learning for 2016?
What's Trending in Talent and Learning for 2016?
 
2015 Upload Campaigns Calendar - SlideShare
2015 Upload Campaigns Calendar - SlideShare2015 Upload Campaigns Calendar - SlideShare
2015 Upload Campaigns Calendar - SlideShare
 
What to Upload to SlideShare
What to Upload to SlideShareWhat to Upload to SlideShare
What to Upload to SlideShare
 
Getting Started With SlideShare
Getting Started With SlideShareGetting Started With SlideShare
Getting Started With SlideShare
 

Similar to Lec1

Advanced Datastructures and algorithms CP4151unit1b.pdf
Advanced Datastructures and algorithms CP4151unit1b.pdfAdvanced Datastructures and algorithms CP4151unit1b.pdf
Advanced Datastructures and algorithms CP4151unit1b.pdf
Sheba41
 
Data Structure & Algorithms - Introduction
Data Structure & Algorithms - IntroductionData Structure & Algorithms - Introduction
Data Structure & Algorithms - Introduction
babuk110
 
asymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysisasymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysis
Anindita Kundu
 
lecture 1
lecture 1lecture 1
lecture 1
sajinsc
 
Algorithm chapter 2
Algorithm chapter 2Algorithm chapter 2
Algorithm chapter 2
chidabdu
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihm
Sajid Marwat
 
Data structures notes for college students btech.pptx
Data structures notes for college students btech.pptxData structures notes for college students btech.pptx
Data structures notes for college students btech.pptx
KarthikVijay59
 

Similar to Lec1 (20)

Annotations.pdf
Annotations.pdfAnnotations.pdf
Annotations.pdf
 
Advanced Datastructures and algorithms CP4151unit1b.pdf
Advanced Datastructures and algorithms CP4151unit1b.pdfAdvanced Datastructures and algorithms CP4151unit1b.pdf
Advanced Datastructures and algorithms CP4151unit1b.pdf
 
Data Structure & Algorithms - Introduction
Data Structure & Algorithms - IntroductionData Structure & Algorithms - Introduction
Data Structure & Algorithms - Introduction
 
3 analysis.gtm
3 analysis.gtm3 analysis.gtm
3 analysis.gtm
 
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
 
Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1
 
asymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysisasymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysis
 
Analysis.ppt
Analysis.pptAnalysis.ppt
Analysis.ppt
 
Daa unit 6_efficiency of algorithms
Daa unit 6_efficiency of algorithmsDaa unit 6_efficiency of algorithms
Daa unit 6_efficiency of algorithms
 
Data Structures- Part2 analysis tools
Data Structures- Part2 analysis toolsData Structures- Part2 analysis tools
Data Structures- Part2 analysis tools
 
Alg1
Alg1Alg1
Alg1
 
Introducción al Análisis y diseño de algoritmos
Introducción al Análisis y diseño de algoritmosIntroducción al Análisis y diseño de algoritmos
Introducción al Análisis y diseño de algoritmos
 
lecture 1
lecture 1lecture 1
lecture 1
 
Asymptotic Notation and Complexity
Asymptotic Notation and ComplexityAsymptotic Notation and Complexity
Asymptotic Notation and Complexity
 
Slide2
Slide2Slide2
Slide2
 
Analysis of Algorithum
Analysis of AlgorithumAnalysis of Algorithum
Analysis of Algorithum
 
Algorithm chapter 2
Algorithm chapter 2Algorithm chapter 2
Algorithm chapter 2
 
Time complexity.ppt
Time complexity.pptTime complexity.ppt
Time complexity.ppt
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihm
 
Data structures notes for college students btech.pptx
Data structures notes for college students btech.pptxData structures notes for college students btech.pptx
Data structures notes for college students btech.pptx
 

More from Nikhil Chilwant (20)

The joyless economy
The joyless economyThe joyless economy
The joyless economy
 
IIT Delhi training pioneer ct (acemicromatic )
IIT Delhi training pioneer ct (acemicromatic )IIT Delhi training pioneer ct (acemicromatic )
IIT Delhi training pioneer ct (acemicromatic )
 
Lec39
Lec39Lec39
Lec39
 
Lec38
Lec38Lec38
Lec38
 
Lec37
Lec37Lec37
Lec37
 
Lec36
Lec36Lec36
Lec36
 
Lec35
Lec35Lec35
Lec35
 
Lec34
Lec34Lec34
Lec34
 
Lec33
Lec33Lec33
Lec33
 
Lec32
Lec32Lec32
Lec32
 
Lec30
Lec30Lec30
Lec30
 
Lec29
Lec29Lec29
Lec29
 
Lec28
Lec28Lec28
Lec28
 
Lec27
Lec27Lec27
Lec27
 
Lec26
Lec26Lec26
Lec26
 
Lec25
Lec25Lec25
Lec25
 
Lec24
Lec24Lec24
Lec24
 
Lec23
Lec23Lec23
Lec23
 
Lec21
Lec21Lec21
Lec21
 
Lec20
Lec20Lec20
Lec20
 

Recently uploaded

Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 

Recently uploaded (20)

Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 

Lec1

  • 1. Data Structures and Algorithms … Algorithm: Outline, the essence of a computational procedure, step-by-step instructions … Program: an implementation of an algorithm in some programming language … Data structure: Organizationof data needed to solve the problem
  • 2. Algorithmic problem …Infinite number of input instancessatisfying the specification. For eg: A sorted, non-decreasing sequence of natural numbers of non-zero, finite length: … 1, 20, 908, 909, 100000, 1000000000. … 3. Specification of input ? Specification of output as a function of input
  • 3. Algorithmic Solution …Algorithm describes actions on the input instance … Infinitely many correct algorithms for the same algorithmic problem Input instance, adhering to the specification Algorithm Output related to the input as required
  • 4. What is a Good Algorithm? …Efficient: … Running time … Space used … Efficiency as a function of input size: … The number of bits in an input number … Number of data elements (numbers, points)
  • 5. Measuring the Running Time Experimental Study … Write a programthat implements the algorithm … Run the program with data sets of varying size and composition. … Use a method like System.currentTimeMillis()to get an accurate measure of the actual running time. How should we measure the running time of an algorithm?
  • 6. Limitations of Experimental Studies … It is necessary to implementand test the algorithm in order to determine its running time. … Experiments can be done only on a limited set of inputs, and may not be indicative of the running time on other inputs not included in the experiment. … In order to compare two algorithms, the same hardware and software environmentsshould be used.
  • 7. Beyond Experimental Studies We will develop a general methodologyfor analyzing running time of algorithms. This approach … Uses a high-level descriptionof the algorithm instead of testing one of its implementations. … Takes into account all possible inputs. … Allows one to evaluate the efficiency of any algorithm in a way that is independent of the hardware and software environment.
  • 8. Pseudo-Code …A mixture of natural language and high-level programming concepts that describes the main ideas behind a generic implementation of a data structure or algorithm. … Eg: AlgorithmarrayMax(A, n): Input:An array A storing n integers. Output:The maximum element in A. currentMax ←A[0] for i ←1to n-1do ifcurrentMax< A[i] thencurrentMax ←A[i] returncurrentMax
  • 9. Pseudo-Code It is more structured than usual prose but less formal than a programming language … Expressions: … use standard mathematical symbols to describe numeric and boolean expressions … use ←for assignment (“=” in Java) … use = for the equality relationship (“==” in Java) … Method Declarations: … Algorithmname(param1, param2)
  • 10. Pseudo Code …Programming Constructs: … decision structures: if ... then ... [else ... ] … while-loops: while ... do … repeat-loops: repeat ... until ... … for-loop: for ... do … array indexing: A[i], A[i,j] … Methods: … calls: object method(args) … returns: return value
  • 11. Analysis of Algorithms …Primitive Operation:Low-level operation independent of programming language. Can be identified in pseudo-code. For eg: … Data movement (assign) … Control (branch, subroutine call, return) … arithmetic an logical operations (e.g. addition, comparison) … By inspecting the pseudo-code, we can count the number of primitive operations executed by an algorithm.
  • 12. Sort Example: Sorting INPUT sequence of numbers a1, a2, a3,….,an b1,b2,b3,….,bn OUTPUT a permutation of the sequence of numbers 2 5 4 10 7 2 45 7 10 Correctness (requirements for the output) For any given input the algorithm halts with the output: • b1< b2< b3< …. < bn •b1, b2, b3, …., bnis a permutation ofa1, a2, a3,….,an Running time Depends on • number of elements(n) •how (partially) sortedthey are • algorithm
  • 13. Insertion Sort A 1 n j 3 6 8 4 9 7 2 5 1 i Strategy • Start “empty handed” •Insert a card in the rightposition of the already sortedhand •Continue until all cards areinserted/sorted INPUT: A[1..n] –an array of integers OUTPUT: a permutation of A such that A[1]≤ A[2]≤ …≤A[n] forj←2 to n do key ← A[j] Insert A[j]into the sorted sequence A[1..j-1] i←j-1 while i>0 and A[i]>key do A[i+1]←A[i] i-- A[i+1]←key
  • 14. Analysis of Insertion Sort forj←2 to ndo key←A[j] Insert A[j]into the sorted sequence A[1..j-1] i←j-1 while i>0 and A[i]>key do A[i+1]←A[i] i-- A[i+1] ←key costc1 c2 0 c3 c4 c5 c6 c7 timesnn-1n-1n-1n-1 2njjt=Σ2(1)njjt=−Σ2(1)njjt=−Σ Total time = n(c1+c2+c3+c7) + Σnj=2tj(c4+c5+c6) –(c2+c3+c5+c6+c7)
  • 15. Best/Worst/Average Case …Best case: elements already sorted; tj=1, running time = f(n),i.e., lineartime. … Worst case: elements are sorted in inverse order; tj=j, running time = f(n2),i.e., quadratictime … Average case: tj=j/2, running time = f(n2), i.e.,quadratictime Total time = n(c1+c2+c3+c7) + Σnj=2tj(c4+c5+c6) –(c2+c3+c5+c6+c7)
  • 16. Best/Worst/Average Case (2) …For a specific size of input n, investigate running times for different input instances:
  • 17. Best/Worst/Average Case (3) For inputs of all sizes: 1n 2n 3n 4n 5n 6n Input instance size Running time 1 2 3 4 5 6 7 8 9 10 11 12 ….. best-case average-case worst-case
  • 18. Best/Worst/Average Case (4) …Worst case is usually used: It is an upper- bound and in certain application domains (e.g., air traffic control, surgery) knowing the worst- casetime complexity is of crucial importance … For some algorithms worst caseoccurs fairly often … Average caseis often as bad as the worst case … Finding average case can be very difficult
  • 19. Asymptotic Analysis …Goal: to simplify analysis of running time by getting rid of”details”, which may be affected by specific implementation and hardware … like “rounding”: 1,000,001≈1,000,000 … 3n2≈n2 … Capturing the essence: how the running time of an algorithm increases with the size of the input in the limit. … Asymptotically more efficient algorithms are best for all but small inputs
  • 20. Asymptotic Notation …The “big-Oh” O-Notation … asymptotic upper bound … f(n) is O(g(n)), if there exists constants cand n0, s.t. f(n) ≤c g(n)for n≥n0 … f(n) and g(n) are functionsover non- negative integers … Used for worst-caseanalysis)(nf ()cgn⋅ 0n Input Size Running Time
  • 21. Example For functions f(n)andg(n) there are positive constants cand n0such that: f(n) ≤ c g(n)for n ≥ n0 conclusion: 2n+6 is O(n).
  • 22. Another Example On the other hand… n2is not O(n) because there is no cand n0such that: n2≤ cnfor n ≥ n0 The graph to the right illustrates that no matter how large a cis chosen there is an nbig enough that n2 > cn) .
  • 23. Asymptotic Notation …Simple Rule: Drop lower order terms and constant factors. … 50 n log n is O(n log n) … 7n -3 is O(n) … 8n2log n + 5n2+ n is O(n2log n) … Note: Even though (50 n log n) isO(n5), it is expected that such an approximation be of as small an order as possible
  • 24. Asymptotic Analysis of Running Time …Use O-notation to express number of primitive operations executed as function of input size. … Comparing asymptotic running times … an algorithm that runs in O(n) time is better than one that runs in O(n2) time … similarly, O(log n) is better than O(n) … hierarchy of functions: log n < n < n2< n3< 2n … Caution!Beware of very large constant factors. An algorithm running in time 1,000,000 n is still O(n) but might be less efficient than one running in time 2n2, which is O(n2)
  • 25. Example of Asymptotic Analysis AlgorithmprefixAverages1(X): Input: An n-element array X of numbers. Output:An n-element array A of numbers such that A[i] is the average of elements X[0], ... , X[i]. fori ←0 ton-1 do a ←0 forj ←0 toi do a ←a + X[j] A[i] ←a/(i+1) returnarray A Analysis: running time is O(n2) 1 step i iterations with i=0,1,2...n- 1 n iterations
  • 26. A Better Algorithm AlgorithmprefixAverages2(X): Input: An n-element array X of numbers. Output:An n-element array A of numbers such that A[i] is the average of elements X[0], ... , X[i]. s ←0 fori ←0 ton do s ←s + X[i] A[i] ←s/(i+1) returnarray A Analysis: Running time is O(n)
  • 27. Asymptotic Notation (terminology) …Special classes of algorithms: … Logarithmic: O(log n) … Linear: O(n) … Quadratic: O(n2) … Polynomial:O(nk), k ≥ 1 … Exponential: O(an), a > 1 … “Relatives” of the Big-Oh … Ω(f(n)): Big Omega-asymptotic lowerbound … Θ(f(n)): Big Theta-asymptotic tightbound
  • 28. …The “big-Omega” Ω− Notation … asymptotic lower bound … f(n) isΩ(g(n))if there exists constants cand n0, s.t. c g(n) ≤f(n) for n ≥n0 … Used to describe best- case running times or lower bounds for algorithmic problems … E.g., lower-bound for searching in an unsorted array is Ω(n). Input Size Running Time )(nf ()cgn⋅ 0n Asymptotic Notation
  • 29. …The “big-Theta” Θ−Notation … asymptotically tight bound … f(n) =Θ(g(n))if there exists constants c1, c2,and n0, s.t. c1g(n) ≤f(n) ≤c2g(n) for n ≥ n0 … f(n)is Θ(g(n)) if and only if f(n)is Ο(g(n)) and f(n)is Ω(g(n)) … O(f(n)) is often misused instead of Θ(f(n)) Asymptotic Notation Input Size Running Time )(nf 0n )(ngc⋅2)(ngc⋅1
  • 30. Asymptotic Notation Two more asymptotic notations … "Little-Oh" notation f(n) iso(g(n)) non-tight analogue of Big-Oh … For every c, there should exist n0 ,s.t. f(n) ≤c g(n)for n ≥n0 … Used for comparisonsof running times. If f(n)=o(g(n)), it is said that g(n) dominates f(n). … "Little-omega" notation f(n) isω(g(n)) non-tight analogue of Big-Omega
  • 31. Asymptotic Notation …Analogy with real numbers … f(n) = O(g(n))≅f ≤ g … f(n) = Ω(g(n))≅f ≥g … f(n) = Θ(g(n))≅f = g … f(n) = o(g(n))≅f < g … f(n) = ω(g(n))≅f > g … Abuse of notation: f(n) = O(g(n)) actually means f(n) ∈O(g(n))
  • 32. Comparison of Running Times Running Time Maximum problem size (n) 1 second 1 minute 1 hour 400n 2500 150000 9000000 20nlog n 4096 166666 7826087 2n2 707 5477 42426 n4 31 88 244 2n 19 25 31
  • 33. Correctness of Algorithms …The algorithm is correctif for any legal input it terminates and produces the desired output. … Automatic proof of correctness is not possible … But there are practical techniques and rigorous formalisms that help to reason about the correctness of algorithms
  • 34. Partial and Total Correctness …Partial correctness Any legal input Algorithm Output IFthis point is reached, THENthis is the desired output …Totalcorrectness Any legal input Algorithm Output INDEEDthis point is reached, ANDthis is the desired output
  • 35. Assertions …To prove correctness we associate a number of assertions(statements about the state of the execution) with specific checkpoints in the algorithm. … E.g., A[1], …, A[k]form an increasing sequence … Preconditions–assertions that must be valid beforethe execution of an algorithm or a subroutine … Postconditions–assertions that must be valid afterthe execution of an algorithm or a subroutine
  • 36. Loop Invariants …Invariants–assertions that are valid any time they are reached (many times during the execution of an algorithm, e.g., in loops) … We must show three things about loop invariants: … Initialization –it is true prior to the first iteration … Maintenance –if it is true before an iteration, it remains true before the next iteration … Termination –when loop terminates the invariant gives a useful property to show the correctness of the algorithm
  • 37. Example of Loop Invariants (1) …Invariant: at the start of each forloop, A[1…j-1] consists of elements originally in A[1…j-1] but in sorted order forj ←2 to length(A) dokey ←A[j] i ←j-1 while i>0 and A[i]>key do A[i+1] ←A[i] i-- A[i+1] ←key
  • 38. Example of Loop Invariants (2) …Invariant: at the start of each for loop, A[1…j-1] consists of elements originally in A[1…j-1] but in sorted order forj ←2 to length(A) dokey ←A[j] i ←j-1 while i>0 and A[i]>key do A[i+1]←A[i] i-- A[i+1] ←key …Initialization: j = 2, the invariant trivially holds because A[1] is a sorted array ☺
  • 39. Example of Loop Invariants (3) …Invariant: at the start of each for loop, A[1…j-1] consists of elements originally in A[1…j-1] but in sorted order forj ←2 to length(A) dokey ←A[j] i ←j-1 while i>0 and A[i]>key do A[i+1] ←A[i] i-- A[i+1] ←key …Maintenance: the inner while loop moves elements A[j-1], A[j-2], …, A[j-k] one position right without changing their order. Then the former A[j] element is inserted into k-th position so that A[k-1] ≤ A[k] ≤ A[k+1]. A [1…j-1] sorted + A[j] → A[1…j] sorted
  • 40. Example of Loop Invariants (4) …Invariant: at the start of each for loop, A[1…j-1] consists of elements originally in A[1…j-1] but in sorted order forj ←2 to length(A) dokey ←A[j] i ←j-1 while i>0 and A[i]>key do A[i+1] ←A[i] i-- A[i+1] ←key …Termination: the loop terminates, when j=n+1. Then the invariant states: “A[1…n] consists of elements originally in A[1…n] but in sorted order” ☺
  • 41. Math You Need to Review …Properties of logarithms: logb(xy) = logbx + logby logb(x/y) = logbx -logby logbxa= a logbx logba = logxa/logxb … Properties of exponentials: a(b+c)= abac; abc= (ab)c ab/ac= a(b-c); b = alogab … Floor:⎣x⎦= the largest integer ≤ x … Ceiling:⎡x⎤= the smallest integer ≥ x
  • 42. Math Review …Geometric progression … given an integer n0and a real number 0< a ≠1 … geometric progressions exhibit exponential growth … Arithmetic progression 12011... 1nniniaaaaaa+ = − =++++= −Σ20123... 2ninnin= + =++++=Σ
  • 43. Summations …The running time of insertion sort is determined by a nested loop … Nested loops correspond to summations forj←2 to length(A) key←A[j] i←j-1 while i>0 and A[i]>key A[i+1]←A[i] i←i-1 A[i+1]←key22(1)()njjOn=−=Σ
  • 44. Proof by Induction …We want to show that property P is true for all integers n ≥n0 … Basis: prove that Pis true for n0 … Inductive step: prove that if Pis true for all ksuch that n0 ≤k ≤n –1 then Pis also true for n … Example … Basis 0(1)() for 12ninnSnin= + ==≥Σ101(11)(1) 2iSi= + ==Σ
  • 45. Proof by Induction (2) 01002(1)() for 1k12()(1) (11)(2)(1) 22(1) 2kinniikkSkinSniinSnnnnnnnnnn= − == + ==≤≤− ==+=−+= −+−+ =−+== + = ΣΣΣ …Inductive Step