Space complexity
Unit I Part 2
Creative Commons License
Space Complexity by Bhanusree Yalamanchili is licensed under a Creative Commons
Attribution-ShareAlike 4.0 International License.
Space Complexity
Amount of computer memory that is required during program execution as a function of input
size.
OR
Space complexity is the amount of memory it needs to run to completion
Space=Fixed part +Variable part
Fixed: varies from problem to problem. Includes space needed for storing instructions
variables , constants and structured variables.[arrays , struct]
Variable: varies from program to program. Includes space needed for stack and for
structured variables that are dynamically allocated during run time.
219-01-2017 VNRVJIET::CSE Author: Y.Bhanusree, Assistant Professor
 Fixed Space Requirements (C)
Independent of the characteristics of the inputs and outputs
 instruction space(space for code)
 space for simple variables, fixed-size structured variable, constants
 Variable Space Requirements (SP(I))
depend on the instance characteristic I
 number, size, values of inputs and outputs associated with I
 recursive stack space, formal parameters, local variables, return address
3
S(P)=C+SP(I)
19-01-2017 VNRVJIET::CSE Author: Y.Bhanusree, Assistant Professor
Algorithm abc(a,b,c)
{
return a+b*c+(a-b-)/a+b+4.0
}
Algorithm Sum(a,n)
{
s:=0
for i:=1 to n do
s:=s+a[i];
return s;
}
19-01-2017 VNRVJIET::CSE Author: Y.Bhanusree, Assistant Professor 4
Ex:1
float abc(float a, float b, float c)
{
return a + b + b * c + (a + b - c) / (a + b) + 4.00;
}
Sabc(I) = 0
519-01-2017 VNRVJIET::CSE Author: Y.Bhanusree, Assistant Professor
Ex:2
float sum(float list[ ], int n)
{
float tempsum = 0;
int i;
for (i = 0; i<n; i++)
tempsum += list [i];
return tempsum;
}
Ssum(I) = 0
19-01-2017 VNRVJIET::CSE Author: Y.Bhanusree, Assistant Professor 6
Exercise
Find space complexity for the following algorithms:
• Swap
• Maximum
• Minimum
• Selection sort
• Matrix display
• Matrix multiplication
19-01-2017 VNRVJIET::CSE Author: Y.Bhanusree, Assistant Professor 7
Ex:3 Recursive function for summing a list of numbers
float sum(float list[ ], int n)
{
if (n)
return sum(list, n-1) + list[n];
else
return 0;
}
8
Type Name Number of bytes
parameter: float
parameter: integer
return address:(used internally)
list [ ]
n
4
2
2(unless a far address)
TOTAL per recursive call 8
Ssum(I)=Ssum(n)=8n
19-01-2017 VNRVJIET::CSE Author: Y.Bhanusree, Assistant Professor

Space complexity

  • 1.
    Space complexity Unit IPart 2 Creative Commons License Space Complexity by Bhanusree Yalamanchili is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
  • 2.
    Space Complexity Amount ofcomputer memory that is required during program execution as a function of input size. OR Space complexity is the amount of memory it needs to run to completion Space=Fixed part +Variable part Fixed: varies from problem to problem. Includes space needed for storing instructions variables , constants and structured variables.[arrays , struct] Variable: varies from program to program. Includes space needed for stack and for structured variables that are dynamically allocated during run time. 219-01-2017 VNRVJIET::CSE Author: Y.Bhanusree, Assistant Professor
  • 3.
     Fixed SpaceRequirements (C) Independent of the characteristics of the inputs and outputs  instruction space(space for code)  space for simple variables, fixed-size structured variable, constants  Variable Space Requirements (SP(I)) depend on the instance characteristic I  number, size, values of inputs and outputs associated with I  recursive stack space, formal parameters, local variables, return address 3 S(P)=C+SP(I) 19-01-2017 VNRVJIET::CSE Author: Y.Bhanusree, Assistant Professor
  • 4.
    Algorithm abc(a,b,c) { return a+b*c+(a-b-)/a+b+4.0 } AlgorithmSum(a,n) { s:=0 for i:=1 to n do s:=s+a[i]; return s; } 19-01-2017 VNRVJIET::CSE Author: Y.Bhanusree, Assistant Professor 4
  • 5.
    Ex:1 float abc(float a,float b, float c) { return a + b + b * c + (a + b - c) / (a + b) + 4.00; } Sabc(I) = 0 519-01-2017 VNRVJIET::CSE Author: Y.Bhanusree, Assistant Professor
  • 6.
    Ex:2 float sum(float list[], int n) { float tempsum = 0; int i; for (i = 0; i<n; i++) tempsum += list [i]; return tempsum; } Ssum(I) = 0 19-01-2017 VNRVJIET::CSE Author: Y.Bhanusree, Assistant Professor 6
  • 7.
    Exercise Find space complexityfor the following algorithms: • Swap • Maximum • Minimum • Selection sort • Matrix display • Matrix multiplication 19-01-2017 VNRVJIET::CSE Author: Y.Bhanusree, Assistant Professor 7
  • 8.
    Ex:3 Recursive functionfor summing a list of numbers float sum(float list[ ], int n) { if (n) return sum(list, n-1) + list[n]; else return 0; } 8 Type Name Number of bytes parameter: float parameter: integer return address:(used internally) list [ ] n 4 2 2(unless a far address) TOTAL per recursive call 8 Ssum(I)=Ssum(n)=8n 19-01-2017 VNRVJIET::CSE Author: Y.Bhanusree, Assistant Professor