Week 5

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 37

DESIGN AND ANALYSIS OF

ALGORITHMS
Course Code : AR212
Credit Hours :3
Prerequisite : AR211
Instructor Information

Name :Dr. Aryaf Abdallah Aladwan


Office No. --
Tel (Ext) None
E-mail aryaf_aladwan@bau.edu.jo
Office Hours Sun, Tue, Thu 10:00-11:00, 15:00-16:00
Class Times Building Day Start Time End Time Room No.
AI Sun -Tue, :0014 :0015 AI LAB
Thu
Salt Technical Sun -Tue, 12:00 :0013 8
College Thu
This course is to design efficient computer algorithms, prove their
correctness, and analyze their running times. it includes mathematical
analysis of algorithms (summations and recurrences), advanced data
structures (balanced search trees), algorithm design techniques (divide-
and-conquer, dynamic programming, and greedy algorithms), graph
algorithms (breadth-first and depth-first search, minimum spanning
trees, shortest paths).
Course Title: DESIGN AND ANALYSIS OF ALGORITHMS
Credit Hour(3)

[Pre-req. AR211
Textbook: Introduction to Algorithms, Thomas Cormen, Charls Leiserson & Ronald Rivest. 3rd Edition,
MIT Press, 2009.

Image of the textbook Cover


COURSE OBJECTIVES

The main learning objectives of the course are to:


1. Learn about the use of several design techniques and the importance of studying the complexity of
a particular algorithm
2. Computational complexity analysis of algorithms and worst-case algorithm runtime analysis using
asymptotic analysis and mathematical analysis of algorithms (summations and recursion)
3. Writing and analyzing recursive relationships for recursive algorithms
4. Describe the divide and conquer approach as a principle for algorithm design
5. Learn about searching and sorting algorithms
6. Ability to design, analyze and validate graph algorithms and shortest path algorithms
7. Ability to design and analyze algorithms that follow the approach of greedy algorithms
8. Understand dynamic programming algorithms and optimization algorithms and know when the
algorithm design situation calls for it.
9. Learn about advanced data structures
COURSE SYLLABUS

Week Course Topic


Week 1 Introduction to Algorithms, The Efficiency of Algorithms, Attributes of Algorithms. A Choice of
Algorithms. Classifications of algorithms: by implementation, by design, by field of study and by
complexity.
Week 2 Mathematical Background (summations and recurrences)
Week 3 Measuring Efficiency, Measuring the asymptotic growth of functions. The basic techniques for
manipulating bounded summations, searching techniques (linear search, binary search).
Week 4 Techniques for problem-solving: Divide and Conquer, the General Method, Recurrence Equations,
Solving Recurrence Equations (Master and iteration Methods).
Week 5 and 6 Sorting techniques based on Divide and Conquer: Merge Sort.
Quick Sort, Strassen's Matrix Multiplications.
Week 7 Other sorting techniques: Insertion sort, Selection Sort, Heap Sort
Week 8 Midterm Exam
Week 9 Graph Algorithms, Formal Definition of Graphs. Graphs Types.
Graph Terminologies, Strongly Connected Graph, Representations of Graphs.
Week 10 Graph Traversal, Breadth-First Search Algorithm, Depth-First Search Algorithm. Topological Sort
Algorithm.
Week 11 Greedy Algorithms, Minimum spanning trees
Week 12 Shortest paths algorithms
Week 13 Dynamic Programming
Week 14 Optimization Algorithms
Week 15 Advanced data structures (balanced search trees)
Week 16 Final Exam
Week 5
Chapter 5
Divide and Conquer

By:
Dr. Aryaf A. Al-adwan
Autonomous Systems Dept.
Faculty of Artificial Intelligence
Design and Analysis of Algorithms Course
Divide and ConquerStrategy
• Divide-and-conquer, breaks a problem into sub-problems that are
similar to the original problem, recursively solves the sub problems,
and finally combines the solutions to the sub problems to solve the
original problem.
• You should think of a divide-and-conquer algorithm as having three
parts:
1)Divide the problem into a number of subproblems that are smaller
instances of the same problem.
2)Conquer the subproblems by solving them recursively. If they are
small enough, solve the subproblems as base cases.
3) Combine the solutions to the subproblems into the solution for the
original problem.
Divide and ConquerGeneral Method
Divide and ConquerTime

• The time for such an algorithm can be expressed by


adding the work that they perform at the top level of
their recursion (to divide the problems into
subproblems and then combine the subproblem
solutions) together with the time made in the
recursive calls of the algorithm.

6/28
Common Divide and ConquerAlgorithms
• Binary Search
• Tower of Hanoi
• Merge Sort
• Quick Sort
• Strassen's Matrix Multiplication Algorithm
• Finding maximum and minimum
• Multiplication of Large Integers
Binary SearchAlgorithm
• Given a sorted array arr[] of n elements, write a
function to search a given element x in arr[].
• A simple approach is to do linear search. The time
complexity of the linear search algorithm is O(n).
Another approach to perform the same task is using
Binary Search.
BinarySearch
• It is a search algorithm that finds the position of a key within a sorted
array.
• Binary search compares the key to the middle element of the array. If
they are not equal, the half in which the key cannot lie is eliminated
and the search continues on the remaining half.
• Again taking the middle element to compare to the key and
repeating this until the key is found.
• If the search ends with the remaining half being empty, then the key
is not in the array.
Binary Search AlgorithmImplementation
1) Iterative Binary Search
2) Recursive Binary Search

Note that the list must be sorted in both cases


Iterative Binary SearchAlgorithm

int binarySearch(int[] A, int x)


{
int low = 0, high = A.length - 1;
while (low <= high)
{
int mid = (low + high) / 2;
if (x == A[mid])
return mid;
else if (x < A[mid])
high = mid - 1;
else
low = mid + 1;
}
return -1;
}
Recursive Binary SearchAlgorithm

int binarySearch(int[] A, int low, int high, int x)


{
if (low > high) {
return -1;
}
int mid = (low + high) / 2;
if (x == A[mid]) {
return mid;
}
else if (x < A[mid]) {
return binarySearch(A, low, mid - 1, x);
}
else {
return binarySearch(A, mid + 1, high, x);
}
Recursive Binary Search Algorithm

0 1 2 3 4 5 6 7 8
2 6 13 21 36 47 63 81 97

• int binarySearch(int[] A, int low, int high, int


• Binarysearch(A,0,8,21) ➔ mid=4 x)
• {
• Binarysearch(A,0,3,21) ➔ mid=1 • if (low > high) {
• Binarysearch(A,2,3,21) ➔ mid=2 • return -1;

• Binarysearch(A,3,3,21) ➔ mid=3 • }
• int mid = (low + high) / 2;
• Return A[mid] = 21 ➔ found if (x == A[mid]) {
• return mid;
• }
• If not found, then low > high • else if (x < A[mid]) {

• The function will return -1 • return binarySearch(A, low, mid - 1, x);


Binary Search and Divide &Conquer
Iterative Binary SearchAnalysis

20

21

22

23

2 k=n
Log 2 k = log n ➔ K = log n
Recursive Binary SearchAnalysis
int binarySearch(int[] A, int low, int high, int x) ➔ T(n)
{
if (low > high) { ➔ 1 ➔ since it will be executed one time when notfound
return -1;
}
int mid = (low + high) / 2; ➔ 1 ➔ since it will be executed one time in every recursivecall
if (x == A[mid]) { ➔ 1 ➔ since it will be executed one time in every recursivecall
return mid;
}
else if (x < A[mid]) { ➔ 1 ➔ since it will be executed one time in every recursivecall
return binarySearch(A, low, mid - 1, x);
}
else { ➔ T(n/2) ➔ At each recursive call we execute the binary search on n/2
return binarySearch(A, mid + 1, high, x);
}
}

T(n) = T(n/2) + 3 ➔ T(n) = T(n/2) + 3


Solving Binary SearchRecurrence
1 𝑛= 1
• 𝑇𝑛 = 𝑛
+1 𝑛> 1
൝𝑇 2

• Solution Using Master Theorem


• a=1,b=2
• f(n) = 1 ➔ k=0 , p=0
• Log b a = 0 ➔ Log b a = k ➔ case 2 satisfied
• p = 0 ➔ p>-1
• T(n) = n k log p+1 n
• T(n) = n 0 log 1 n
• T(n) = O( log n )
➔ This is the only algorithm in computer science that has log n time complexity
Linear search vs. Binarysearch
Tower ofHanoi
• Tower of Hanoi is a mathematical puzzle where we have three
rods and n disks.
• The objective of the puzzle is to move all the disks to another
rod, based on the following simple rules:
1) Only one disk can be moved at a time.
2)Each move consists of taking the upper disk from one of the
stacks and placing it on top of another stack i.e. a disk can only
be moved if it is the uppermost disk on a stack.
3) No disk may be placed on top of a smaller disk.
Number of Moves = 7
Number of Disks Source Auxiliary Destination

void TOH(int n, int A, int B, int C)


RecursiveSolution
{
if (n == 0)
return ;

TOH(n - 1, A, C, B);
cout << "Move disk from “
<< A<< " to”<< C<< endl;
TOH(n - 1, B, A, C);
}

void main()
{
int n = 3; // Number of disks
TOH(n, 1,2,3); // 1, 2 and 3 are names of rods
}
RecursionTree A=1
B =2
C= 3

TOH(n - 1, A, C, B); TOH(n - 1, B, A, C);

TOH(n - 1, A, C, B); TOH(n - 1, B, A, C);

Moves ➔
Output

Move Disk from 1 to 3


Move Disk from 1 to 2
Move Disk from 3 to 2
Move Disk from 1 to 3
Move Disk from 2 to 1
Move Disk from 2 to 3
Move Disk from 1 to 3
TimeAnalysis
void TOH(int n, int A, int B, int C) ➔ T(n)
{
if (n == 0) ➔1
return ; // base case

TOH(n - 1, A, C, B); ➔T(n-1)


cout << "Move disk from " << A<< " to”<< C<< endl; ➔1
TOH(n - 1, B, A, C); ➔ T(n-1)
}

T(n)=2T(n-1)+1
Solving the recurrence usingIteration
• T(n)=2T(n-1)+1 ……………… Eq 1
• Solution
• T(n-1) = 2T(n-2)+1 …………………Eq 2
• Substitute Eq2 in Eq1
• T(n)= 2[2[T(n-2)+1]+1 ➔ T(n) = 2 2 T(n-2)+2+1 ……………… Eq3
• T(n-2)= 2T(n-3)+1 ………………..Eq4
• Substitute Eq4 in Eq3
• T(n)= 2 2 [2T(n-3)+1]+2+1 ➔ T(n)=2 3 T(n-3) +4+2+1
• T(n) = 2k T(n-k)+2 (k-1) + 2 (k-2) + ……………+ 2 2 + 2 1 +1
Cont.
• Base condition T(0) == 1
• n–k=0
• n = k;
• put, k = n
• T(n) = 2n T(0) + 2(n-1) + 2(n-2) + ............ +22 +21 +1
• T(n) = 2n + 2(n-1) + 2(n-2) + ............ +22 +21 + 1 ➔
• It is Geometric Progression (GP) series, and sum is 2(n+1) - 1
• T(n) = O(2n ) ➔ Bad
Number ofmovesinrecursiveTowerofHanoi
MergeSort
• The problem here is to sort a list of n elements.
• Merge Sort is a Divide and Conquer algorithm.
• Merge sort works as follows:
• Divide the unsorted list into n sublists, each containing one
element (a list of one element is considered sorted).
• Repeatedly merge sublists to produce new sorted sublists
until there is only one sublist remaining. This will be the
sorted list.
Merge SortandDivide &Conquer
• To sort an array A with n elements using the merge sort :
1) Divide:
- Divide the array A with n elements into 2 subarrays with n/2 elements.
2) Conquer:
- Sort the subarrays recursively using merge sort.
- when the size of the subarray is 1, then stop.
3) Combine:
- Merge the 2 sorted subarrays.

You might also like