SlideShare a Scribd company logo
Space Complexity
• Components of Space Complexity
– Instruction space
– Data Space
– Environmental Stack Space
Instruction space
• Space needed to store the compiled version of
the program instructions.
• The compiler used to compile the program
into machine code
– Suppose we have an expression a+b+c*d, the
compiler computes this as (c*d)+a+b and generate
the shorter and more time-efficient code.
Data Space
• The space needed to store all the constants
and variables.
• Data space has two components
1. Space needed by constants and simple variables.
2. Space needed by dynamically allocated objects such
as arrays and class instances
Environment Stack Space
• The environment stack is used to save information
needed to resume execution of partially completed
functions and methods.
• Beginning performance analysts often ignore the
space needed by the environment stack because
they don’t understand how functions are invoked
and what happens on termination.
• Each time a function is invoked the following data
are saved on the environment stack:
– The return address
– The values of all local variables and formal parameters in
the functions being invoked (necessary for recursive
functions only).
#include <iostream.h>
void easy(int N)
{
if (N < 1) return;
easy(N-2);
cout<<N;
easy(N-3);
cout<<N;
}
void main()
{
easy(4);
}
Examples
int main()
{ int i, sum=0;
int n;
cin>>n;
for (i=0; i<n; i++)
sum+=i;
}
Time Complexity - O(n)
Space complexity – O(1)
Examples
int main()
{ int n;
cin>>n;
int *arr;
arr=new int [n];
for (int i=0; i<n; i++)
for (int j=0; j<i; j++)
{
some statements;
}
return 0;
}}
Time Complexity - O(n2)
Space complexity – O(n)
const int n=100;
int main()
{
for (int i=0;i<n;i++)
f();
return 0;
}
void f()
{ int a[n];
for (j=0;j<n;j++)
{
some statements;
}
}
Time Complexity - O(n2)
Space complexity – O(n)
int n=64;
steps=0;
for (int i=1; i<=n;i*=2)
steps++;
cout<<steps;
int* A;
A=new int[steps];
….
….
Time Complexity - O(log n)
Space complexity – O(log n)
Data Structures
Linear
(Lists, Arrays,
Linked lists,
Stacks)
Non-Linear
(Trees,
Graphs…)

More Related Content

PDF
Data Structures Notes
DOCX
Space Complexity in Data Structure.docx
PPT
DAA-Introduction-Divide and Conquer Technique
PPTX
Space complexity
PPT
Chapter 7 Run Time Environment
PPTX
Data structure introduction
PPTX
complexity.pptx
PPTX
Process of algorithm evaluation
Data Structures Notes
Space Complexity in Data Structure.docx
DAA-Introduction-Divide and Conquer Technique
Space complexity
Chapter 7 Run Time Environment
Data structure introduction
complexity.pptx
Process of algorithm evaluation

Similar to CS-102 DS-class04a Lectures DS Class.pdf (20)

PPTX
32d0d9c6-f554-4b97-b1c2-3fc16c5ce70f.pptx
PPTX
Performance analysis(Time & Space Complexity)
PPT
ALGORITHM-ANALYSIS.ppt
PPTX
Detailed explanation of space complexity in algorithms, based on Goodrich, Ta...
PPT
Runtimeenvironment
PPT
Design and analysis of algorithm lecture 3 .ppt
PPTX
Recurrence Relation
PPT
01 introduction to cpp
PPT
Introduction to data structures and Algorithm
PPT
Stack and heap allocation
PPT
Cupdf.com introduction to-data-structures-and-algorithm
PPT
Introduction to data structures and Algorithm
PPTX
Analysis of algorithn class 2
PPTX
Space complexity-DAA.pptx
PPTX
iii-ii cd nCompiler design UNIT-V-1.pptx
DOCX
Data structure notes for introduction, complexity
PPTX
Time and Space Complexity Analysis.pptx
PPTX
Compiler Design_Run time environments.pptx
PPTX
DataSructure-Time and Space Complexity.pptx
PPTX
III_Data Structure_Module_1.pptx
32d0d9c6-f554-4b97-b1c2-3fc16c5ce70f.pptx
Performance analysis(Time & Space Complexity)
ALGORITHM-ANALYSIS.ppt
Detailed explanation of space complexity in algorithms, based on Goodrich, Ta...
Runtimeenvironment
Design and analysis of algorithm lecture 3 .ppt
Recurrence Relation
01 introduction to cpp
Introduction to data structures and Algorithm
Stack and heap allocation
Cupdf.com introduction to-data-structures-and-algorithm
Introduction to data structures and Algorithm
Analysis of algorithn class 2
Space complexity-DAA.pptx
iii-ii cd nCompiler design UNIT-V-1.pptx
Data structure notes for introduction, complexity
Time and Space Complexity Analysis.pptx
Compiler Design_Run time environments.pptx
DataSructure-Time and Space Complexity.pptx
III_Data Structure_Module_1.pptx
Ad

More from ssuser034ce1 (20)

PDF
CSN221_Lec_27 Computer Architecture and Microprocessor
PDF
CSN221_Lec_26 Computer Architecture and Microprocessor
PDF
CSN221_Lec_25 Computer Architecture and Microprocessor
PDF
CSN221_Lec_36 Computer Architecture and Microprocessor
PDF
CSN221_Lec_35 Computer Architecture and Microprocessor
PDF
CSN221_Lec_34 Computer Architecture and Microprocessor
PDF
CSN221_Lec_22.pdf Computer Architecture and Microprocessor
PDF
CSN221_Lec_17.pdf Multi Cycle Datapath Design
PDF
CSN221_Lec_16.pdf MIPS ISA and Datapath design
PDF
CSN221_Lec_15.pdf MIPS ISA and Datapath design
PDF
Computer Architecture CSN221_Lec_37_SpecialTopics.pdf
PDF
CSN221_Lec_5.pdf Computer Organization, CPU Structure and Functions
PDF
CSN221_Lec_4.pdf Computer Organization & Architecture
PDF
CS-102 Data Structures huffman coding.pdf
PDF
CS-102 Data Structures HashFunction CS102.pdf
PDF
CS-102 Data Structure lectures on Graphs
PDF
CS-102 DS-class03 Class DS Lectures .pdf
PDF
CS-102 DS-class_01_02 Lectures Data .pdf
PDF
CS-102 BT_24_3_14 Binary Tree Lectures.pdf
PDF
CS-102 Course_ Binary Tree Lectures .pdf
CSN221_Lec_27 Computer Architecture and Microprocessor
CSN221_Lec_26 Computer Architecture and Microprocessor
CSN221_Lec_25 Computer Architecture and Microprocessor
CSN221_Lec_36 Computer Architecture and Microprocessor
CSN221_Lec_35 Computer Architecture and Microprocessor
CSN221_Lec_34 Computer Architecture and Microprocessor
CSN221_Lec_22.pdf Computer Architecture and Microprocessor
CSN221_Lec_17.pdf Multi Cycle Datapath Design
CSN221_Lec_16.pdf MIPS ISA and Datapath design
CSN221_Lec_15.pdf MIPS ISA and Datapath design
Computer Architecture CSN221_Lec_37_SpecialTopics.pdf
CSN221_Lec_5.pdf Computer Organization, CPU Structure and Functions
CSN221_Lec_4.pdf Computer Organization & Architecture
CS-102 Data Structures huffman coding.pdf
CS-102 Data Structures HashFunction CS102.pdf
CS-102 Data Structure lectures on Graphs
CS-102 DS-class03 Class DS Lectures .pdf
CS-102 DS-class_01_02 Lectures Data .pdf
CS-102 BT_24_3_14 Binary Tree Lectures.pdf
CS-102 Course_ Binary Tree Lectures .pdf
Ad

Recently uploaded (20)

PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
PPT on Performance Review to get promotions
PPTX
web development for engineering and engineering
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
Sustainable Sites - Green Building Construction
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
DOCX
573137875-Attendance-Management-System-original
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
UNIT 4 Total Quality Management .pptx
PDF
Digital Logic Computer Design lecture notes
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
Internet of Things (IOT) - A guide to understanding
PPT
Project quality management in manufacturing
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Model Code of Practice - Construction Work - 21102022 .pdf
PPT on Performance Review to get promotions
web development for engineering and engineering
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Sustainable Sites - Green Building Construction
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
573137875-Attendance-Management-System-original
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Automation-in-Manufacturing-Chapter-Introduction.pdf
bas. eng. economics group 4 presentation 1.pptx
CH1 Production IntroductoryConcepts.pptx
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
UNIT 4 Total Quality Management .pptx
Digital Logic Computer Design lecture notes
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Internet of Things (IOT) - A guide to understanding
Project quality management in manufacturing

CS-102 DS-class04a Lectures DS Class.pdf

  • 1. Space Complexity • Components of Space Complexity – Instruction space – Data Space – Environmental Stack Space
  • 2. Instruction space • Space needed to store the compiled version of the program instructions. • The compiler used to compile the program into machine code – Suppose we have an expression a+b+c*d, the compiler computes this as (c*d)+a+b and generate the shorter and more time-efficient code.
  • 3. Data Space • The space needed to store all the constants and variables. • Data space has two components 1. Space needed by constants and simple variables. 2. Space needed by dynamically allocated objects such as arrays and class instances
  • 4. Environment Stack Space • The environment stack is used to save information needed to resume execution of partially completed functions and methods. • Beginning performance analysts often ignore the space needed by the environment stack because they don’t understand how functions are invoked and what happens on termination. • Each time a function is invoked the following data are saved on the environment stack: – The return address – The values of all local variables and formal parameters in the functions being invoked (necessary for recursive functions only).
  • 5. #include <iostream.h> void easy(int N) { if (N < 1) return; easy(N-2); cout<<N; easy(N-3); cout<<N; } void main() { easy(4); }
  • 6. Examples int main() { int i, sum=0; int n; cin>>n; for (i=0; i<n; i++) sum+=i; } Time Complexity - O(n) Space complexity – O(1)
  • 7. Examples int main() { int n; cin>>n; int *arr; arr=new int [n]; for (int i=0; i<n; i++) for (int j=0; j<i; j++) { some statements; } return 0; }} Time Complexity - O(n2) Space complexity – O(n)
  • 8. const int n=100; int main() { for (int i=0;i<n;i++) f(); return 0; } void f() { int a[n]; for (j=0;j<n;j++) { some statements; } } Time Complexity - O(n2) Space complexity – O(n)
  • 9. int n=64; steps=0; for (int i=1; i<=n;i*=2) steps++; cout<<steps; int* A; A=new int[steps]; …. …. Time Complexity - O(log n) Space complexity – O(log n)
  • 10. Data Structures Linear (Lists, Arrays, Linked lists, Stacks) Non-Linear (Trees, Graphs…)