0% found this document useful (0 votes)
21 views

Chapter 1

Uploaded by

abrhamgetachew99
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Chapter 1

Uploaded by

abrhamgetachew99
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Chapter one

Introduction and Elementary Data Structures

1
outlines
 Introduction to Algorithm analysis
 Asymptotic Notations
 Analysis of Algorithm
 Review of elementary Data Structures
 Heaps
 Hashing
 Set Representation
 UNION, FIND Operation

2
Algorithm
• An algorithm is a set of steps of operations to solve a
problem performing calculation, data processing, and
automated reasoning tasks.
• An algorithm is an efficient method that can be
expressed within finite amount of time and space.
• An algorithm is the best way to represent the solution of
a particular problem in a very simple and efficient way.
• If we have an algorithm for a specific problem, then we
can implement it in any programming language, meaning
that the algorithm is independent from any
programming languages.

3
Algorithm Design
• The important aspects of algorithm design include creating an
efficient algorithm to solve a problem in an efficient way using
minimum time and space.
• To solve a problem, different approaches can be followed. Some of
them can be efficient with respect to time consumption, whereas
other approaches may be memory efficient.
• However, one has to keep in mind that both time consumption and
memory usage cannot be optimized simultaneously.
• If we require an algorithm to run in lesser time, we have to invest
in more memory and if we require an algorithm to run with lesser
memory, we need to have more time.

4
cont..

5
Problem Development Steps
 The following steps are involved in solving computational
problems.
 Problem definition
 Development of a model
 Specification of an Algorithm
 Designing an Algorithm
 Checking the correctness of an Algorithm
 Analysis of an Algorithm
 Implementation of an Algorithm
 Program testing
 Documentation
6
How to Write an Algorithm?
• There are no well-defined standards for writing algorithms.
Rather, it is problem and resource dependent. Algorithms are
never written to support a particular programming code.
• As we know that all programming languages share basic code
constructs like loops (do, for, while), flow-control (if-else), etc.
These common constructs can be used to write an algorithm.
• We write algorithms in a step-by-step manner, but it is not always
the case. Algorithm writing is a process and is executed after the
problem domain is well-defined. That is, we should know the
problem domain, for which we are designing a solution.

7
Cont..

• Algorithms tell the programmers how to code the program.


Algorithm for adding two numbers
Step 1 − START ADD
Step 2 − get values of a & b
Step 3 − c ← a + b
Step 4 − display c
Step 5 − STOP
• It makes it easy for the analyst to analyze the algorithm ignoring
all unwanted definitions.
• ones can observe what operations are being used and how the
process is flowing.

8
Characteristics of Algorithms
 Not all procedures can be called an algorithm. An algorithm should have the
following characteristics −
• Unambiguous − Algorithm should be clear and unambiguous. Each of its
steps (or phases), and their inputs/outputs should be clear and must lead to
only one meaning.
• Input − An algorithm should have 0 or more well-defined inputs.
• Output − An algorithm should have 1 or more well-defined outputs, and
should match the desired output.
• Finiteness − Algorithms must terminate after a finite number of steps.
• Feasibility − Should be feasible with the available resources.
• Independent − An algorithm should have step-by-step directions, which
should be independent of any programming code.

9
Algorithm analysis
Why we should analyze algorithms?
 Predict the resources that the algorithm requires
 Computational time (CPU consumption)
 Memory space (RAM consumption)

 Communication bandwidth consumption

 The running time of an algorithm is:


 The total number of primitive operations executed
(machine independent steps)
 Also known as algorithm complexity

10
Time Complexity
Worst-case
An upper bound on the running time for any input
of given size
Average-case
 Assume all inputs of a given size are equally likely
 Best-case
 The lower bound on the running time

11
Time Complexity – Example

Sequential search in a list of size n


 Worst-case:
n comparisons
Best-case:
1 comparison
Average-case:
n/2 comparisons

12
Asymptotic notations
Algorithm complexity is rough estimation of the
number of steps performed by given
computation depending on the size of the input
data
 Measured through asymptotic notation
 O(g) where g is a function of the input data size

 Examples:
 Linear complexity O(n) – all elements are processed once (or
constant number of times)
 Quadratic complexity O( n2) – each of the
elementsis processed n times

13
O-notation
Asymptotic upper bound

14
f(n)=O(g(n)) iff there exist a positive constant c and
non-negative integer n0 such that
f(n)  cg(n) for all
nn0.
 g(n) is said to be an upper bound of f(n).

15
Example

The running time is O(n2) means there is a


function f(n) that is O(n2) such that for any
value of n, no matter what particular input
of size n is chosen, the running time of that
input is bounded from above by the value
f(n).
3 * n2 + n/2 + 12 ∈ O(n2)

16
Ω notation
Asymptotic lower bound

17
Example

When we say that the running time (no modifier) of


an algorithm is Ω (g(n)).
 we mean that no matter what particular input of
size n is chosen for each value of n, the running
time on that input is at least a constant times g(n),
for sufficiently large n.
n3 + 20n ∈ Ω(n2 )

18
Θ notation

g(n) is an asymptotically tight bound of f(n)

19
Example

20
part II cont..

21
Q &A

22

You might also like