Professional Documents
Culture Documents
LEC1 01 خوارزميات أ زاهر مترجمة للطبع
LEC1 01 خوارزميات أ زاهر مترجمة للطبع
AKRAM_LOQMAN_773133872 1
AKRAM_LOQMAN_773133872 2
Course Objectives
This course introduces students to the analysis
and design of computer algorithms. Upon
completion of this course, students will be able
to do the following:
Analyze the asymptotic performance of
algorithms.
Demonstrate a familiarity with major
algorithms and data structures.
Apply important algorithmic design
paradigms and methods of analysis.
Synthesize efficient algorithms in common
engineering design situations. 3
AKRAM_LOQMAN_773133872
4
AKRAM_LOQMAN_773133872
Course Objectives
In this course we will cover the following
topics:
Understand foundations of algorithms
&& design and analysis various variants
algorithms
Accuracy
Efficiency
Comparing efficiencies
AKRAM_LOQMAN_773133872 5
AKRAM_LOQMAN_773133872 6
Algorithms
The word algorithm comes from the name of a
Persian mathematician Abu Ja far Mohammed
ibn-i Musa al Khowarizmi.
AKRAM_LOQMAN_773133872 8
What is a program?
A program is the expression of an
algorithm in a programming language
AKRAM_LOQMAN_773133872 9
AKRAM_LOQMAN_773133872 10
Problem-Solving (Science and Engineering)
Analysis
How does it work?
Breaking a system down to known components
How the components relate to each other
Breaking a process down to known functions
Synthesis
Building tools and toys!
What components are needed
How the components should be put together
Composing functions to form a process
AKRAM_LOQMAN_773133872 11
12
AKRAM_LOQMAN_773133872
Problem Solving Using Computers
Problem:
Strategy:
Algorithm:
Input:
Output:
Step:
Analysis:
Correctness:
Time & Space:
Optimality:
Implementation:
Verification: AKRAM_LOQMAN_773133872 13
AKRAM_LOQMAN_773133872 14
Why need algorithm analysis ?
Writing a working program is not good
enough
16
AKRAM_LOQMAN_773133872
Analyzing Algorithms and Problems
We analyze algorithms with the intention
of improving them, if possible, and for
choosing among several available for a
problem.
Correctness
Amount of work done, and space used
Optimality, Simplicity
AKRAM_LOQMAN_773133872 17
AKRAM_LOQMAN_773133872 18
Correctness can be proved!
An algorithm consists of sequences of
steps (operations, instructions,
statements) for transforming inputs
(preconditions) to outputs (postconditions)
Proving
if the preconditions are satisfied,
then the postconditions will be true,
when the algorithm terminates.
AKRAM_LOQMAN_773133872 19
AKRAM_LOQMAN_773133872 20
Amount of work done
We want a measure of work that tells us
something about the efficiency of the
method used by the algorithm
AKRAM_LOQMAN_773133872 22
Amount of work done
Counting passes through loops
Basic Operation
Identify a particular operation
fundamental to the problem
the total number of operations
performed is roughly proportional to
the number of basic operations
Identifying the properties of the inputs
that affect the behavior of the algorithm
AKRAM_LOQMAN_773133872 23
AKRAM_LOQMAN_773133872 24
Space Usage
If memory cells used by the
algorithms depends on the particular
input,
AKRAM_LOQMAN_773133872 26
Optimality the best possible
Each problem has inherent complexity
There is some minimum amount of work
required to solve it.
To analyze the complexity of a problem,
we choose a class of algorithms, based on
which
prove theorems that establish a lower
bound on the number of operations
needed to solve the problem.
Lower bound (for the worst case)
AKRAM_LOQMAN_773133872 27
AKRAM_LOQMAN_773133872 28
Simplicity
AKRAM_LOQMAN_773133872 29
AKRAM_LOQMAN_773133872 30
Designing Algorithms
Problem solving using Computer
AKRAM_LOQMAN_773133872 31
AKRAM_LOQMAN_773133872 32
The problem of sorting
Input: sequence a1, a2, , an of numbers.
Example:
Input: 8 2 4 9 3 6
Output: 2 3 4 6 8 9
AKRAM_LOQMAN_773133872 33
AKRAM_LOQMAN_773133872
34
Analysis of algorithms
The theoretical study of computer-program performance
and resource usage.
AKRAM_LOQMAN_773133872
36
Analysis of algorithms
Why study algorithms and performance?
AKRAM_LOQMAN_773133872 38
Running Time
The running time depends on the input: an
already sorted sequence is easier to sort.
40
AKRAM_LOQMAN_773133872
Kinds of analyses
Worst-case: (usually)
T(n) = maximum time of algorithm on any input of
size n.
Average-case: (sometimes)
T(n) = expected time of algorithm over all inputs of
size n.
Need assumption of statistical distribution of inputs.
Best-case:
Cheat with a slow algorithm that works fast on
some input. AKRAM_LOQMAN_773133872 41
Tn
n
Tn
n
AKRAM_LOQMAN_773133872 42
Machine-Independent time
The RAM Model
Machine independent algorithm design depends
on a hypothetical computer called Random Acces
Machine (RAM).
Assumptions:
Each simple operation such as +, -, if ...etc takes
exactly one time step.
Loops and subroutines are not considered
simple operations.
Each memory acces takes exactly one time step.
AKRAM_LOQMAN_773133872 43
RAM
if
44
AKRAM_LOQMAN_773133872
Machine-independent time
What is sort s worst-case time?
BIG IDEA:
Ignore machine-dependent constants.
Look at growth of T (n) as n
Asymptotic Analysis 45
AKRAM_LOQMAN_773133872
n Tn
46
AKRAM_LOQMAN_773133872
Asymptotic Analysis
Engineering:
Example:
AKRAM_LOQMAN_773133872 47
AKRAM_LOQMAN_773133872 48
Asymptotic performance
When n gets large enough, a 2) algorithm
always beats a 3) algorithm.
T(n)
AKRAM_LOQMAN_773133872 n n0 49
n2) n
n3)
T(n)
AKRAM_LOQMAN_773133872 n n0 50