1 / 48

Data Structures and Algorithms CSE 246

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.

amaris
Download Presentation

Data Structures and Algorithms CSE 246

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data Structures and AlgorithmsCSE 246 Fall-2012 Lecture#13

  2. 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.

  3. 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

  4. 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

  5. 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.

  6. 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

  7. Elementary Sorting Methods Lecture #13

  8. 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

  9. 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

  10. 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

  11. 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

  12. Compare A Bubble Sort Example We start by comparing the first two elements in the List. Muhammad Usman Arif

  13. Swap A Bubble Sort Example Muhammad Usman Arif

  14. Compare A Bubble Sort Example Muhammad Usman Arif

  15. Swap A Bubble Sort Example Muhammad Usman Arif

  16. Compare A Bubble Sort Example Muhammad Usman Arif

  17. Swap A Bubble Sort Example Muhammad Usman Arif

  18. Compare A Bubble Sort Example Muhammad Usman Arif

  19. Swap A Bubble Sort Example Muhammad Usman Arif

  20. Compare A Bubble Sort Example Muhammad Usman Arif

  21. 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

  22. 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

  23. Swap A Bubble Sort Example Muhammad Usman Arif

  24. Compare A Bubble Sort Example Muhammad Usman Arif

  25. Swap A Bubble Sort Example Muhammad Usman Arif

  26. Compare A Bubble Sort Example Muhammad Usman Arif

  27. Swap A Bubble Sort Example Muhammad Usman Arif

  28. Compare A Bubble Sort Example Muhammad Usman Arif

  29. 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

  30. Compare A Bubble Sort Example We start with the first two elements again at the beginning of the third pass. Muhammad Usman Arif

  31. Swap A Bubble Sort Example Muhammad Usman Arif

  32. Compare A Bubble Sort Example Muhammad Usman Arif

  33. Swap A Bubble Sort Example Muhammad Usman Arif

  34. Compare A Bubble Sort Example Muhammad Usman Arif

  35. 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

  36. Compare A Bubble Sort Example The beginning of the fourth pass... Muhammad Usman Arif

  37. Swap A Bubble Sort Example Muhammad Usman Arif

  38. Compare A Bubble Sort Example Muhammad Usman Arif

  39. Swap A Bubble Sort Example The end of the fourth pass stops at element number n - 3. Muhammad Usman Arif

  40. Compare A Bubble Sort Example The beginning of the fifth pass... Muhammad Usman Arif

  41. 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

  42. What “Swapping” Means TEMP 6 Place the first element into the Temporary Variable. Muhammad Usman Arif

  43. What “Swapping” Means TEMP 6 Replace the first element with the second element. Muhammad Usman Arif

  44. What “Swapping” Means TEMP 6 Replace the second element with the Temporary Variable. Muhammad Usman Arif

  45. Java Code For Bubble Sort Muhammad Usman Arif

  46. Java Code for Bubble sort Muhammad Usman Arif

  47. 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

  48. 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

More Related