Fundamental of Algorithms
Fundamental of Algorithms
Fundamentals of Algorithms
Lecture #1
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
What is an algorithm?
problem
algorithm
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Algorithm
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Notion of algorithm and problem
problem
algorithm
algorithmic solution
(different from a conventional solution)
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Example of computational problem: sorting
Statement of problem:
• Input: A sequence of n numbers <a1, a2, …, an>
• Output: A reordering of the input sequence <a´1, a´2, …, a´n> so that a´i
≤ a´j whenever i < j
Algorithms:
• Selection sort
• Insertion sort
• Merge sort
• (many others)
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Selection Sort
Algorithm:
for i=1 to n
swap a[i] with smallest of a[i],…,a[n]
Sorting
Searching
Shortest paths in a graph
Minimum spanning tree
Primality testing
Traveling salesman problem
Knapsack problem
Chess
Towers of Hanoi
Program termination
Proving correctness
• Empirical analysis
Optimality
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Algorithm design strategies
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Analysis of Algorithms
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
What is an algorithm?
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Why study algorithms?
Theoretical importance
Practical importance
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Euclid’s Algorithm
As per Euclid's algorithm for the greatest common divisor, the GCD
of two positive integers (a, b) can be calculated as:
If a = 0, then GCD (a, b) = b as GCD (0, b) = b.
If b = 0, then GCD (a, b) = a as GCD (a, 0) = a.
If both a≠0 and b≠0, we write 'a' in quotient remainder form (a =
b×q + r) where q is the quotient and r is the remainder, and a>b.
Find the GCD (b, r) as GCD (b, r) = GCD (a, b)
We repeat this process until we get the remainder as 0.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
For a set of two positive integers (a, b) we use the below-given
steps to find the greatest common divisor:
Step 1: Write the divisors of positive integer "a".
Step 2: Write the divisors of positive integer "b".
Step 3: Enlist the common divisors of "a" and "b".
Step 4: Now find the divisor which is the highest of both "a"
and "b".
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Example: Find the greatest common divisor of 13 and 48.
Solution: We will use the below steps to determine the greatest
common divisor of (13, 48).
Divisors of 13 are 1, and 13.
Divisors of 48 are 1, 2, 3, 4, 6, 8, 12, 16, 24 and 48.
The common divisor of 13 and 48 is 1.
The greatest common divisor of 13 and 48 is 1.
Thus, GCD(13, 48) = 1.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
GCD using LCM Method
The steps to calculate the GCD of (a, b) using the LCM method
is:
Step 1: Find the product of a and b.
Step 2: Find the least common multiple (LCM) of a and b.
Step 3: Divide the values obtained in Step 1 and Step 2.
Step 4: The obtained value after division is the greatest
common divisor of (a, b).
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Example: Find the greatest common divisor of 15 and 70
using the LCM method.
Solution: The greatest common divisor of 15 and 70 can be
calculated as:
The product of 15 and 70 is given as, 15 × 70
The LCM of (15, 70) is 210.
GCD (15, 20) = (15 × 70)/ 210 = 5.
∴ The greatest common divisor of (15, 70) is 5.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Two descriptions of Euclid’s algorithm
while n ≠ 0 do
r ← m mod n
m← n
n←r
return m
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Other methods for computing gcd(m,n)
Middle-school procedure
Step 1 Find the prime factorization of m
Step 2 Find the prime factorization of n
Step 3 Find all the common prime factors
Step 4 Compute the product of all the common prime factors
and return it as gcd(m,n)
Is this an algorithm?
Example: 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Time complexity: O(n)
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Fundamentals of Algorithmic Problem Solving
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Two main issues related to algorithms
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Algorithm design techniques/strategies
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Implementation Method
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Exact or Approximate: Algorithms that are capable of finding
an optimal solution for any problem are known as the exact
algorithm. For all those problems, where it is not possible to
find the most optimized solution, an approximation algorithm
is used. Approximate algorithms are the type of algorithms that
find the result as an average outcome of sub outcomes to a
problem.
Example: For NP-Hard Problems, approximation algorithms
are used. Sorting algorithms are the exact algorithms.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Serial or Parallel or Distributed Algorithms: In serial
algorithms, one instruction is executed at a time while parallel
algorithms are those in which we divide the problem into
subproblems and execute them on different processors. If
parallel algorithms are distributed on different machines, then
they are known as distributed algorithms.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Design Method
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Dynamic Programming: The approach of Dynamic
programming is similar to divide and conquer. The difference is
that whenever we have recursive function calls with the same
result, instead of calling them again we try to store the result in
a data structure in the form of a table and retrieve the results
from the table. Thus, the overall time complexity is reduced.
“Dynamic” means we dynamically decide, whether to call a
function or retrieve values from the table.
Example: 0-1 Knapsack, subset-sum problem.
Linear Programming: In Linear Programming, there are
inequalities in terms of inputs and maximizing or minimizing
some linear functions of inputs.
Example: Maximum flow of Directed Graph
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Reduction(Transform and Conquer): In this method, we
solve a difficult problem by transforming it into a known
problem for which we have an optimal solution. Basically, the
goal is to find a reducing algorithm whose complexity is not
dominated by the resulting reduced algorithms.
Example: Selection algorithm for finding the median in a list
involves first sorting the list and then finding out the middle
element in the sorted list. These techniques are also
called transform and conquer.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Backtracking: This technique is very useful in solving
combinatorial problems that have a single unique solution.
Where we have to find the correct combination of steps that
lead to fulfillment of the task. Such problems have multiple
stages and there are multiple options at each stage. This
approach is based on exploring each available option at every
stage one-by-one. While exploring an option if a point is
reached that doesn’t seem to lead to the solution, the program
control backtracks one step, and starts exploring the next
option. In this way, the program explores all possible course of
actions and finds the route that leads to the solution.
Example: N-queen problem, maize problem.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Branch and Bound: This technique is very useful in solving
combinatorial optimization problem that have multiple
solutions and we are interested in find the most optimum
solution. In this approach, the entire solution space is
represented in the form of a state space tree. As the program
progresses each state combination is explored, and the previous
solution is replaced by new one if it is not the optimal than the
current solution.
Example: Job sequencing, Travelling salesman problem.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Design Approaches
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Other Classifications
Randomized Algorithms: Algorithms that make random choices for
faster solutions are known as randomized algorithms.
Example: Randomized Quicksort Algorithm.
Classification by complexity: Algorithms that are classified on the
basis of time taken to get a solution to any problem for input size. This
analysis is known as time complexity analysis.
Example: Some algorithms take O(n), while some take exponential
time.
Classification by Research Area: In CS each field has its own
problems and needs efficient algorithms.
Example: Sorting Algorithm, Searching Algorithm, Machine
Learning etc.
Branch and Bound Enumeration and Backtracking: These are
mostly used in Artificial Intelligence.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Algorithm design techniques/strategies
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Analysis of algorithms
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Important problem types
sorting
searching
string processing
graph problems
combinatorial problems
geometric problems
numerical problems
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Sorting (I)
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Sorting (II)
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Selection Sort
Algorithm SelectionSort(A[0..n-1])
//The algorithm sorts a given array by selection sort
//Input: An array A[0..n-1] of orderable elements
//Output: Array A[0..n-1] sorted in ascending order
for i 0 to n – 2 do
min i
for j i + 1 to n – 1 do
if A[j] < A[min]
min j
swap A[i] and A[min]
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Searching
Find a given value, called a search key, in a given set.
Examples of searching algorithms
• Sequential search
• Binary search …
Input: sorted array a_i < … < a_j and key x;
m (i+j)/2;
while i < j and x != a_m do
if x < a_m then j m-1
else i m+1;
if x = a_m then output a_m;
Time: O(log n)
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
String Processing
Examples:
(i) searching for a word or phrase on WWW or in a
Word document
(ii) searching for a short read in the reference genomic
sequence
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1