Anda di halaman 1dari 24

Dr.

ISSAM ALHADID
Modification date: 22-2-2019
 • Algorithm Definitions
 • Algorithm Characteristics
 • Algorithm Components
 • Algorithms Design Methods
 • Algorithms Examples
 • Algorithm’s Efficiency
 A tool for solving a well-specified computational
problem

 An algorithm is said to be correct if, for every input


instance, it halts with the correct output
 Algorithms must be:
◦ Correct: For each input produce an appropriate
output
◦ Efficient: run as quickly as possible, and use as little
memory as possible – more about this later
 effective method (or procedure) : a procedure
that reduces the solution of some class of
problems to a series of rote steps which:
◦ always give some answer rather than ever give
no answer;
◦ always give the right answer and never give a
wrong answer;
◦ always be completed in a finite number of
steps, rather than in an infinite number;
◦ work for all instances of problems of the class.
 Algorithm : an effective method expressed as
a finite list of well-defined instructions for
calculating a function.
 An algorithm must possess the following
properties:
◦ finiteness: The algorithm must always terminate after a
finite number of steps.
◦ definiteness: Each step must be precisely defined; the
actions to be carried out must be rigorously and
unambiguously specified for each case.
◦ input: An algorithm has zero or more inputs, taken from a
specified set of objects.
◦ output: An algorithm has one or more outputs, which have
a specified relation to the inputs.
◦ effectiveness: All operations to be performed must be
sufficiently basic that they can be done exactly and in
finite length.
 For each problem or class of problems, there
may be many different algorithms.
 For each algorithm, there may be many
different implementations (programs).
 An algorithm may be expressed in a number of ways,
including:
 natural language: usually verbose (detailed) and
ambiguous
 flow charts: avoid most (if not all) issues of
ambiguity; difficult to modify (specialized tools;
largely standardized)
 pseudo-code: also avoids most issues of
ambiguity; unclear common elements of
programming languages; no particular agreement
on syntax
 programming language: tend to require expressing
low-level details that are not necessary for a high-
level understanding
 Variables and values: temporary and carry are both
temporary values used to store additional information the
algorithm needs to function correctly or effectively.
 Instructions: series of sub-algorithms, each performing a
smaller task.
 Procedures: An algorithm may not be able to do all the
work on its own. Usually, a large, complex algorithm can
be broken up into parts, each perfuming a specific task.
 Selections: Conditionals allow an algorithm to selectively
execute instructions based on certain conditions which
must be satisfied.
 Repetitions: the act of repeating a set of instructions while
(or until) a goal is reached or a condition is satisfied
 Performance. While initial algorithms may seem to
function, you may discover that they may not be the best
choice in the long term.
 In order to have a good performance of an
algorithm the following must be avoided if
possible in the design of an algorithm:
 􀂃 Nested loops.
 􀂃 Nested if statements.
 􀂃 Nested procedure calls.
 Additionally, the time complexity must be
less than n2, where n is the input size of an
algorithm.
the time complexity is the computational
complexity that describes the amount of time
it takes to run an algorithm.

Time complexity depends on :


- Computational complexity (CPU, Memory…)
- Algorithm Instructions

- Time complexity is denoted by T(n)


- What is the variable (n) ???
 algorithm efficiency A measure of the average
execution time necessary for an algorithm to
complete work on a set of data.
 algorithmic efficiency is a property of
an algorithm which relates to the number
of computational resources used by the
algorithm.
 Big O notation is used in Computer Science to
describe the performance or complexity of an
algorithm.
 Big O specifically describes the worst-case
scenario, and can be used to describe the
execution time required or the space used (e.g.
in memory or on disk) by an algorithm.
 The worst time complexity = O(n)
 big O notation is used to classify algorithms
according to how their running time or space
requirements grow as the input size (n) grows
 An algorithm is said to take linear time, or O(n)
time, if its time complexity is O(n). Informally,
this means that the running time increases at
most linearly with the size of the input.
 More precisely, this means that there is a
constant c such that the running time is at most
cn for every input of size n.
 For example, a procedure that adds up all
elements of a list requires time proportional to
the length of the list (n) , if the adding time is
constant (c), or, at least, bounded by a constant
(c).
 log2(n)…
 Recall…..
 If x = by then …. logb(x) = y
◦ X= 1000
◦ X = 103 …… log10(x) = 3
IF n = 8 , base = 2 find the result of log2(8)
 x = by then …. logb(x) = y
 log2(8) = y  8 = 2y  y = 3
 log2(8) = 3
Computer A
execute one billion
instructions per Sec.

Computer B
execute ten million
instructions per Sec.
 An algorithm is O(n3) requires 50 seconds to
solve a problem of size n=16. How long will
it take the algorithm to solve a problem of
size n=20 ?
 Soln.
 T(n) = C * n3 , where C is a constant …..(1)
 T(16) = C * 163 = 50 Seconds……………(2)
 If size = 20 ???
 T(20) = C * 203 = ??? Seconds ………..…(3)
 From eq.(2)
 T(16)/ 163 = C
 But T(16) = 50 …. Then
 50/163 = C …
 use the value of C in the eq.(3)
 Where C = 0.01220703125
 T(20) = 50/163 * 203 = 97.65625 Seconds
Time complexity depends on :
- Computational complexity (CPU, Memory…) ?!
- Algorithm Instructions

- What do you think ???

Anda mungkin juga menyukai