SlideShare uma empresa Scribd logo
1 de 10
SPACE COMPLEXITY
NAME: MOUNIKA
PIN: 21BQ5A0514
Introduction to Space complexity
Calculation of Space Complexity
for an Algorithm
Example of Space Complexity for
an Algorithm
INTRODUCTION TO SPACE COMPLEXITY
 Space complexity refers to the amount of memory or storage
space required by an algorithm to solve a problem.
 Space complexity is usually measured in terms of the number of
bits or bytes required to store the data used by the algorithm.
This includes both the input data and any additional memory
used by the algorithm during its execution.
CALCULATION OF SPACE COMPLEXITY FOR AN ALGORITHM
 Space complexity of an algorithm is sum of space required for fixed part of algorithm and space
required for variable part of algorithm.
 Space Complexity = Space required for fixed part +Space required for
variable part
Space Complexity = Space required for fixed part + Space required for variable
part
To estimate the memory requirement we need to focus on two parts:
(1) A fixed part: It is independent of the input size. It includes memory for instructions
(code), constants, variables, etc.
(2) A variable part: It is dependent on the input size. It includes memory for recursion stack,
referenced variables, etc.
Under fixed part, the space for the following is considered
1) Codeofalgorithm
2) Simplevariablesorlocalvariables
3) Definedconstants
Under variable part, the space for the following is considered
1) Variableswhosesizevariesfromoneinstanceoftheproblemtoanotherinstance(arrays,structuresandsoon)
2) Globalorreferencedvariables
3) Recursionstack
Recursion stack space is considered only for recursive algorithms. For each call of recursive
algorithm, the following information is stored in recursion stack
1) Valuesofformalparameters
2) Valuesoflocalvariables
3) Returnvalue
Example: Calculate space complexity of the following algorithm
Algorithm Add(a, b)
{
c := a + b;
write c;
}
Space complexity=space for fixed part + space for variable part
Space for fixed part:
Space for code=c words
Space for simple variables=3 (a, b, c) words
Space for defined constants=0 words
Space for variable part:
Space for arrays=0 words
Space for global variables=0 words
Space for recursion stack=0 words
Space complexity=c+3 +0+0+0+0=(c+3) words
Example : calculate space complexity for the following recursive algorithm
Algorithm Rsum(a, n)
// a is an array of size
{
if n = 0 then
return 0;
else
return a[n] + Rsum(a, n-1);
}
Space for fixed part:
Space for code=c words
Space for simple variables=1 (n) word
Space for defined constants=0 words
Space for variable part:
Space for arrays=n words
Space for global variables=0 words Space for recursion
stack=3(n+1) words
For each call of the algorithm, three values are stored in recursion stack (formal parameters: n,
starting address of array and return value). The algorithm is called for n+1 times. Total space
required by the recursion stack is (n+1)*3 words.
Space complexity = c+1+0+n+0+(n+1)3=(c+4n+4) words
Space complexity-DAA.pptx

Mais conteúdo relacionado

Mais procurados

Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
Ehtisham Ali
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysis
sumitbardhan
 
Elementary data organisation
Elementary data organisationElementary data organisation
Elementary data organisation
Muzamil Hussain
 
Performance analysis and randamized agoritham
Performance analysis and randamized agorithamPerformance analysis and randamized agoritham
Performance analysis and randamized agoritham
lilyMalar1
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1
Shashwat Shriparv
 

Mais procurados (20)

Abstract Data Types
Abstract Data TypesAbstract Data Types
Abstract Data Types
 
Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
 
Big o notation
Big o notationBig o notation
Big o notation
 
Symbol table management and error handling in compiler design
Symbol table management and error handling in compiler designSymbol table management and error handling in compiler design
Symbol table management and error handling in compiler design
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
Run time storage
Run time storageRun time storage
Run time storage
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysis
 
Elementary data organisation
Elementary data organisationElementary data organisation
Elementary data organisation
 
Big o notation
Big o notationBig o notation
Big o notation
 
Recursion
RecursionRecursion
Recursion
 
Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - Graph
 
Randomized algorithms ver 1.0
Randomized algorithms ver 1.0Randomized algorithms ver 1.0
Randomized algorithms ver 1.0
 
Algorithm analysis and efficiency
Algorithm analysis and efficiencyAlgorithm analysis and efficiency
Algorithm analysis and efficiency
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
 
Performance analysis and randamized agoritham
Performance analysis and randamized agorithamPerformance analysis and randamized agoritham
Performance analysis and randamized agoritham
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1
 
Lecture optimal binary search tree
Lecture optimal binary search tree Lecture optimal binary search tree
Lecture optimal binary search tree
 
Relationship Among Token, Lexeme & Pattern
Relationship Among Token, Lexeme & PatternRelationship Among Token, Lexeme & Pattern
Relationship Among Token, Lexeme & Pattern
 
Insertion Sorting
Insertion SortingInsertion Sorting
Insertion Sorting
 

Semelhante a Space complexity-DAA.pptx

Module 1 notes of data warehousing and data
Module 1 notes of data warehousing and dataModule 1 notes of data warehousing and data
Module 1 notes of data warehousing and data
vijipersonal2012
 
Intro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technologyIntro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technology
worldchannel
 
Lec2&3 data structure
Lec2&3 data structureLec2&3 data structure
Lec2&3 data structure
Saad Gabr
 
Data Structure & Algorithms - Mathematical
Data Structure & Algorithms - MathematicalData Structure & Algorithms - Mathematical
Data Structure & Algorithms - Mathematical
babuk110
 

Semelhante a Space complexity-DAA.pptx (20)

Module 1 notes of data warehousing and data
Module 1 notes of data warehousing and dataModule 1 notes of data warehousing and data
Module 1 notes of data warehousing and data
 
VCE Unit 01 (2).pptx
VCE Unit 01 (2).pptxVCE Unit 01 (2).pptx
VCE Unit 01 (2).pptx
 
VCE Unit 01 (1).pptx
VCE Unit 01 (1).pptxVCE Unit 01 (1).pptx
VCE Unit 01 (1).pptx
 
Buffer overflow attack
Buffer overflow attackBuffer overflow attack
Buffer overflow attack
 
data structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysoredata structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysore
 
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
 
Process of algorithm evaluation
Process of algorithm evaluationProcess of algorithm evaluation
Process of algorithm evaluation
 
Unit i basic concepts of algorithms
Unit i basic concepts of algorithmsUnit i basic concepts of algorithms
Unit i basic concepts of algorithms
 
Intro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technologyIntro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technology
 
Lec2&3 data structure
Lec2&3 data structureLec2&3 data structure
Lec2&3 data structure
 
Lec2
Lec2Lec2
Lec2
 
Lec2&3_DataStructure
Lec2&3_DataStructureLec2&3_DataStructure
Lec2&3_DataStructure
 
Data Structure & Algorithms - Mathematical
Data Structure & Algorithms - MathematicalData Structure & Algorithms - Mathematical
Data Structure & Algorithms - Mathematical
 
Unit 3
Unit 3 Unit 3
Unit 3
 
Complexity
ComplexityComplexity
Complexity
 
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...
 
Functions, Strings ,Storage classes in C
 Functions, Strings ,Storage classes in C Functions, Strings ,Storage classes in C
Functions, Strings ,Storage classes in C
 
Space Complexity in Data Structure.docx
Space Complexity in Data Structure.docxSpace Complexity in Data Structure.docx
Space Complexity in Data Structure.docx
 
Programming in C sesion 2
Programming in C sesion 2Programming in C sesion 2
Programming in C sesion 2
 
E04612529
E04612529E04612529
E04612529
 

Mais de mounikanarra3 (15)

unit-2.pdf
unit-2.pdfunit-2.pdf
unit-2.pdf
 
Unit - 4.pptx
Unit - 4.pptxUnit - 4.pptx
Unit - 4.pptx
 
UNIT-1 (4).pdf
UNIT-1 (4).pdfUNIT-1 (4).pdf
UNIT-1 (4).pdf
 
functionsinc-130108032745-phpapp01.pdf
functionsinc-130108032745-phpapp01.pdffunctionsinc-130108032745-phpapp01.pdf
functionsinc-130108032745-phpapp01.pdf
 
travelingsalesmanproblem-170122053648.pdf
travelingsalesmanproblem-170122053648.pdftravelingsalesmanproblem-170122053648.pdf
travelingsalesmanproblem-170122053648.pdf
 
EEM MID2.PPT.pptx
EEM MID2.PPT.pptxEEM MID2.PPT.pptx
EEM MID2.PPT.pptx
 
MID2 UML (1).pptx
MID2 UML (1).pptxMID2 UML (1).pptx
MID2 UML (1).pptx
 
(PAD_5)Dynamic_Programming.ppt
(PAD_5)Dynamic_Programming.ppt(PAD_5)Dynamic_Programming.ppt
(PAD_5)Dynamic_Programming.ppt
 
sequencediagram-150302224029-conversion-gate01 (1).pdf
sequencediagram-150302224029-conversion-gate01 (1).pdfsequencediagram-150302224029-conversion-gate01 (1).pdf
sequencediagram-150302224029-conversion-gate01 (1).pdf
 
UML.PPT.pptx
UML.PPT.pptxUML.PPT.pptx
UML.PPT.pptx
 
exp227-jan-170127160848 (3) (1).pdf
exp227-jan-170127160848 (3) (1).pdfexp227-jan-170127160848 (3) (1).pdf
exp227-jan-170127160848 (3) (1).pdf
 
stephenhawkingppt-160402093003.pdf
stephenhawkingppt-160402093003.pdfstephenhawkingppt-160402093003.pdf
stephenhawkingppt-160402093003.pdf
 
CP-STRING (1).ppt
CP-STRING (1).pptCP-STRING (1).ppt
CP-STRING (1).ppt
 
Array.pdf
Array.pdfArray.pdf
Array.pdf
 
routing.pptx
routing.pptxrouting.pptx
routing.pptx
 

Último

Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
Kamal Acharya
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
jaanualu31
 

Último (20)

Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network Devices
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
 
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxOrlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 

Space complexity-DAA.pptx

  • 2. Introduction to Space complexity Calculation of Space Complexity for an Algorithm Example of Space Complexity for an Algorithm
  • 3. INTRODUCTION TO SPACE COMPLEXITY  Space complexity refers to the amount of memory or storage space required by an algorithm to solve a problem.  Space complexity is usually measured in terms of the number of bits or bytes required to store the data used by the algorithm. This includes both the input data and any additional memory used by the algorithm during its execution.
  • 4. CALCULATION OF SPACE COMPLEXITY FOR AN ALGORITHM  Space complexity of an algorithm is sum of space required for fixed part of algorithm and space required for variable part of algorithm.  Space Complexity = Space required for fixed part +Space required for variable part Space Complexity = Space required for fixed part + Space required for variable part To estimate the memory requirement we need to focus on two parts: (1) A fixed part: It is independent of the input size. It includes memory for instructions (code), constants, variables, etc. (2) A variable part: It is dependent on the input size. It includes memory for recursion stack, referenced variables, etc.
  • 5. Under fixed part, the space for the following is considered 1) Codeofalgorithm 2) Simplevariablesorlocalvariables 3) Definedconstants Under variable part, the space for the following is considered 1) Variableswhosesizevariesfromoneinstanceoftheproblemtoanotherinstance(arrays,structuresandsoon) 2) Globalorreferencedvariables 3) Recursionstack Recursion stack space is considered only for recursive algorithms. For each call of recursive algorithm, the following information is stored in recursion stack 1) Valuesofformalparameters 2) Valuesoflocalvariables 3) Returnvalue
  • 6. Example: Calculate space complexity of the following algorithm Algorithm Add(a, b) { c := a + b; write c; } Space complexity=space for fixed part + space for variable part Space for fixed part: Space for code=c words Space for simple variables=3 (a, b, c) words Space for defined constants=0 words
  • 7. Space for variable part: Space for arrays=0 words Space for global variables=0 words Space for recursion stack=0 words Space complexity=c+3 +0+0+0+0=(c+3) words
  • 8. Example : calculate space complexity for the following recursive algorithm Algorithm Rsum(a, n) // a is an array of size { if n = 0 then return 0; else return a[n] + Rsum(a, n-1); } Space for fixed part: Space for code=c words Space for simple variables=1 (n) word Space for defined constants=0 words
  • 9. Space for variable part: Space for arrays=n words Space for global variables=0 words Space for recursion stack=3(n+1) words For each call of the algorithm, three values are stored in recursion stack (formal parameters: n, starting address of array and return value). The algorithm is called for n+1 times. Total space required by the recursion stack is (n+1)*3 words. Space complexity = c+1+0+n+0+(n+1)3=(c+4n+4) words