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

Week 6

This document provides information about the Design and Analysis of Algorithms course. It includes: - The course code, credit hours, prerequisites, instructor details, class times and location. - A brief description of the course content which includes designing efficient algorithms, analyzing running times using asymptotic analysis and mathematical techniques, and learning about common algorithm design paradigms. - The course objectives which are to learn algorithm design techniques, complexity analysis, and analyze specific algorithms like searching, sorting, graphs, greedy algorithms and dynamic programming. - An outline of the course syllabus covering these topics over 16 weeks. - Examples of algorithms discussed in more detail, including Merge Sort, Quicksort, and the partition process

Uploaded by

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

Week 6

This document provides information about the Design and Analysis of Algorithms course. It includes: - The course code, credit hours, prerequisites, instructor details, class times and location. - A brief description of the course content which includes designing efficient algorithms, analyzing running times using asymptotic analysis and mathematical techniques, and learning about common algorithm design paradigms. - The course objectives which are to learn algorithm design techniques, complexity analysis, and analyze specific algorithms like searching, sorting, graphs, greedy algorithms and dynamic programming. - An outline of the course syllabus covering these topics over 16 weeks. - Examples of algorithms discussed in more detail, including Merge Sort, Quicksort, and the partition process

Uploaded by

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

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 [email protected]
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 6
Merge SortAlgorithm
Algorithm MergeSort( low , high )
{
if ( low < high )
{
mid = ( low + high ) / 2;
MergeSort( low , mid );
MergeSort( mid+1 , high );
Merge (low, mid, high);
}
}
Merge
Algorithm Merge(A,B,m,n)
Algorithm
A B C {
2 5 2
i=1; j=1; k=1;
i j k while(i<=m && j<=n)
 8 9  5
{
15 12 8 if (A[i] < B[j])
18 17 9 C[k++]=A[i++];
else
12
C[k++]=B[j++];
15 }
m=
4 17 for(; i<=m;i++) // for the remaining elements in
A
n=4 18 C[k++]=A[i];
for(; j<=n;j++) // for the remaining elements in
C[k++]=B[j]; B
}
Another Example OfMerging
Merge SortAnalysis
Algorithm MergeSort( low , high )  T(n)
{
if ( low < high ) T (1)
{
mid = ( low + high ) / 2;  T(1)
MergeSort( low , mid ); 
MergeSort( mid+1 , high ); T(n/2)
Merge (low, mid, high); 
} T(n/2)
} Total:  T(n)
T(n) = O(1) n=1
T(n) = 2T(n/2) + n + 1 n>1
Merge Sort as arecurrence
• Merge sorting a list involves splitting the list in two
and recursively merge sorting each half of the list.
• a = 2 since we call merge sort twice at every recursion (once on
each half).
• b = 2 since each of the new subproblem has half
the elements in the original list.
• f(n) = n since combining the results of the subproblems, the
merge operation, is O(n).
Solving Merge SortRecurrence
1 𝑛= 1
• 𝑇𝑛 𝑛
2
+ 𝑛 𝑛>
= 1
• ൝Solution
2 Using Master Theorem
• 𝑇a = 2 , b = 2
• f(n) = n  k=1 , p=0
• Log b a = 1  Log b a = k  case 2
satisfied
• p = 0  p>-1
• T(n) = n k log p+1 n
• T(n) = n 1 log 1 n
• T(n) = O(n log n )
Please ,
Solve this recurrence using the iteration
method?
Quick SortAlgorithm

• The problem here is to sort a list of n elements.


• Quick Sort is a Divide and Conquer algorithm.
• This algorithm divides the list into three main
parts:
1) Elements less than the Pivot element
2) Pivot element(Central element)
3) Elements greater than the pivot element
QuickSort and Divide &
Conquer
• Divide
The array is divided into subparts taking pivot as the partitioning point.
The elements smaller than the pivot are placed to the left of the pivot
and the elements greater than the pivot are placed to the right.
• Conquer
The left and the right subparts are again partitioned using the by selecting
pivot elements for them. This can be achieved by recursively passing the
subparts into the algorithm.
• Combine
This step does not play a significant role in quicksort. The array is already
sorted at the end of the conquer step.
How to choose
thepivot?
• There are many different versions of quicksort that pick pivot in
different ways:
1. Always pick first element as pivot.
2. Always pick last element as pivot.
3. Pick a random element as pivot.
4. Pick the median of three as pivot.
Example (pivot as firstelement)
Example (pivot as lastelement)
QuicksortAlgorit
hm
quickSort (arr[], low, high)
{
if (low < high)
{
pivot = partition(arr, low, high);
quickSort(arr, low, pivot - 1); // Before pivot
quickSort(arr, pivot + 1, high); // After pivot
}
}
Example on
PartitionAlgorithm
l h
10 16 8 12 15 6 3 9 5 22
Pivot 
i j
All the elements smaller than 10 should be on left side and all greater should be on the right-hand side

So check if any number > pivot send it on the right side and if any number < pivot send it on the left side

increment i until you find an element > 10 and decrement j until you find element < 10  swap them
10 16 8 12 15 6 3 9 5 22

i j
10 5 8 12 15 6 3 9 16 22

10 5 8 12 15 6 3 9 16 22

i j
Example on PartitionAlgorithm
10 5 8 9 15 6 3 12 16 22

10 5 8 9 15 6 3 12 16 22

i j
10 5 8 9 3 6 15 12 16 22

10 5 8 9 3 6 15 12 16 22

j i
j < i  stop  swap the pivot with
6 A[j]
5 8 9 3 10 15 12 16 22

pivot
< pivot > pivot
Animated Gif forpartitioning
int Partition(int a[], int low, int high)
{
int pivot = Partition
a[low]; int i = low
;int j = high+1 ; Algorith
while(1)
{ m
do {
i++;
} while (a[i] < pivot);

do {
j--;
} while (a[j] > pivot);

if(i >= j)
return j;

swap(a[i], a[j]);
}
}
Quick SortAnalysis
• The time complexity of quick sort depends directly on the :
1) The location of the pivot
2) The state of the array
Worst CasePartitioning
• At every step, partition() splits the array as unequally as possible ( k
= 1 or k = n).
• the worst occurs in following cases.
1) Array is already sorted in same order.
2) Array is already sorted in reverse order.
SortedArr
ay
Worst CasePartitioning
Best CasePartitioning
• The best case occurs :
• when the partition process always picks the middle element as pivot.
• Every time the size of the two subarrays are equal.
Best CasePartitioning
ChoosingPiv
ot
• Do not choose the first element of A because if the array is originally
nearly sorted or reversed sorted, All the elements will go to only
one partition.
• Choose the pivot randomly.
• Another way is to choose the median value from the first, the
last, and the middle element of the array.  sort them then
choose middle as pivot.
Merge SortVs.Quick
Sort
BASIS FOR COMPARISON QUICK SORT MERGE SORT

Partitioning of the elements The splitting of a list of Array is always divided into
in the array elements is not necessarily half (n/2).
divided into half.
2
Worst case complexity O(n ) O(n log n)

Works well on Smaller array Operates fine in any type of


array.
Speed Faster than other sorting Consistent speed in all type
algorithms for small data of data sets.
set.

Additional storage space Less More


requirement
Efficiency Inefficient for larger arrays. More efficient.
Implementations Links
+Explanations
• Binary Search: https://www.geeksforgeeks.org/binary-search/
• Tower Of Hanoi: https://www.geeksforgeeks.org/c-program-for-tower-of-hanoi/
• Merge Sort: https://www.geeksforgeeks.org/merge-sort/
• Quick Sort: https://www.geeksforgeeks.org/quick-sort/
• Quick Sort best and worst cases : https://www.youtube.com/watch?v=PJnHzFypGaI
• Quick Sort Vs. Merge Sort: https://www.youtube.com/watch?v=es2T6KY45cA
Can we accelerate quicksort using linked
list? Explain your answer?
End of
Chapter5

You might also like