Lecture1 Clean
Lecture1 Clean
Introduction
what is a computer program?
what is an algorithm?
fundamental structure of a computer program
set of instructions written in an abstract form, pseudo-code or flowchart,
independently from any specific computer or programming language
A Valid Algorithm is:
A sequence of unambiguous instructions for solving a problem,
i.e. for obtaining the required output for any legitimate input in
a finite amount of time.
Time efficiency
Space efficiency
Optimality
● Approaches
Theoretical analysis
Empirical analysis
what are the costs of running a computer program on a computer?
Computation time and Storage space required.
cost analysis: we must have an idea of the running time and memory size
required to solve a problem of a certain size
Usually some problem types become intractable when problem size increases.
○ Is the best possible running time for a problem reasonably finite for practical purposes?
● Running Time is the real clock time the machine takes to execute those operations in
the algorithm, this depends on many factors like machine's configuration, current
load in that machine and many more and thus it is not fixed for a particular
algorithm
● Time complexity is the measurement of an algorithm's time behavior as input size
increases.
● Time complexity can also be calculated from the logic behind the algorithm/code.
Often referred to as the complexity of the algorithm.
● Units of measuring running time: Identify and count the basic operation(s) in
the algorithm.
Complexity and Input
● Complexity of an algorithm generally depends on input size.
○ the properties of the objects in the problem, e.g. checking if a given integer is a prime number.
Examples
Problem Size of input
Find x in an array The number of the
elements in the array
Multiply two matrices The dimensions of the
matrices
Sort an array The number of elements
in the array
Traverse a binary tree The number of nodes
Solve a system of linear The number of equations,
equations or the number of the
unknowns, or both
Examples of Basic Operations
Problem Basic Operation
Find x in an array Comparison of x with
an entry in the array
Multiplying two Multiplication of two
matrices with real real numbers
entries
Sort an array of Comparison of two
numbers array entries plus
moving elements in
the array
Traverse a tree Traverse an edge
Work done by an algorithm
Let
T(n): running time
clear and straightforward (the brain must easily comprehend its structure)
without any detail of implementation (unnecessary for analysis)
independent from any programming language
● while construct
● block of code represented with indentation and vertically stretched square bracket
2D array: A[1..n , 1..m] with 1..n and 1..m ranges of indices element: A[i , j]
C code:
#include <stdio.h>
#define N 100
void main()
{ pseudo-code:
int i , n = 0;
double v , x[N] , max; max - ∞
for i 1 to n
while (scanf("%lf" , &v) != EOF) if max < x[i]
{ n++; if (n > N) exit(0); max x[i]
x[n - 1] = v; }
max = -1e20;
for (i = 0 ; i < n ; i++)
if (max < x[i])
max = x[i];
memory size inferior to the RAM (or virtual memory) of our computer.
find the bottlenecks, i.e. the parts of the program that will take the most time
no parallelism, no concurrency
Types of formulas for basic operation count
● Exact formula
e.g., T(n) = n(n-1)/2
running time:
● an iterative algorithm is made of loops (eventually nested) and of tests over the data
● we must count the total number of iterations and express it as an algebraic expression
loops:
● header line of loop executed one more time than the number of iterations:
○ assignment and test on index executed one more time exit from the loop
Line
i i<4 printing
1. for i 0 to 3
2. printf("\n i = %d" , i); 0 true i=0
3. printf("\n i = %d" , i); 1 true i=1
2 true i=2
3 true i=3
4 false
Example: