Design Analysis and Algorithm - Chapter01 Duc Anany V. Levitin 3e
Design Analysis and Algorithm - Chapter01 Duc Anany V. Levitin 3e
Introduction
1
Lecture Contents
1. What is an algorithm?
2. Fundamentals of Algorithmic Problem Solving
3. Important Problem Types
4. Fundamental Data Structures
2
1. What is an Algorithm?
3
1. What is an Algorithm?
problem
algorithm
4
Why Do We Study Algorithms?
• Theoretical importance
- the core of computer science
• Practical importance
- A practitioner’s toolkit of known algorithms
- Framework for designing and analyzing
algorithms for new problems
5
Requirements of an Algorithm
1. Finiteness
terminates after finite number of steps
2. Definiteness
rigorously and unambiguously specified
3. Input
valid inputs are clearly specified
6
Requirements of an Algorithm
4. Output
can be proved to produce the correct output
given a valid input
5. Effectiveness
steps are sufficiently simple and basic
7
Finding the Greatest Common Divisor
• Euclid’s algorithm
• Consecutive integer checking algorithm
• Middle-school procedure
8
Euclid’s Algorithm
9
Euclid’s Algorithm
10
Two Expressions of Euclid’s Algorithm
Description
Step 1 If n = 0, return m and stop;
otherwise go to Step 2
Step 2 Divide m by n and assign value of
remainder to r
Step 3 Assign value of n to m and value of r to n.
Go to Step 1.
11
Two Expressions of Euclid’s Algorithm
Pseudocode
while n do
mod n
return m
12
Other Methods for Computing gcd(m, n)
Middle-school procedure
Step 1 Find prime factorization of m
Step 2 Find prime factorization of n
Step 3 Find all common prime factors
Step 4 Compute product of all common prime
factors and return it as gcd(m, n)
Q: Is this an algorithm?
14
Sieve of Eratosthenes
Input: Integer n 2
Output: List of primes less than or equal to n
for p to n do A[p p
for p to n do
if A[p] 0
//p hasn’t been previously eliminated from the list
j p*p
while j n do
A[j]
j j+p
15
Sieve of Eratosthenes
16
Lecture Contents
1. What is an algorithm?
2. Fundamentals of Algorithmic Problem
Solving
3. Important Problem Types
4. Fundamental Data Structures
17
18
Basic Issues Related to Algorithms
1. What is an algorithm?
2. Fundamentals of Algorithmic Problem Solving
3. Important Problem Types
4. Fundamental Data Structures
22
3. Important Problem Types
• Searching
• Sorting
• Geometric problems (Computational geometry)
• Combinatorial problems (Combinatorics)
• Graph problems (Graph theory)
• String processing
• Numerical problems
23
3. Important Problem Types
• Searching
- Linear (or sequential) search
- Binary search
- Interpolation search
24
3. Important Problem Types
• Sorting
- Selection sort, bubble sort, insertion sort
- Merge sort, quick sort, heap sort
25
3. Important Problem Types
26
3. Important Problem Types
27
3. Important Problem Types
29
3. Important Problem Types
• String processing
- String Matching Problem
• Numerical problems
- Solving equation f(x) = 0
- Solving system of equations
- Evaluating polynomial p(x) at x = x0
30
Lecture Contents
1. What is an algorithm?
2. Fundamentals of Algorithmic Problem Solving
3. Important Problem Types
4. Fundamental Data Structures
31
4. Fundamental Data Structures
• stack (LIFO)
• queue (FIFO)
• priority queue
• graph
• tree
33
Exercises
34
Exercises
35
References
36
References
37