SlideShare a Scribd company logo
1 of 19
Time and Space Complexity
Designing an efficient algorithm for a program plays a crucial role
in a large scale computer system.
Time complexity and space complexity are the two most
important considerations for deciding the efficiency of an
algorithm.
The time complexity of an algorithm is the number of
instructions that it needs to run to completion.
The space complexity of an algorithm is the amount of
memory that it needs to run to completion.
The analysis of running time generally has received more
attention than memory because any program that uses huge
amounts of memory automatically requires a lot of time.
Eng. Abdulahi Time and Space Complexity
Time Complexity
In analyzing algorithm we will not consider the following
information although they are very important.
1 The machine we are executing on.
The machine language instruction
set.
The time required by each machine instruction
The translation, a compiler will make from the source to the
machine language.
2
3
4
The exact time we determine would no apply to many machines.
There would be the problem of the compiler which could vary
from machine to machine.
It is often difficult to get reliable timing figures because of clock
limitations and a multi-programming or time sharing
environment.
We will concentrate on developing only the frequency count for
all statements.
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 2/ 50
Time Complexity
1: x ← x + 1
1: for I ← 1 to n do
▷Frequency count is 1
▷Frequency count is n+1
▷Frequency count is n
2: x ← x + 1;
3: end for
1: for I ← 1 to n do ▷Frequency count is n+1
▷Frequency count is n(n+1)
▷Frequency count is n2
2: for J ← 1 to n do
3: x ← x + 1;
4: end for
5: end for
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 3/ 50
Space Complexity
The space needed by a program is the sum of the following components.
Fixed space requirement: The component refers to space
requirement that do not depend on the number and size of the
program’s inputs and outputs. The fixed requirements include the
instruction space (space needed to store the code), space for
simple variables, fixed size structured variable and
constants.
Variable space requirement: This component consists of the
space needed by structured variables whose size depends on the
particular instance i, of the problem being solved. It also includes
the additional space required when a function uses recursion.
The space requirement S(P) of an algorithm P may therefore be written
as S(P) = c + SP, where c and SP are the constant and instance
characteristics, respectively. First, we need to determine which
instance characteristics to use for a give problem to reduce the space
requirements.
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 4/ 50
Space Complexity
Algorithm 2 Square of the given Number
1: procedure GETSqUARE(n)
2: return n*n
3: end procedure
We can solve the problem without consuming any extra space,
hence the space complexity is constant.
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 5/ 50
Space Complexity
Algorithm 3 Sum of array elements
1: procedure CALCULAT E SUM(A,n)
2: sum ← 0
3: for i ← 0 to n −1 do
4: sum ← sum + A[i]
5: end for
6: end procedure
n, sum and i take constant sum of 3 units, but the variable A is an array,
it’s space consumption increases with the increase of input size n.
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 6/ 50
Basics
The main idea of asymptotic analysis is to have a measure of
efficiency of algorithms that doesn’t depend on machine
specific constants.
Asymptotic analysis of an algorithm refers to defining the
mathematical boundation/framing of its run-time
performance.
It doesn’t require algorithms to be implemented and time
taken by programs to be compared.
Asymptotic notations are mathematical tools to represent
time complexity of algorithms for asymptotic
analysis.
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 7/ 50
O (Big-Oh) notation
Big-Oh is used as a tight upper-bound on the growth of an
algorithm’s effort (this effort is described by the function f(n)).
Let f(n) and g(n) be functions that map positive integers to positive
real numbers. We say that f(n) is O(g(n)) or f(n) ∈O(g(n)), if
there exists a real constant c > 0 and there exists an integer constant
n0 ≥1 such that f(n) ≤cg(n) for every integer n ≥n0.
In other words O(g(n)) = {f(n): there exist positive constants c and
n0 such that 0 ≤f(n) ≤cg(n) for all n ≥n0}
Figure 2.2: f(n) ∈O(g(n))
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 8/ 50
O (Big-Oh) notation
Question1:Consider the function f(n) = 6n+ 135. Clearly. f(n) is non-
negative for all integers n ≥0. We wish to show that f(n)=O(n2).
According to the Big-oh definition, in order to show this we need to find
an integer n0, and a constant c > 0 such that for all integers, n ≥n0, f(n)
= c(n2)
Answer: Suppose we choose c = 1, and f(n) = cn2.
⇒ 6n+135 = cn2 = n2 [Since c = 1] n2-6n-135 = 0
⇒ (n-15)(n+9) = 0
Since (n+9) > 0 for all values n ≥0, we conclude that (n-15) = 0
0
⇒ n0 = 15 for c = 1 √
For c = 2, n = (6 + 1116)/4 ≈9.9
√
For c = 4, n0 = (6 + 2196)/8 ≈6.7
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 9/ 50
Ω (Big-Omega) notation
Big-Omega (Ω) is the tight lower bound notation.
Let f(n) and g(n) be functions that map positive integers to positive
real numbers. We say that f(n) is Ω(g(n)) or f(n) ∈Ω(g(n)) if there
exists a real constant c > 0 and there exists an integer constant n0 ≥
1 such that f(n) ≥cg(n) for every integer n ≥n0.
In other words Ω(g(n)) = {f(n): there exist positive constants c and
n0 such that 0 ≤cg(n) ≤f(n) for all n ≥n0}.
Figure 2.3: f(n) ∈Ω(g(n))
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 10/ 50
Ω (Big-Omega) notation
Question 2:Consider the function f(n)= 3n2-24n+72. Clearly f(n) is
non-negative for all integers n ≥0. We wish to show that f(n) = Ω(n2).
According to the big-omega definition, in order to show this weneed to
find an integer n0,and a constant c > 0 such that for all integers n = n0,
f(n) = cn2.
Answer: Suppose we choosc c = 1, Then f(n) = cn2
⇒ 3n2-24n+72 = n2
⇒ 2n2-24n+72 = 0
⇒ 2(n-6)2 = 0
Since (n-6)2 = 0, we conclude that n0 = 6.
So we have that for c = 1 and n ≥6, f(n) = cn2. Hence f(n) = Ω(n2).
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 11/ 50
θ (Theta) notation
Let f(n) and g(n) be functions that map positive integers to positive
real numbers. We say that f(n) is θ(g(n)) or f(n) ∈θ(g(n)) if and
only if f(n) ∈O(g(n)) and f(n) ∈Ω(g(n))
θ(g(n)) = {f(n): there exist positive constants c1, c2and n0 such
that 0 ≤c1g(n) ≤f(n) ≤c2g(n) for all n ≥n0}
Figure 2.6: f(n) ∈θ(g(n))
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 12/ 50
Types of Time Complexities
Time complexity usually depends on the size of the algorithm and
input.
The best-case time complexity of an algorithm is a measure of the
minimum time that the algorithm will require for an input of size n.
The worst-case time complexity of an algorithm is a measure of the
maximum time that the algorithm will require for an input of size n.
After knowing the worst-case time complexity, we can guarantee that
the algorithm will never take more than this time.
The time that an algorithm will require to execute a typical input
data of size n is known as average-case time complexity.
We can say that the value that is obtained by averaging the running
time of an algorithm for all possible inputs of size n can determine
average-case time complexity.
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 13/ 50
Rules for Complexity Analysis
Algorithm 4 Prefix-sum
1: procedure PREFIX-SUM(A,n)
2: for i ← n −1 to 0 do
sum ← 0
for j ← 0 to i do
sum ← sum + A[j]
end for
3:
4:
5:
6:
7: A[i] ← sum
8: end for
9: end procedure
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 14/ 50
Rules for Complexity Analysis
Table 3.3: Time Complexity calculation of Prefix-sum algorithm
Stateme
nt
Frequency Count Time
1 1 O(1)
2 n+1 O(n)
3 n O(n)
4 (n+1) + n + ....+ 2 O(n2)
5 n + (n-1) + ...+ 1 O(n2)
6 n + (n-1) + ...+ 1 O(n2)
7 n O(n)
8 n O(n)
9 1 O(1)
f(n) (n+1)(n+2)/2 + n(n+1) + 4n + 2 O(n2
)
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 15/ 50
Space Complexity Analysis
Example 1
In Algorithm 2, the variable noccupies a constant 4Bytes of
memory. The function call and return statement come under the
auxiliary space and let’s assume 4 Bytes all together.
The total space complexity is 8 Bytes. Algorithm 2 has a space
complexity of O(1).
Example 2
In Algorithm 3, the variables n, sum, and i occupy a constant 12
Bytes of memory. The function call, initialisation of the for loop
and write function all come under the auxiliary space and let’s
assume 4 Bytes all together.
The total space complexity is 4n + 16 Bytes. Algorithm 3 has a
space complexity of O(n).
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 16/ 50
Space Complexity Analysis
Algorithm 5 Factorial of a number
1: procedure FACTORIAL (n)
2: fact ← 1
3: for i ← 1 to n do
4: fact ← fact + i
5: end for
6: return fact
7: end procedure
The variables n, fact, and i occupy a constant 12 Bytes of memory. The
function call, initializingthe for loop and returnstatement all come
under the auxiliary space and let’s assume 4 Bytes all together.
The total space complexity is 16 Bytes. Algorithm 5 has a space
complexity of O(1).
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 17/ 50
Space Complexity Analysis
Algorithm 6 Recursive: Factorial of a number
1: procedure FACTORIAL (n)
2: if (n ≤1) then
3: return 1
4: else
5: return n ∗
FACTORIAL(n −1)
6: end if
7: end procedure
The variable n occupies a constant 4 Bytes of memory. The function
call, if and else conditions and return statement all come under the
auxiliary space and let’s assume 4 Bytes all together.
The total space complexity is 4n+4 Bytes. Algorithm 6 has a space
complexity of O(n).
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 18/ 50
Space Complexity Analysis
Algorithm 7 Summation of two numbers
1: procedure ADDITION(a, b)
2: c ← a+ b
3: write c
4: end procedure
The variables a, band c occupy a constant 12Bytes of memory. The
function call, if and else conditions and write function all come under
the auxiliary space and let’s assume 4 Bytes all together.
The total space complexity is 16 Bytes. Algorithm 7 has a space
complexity of O(1).
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 19/ 50

More Related Content

Similar to Ch-2 final exam documet compler design elements

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 ...TechVision8
 
Data_Structure_and_Algorithms_Lecture_1.ppt
Data_Structure_and_Algorithms_Lecture_1.pptData_Structure_and_Algorithms_Lecture_1.ppt
Data_Structure_and_Algorithms_Lecture_1.pptISHANAMRITSRIVASTAVA
 
DSA Complexity.pptx What is Complexity Analysis? What is the need for Compl...
DSA Complexity.pptx   What is Complexity Analysis? What is the need for Compl...DSA Complexity.pptx   What is Complexity Analysis? What is the need for Compl...
DSA Complexity.pptx What is Complexity Analysis? What is the need for Compl...2022cspaawan12556
 
Introduction to data structures and complexity.pptx
Introduction to data structures and complexity.pptxIntroduction to data structures and complexity.pptx
Introduction to data structures and complexity.pptxPJS KUMAR
 
Data Structures Notes
Data Structures NotesData Structures Notes
Data Structures NotesRobinRohit2
 
Master’s theorem Derivative Analysis
Master’s theorem Derivative AnalysisMaster’s theorem Derivative Analysis
Master’s theorem Derivative AnalysisIRJET Journal
 
CS-102 DS-class_01_02 Lectures Data .pdf
CS-102 DS-class_01_02 Lectures Data .pdfCS-102 DS-class_01_02 Lectures Data .pdf
CS-102 DS-class_01_02 Lectures Data .pdfssuser034ce1
 
Analysis Of Algorithms I
Analysis Of Algorithms IAnalysis Of Algorithms I
Analysis Of Algorithms ISri Prasanna
 
Complexity Analysis
Complexity Analysis Complexity Analysis
Complexity Analysis Shaista Qadir
 
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.pptxKarthikVijay59
 
module1_Introductiontoalgorithms_2022.pdf
module1_Introductiontoalgorithms_2022.pdfmodule1_Introductiontoalgorithms_2022.pdf
module1_Introductiontoalgorithms_2022.pdfShiwani Gupta
 
Aad introduction
Aad introductionAad introduction
Aad introductionMr SMAK
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexityAnkit Katiyar
 
analysis of algorithms
analysis of algorithmsanalysis of algorithms
analysis of algorithmsMyMovies15
 

Similar to Ch-2 final exam documet compler design elements (20)

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 ...
 
Data_Structure_and_Algorithms_Lecture_1.ppt
Data_Structure_and_Algorithms_Lecture_1.pptData_Structure_and_Algorithms_Lecture_1.ppt
Data_Structure_and_Algorithms_Lecture_1.ppt
 
DSA Complexity.pptx What is Complexity Analysis? What is the need for Compl...
DSA Complexity.pptx   What is Complexity Analysis? What is the need for Compl...DSA Complexity.pptx   What is Complexity Analysis? What is the need for Compl...
DSA Complexity.pptx What is Complexity Analysis? What is the need for Compl...
 
Introduction to data structures and complexity.pptx
Introduction to data structures and complexity.pptxIntroduction to data structures and complexity.pptx
Introduction to data structures and complexity.pptx
 
Data Structures Notes
Data Structures NotesData Structures Notes
Data Structures Notes
 
Analysis of Algorithum
Analysis of AlgorithumAnalysis of Algorithum
Analysis of Algorithum
 
Master’s theorem Derivative Analysis
Master’s theorem Derivative AnalysisMaster’s theorem Derivative Analysis
Master’s theorem Derivative Analysis
 
CS-102 DS-class_01_02 Lectures Data .pdf
CS-102 DS-class_01_02 Lectures Data .pdfCS-102 DS-class_01_02 Lectures Data .pdf
CS-102 DS-class_01_02 Lectures Data .pdf
 
Analysis Of Algorithms I
Analysis Of Algorithms IAnalysis Of Algorithms I
Analysis Of Algorithms I
 
Complexity Analysis
Complexity Analysis Complexity Analysis
Complexity Analysis
 
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
 
module1_Introductiontoalgorithms_2022.pdf
module1_Introductiontoalgorithms_2022.pdfmodule1_Introductiontoalgorithms_2022.pdf
module1_Introductiontoalgorithms_2022.pdf
 
Aad introduction
Aad introductionAad introduction
Aad introduction
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
 
Analysis of algo
Analysis of algoAnalysis of algo
Analysis of algo
 
Big Oh.ppt
Big Oh.pptBig Oh.ppt
Big Oh.ppt
 
Lec7
Lec7Lec7
Lec7
 
Lec7.ppt
Lec7.pptLec7.ppt
Lec7.ppt
 
Lec7.ppt
Lec7.pptLec7.ppt
Lec7.ppt
 
analysis of algorithms
analysis of algorithmsanalysis of algorithms
analysis of algorithms
 

Recently uploaded

Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 

Recently uploaded (20)

Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 

Ch-2 final exam documet compler design elements

  • 1. Time and Space Complexity Designing an efficient algorithm for a program plays a crucial role in a large scale computer system. Time complexity and space complexity are the two most important considerations for deciding the efficiency of an algorithm. The time complexity of an algorithm is the number of instructions that it needs to run to completion. The space complexity of an algorithm is the amount of memory that it needs to run to completion. The analysis of running time generally has received more attention than memory because any program that uses huge amounts of memory automatically requires a lot of time. Eng. Abdulahi Time and Space Complexity
  • 2. Time Complexity In analyzing algorithm we will not consider the following information although they are very important. 1 The machine we are executing on. The machine language instruction set. The time required by each machine instruction The translation, a compiler will make from the source to the machine language. 2 3 4 The exact time we determine would no apply to many machines. There would be the problem of the compiler which could vary from machine to machine. It is often difficult to get reliable timing figures because of clock limitations and a multi-programming or time sharing environment. We will concentrate on developing only the frequency count for all statements. Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 2/ 50
  • 3. Time Complexity 1: x ← x + 1 1: for I ← 1 to n do ▷Frequency count is 1 ▷Frequency count is n+1 ▷Frequency count is n 2: x ← x + 1; 3: end for 1: for I ← 1 to n do ▷Frequency count is n+1 ▷Frequency count is n(n+1) ▷Frequency count is n2 2: for J ← 1 to n do 3: x ← x + 1; 4: end for 5: end for Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 3/ 50
  • 4. Space Complexity The space needed by a program is the sum of the following components. Fixed space requirement: The component refers to space requirement that do not depend on the number and size of the program’s inputs and outputs. The fixed requirements include the instruction space (space needed to store the code), space for simple variables, fixed size structured variable and constants. Variable space requirement: This component consists of the space needed by structured variables whose size depends on the particular instance i, of the problem being solved. It also includes the additional space required when a function uses recursion. The space requirement S(P) of an algorithm P may therefore be written as S(P) = c + SP, where c and SP are the constant and instance characteristics, respectively. First, we need to determine which instance characteristics to use for a give problem to reduce the space requirements. Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 4/ 50
  • 5. Space Complexity Algorithm 2 Square of the given Number 1: procedure GETSqUARE(n) 2: return n*n 3: end procedure We can solve the problem without consuming any extra space, hence the space complexity is constant. Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 5/ 50
  • 6. Space Complexity Algorithm 3 Sum of array elements 1: procedure CALCULAT E SUM(A,n) 2: sum ← 0 3: for i ← 0 to n −1 do 4: sum ← sum + A[i] 5: end for 6: end procedure n, sum and i take constant sum of 3 units, but the variable A is an array, it’s space consumption increases with the increase of input size n. Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 6/ 50
  • 7. Basics The main idea of asymptotic analysis is to have a measure of efficiency of algorithms that doesn’t depend on machine specific constants. Asymptotic analysis of an algorithm refers to defining the mathematical boundation/framing of its run-time performance. It doesn’t require algorithms to be implemented and time taken by programs to be compared. Asymptotic notations are mathematical tools to represent time complexity of algorithms for asymptotic analysis. Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 7/ 50
  • 8. O (Big-Oh) notation Big-Oh is used as a tight upper-bound on the growth of an algorithm’s effort (this effort is described by the function f(n)). Let f(n) and g(n) be functions that map positive integers to positive real numbers. We say that f(n) is O(g(n)) or f(n) ∈O(g(n)), if there exists a real constant c > 0 and there exists an integer constant n0 ≥1 such that f(n) ≤cg(n) for every integer n ≥n0. In other words O(g(n)) = {f(n): there exist positive constants c and n0 such that 0 ≤f(n) ≤cg(n) for all n ≥n0} Figure 2.2: f(n) ∈O(g(n)) Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 8/ 50
  • 9. O (Big-Oh) notation Question1:Consider the function f(n) = 6n+ 135. Clearly. f(n) is non- negative for all integers n ≥0. We wish to show that f(n)=O(n2). According to the Big-oh definition, in order to show this we need to find an integer n0, and a constant c > 0 such that for all integers, n ≥n0, f(n) = c(n2) Answer: Suppose we choose c = 1, and f(n) = cn2. ⇒ 6n+135 = cn2 = n2 [Since c = 1] n2-6n-135 = 0 ⇒ (n-15)(n+9) = 0 Since (n+9) > 0 for all values n ≥0, we conclude that (n-15) = 0 0 ⇒ n0 = 15 for c = 1 √ For c = 2, n = (6 + 1116)/4 ≈9.9 √ For c = 4, n0 = (6 + 2196)/8 ≈6.7 Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 9/ 50
  • 10. Ω (Big-Omega) notation Big-Omega (Ω) is the tight lower bound notation. Let f(n) and g(n) be functions that map positive integers to positive real numbers. We say that f(n) is Ω(g(n)) or f(n) ∈Ω(g(n)) if there exists a real constant c > 0 and there exists an integer constant n0 ≥ 1 such that f(n) ≥cg(n) for every integer n ≥n0. In other words Ω(g(n)) = {f(n): there exist positive constants c and n0 such that 0 ≤cg(n) ≤f(n) for all n ≥n0}. Figure 2.3: f(n) ∈Ω(g(n)) Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 10/ 50
  • 11. Ω (Big-Omega) notation Question 2:Consider the function f(n)= 3n2-24n+72. Clearly f(n) is non-negative for all integers n ≥0. We wish to show that f(n) = Ω(n2). According to the big-omega definition, in order to show this weneed to find an integer n0,and a constant c > 0 such that for all integers n = n0, f(n) = cn2. Answer: Suppose we choosc c = 1, Then f(n) = cn2 ⇒ 3n2-24n+72 = n2 ⇒ 2n2-24n+72 = 0 ⇒ 2(n-6)2 = 0 Since (n-6)2 = 0, we conclude that n0 = 6. So we have that for c = 1 and n ≥6, f(n) = cn2. Hence f(n) = Ω(n2). Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 11/ 50
  • 12. θ (Theta) notation Let f(n) and g(n) be functions that map positive integers to positive real numbers. We say that f(n) is θ(g(n)) or f(n) ∈θ(g(n)) if and only if f(n) ∈O(g(n)) and f(n) ∈Ω(g(n)) θ(g(n)) = {f(n): there exist positive constants c1, c2and n0 such that 0 ≤c1g(n) ≤f(n) ≤c2g(n) for all n ≥n0} Figure 2.6: f(n) ∈θ(g(n)) Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 12/ 50
  • 13. Types of Time Complexities Time complexity usually depends on the size of the algorithm and input. The best-case time complexity of an algorithm is a measure of the minimum time that the algorithm will require for an input of size n. The worst-case time complexity of an algorithm is a measure of the maximum time that the algorithm will require for an input of size n. After knowing the worst-case time complexity, we can guarantee that the algorithm will never take more than this time. The time that an algorithm will require to execute a typical input data of size n is known as average-case time complexity. We can say that the value that is obtained by averaging the running time of an algorithm for all possible inputs of size n can determine average-case time complexity. Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 13/ 50
  • 14. Rules for Complexity Analysis Algorithm 4 Prefix-sum 1: procedure PREFIX-SUM(A,n) 2: for i ← n −1 to 0 do sum ← 0 for j ← 0 to i do sum ← sum + A[j] end for 3: 4: 5: 6: 7: A[i] ← sum 8: end for 9: end procedure Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 14/ 50
  • 15. Rules for Complexity Analysis Table 3.3: Time Complexity calculation of Prefix-sum algorithm Stateme nt Frequency Count Time 1 1 O(1) 2 n+1 O(n) 3 n O(n) 4 (n+1) + n + ....+ 2 O(n2) 5 n + (n-1) + ...+ 1 O(n2) 6 n + (n-1) + ...+ 1 O(n2) 7 n O(n) 8 n O(n) 9 1 O(1) f(n) (n+1)(n+2)/2 + n(n+1) + 4n + 2 O(n2 ) Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 15/ 50
  • 16. Space Complexity Analysis Example 1 In Algorithm 2, the variable noccupies a constant 4Bytes of memory. The function call and return statement come under the auxiliary space and let’s assume 4 Bytes all together. The total space complexity is 8 Bytes. Algorithm 2 has a space complexity of O(1). Example 2 In Algorithm 3, the variables n, sum, and i occupy a constant 12 Bytes of memory. The function call, initialisation of the for loop and write function all come under the auxiliary space and let’s assume 4 Bytes all together. The total space complexity is 4n + 16 Bytes. Algorithm 3 has a space complexity of O(n). Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 16/ 50
  • 17. Space Complexity Analysis Algorithm 5 Factorial of a number 1: procedure FACTORIAL (n) 2: fact ← 1 3: for i ← 1 to n do 4: fact ← fact + i 5: end for 6: return fact 7: end procedure The variables n, fact, and i occupy a constant 12 Bytes of memory. The function call, initializingthe for loop and returnstatement all come under the auxiliary space and let’s assume 4 Bytes all together. The total space complexity is 16 Bytes. Algorithm 5 has a space complexity of O(1). Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 17/ 50
  • 18. Space Complexity Analysis Algorithm 6 Recursive: Factorial of a number 1: procedure FACTORIAL (n) 2: if (n ≤1) then 3: return 1 4: else 5: return n ∗ FACTORIAL(n −1) 6: end if 7: end procedure The variable n occupies a constant 4 Bytes of memory. The function call, if and else conditions and return statement all come under the auxiliary space and let’s assume 4 Bytes all together. The total space complexity is 4n+4 Bytes. Algorithm 6 has a space complexity of O(n). Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 18/ 50
  • 19. Space Complexity Analysis Algorithm 7 Summation of two numbers 1: procedure ADDITION(a, b) 2: c ← a+ b 3: write c 4: end procedure The variables a, band c occupy a constant 12Bytes of memory. The function call, if and else conditions and write function all come under the auxiliary space and let’s assume 4 Bytes all together. The total space complexity is 16 Bytes. Algorithm 7 has a space complexity of O(1). Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 19/ 50