SlideShare a Scribd company logo
1 of 10
Download to read offline
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

Similar to CS-102 DS-class04a Lectures DS Class.pdf

Educational Objectives After successfully completing this assignmen.pdf
Educational Objectives After successfully completing this assignmen.pdfEducational Objectives After successfully completing this assignmen.pdf
Educational Objectives After successfully completing this assignmen.pdf
rajeshjangid1865
 

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

Unit-1_Digital Computers, number systemCOA[1].pptx
Unit-1_Digital Computers, number systemCOA[1].pptxUnit-1_Digital Computers, number systemCOA[1].pptx
Unit-1_Digital Computers, number systemCOA[1].pptx
 
Computer programming and utilization
Computer programming and utilizationComputer programming and utilization
Computer programming and utilization
 
CNIT 127: Ch 2: Stack Overflows in Linux
CNIT 127: Ch 2: Stack Overflows in LinuxCNIT 127: Ch 2: Stack Overflows in Linux
CNIT 127: Ch 2: Stack Overflows in Linux
 
MapReduce: Ordering and Large-Scale Indexing on Large Clusters
MapReduce: Ordering and  Large-Scale Indexing on Large ClustersMapReduce: Ordering and  Large-Scale Indexing on Large Clusters
MapReduce: Ordering and Large-Scale Indexing on Large Clusters
 
Ch05
Ch05Ch05
Ch05
 
02 functions, variables, basic input and output of c++
02   functions, variables, basic input and output of c++02   functions, variables, basic input and output of c++
02 functions, variables, basic input and output of c++
 
Parallel processing and pipelining
Parallel processing and pipeliningParallel processing and pipelining
Parallel processing and pipelining
 
Logic synthesis,flootplan&placement
Logic synthesis,flootplan&placementLogic synthesis,flootplan&placement
Logic synthesis,flootplan&placement
 
Scala and spark
Scala and sparkScala and spark
Scala and spark
 
Computer organization basics
Computer organization  basicsComputer organization  basics
Computer organization basics
 
Educational Objectives After successfully completing this assignmen.pdf
Educational Objectives After successfully completing this assignmen.pdfEducational Objectives After successfully completing this assignmen.pdf
Educational Objectives After successfully completing this assignmen.pdf
 
Principal Sources of Optimization in compiler design
Principal Sources of Optimization in compiler design Principal Sources of Optimization in compiler design
Principal Sources of Optimization in compiler design
 
Apache Hadoop MapReduce Tutorial
Apache Hadoop MapReduce TutorialApache Hadoop MapReduce Tutorial
Apache Hadoop MapReduce Tutorial
 
Apache Spark: What's under the hood
Apache Spark: What's under the hoodApache Spark: What's under the hood
Apache Spark: What's under the hood
 
Computer architecture short note (version 8)
Computer architecture short note (version 8)Computer architecture short note (version 8)
Computer architecture short note (version 8)
 
Data Structures Notes
Data Structures NotesData Structures Notes
Data Structures Notes
 
Stream processing from single node to a cluster
Stream processing from single node to a clusterStream processing from single node to a cluster
Stream processing from single node to a cluster
 
Embedded _c_
Embedded  _c_Embedded  _c_
Embedded _c_
 
Dache: A Data Aware Caching for Big-Data using Map Reduce framework
Dache: A Data Aware Caching for Big-Data using Map Reduce frameworkDache: A Data Aware Caching for Big-Data using Map Reduce framework
Dache: A Data Aware Caching for Big-Data using Map Reduce framework
 
127 Ch 2: Stack overflows on Linux
127 Ch 2: Stack overflows on Linux127 Ch 2: Stack overflows on Linux
127 Ch 2: Stack overflows on Linux
 

More from ssuser034ce1 (12)

CS-102 Data Structures huffman coding.pdf
CS-102 Data Structures huffman coding.pdfCS-102 Data Structures huffman coding.pdf
CS-102 Data Structures huffman coding.pdf
 
CS-102 Data Structures HashFunction CS102.pdf
CS-102 Data Structures HashFunction CS102.pdfCS-102 Data Structures HashFunction CS102.pdf
CS-102 Data Structures HashFunction CS102.pdf
 
CS-102 Data Structure lectures on Graphs
CS-102 Data Structure lectures on GraphsCS-102 Data Structure lectures on Graphs
CS-102 Data Structure lectures on Graphs
 
CS-102 DS-class03 Class DS Lectures .pdf
CS-102 DS-class03 Class DS Lectures .pdfCS-102 DS-class03 Class DS Lectures .pdf
CS-102 DS-class03 Class DS Lectures .pdf
 
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
 
CS-102 BT_24_3_14 Binary Tree Lectures.pdf
CS-102 BT_24_3_14 Binary Tree Lectures.pdfCS-102 BT_24_3_14 Binary Tree Lectures.pdf
CS-102 BT_24_3_14 Binary Tree Lectures.pdf
 
CS-102 Course_ Binary Tree Lectures .pdf
CS-102 Course_ Binary Tree Lectures .pdfCS-102 Course_ Binary Tree Lectures .pdf
CS-102 Course_ Binary Tree Lectures .pdf
 
CS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdfCS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdf
 
CS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdfCS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdf
 
CS-102 AVLSv2.pdf
CS-102 AVLSv2.pdfCS-102 AVLSv2.pdf
CS-102 AVLSv2.pdf
 
CS-102 AVLS.pdf
CS-102 AVLS.pdfCS-102 AVLS.pdf
CS-102 AVLS.pdf
 
SURA presentation_.pptx
SURA presentation_.pptxSURA presentation_.pptx
SURA presentation_.pptx
 

Recently uploaded

Cybercrimes in the Darknet and Their Detections: A Comprehensive Analysis and...
Cybercrimes in the Darknet and Their Detections: A Comprehensive Analysis and...Cybercrimes in the Darknet and Their Detections: A Comprehensive Analysis and...
Cybercrimes in the Darknet and Their Detections: A Comprehensive Analysis and...
dannyijwest
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
Introduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptxIntroduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptx
hublikarsn
 

Recently uploaded (20)

Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...
 
UNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptxUNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptx
 
Cybercrimes in the Darknet and Their Detections: A Comprehensive Analysis and...
Cybercrimes in the Darknet and Their Detections: A Comprehensive Analysis and...Cybercrimes in the Darknet and Their Detections: A Comprehensive Analysis and...
Cybercrimes in the Darknet and Their Detections: A Comprehensive Analysis and...
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
Electromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptxElectromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptx
 
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
 
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 ...
 
Adsorption (mass transfer operations 2) ppt
Adsorption (mass transfer operations 2) pptAdsorption (mass transfer operations 2) ppt
Adsorption (mass transfer operations 2) ppt
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
Ground Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth ReinforcementGround Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth Reinforcement
 
litvinenko_Henry_Intrusion_Hong-Kong_2024.pdf
litvinenko_Henry_Intrusion_Hong-Kong_2024.pdflitvinenko_Henry_Intrusion_Hong-Kong_2024.pdf
litvinenko_Henry_Intrusion_Hong-Kong_2024.pdf
 
Worksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptxWorksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptx
 
Introduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptxIntroduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptx
 
Unsatisfied Bhabhi ℂall Girls Ahmedabad Book Esha 6378878445 Top Class ℂall G...
Unsatisfied Bhabhi ℂall Girls Ahmedabad Book Esha 6378878445 Top Class ℂall G...Unsatisfied Bhabhi ℂall Girls Ahmedabad Book Esha 6378878445 Top Class ℂall G...
Unsatisfied Bhabhi ℂall Girls Ahmedabad Book Esha 6378878445 Top Class ℂall G...
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
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
 
Introduction to Artificial Intelligence ( AI)
Introduction to Artificial Intelligence ( AI)Introduction to Artificial Intelligence ( AI)
Introduction to Artificial Intelligence ( AI)
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and properties
 
Fundamentals of Internet of Things (IoT) Part-2
Fundamentals of Internet of Things (IoT) Part-2Fundamentals of Internet of Things (IoT) Part-2
Fundamentals of Internet of Things (IoT) Part-2
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 

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…)