Computer Science Dept

CLA CS-101: Introduction to Computers

- There were questions about things we haven't studied. We had no idea about them.---It was "impossible" to answer them.
- There were questions that took a very long time. They required a lot of calculations and a lot of thinking.---They were time consuming.
- There were questions that required a lot of writing. The answers were easy but needed a lot of paper to write them.---They were space consuming.

* To evaluate the complexity of a program is to study how the
space/time requirements of the program change with the problem
size.*

Let's look at an example. Suppose we want to write a program to compute the sum of a list of numbers. The program goes into a loop (for a predetermined number of iterations equal to the size of the list, for example 100 or 200, ... etc.) asking the user to enter a number and keeps adding these numbers. What is the space complexity of that program?

There are three simple steps that one should follow:

- Identify the parameter(s) that determine the problem size,
- Find out how much space (i.e. memory) is needed for a particular size,
- Find out how much space (i.e. memory) is needed for twice the size considered earlier,
- Repeat step 3 many times until you establish a relationship between the size of the problem and its space requirements. That relationship is the space complexity of that program.

- The problem size is obviously the size of the list of numbers to be added.
- Let's assume that we have a list of 10 numbers. Obviously, we need a variable where the numbers are to be eneterd (one at a time), and a variable (initially 0) where a running sum is to be kept. Thus we need two variables.
- Let's assume that we have a list of 20 numbers (twice as before). Obviously, it doesn't matter; we still need only two variables.
- Let's assume that we have a list of 40 numbers (twice as before). Obviously, it still doesn't matter; we still need only two variables.
- Obviously, we may conclude that no matter how many numbers we want
to add, we still need a constant number of variables, thus we say
that the relationship between problem size and space (i.e. the
space complexity of the program) is
*constant*.

There are three simple steps that one should follow:

- Identify the parameter(s) that determine the problem size,
- Find out how much time (approximately) is needed for a particular size,
- Find out how much time is needed for twice the size considered earlier,
- Repeat step 3 many times until you establish a relationship between the size of the problem and its time requirements. That relationship is the time complexity of that program.

- The problem size is obviously the size of the list of numbers to be added.
- Let's assume that we have a list of 10 numbers. Let's assume that it takes a micro-second to do one addition. Thus, we need 10 micro-seconds to add the 10 numbers, since in each iteration we have to do one addition.
- Now let's assume that we have a list of 20 numbers (twice as before). Obviously, now we need 20 micro-seconds to finish adding all 20 numbers.
- Now let's assume that we have a list of 40 numbers (twice as before). Obviously, now we need 40 micro-seconds to finish adding all 40 numbers.
- Obviously, we may conclude that the time we need to compute the
total of a list of numbers grows proportional to the size of that
list. In other words, doubling the size of the list results in
doubling the time needed to add the list. We call this
relationship a
*linear*relationship. Thus the time complexity of the program is*linear*.

[ ... To be Continued ... ]

(C) Copyright 1995.

This document has been prepared by Professor Azer Bestavros <

Created on:April 13, 1995.Updated on:April 12, 1995.