Chapter One: Introduction To Theory of Algorithm
Chapter One: Introduction To Theory of Algorithm
Chapter One: Introduction To Theory of Algorithm
INTRODUCTION TO
THEORY OF ALGORITHM
chapter 1
OUTLINE
2
➢ Application of Algorithm
➢ Why we need to Study Algorithms
➢ Brain Storming examples on Algorithm
➢ Classification of Algorithm
➢ Classification of Algorithm by implementation
➢ Classification of Algorithm by design paradigm
➢ Important assumption in this course
➢ Elementary Sorting Algorithm
➢ Selection sort
➢ insertion sort
➢ Bubble sort
chapter 1
ALGORITHM
3
chapter 1
PROPERTIES OF ALGORITHM
4
Finite
Every valid algorithm must complete or terminate after a
finite number of steps.
Every algorithm should have a beginning (start) and a halt
(end) step
The first step (start step) and last step (halt step) must be
clearly noted
If you trace out the instructions of an algorithm, then for all
cases the algorithm must terminate after a finite number of
steps
It must eventually stop either with the right output or with a
statement that no solution is possible
chapter 1
PROPERTIES OF ALGORITHM
5
Finite
Finiteness is an issue for computer algorithms because
Computer algorithms often repeat instructions
If the algorithm doesn’t specify when to stop, the computer
will continue to repeat the instructions forever
chapter 1
PROPERTIES OF ALGORITHM
6
Sequence
chapter 1
PROPERTIES OF ALGORITHM
7
Unambiguous
Define rigorously the sequence of operations performed for
transforming the inputs into the outputs
No ambiguous statements are allowed:
Each step of an algorithm must be clearly and precisely
defined, having one and only one interpretation.
At each point in computation, one should be able to tell
exactly what will happen next
Algorithms must specify every step.
It must be composed of concrete steps
chapter 1
PROPERTIES OF ALGORITHM
8
Unambiguous
Every detail of each step must be spelled out, including how
to handle errors
This ensures that if the algorithm is performed at different
times or by different systems using the same data, the output
will be the same.
chapter 1
PROPERTIES OF ALGORITHM
9
Input specified
The inputs are the data that will be transformed during the
computation to produce the output
An input to an algorithm specifies an instance of the problem
the algorithm solves
Every algorithm should have a specified number (zero or
more) input values (or quantities) which are externally
supplied
We must specify the type of data and the amount of data
Note that, correct algorithm is not one that works most of the
time but one that works correctly for all legitimate inputs
chapter 1
PROPERTIES OF ALGORITHM
10
Output specified
The output is the data resulting from the computation
It is the intended result
Every algorithm should have one or a sequence of output
values
There must be one or more result values
A possible output for some computations is a statement that
there can be no output, i.e., no solution is possible
The algorithm can be proved to produce the correct output
given a valid input.
chapter 1
CORRECT ALGORITHM
11
chapter 1
INCORRECT ALGORITHM
12
An incorrect algorithm
might not halt at all on some input instances, or
might halt with a wrong answer.
chapter 1
APPLICATION OF ALGORITHM
13
➢ The resource refers to the data that we can operate and the
format that it appears
chapter 1
APPLICATION OF ALGORITHM
14
➢ Lets assume two public buses start travelling from one city to
another city at almost the same time. However, one of the bus
arrive long time after arrival of the other bus. Why?
chapter 1
APPLICATION OF ALGORITHM
15
chapter 1
WHY WE NEED TO STUDY ALGORITHM
16
chapter 1
BRAIN STORMING
(Example on algorithm)
17
Example
➢ Find the Greatest Common Divisor (GCD) of two integer
numbers (n, m)
➢ k is said to be the GCD of n and m if k is the largest
possible integer less than or equal to both n and m that
divides both n and m without remainder
➢ i.e k s such that s = min(n,m), m mod k = n mod k = 0
➢ Properties of GCD(n,m)
➢ GCD(n,m) = GCD(m,n)
➢ GCD(0,n) = GCD(n,0) = n
chapter 1
BRAIN STORMING
(Example on algorithm)
18
Example
➢ Three different algorithm will be reviewed in this discussion
to computer GCD(n,m) each differ in
➢ Idea of implementation,
➢ Level of sophistication, and
➢ Efficiency
chapter 1
BRAIN STORMING
(Example on algorithm)
19
Example
➢ The three approaches are:
1. Middle-school procedure
➢ Prime factorization
2. Consecutive integer checking algorithm
3. Euclid’s algorithm (option 1)
4. Euclid’s algorithm (option 2)
chapter 1
BRAIN STORMING
(Example on algorithm)
20
Middle-school procedure
➢ Is a procedure for finding GCD using our knowledge of
middle-school
➢ To do this,
➢ Find prime factors (divisors) of the given inputs: m and n
➢ Identify the common prime factors of m and n
➢ Compute the product of all the common factors which
will be the GCD(n,m)
chapter 1
BRAIN STORMING
(Example on algorithm)
21
chapter 1
BRAIN STORMING
(Example on algorithm)
22
chapter 1
BRAIN STORMING
(Example on algorithm)
23
chapter 1
BRAIN STORMING
(Example on algorithm)
24
chapter 1
BRAIN STORMING
(Example on algorithm)
25
chapter 1
BRAIN STORMING
(Example on algorithm)
27
chapter 1
BRAIN STORMING
(Example on algorithm)
28
chapter 1
29
#include <iostream>
using namespace std;
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b); }
int main()
{ int a , b; cout<<"Enter the values of a and b: "<<endl;
cin>>a>>b; cout<<"GCD of "<< a <<" and "<< b <<" is
"<< gcd(a, b); return 0; }
chapter 1
BRAIN STORMING
(Example on algorithm)
30
Example:
➢ How do you compute whether a given number is prime or
not?
➢ Is there any other mechanism to do that?
chapter 1
BRAIN STORMING
(Example on algorithm)
31
Example:
➢ Design an algorithm that identify the common factors in two
prime expressions
chapter 1
CLASSIFICATION OF ALGORITHM
32
chapter 1
CLASSIFICATION OF ALGORITHM
(by implementation)
33
chapter 1
CLASSIFICATION OF ALGORITHM
(by implementation)
34
chapter 1
CLASSIFICATION OF ALGORITHM
(by implementation)
35
chapter 1
CLASSIFICATION OF ALGORITHM
(by design paradigm)
38
chapter 1
CLASSIFICATION OF ALGORITHM
(by design paradigm)
39
chapter 1
CLASSIFICATION OF ALGORITHM
(by design paradigm)
40
chapter 1
CLASSIFICATION OF ALGORITHM
(by design paradigm)
41
2. dynamic programming
➢ “Programming” in this context refers to a tabular method of
solving problem, not written code in computer programming
language
➢ In dymamic programming, every subproblem share the same
subsubproblems and each of the subsubproblems will be
solved once and answer will be stored in a table so that
every subproblems will not solve the subsubproblems again
and again
chapter 1
CLASSIFICATION OF ALGORITHM
(by design paradigm)
42
2. dynamic programming
➢ For example you may wish to move from start to goal as shown
bellow. You may have three options at the start with different cost
which you don’t know the best route either through A, B or C
➢ Moreover, you may change the start location to be A, B and C
and find the best route and add with the cost from start to A, B,
and C respectively to answer the problem
chapter 1
CLASSIFICATION OF ALGORITHM
(by design paradigm)
43
chapter 1
CLASSIFICATION OF ALGORITHM
(by design paradigm)
44
chapter 1
IMPORTANT ASSUMPTIONS
(for the course)
45
chapter 1
ELEMENTARY SORTING ALGORITHM
46
chapter 1
ELEMENTARY SORTING ALGORITHM
47
2. Bubble sort
3. Insertion sort
chapter 1
ELEMENTARY SORTING ALGORITHM
48
1. Selection sort
Given a list of objects from index [i, MAX], it searches for
the minIndex which holds the minimum element of all and
swap this element with the element at index i.
Do this fom all possible value of i from 0 to (MAX-1)
where MAX is the size of the list
chapter 1
ELEMENTARY SORTING ALGORITHM
49
SelectionSort (A, n)
for i 0 to n-1
minIndex i
//find the actual minIndex from index [I, n]
for j i+1 to n
if(A[j] < A[minIndex])
minIndex j
temp A[minIndex]
chapter 1
ELEMENTARY SORTING ALGORITHM
50
2. insertion sort
Given a sub list of object in the index range [0,i] where all the
element in the index range [0 , i-1] are sorted; find the place
where the element at index i will be inserted by shifting all the
elements in the range [0, i-1] one step to the right until we found
the index for the element at index i.
This produce the list of objects in the range [0,i] to be sorted
Process this for all i from 1 to n
chapter 1
ELEMENTARY SORTING ALGORITHM
51
2. insertion sort
10 24 45 19
Insert 19 (Given i=3)
10 24 24 45 To Insert 19 we found index 1 by
10 19 24 45 replacing 24
chapter 1
ELEMENTARY SORTING ALGORITHM
52
3. Bubble sort
Given a list of unsorted object from index 0 to j, compare
each of the adjacent objects and do swapping when they are
no in order from left to right
This permit the element at index j be the maximum of the
list
Continue by reducing the last index (j) by one until j
becomes 0.
Start j at n-1
chapter 1
ELEMENTARY SORTING ALGORITHM
54
chapter 1