480 likes | 628 Views
Data Structures and Algorithms CSE 246. Fall-2012 Lecture#13. Multiplication of large integer. The Karatsuba Ofman algorithm provides a striking example of how the “Divide and Conquer” technique can achieve an asymptotic speedup over an ancient algorithm.
E N D
Data Structures and AlgorithmsCSE 246 Fall-2012 Lecture#13
Multiplication of large integer Quratulain The Karatsuba Ofman algorithm provides a striking example of how the “Divide and Conquer” technique can achieve an asymptotic speedup over an ancient algorithm. The school classroom method of multiplying two n-digit integers requires O(n2) digit operations. He show that a simple recursive algorithm solves the problem in (nα) digit operations, where α = log2 3 = 1.58.
Karatsuba Algorithm Quratulain • X=4837 • Y=5813 X and Y are 4-digit number. Divide numbers in to half until you get number of digit one. A=48, B=37 , C=58, D=13 Therefore X*Y= (10n/2 A+B) (10n/2C+D) By multiplying =10n/2 A 10n/2 C + 10n/2 AD+ 10n/2BC +BD = 10n/2 AC + 10n/2 (AD+BC)+BD only 4 multiplication
KARATSUBA Algorithm Quratulain = 10n/2 AC + 10n/2 (AD+BC)+BD ……(1) Now replace (AD+BC) with only one multiplication (A+B)(C+D)=AC+AD+BC+BD =this inludes (AD+BC) Thus, (AD+BC)=(A+B)(C+D)-AC-BD Finally, replace (AD+BC) in eq(1) , it become = 10n/2 AC + 10n/2 ((A+B)(C+D)-AC-BD)+BD
Algorithm • Multiply(X , Y){ • Size=max(X.length,Y.length) • If (Size==1) • return X*Y; • A=11 ,B=13, C=14, D=12 • AC=multiply(A,C) • BD=multiply(B,D) • ApB=Add(A,B) • CpD=Add(B,D) • Term=multiply(ApB,CpD) • G=Term-AC-BD • Newsize=Size/2 • Temp=Add(BD,AddZero(G,Newsize)) • return Add(Temp,AddZero(AC,Size)) • } Quratulain multiply(1113,1412) Call recursively until number is of digit one.
Visual representation of function call X=1213 , Y=1412 Size=4 A=12 ,B=13, C=14, D=12 AC=multiply(A,C) BD=multiply(B,D) ApB=Add(A,B) CpD=Add(B,D) Term=multiply(ApB,CpD) G=Term-AC-BD Newsize=2 Temp=Add(BD,AddZero(G,newsize)) Return Add(Temp,AddZero(Temp,size)) X=1 , Y=1 Size=1 Return X*Y X=1 , Y=1 Size=1 Return X*Y Call X=3, Y=2 Size=1 Return X*Y Ret 69 X=2 , Y=4 Size=1 Return X*Y X=13 , Y=12 size=2 A=1 ,B=3, C=1, D=2 AC=multiply(A,C) BD=multiply(B,D) ApB=Add(A,B) CpD=Add(B,D) Term=multiply(ApB,CpD) G=Term-AC-BD Newsize=1 Temp=Add(BD,AddZero(G,newsize)) Return Add(Temp,AddZero(AC,size)) X=12 , Y=14 size=2 A=1 ,B=2, C=1, D=4 AC=multiply(A,C) BD=multiply(B,D) ApB=Add(A,B) CpD=Add(B,D) Term=multiply(ApB,CpD) G=Term-AC-BD Newsize=1 Temp=Add(BD,AddZero(G,newsize)) Return Add(Temp,AddZero(AC,size)) Return 1+3 Return 1+2 Return 1+2 Return 1+4 X=25 , Y=26 size=4 A=12 ,B=13, C=14, D=11 AC=multiply(A,C) BD=multiply(B,D) ApB=Add(A,B) CpD=Add(B,D) Term=multiply(ApB,CpD) G=Term-AC-BD Newsize=2 Temp=Add(BD,AddZero(G,newsize)) Return Add(Temp,AddZero(Temp,size)) X=3 , Y=5 Size=1 Return X*Y X=4 , Y=3 Size=1 Return X*Y Return 8+60 Return 6+50 Return 68+100 Return 56+100 Do it …. And compute final answer Quratulain
Elementary Sorting Methods Lecture #13
Why study elementary methods? • They provide context in which we can learn terminology and basic mechanisms for sorting algorithms • These simple methods are actually more effective than the more powerful general-purpose methods in many applications of sorting • Third, several of the simple methods extend to better general-purpose methods or are useful in improving the efficiency of more sophisticated methods. Muhammad Usman Arif
Time • Elementary methods we discuss take time N2 to sort N randomly arranged items. • These methods might be fast than most of the sophisticated algorithms for smaller files. • Not suitable for large files. Muhammad Usman Arif
External or Internal? • If the file to be sorted will fit into memory, then the sorting method is called internal • Internal sort can access any item easily • Sorting files from tape or disk is called external sorting • External sort must access items sequentially, or at least in large blocks Muhammad Usman Arif
The Bubble SortAlgorithm The Bubble Sort compares adjacent elements in a list, and “swaps” them if they are not in order. Each pair of adjacent elements is compared and swapped until the largest element “bubbles” to the bottom. Repeat this process each time stopping one indexed element less, until you compare only the first two elements in the list. We know that the array is sorted after both of the nested loops have finished. Muhammad Usman Arif
Compare A Bubble Sort Example We start by comparing the first two elements in the List. Muhammad Usman Arif
Swap A Bubble Sort Example Muhammad Usman Arif
Compare A Bubble Sort Example Muhammad Usman Arif
Swap A Bubble Sort Example Muhammad Usman Arif
Compare A Bubble Sort Example Muhammad Usman Arif
Swap A Bubble Sort Example Muhammad Usman Arif
Compare A Bubble Sort Example Muhammad Usman Arif
Swap A Bubble Sort Example Muhammad Usman Arif
Compare A Bubble Sort Example Muhammad Usman Arif
Swap A Bubble Sort Example As you can see, the largest number has “bubbled” down to the bottom of the List after the first pass through the List. Muhammad Usman Arif
Compare A Bubble Sort Example For our second pass through the List, we start by comparing these first two elements in the List. Muhammad Usman Arif
Swap A Bubble Sort Example Muhammad Usman Arif
Compare A Bubble Sort Example Muhammad Usman Arif
Swap A Bubble Sort Example Muhammad Usman Arif
Compare A Bubble Sort Example Muhammad Usman Arif
Swap A Bubble Sort Example Muhammad Usman Arif
Compare A Bubble Sort Example Muhammad Usman Arif
Swap A Bubble Sort Example At the end of the second pass, we stop at element number n - 1, because the largest element in the List is already in the last position. Muhammad Usman Arif
Compare A Bubble Sort Example We start with the first two elements again at the beginning of the third pass. Muhammad Usman Arif
Swap A Bubble Sort Example Muhammad Usman Arif
Compare A Bubble Sort Example Muhammad Usman Arif
Swap A Bubble Sort Example Muhammad Usman Arif
Compare A Bubble Sort Example Muhammad Usman Arif
Swap A Bubble Sort Example At the end of the third pass, we stop comparing and swapping at element number n - 2. Muhammad Usman Arif
Compare A Bubble Sort Example The beginning of the fourth pass... Muhammad Usman Arif
Swap A Bubble Sort Example Muhammad Usman Arif
Compare A Bubble Sort Example Muhammad Usman Arif
Swap A Bubble Sort Example The end of the fourth pass stops at element number n - 3. Muhammad Usman Arif
Compare A Bubble Sort Example The beginning of the fifth pass... Muhammad Usman Arif
Swap A Bubble Sort Example The last pass compares only the first two elements of the List. After this comparison and possible swap, the smallest element has “bubbled” to the top. Muhammad Usman Arif
What “Swapping” Means TEMP 6 Place the first element into the Temporary Variable. Muhammad Usman Arif
What “Swapping” Means TEMP 6 Replace the first element with the second element. Muhammad Usman Arif
What “Swapping” Means TEMP 6 Replace the second element with the Temporary Variable. Muhammad Usman Arif
Java Code For Bubble Sort Muhammad Usman Arif
Java Code for Bubble sort Muhammad Usman Arif
Big - O Notation Big - O notation is used to describe the efficiency of a search or sort. The actual time necessary to complete the sort varies according to the speed of your system.Big - O notation is an approximate mathematical formula to determine how many operations are necessary to perform the search or sort. The Big - O notation for the Bubble Sort is O(n2), because it takes approximately n2 passes to sort the elements. Muhammad Usman Arif
Bubble sort. Simplest of all, but also slowest • Worst case performance • Best case performance • Makes passes through a sequence of items. • On each pass it compares adjacent elements in pairs and swaps them if they are out of order. Muhammad Usman Arif