SlideShare a Scribd company logo
Algorithms
Definition
An algorithm is any well-defined
computational procedure that
takes some values or set of values
as input and produces some values
or set of values as output
Definition
A sequence of computational steps
that transforms the input into
output
4
Algorithms
Properties of algorithms:
• Input from a specified set,
• Output from a specified set (solution),
• Definiteness of every step in the computation,
• Correctness of output for every possible input,
• Finiteness of the number of calculation steps,
• Effectiveness of each calculation step and
• Generality for a class of problems.
?
Suppose computers were infinitely fast and
computer memory are free
Is there any reason to study algorithm ?
Yes
– Demonstrate that solution methods
terminates and does so with correct
answer.
In reality
• Computers may be fast, but
they are not infinitely fast and
• Memory may be cheap but it is
not free
• Computing time is therefore a
bounded resource and so is the
space in memory
7
Complexity
In general, we are not so much interested
in the time and space complexity for small
inputs.
For example, while the difference in time
complexity between linear and binary
search is meaningless for a sequence with
n=10, it is gigantic for n=230.
8
Complexity
For example, let us assume two algorithms A and
B that solve the same class of problems.
The time complexity of A is 5000n, the one for B
is 1.1n for an input with n elements.
For n = 10, A requires 50,000 steps, but B only
3, so B seems to be superior to A.
For n = 1000, however, A requires 50,00,000
steps, while B requires 2.5x1041 steps.
9
Complexity
Comparison: time complexity of algorithms A and B
Algorithm A Algorithm BInput Size
n
10
100
1,000
1,000,000
5,000n
50,000
5,00,000
50,00,000
5x109
1.1n
3
2.5x1041
13,781
4.8x1041392
10
Complexity
• This means that algorithm B cannot be used
for large inputs, while algorithm A is still
feasible.
• So what is important is the growth of the
complexity functions.
• The growth of time and space complexity with
increasing input size n is a suitable measure
for the comparison of algorithms.
Asymptotic Efficiency
Algorithm
• When the input size is large enough so that
the rate of growth/order of growth of the
running time is relevant.
• That is we are concerned with how the
running time of an algorithm increases as the
size of the input increases without bound.
• Usually, an algorithm that is asymptotically
more efficient will be the best choice. 11
Asymptotic Notation
The notations we use to describe
the asymptotic running time of an
algorithm are defined in terms of
functions whose domains are the
set of natural numbers N = {0, 1,
2,…. }
12
13
Asymptotic Notation
Let f(n) and g(n) be two positive
functions, representing the number
of basic calculations (operations,
instructions) that an algorithm
takes (or the number of memory
words an algorithm needs).
Asymptotic Notation
Q - Big Theta
O – Big O
W - Big Omega
o – Small o
w - Small Omega
14
15
O-Notation
For a given function g(n)
O(g(n)) = {f(n) : there exist
positive constants c and n0 such
that 0  f(n)  c. g(n) for all n 
n0 }
Intuitively: Set of all functions whose rate of
growth is the same as or lower than that of
c. g(n)
O-Notation
16
g(n) is an asymptotic upper bound for f(n).
 f(n) = O(g(n)).
Example
O(g(n)) = {f(n) : there exist positive
constants c and n0 such that 0  f(n) 
c g(n) for all n  n0 }
f(n) = 5n+2
0  5n+2  6n
n=0  020 (no)
n=1  076 (no)
n=2  01212 (yes)
n=3  01718 (yes)
17
So n0=2, c=6, g(n)=n
 f(n)= O(n)
18
Big-O Notation
(Examples)
• f(n) = 5n+2 = O(n)
• f(n)=n/2 –3
0  n/2 –3  n/2; n0=6; c=1/2; f(n)=O(n)
• f(n) = n2-n
0  n2–n  n2; n0=0; c=1; f(n)=O(n2)
• f(n) = n(n+1)/2 = O(n2)
W - Notation
For a given function g(n)
W(g(n)) = {f(n) : there exist
positive constants c and n0 such
that 0  c g(n)  f(n) for all
n  n0}
19
Intuitively: Set of all functions whose
rate of growth is the same as or higher
than that of c.g(n).
W - Notation
20
g(n) is an asymptotic lower bound for f(n).
 f(n) = W(g(n)).
Example
W(g(n)) = {f(n) : there exist positive
constants c and n0 such that 0  c g(n) 
f(n) for all n  n0}
f(n) = 5n+2
0  5n  5n+2
n=0  002 (yes)
n=1  057 (yes)
n=2  01012 (yes)
21
So n0=0, c=5, g(n)=n
 f(n)= W(n)
Q - Notation
For a given function g(n),
Q(g(n)) = {f(n) : there exist positive
constants c1 , c2 , and n0 such
that 0  c1 g(n)  f(n)  c2 g(n)
for all n  n0
f(n)  Q(g(n)) f(n) = Q(g(n))
22
Q - Notation
23
g(n) is an
asymptotically tight
bound for f(n).
f(n) and g(n) are
nonnegative, for large
n.
Example
Q(g(n)) = {f(n) :  positive constants c1, c2,
and n0, such that n  n0, 0  c1g(n) 
f(n)  c2g(n) }
• 5n+2 = Q(n)
Determine the positive constant n0,
c1, and c2 such that the above
conditions satisfies
5n  5n+2  6n
24
Example Contd ...
5n  5n+2 is true for all n  0
5n+2  6n is true for all n2
 5n  5n+2  6n is true for all
n2
 c1=5, c2=6, n0=2, g(n)=n
 f(n)= Q(g(n)) = Q(n)
25
Relations Between Q, O, W
For any two function f(n) and g(n),
we have f(n) = Q(g(n)) if and only if
f(n) = O(g(n)) and f(n) = W(g(n))
That is
Q(g(n)) = O(g(n))  W(g(n))
26
Relations Between Q, O, W
27
28
The Growth of Functions
“Popular” functions g(n) are
n.log n, 1, 2n, n2, n!, n, n3, log n
Listed from slowest to fastest growth:
• 1
• log n
• n
• n log n
• n2
• n3
• 2n
• n!
Comparing Growth Rates
Problem Size
T(n)
log2 n
n
n log2 n
n22n
Example: Find sum of array elements
Input size: n (number of array elements)
Total number of steps: 2n + 3 = f(n)
Algorithm arraySum (A, n)
Input array A of n integers
Output Sum of elements of A # operations
sum  0 1
for i  0 to n  1 do n+1
sum  sum + A [i] n
return sum 1
Algorithm arrayMax(A, n)
Input array A of n integers
Output maximum element of A # operations
currentMax  A[0] 1
for i  1 to n  1 do n
if A [i]  currentMax then n -1
currentMax  A [i] n -1
return currentMax 1
Example: Find max element of an
array
 Input size: n (number of array elements)
 Total number of steps: f(n)=3n

More Related Content

What's hot (20)

PPT
Time complexity
Katang Isip
 
PPT
Algorithm.ppt
Tareq Hasan
 
PDF
Time complexity (linear search vs binary search)
Kumar
 
PPTX
Stressen's matrix multiplication
Kumar
 
PPT
how to calclute time complexity of algortihm
Sajid Marwat
 
PPTX
Asymptotic Notations
Rishabh Soni
 
PPT
lecture 15
sajinsc
 
PPTX
Mathematical Analysis of Recursive Algorithm.
mohanrathod18
 
PPT
Asymptotic notations
Ehtisham Ali
 
PPT
Lec03 04-time complexity
Abbas Ali
 
PPT
Matrix Multiplication(An example of concurrent programming)
Pramit Kumar
 
PPT
Basics & asymptotic notations
Rajendran
 
DOC
Time and space complexity
Ankit Katiyar
 
PPTX
Analysis of algorithn class 3
Kumar
 
PPT
02 asymp
aparnabk7
 
PPTX
strassen matrix multiplication algorithm
evil eye
 
PPT
Slide2
Thiti Sununta
 
PPTX
Computational Complexity
Kasun Ranga Wijeweera
 
PPTX
asymptotic analysis and insertion sort analysis
Anindita Kundu
 
PDF
Lecture 4 asymptotic notations
jayavignesh86
 
Time complexity
Katang Isip
 
Algorithm.ppt
Tareq Hasan
 
Time complexity (linear search vs binary search)
Kumar
 
Stressen's matrix multiplication
Kumar
 
how to calclute time complexity of algortihm
Sajid Marwat
 
Asymptotic Notations
Rishabh Soni
 
lecture 15
sajinsc
 
Mathematical Analysis of Recursive Algorithm.
mohanrathod18
 
Asymptotic notations
Ehtisham Ali
 
Lec03 04-time complexity
Abbas Ali
 
Matrix Multiplication(An example of concurrent programming)
Pramit Kumar
 
Basics & asymptotic notations
Rajendran
 
Time and space complexity
Ankit Katiyar
 
Analysis of algorithn class 3
Kumar
 
02 asymp
aparnabk7
 
strassen matrix multiplication algorithm
evil eye
 
Computational Complexity
Kasun Ranga Wijeweera
 
asymptotic analysis and insertion sort analysis
Anindita Kundu
 
Lecture 4 asymptotic notations
jayavignesh86
 

Similar to Algorithms required for data structures(basics like Arrays, Stacks ,Linked Lists etc) (20)

PPTX
Lecture 2 data structures and algorithms
Aakash deep Singhal
 
PDF
Data Structures and Algorithms - Lec 02.pdf
RameshaFernando2
 
PPTX
1_Asymptotic_Notation_pptx.pptx
pallavidhade2
 
PPT
Algorithms
yashodhaHR2
 
PPT
analysis of algorithms and asymptotic complexity
anurag721001
 
PPT
algorithms-1 master in computer application
hydratedpriyanshuvlo
 
PPTX
Dr hasany 2467_16649_1_lec-2-zabist
Gatewayggg Testeru
 
PPTX
Asymptotic Notations.pptx
SunilWork1
 
PPTX
Asymptotics 140510003721-phpapp02
mansab MIRZA
 
PPTX
Algorithms DM
Rokonuzzaman Rony
 
PPT
AsymptoticAnalysis.ppt
SiddheshUpadhyay3
 
PPT
AsymptoticAnalysis-goal of analysis of algorithms
DesiSmartCooking
 
PPT
Design and analysis of algorithm ppt ppt
srushtiivp
 
PDF
1ST_UNIT_DAdefewfrewfgrwefrAdfdgfdsgevedr (2).pdf
ravisikka1
 
PPTX
Asymptotic Notations
NagendraK18
 
PDF
Data Structure & Algorithms - Mathematical
babuk110
 
PPTX
CS 161 Section 1 Slides - Stanford University
kneeslappercr
 
PPT
How to calculate complexity in Data Structure
debasisdas225831
 
PDF
Unit 1_final DESIGN AND ANALYSIS OF ALGORITHM.pdf
saiscount01
 
PPT
Time complexity.ppt
YekoyeTigabuYeko
 
Lecture 2 data structures and algorithms
Aakash deep Singhal
 
Data Structures and Algorithms - Lec 02.pdf
RameshaFernando2
 
1_Asymptotic_Notation_pptx.pptx
pallavidhade2
 
Algorithms
yashodhaHR2
 
analysis of algorithms and asymptotic complexity
anurag721001
 
algorithms-1 master in computer application
hydratedpriyanshuvlo
 
Dr hasany 2467_16649_1_lec-2-zabist
Gatewayggg Testeru
 
Asymptotic Notations.pptx
SunilWork1
 
Asymptotics 140510003721-phpapp02
mansab MIRZA
 
Algorithms DM
Rokonuzzaman Rony
 
AsymptoticAnalysis.ppt
SiddheshUpadhyay3
 
AsymptoticAnalysis-goal of analysis of algorithms
DesiSmartCooking
 
Design and analysis of algorithm ppt ppt
srushtiivp
 
1ST_UNIT_DAdefewfrewfgrwefrAdfdgfdsgevedr (2).pdf
ravisikka1
 
Asymptotic Notations
NagendraK18
 
Data Structure & Algorithms - Mathematical
babuk110
 
CS 161 Section 1 Slides - Stanford University
kneeslappercr
 
How to calculate complexity in Data Structure
debasisdas225831
 
Unit 1_final DESIGN AND ANALYSIS OF ALGORITHM.pdf
saiscount01
 
Time complexity.ppt
YekoyeTigabuYeko
 
Ad

Recently uploaded (20)

PPTX
apidays Helsinki & North 2025 - Running a Successful API Program: Best Practi...
apidays
 
PPTX
apidays Helsinki & North 2025 - From Chaos to Clarity: Designing (AI-Ready) A...
apidays
 
PDF
Data Chunking Strategies for RAG in 2025.pdf
Tamanna
 
PPTX
ER_Model_Relationship_in_DBMS_Presentation.pptx
dharaadhvaryu1992
 
PDF
The European Business Wallet: Why It Matters and How It Powers the EUDI Ecosy...
Lal Chandran
 
PDF
R Cookbook - Processing and Manipulating Geological spatial data with R.pdf
OtnielSimopiaref2
 
PPTX
GenAI-Introduction-to-Copilot-for-Bing-March-2025-FOR-HUB.pptx
cleydsonborges1
 
PPTX
apidays Helsinki & North 2025 - Agentic AI: A Friend or Foe?, Merja Kajava (A...
apidays
 
PPTX
Climate Action.pptx action plan for climate
justfortalabat
 
PPT
deep dive data management sharepoint apps.ppt
novaprofk
 
PDF
apidays Helsinki & North 2025 - APIs in the healthcare sector: hospitals inte...
apidays
 
PPTX
apidays Singapore 2025 - From Data to Insights: Building AI-Powered Data APIs...
apidays
 
PPTX
apidays Munich 2025 - Building an AWS Serverless Application with Terraform, ...
apidays
 
PPTX
apidays Singapore 2025 - Designing for Change, Julie Schiller (Google)
apidays
 
PPTX
Advanced_NLP_with_Transformers_PPT_final 50.pptx
Shiwani Gupta
 
PPTX
Numbers of a nation: how we estimate population statistics | Accessible slides
Office for National Statistics
 
PDF
WEF_Future_of_Global_Fintech_Second_Edition_2025.pdf
AproximacionAlFuturo
 
PPTX
ER_Model_with_Diagrams_Presentation.pptx
dharaadhvaryu1992
 
PDF
Web Scraping with Google Gemini 2.0 .pdf
Tamanna
 
PDF
Merits and Demerits of DBMS over File System & 3-Tier Architecture in DBMS
MD RIZWAN MOLLA
 
apidays Helsinki & North 2025 - Running a Successful API Program: Best Practi...
apidays
 
apidays Helsinki & North 2025 - From Chaos to Clarity: Designing (AI-Ready) A...
apidays
 
Data Chunking Strategies for RAG in 2025.pdf
Tamanna
 
ER_Model_Relationship_in_DBMS_Presentation.pptx
dharaadhvaryu1992
 
The European Business Wallet: Why It Matters and How It Powers the EUDI Ecosy...
Lal Chandran
 
R Cookbook - Processing and Manipulating Geological spatial data with R.pdf
OtnielSimopiaref2
 
GenAI-Introduction-to-Copilot-for-Bing-March-2025-FOR-HUB.pptx
cleydsonborges1
 
apidays Helsinki & North 2025 - Agentic AI: A Friend or Foe?, Merja Kajava (A...
apidays
 
Climate Action.pptx action plan for climate
justfortalabat
 
deep dive data management sharepoint apps.ppt
novaprofk
 
apidays Helsinki & North 2025 - APIs in the healthcare sector: hospitals inte...
apidays
 
apidays Singapore 2025 - From Data to Insights: Building AI-Powered Data APIs...
apidays
 
apidays Munich 2025 - Building an AWS Serverless Application with Terraform, ...
apidays
 
apidays Singapore 2025 - Designing for Change, Julie Schiller (Google)
apidays
 
Advanced_NLP_with_Transformers_PPT_final 50.pptx
Shiwani Gupta
 
Numbers of a nation: how we estimate population statistics | Accessible slides
Office for National Statistics
 
WEF_Future_of_Global_Fintech_Second_Edition_2025.pdf
AproximacionAlFuturo
 
ER_Model_with_Diagrams_Presentation.pptx
dharaadhvaryu1992
 
Web Scraping with Google Gemini 2.0 .pdf
Tamanna
 
Merits and Demerits of DBMS over File System & 3-Tier Architecture in DBMS
MD RIZWAN MOLLA
 
Ad

Algorithms required for data structures(basics like Arrays, Stacks ,Linked Lists etc)

  • 2. Definition An algorithm is any well-defined computational procedure that takes some values or set of values as input and produces some values or set of values as output
  • 3. Definition A sequence of computational steps that transforms the input into output
  • 4. 4 Algorithms Properties of algorithms: • Input from a specified set, • Output from a specified set (solution), • Definiteness of every step in the computation, • Correctness of output for every possible input, • Finiteness of the number of calculation steps, • Effectiveness of each calculation step and • Generality for a class of problems.
  • 5. ? Suppose computers were infinitely fast and computer memory are free Is there any reason to study algorithm ? Yes – Demonstrate that solution methods terminates and does so with correct answer.
  • 6. In reality • Computers may be fast, but they are not infinitely fast and • Memory may be cheap but it is not free • Computing time is therefore a bounded resource and so is the space in memory
  • 7. 7 Complexity In general, we are not so much interested in the time and space complexity for small inputs. For example, while the difference in time complexity between linear and binary search is meaningless for a sequence with n=10, it is gigantic for n=230.
  • 8. 8 Complexity For example, let us assume two algorithms A and B that solve the same class of problems. The time complexity of A is 5000n, the one for B is 1.1n for an input with n elements. For n = 10, A requires 50,000 steps, but B only 3, so B seems to be superior to A. For n = 1000, however, A requires 50,00,000 steps, while B requires 2.5x1041 steps.
  • 9. 9 Complexity Comparison: time complexity of algorithms A and B Algorithm A Algorithm BInput Size n 10 100 1,000 1,000,000 5,000n 50,000 5,00,000 50,00,000 5x109 1.1n 3 2.5x1041 13,781 4.8x1041392
  • 10. 10 Complexity • This means that algorithm B cannot be used for large inputs, while algorithm A is still feasible. • So what is important is the growth of the complexity functions. • The growth of time and space complexity with increasing input size n is a suitable measure for the comparison of algorithms.
  • 11. Asymptotic Efficiency Algorithm • When the input size is large enough so that the rate of growth/order of growth of the running time is relevant. • That is we are concerned with how the running time of an algorithm increases as the size of the input increases without bound. • Usually, an algorithm that is asymptotically more efficient will be the best choice. 11
  • 12. Asymptotic Notation The notations we use to describe the asymptotic running time of an algorithm are defined in terms of functions whose domains are the set of natural numbers N = {0, 1, 2,…. } 12
  • 13. 13 Asymptotic Notation Let f(n) and g(n) be two positive functions, representing the number of basic calculations (operations, instructions) that an algorithm takes (or the number of memory words an algorithm needs).
  • 14. Asymptotic Notation Q - Big Theta O – Big O W - Big Omega o – Small o w - Small Omega 14
  • 15. 15 O-Notation For a given function g(n) O(g(n)) = {f(n) : there exist positive constants c and n0 such that 0  f(n)  c. g(n) for all n  n0 } Intuitively: Set of all functions whose rate of growth is the same as or lower than that of c. g(n)
  • 16. O-Notation 16 g(n) is an asymptotic upper bound for f(n).  f(n) = O(g(n)).
  • 17. Example O(g(n)) = {f(n) : there exist positive constants c and n0 such that 0  f(n)  c g(n) for all n  n0 } f(n) = 5n+2 0  5n+2  6n n=0  020 (no) n=1  076 (no) n=2  01212 (yes) n=3  01718 (yes) 17 So n0=2, c=6, g(n)=n  f(n)= O(n)
  • 18. 18 Big-O Notation (Examples) • f(n) = 5n+2 = O(n) • f(n)=n/2 –3 0  n/2 –3  n/2; n0=6; c=1/2; f(n)=O(n) • f(n) = n2-n 0  n2–n  n2; n0=0; c=1; f(n)=O(n2) • f(n) = n(n+1)/2 = O(n2)
  • 19. W - Notation For a given function g(n) W(g(n)) = {f(n) : there exist positive constants c and n0 such that 0  c g(n)  f(n) for all n  n0} 19 Intuitively: Set of all functions whose rate of growth is the same as or higher than that of c.g(n).
  • 20. W - Notation 20 g(n) is an asymptotic lower bound for f(n).  f(n) = W(g(n)).
  • 21. Example W(g(n)) = {f(n) : there exist positive constants c and n0 such that 0  c g(n)  f(n) for all n  n0} f(n) = 5n+2 0  5n  5n+2 n=0  002 (yes) n=1  057 (yes) n=2  01012 (yes) 21 So n0=0, c=5, g(n)=n  f(n)= W(n)
  • 22. Q - Notation For a given function g(n), Q(g(n)) = {f(n) : there exist positive constants c1 , c2 , and n0 such that 0  c1 g(n)  f(n)  c2 g(n) for all n  n0 f(n)  Q(g(n)) f(n) = Q(g(n)) 22
  • 23. Q - Notation 23 g(n) is an asymptotically tight bound for f(n). f(n) and g(n) are nonnegative, for large n.
  • 24. Example Q(g(n)) = {f(n) :  positive constants c1, c2, and n0, such that n  n0, 0  c1g(n)  f(n)  c2g(n) } • 5n+2 = Q(n) Determine the positive constant n0, c1, and c2 such that the above conditions satisfies 5n  5n+2  6n 24
  • 25. Example Contd ... 5n  5n+2 is true for all n  0 5n+2  6n is true for all n2  5n  5n+2  6n is true for all n2  c1=5, c2=6, n0=2, g(n)=n  f(n)= Q(g(n)) = Q(n) 25
  • 26. Relations Between Q, O, W For any two function f(n) and g(n), we have f(n) = Q(g(n)) if and only if f(n) = O(g(n)) and f(n) = W(g(n)) That is Q(g(n)) = O(g(n))  W(g(n)) 26
  • 28. 28 The Growth of Functions “Popular” functions g(n) are n.log n, 1, 2n, n2, n!, n, n3, log n Listed from slowest to fastest growth: • 1 • log n • n • n log n • n2 • n3 • 2n • n!
  • 29. Comparing Growth Rates Problem Size T(n) log2 n n n log2 n n22n
  • 30. Example: Find sum of array elements Input size: n (number of array elements) Total number of steps: 2n + 3 = f(n) Algorithm arraySum (A, n) Input array A of n integers Output Sum of elements of A # operations sum  0 1 for i  0 to n  1 do n+1 sum  sum + A [i] n return sum 1
  • 31. Algorithm arrayMax(A, n) Input array A of n integers Output maximum element of A # operations currentMax  A[0] 1 for i  1 to n  1 do n if A [i]  currentMax then n -1 currentMax  A [i] n -1 return currentMax 1 Example: Find max element of an array  Input size: n (number of array elements)  Total number of steps: f(n)=3n