Professional Documents
Culture Documents
CHAPTER ONE
Introduction to Theory of Algorithm
Lecturer Name:
Adem M
3/29/202 1
3
Outline
In this chapter:
• Definition of concepts of algorithms
• System component model
• Application of Algorithm
• Why we need algorithm
• Classification of Algorithm
• By implementation
• By design paradigm
• Elementary Sorting Algorithm
• Insertion sort
• Selection Sort
• Any well-defined computational procedure that takes some value (or set of
values) as an input and produces some value (or set of values) as an output
• A sequence of computational steps that transforms the input into the output
3/29/202 3
3
Cont’d
• An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for
obtaining a required output for any legitimate input in a finite amount of time
• From the above definition, algorithm has the following five properties: Sequence,
Unambiguous, Input, Output, Finite
3/29/202 4
3
System component model
• To apply algorithm first of all there should be aproblem
that needs solution
–Is there any problem for which we can’t design algorithm?
3/29/202 7
3
Properties of an algorithm:Sequence
▪ 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
▪ Between the two every step should have preceding and succeeding steps
▪ That is, each step must have a uniquely defined preceding and succeedingstep
3/29/202 8
3
Properties of an algorithm:Unambiguous
▪ Define rigorously the sequence of operations performed for transforming the inputs into
theoutputs
▪ 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 totell exactly what will happen next
▪ Every detail of each step must be spelled out,including how to handle errors
3/29/202 9
3
Properties of an algorithm: Input specified
• The inputs are the data that will be transformed during the computation to produce the output
• Every algorithm should have a specified number (zero or more) input values (or quantities)
which are externally supplied
• Note that, correct algorithm is not one that works most of the time but one that works
correctly for all legitimate inputs
3/29/202 10
3
Properties of an algorithm: Outputspecified
• The output is the data resulting from the computation
– It is the intended result
• 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 validinput.
3/29/202 11
3
Properties of an algorithm:Finiteness
▪ Every valid algorithm must complete or terminate after a finite number ofsteps.
▪ 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
3/29/202 12
3
Whyneed algorithm analysis ?
▪ If the program is run on a large data set, then the running time becomes anissue
3/29/202 13
3
Algorithm vs.Programs
▪ The design phase is typically much longer than the programming phase
3/29/202 14
3
Cont.…
A computer program is an instance, or concrete representation,
for an algorithm in some programming language
High Level
Language 15
Program
3/29/202 15
3
Solving Problems (1)
3. Select the one that seems the best under the prevailing
circumstances
16
3/29/202 16
3
Solving Problems (2)
17
3/29/202 17
3
One Problem, Many Algorithms
Problem
The statement of the problem specifies, in general terms, the
desired input/output relationship.
Algorithm
The algorithm describes a specific computational procedure for
achieving input/output relationship.
Example
Sorting a sequence of numbers into non-decreasing order.
Algorithms
Various algorithms e.g. merge sort, quick sort, heap sorts etc. 18
3/29/20
23
One Problem, Many Algorithms
Problem
The statement of the problem specifies, in general terms, the
desired input/output relationship.
Algorithm
The algorithm describes a specific computational procedure for
achieving input/output relationship.
Example
Sorting a sequence of numbers into non-decreasing order.
Algorithms
Various algorithms e.g. merge sort, quick sort, heap sorts etc. 19
3/29/20
23
Classification of Algorithm
❖ There are many ways of classifying algorithms and a few of them are shown below:
o Implementation Method
✓ Recursion or Iteration
✓ Procedural or Declarative (non-Procedural)
✓ Serial or Parallel or Distributed
✓ Deterministic or Non-Deterministic
o Design Method
✓ Greedy Method
✓ Divide and Conquer
✓ Dynamic Programming
✓ Linear Programming
3/29/20 20
23
By implementation Method
❖ Recursion or Iteration
✓ A recursive algorithm is one that calls itself repeatedly until a base condition is satisfied. It is a common
method used in functional programming languages like C, C++, etc.
✓ Iterative algorithms use constructs like loops and sometimes other data structures like stacks and queues
to solve the problems.
3/29/20 21
23
Cont.…
❖ Deterministic or Non-Deterministic
✓ Deterministic algorithms solve the problem with a predefined process,
✓ whereas non-deterministic algorithms guess the best solution at each step through the use of heuristics.
3/29/20 22
23
Design Method
❖ Greedy Method
o Greedy algorithms Work in stages.
o In each stage, a decision is made that is good at that point, without bothering about the future
consequences.
o Generally, this means that some local best is chosen.
o It assumes that the local best selection also makes for the global optimal solution.
o Many problems (example: maximum flow for directed graphs) can be discussed using linear
programming.
3/29/202 24
3
Elementary sorting algorithms
❖ Selection Sort
❖ Bubble Sort
3/29/202 25
3 ❖ Insertion Sort
How does Selection Sort work ?
❖Idea:
o Find the second smallest element and exchange it with the element in the
second position
3/29/202 26
3
Cont.…
3/29/202 27
3
How does Bubble Sort work ?
3/29/202 28
3
Cont.…
3/29/202 29
3
Insertion Sort work
3/29/202 30
3
How does Insertion Sort work ?
3/29/202 31
3
Thank You !!!
3/29/202 32
3