SlideShare ist ein Scribd-Unternehmen logo
1 von 47
Divide-and-Conquer ,[object Object],[object Object]
Divide-and-Conquer ,[object Object]
Merge Sort Algorithm Divide: Divide the n-element sequence into two subsequences of n/2 elements each. Conquer: Sort the two subsequences recursively using merge sort. Combine: Merge the two sorted sequences.
How to merge two sorted sequences ,[object Object],[object Object]
Merging Algorithm ,[object Object],[object Object],[object Object],[object Object]
Merging Algorithm ,[object Object],[object Object],[object Object],[object Object]
Merging Algorithm 8. L[n1 + 1]     ∞ 9. R[n2 + 1]     ∞ 10. i    1 11. j    1
Merging Algorithm ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Merging Algorithm (a) … 6 3 2 1 7 5 4 2 A… 12 11 10 k 9 8 17 16 15 14 13 ∞ 7 5 4 2 L 4 3 2 i 1 5 ∞ 6 3 2 1 R 4 3 2 j 1 5
Merging Algorithm (b) … 6 3 2 1 7 5 4 1 A… 12 11 k 10 9 8 17 16 15 14 13 ∞ 7 5 4 2 L 4 3 2 i 1 5 ∞ 6 3 2 1 R 4 3 j 2 1 5
Merging Algorithm (c) … 6 3 2 1 7 5 2 1 A… 12 k 11 10 9 8 17 16 15 14 13 ∞ 7 5 4 2 L 4 3 i 2 1 5 ∞ 6 3 2 1 R 4 3 j 2 1 5
Merging Algorithm (d) … 6 3 2 1 7 2 2 1 A… k 12 11 10 9 8 17 16 15 14 13 ∞ 7 5 4 2 L 4 3 i 2 1 5 ∞ 6 3 2 1 R 4 j 3 2 1 5
Merging Algorithm (e) … 6 3 2 1 3 2 2 1 A… 12 11 10 9 8 k 17 16 15 14 13 ∞ 7 5 4 2 L 4 3 i 2 1 5 ∞ 6 3 2 1 R j 4 3 2 1 5
Merging Algorithm (f) … 6 3 2 4 3 2 2 1 A… 12 11 10 9 8 k 17 16 15 14 13 ∞ 7 5 4 2 L 4 i 3 2 1 5 ∞ 6 3 2 1 R j 4 3 2 1 5
Merging Algorithm (g) … 6 3 5 4 3 2 2 1 A… 12 11 10 9 8 k 17 16 15 14 13 ∞ 7 5 4 2 L i 4 3 2 1 5 ∞ 6 3 2 1 R j 4 3 2 1 5
Merging Algorithm (h) … 6 6 5 4 3 2 2 1 A… 12 11 10 9 8 k 17 16 15 14 13 ∞ 7 5 4 2 L i 4 3 2 1 5 ∞ 6 3 2 1 R 4 3 2 1 j 5
Merging Algorithm (i) … 7 6 5 4 3 2 2 1 A… 12 11 10 9 8 k 17 16 15 14 13 ∞ 7 5 4 2 L 4 3 2 1 i 5 ∞ 6 3 2 1 R 4 3 2 1 j 5
Merge Sort Algorithm ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Operation of Merge Sort 6 1 3 7 5 4 2 2 7 5 4 2 6 3 2 1 5 2 7 4 3 1 6 2 5 2 4 7 1 3 2 6
Analyzing Divide-and-Conquer Algorithm When an algorithm contains a recursive call to itself, its running time can be described by a recurrence equation or recurrence which describes the running time
Recurrence If the problem size is small enough, say n<=c for some constant c, the straightforward solution takes constant time, can be written as  θ (1).
Recurrence ,[object Object],[object Object],[object Object]
Recurrence ,[object Object],[object Object],[object Object],θ (1)  if n <= c aT(n/b) + D(n) + C(n)  otherwise
Recurrence Divide: The divide step computes the middle of the subarray which takes constant time, D(n)= θ (1)
Recurrence Conquer: We recursively solve two subproblems, each of size n/2, which contributes 2T(n/2) to the running time.
Recurrence Combine: Merge procedure takes  θ (n) time on an n-element subarray. C(n)= θ (n) The recurrence T(n)=  θ (1)  if n=1 2T(n/2) +  θ (n)  if n>1
Recurrence Let us rewrite the recurrence T(n)= C represents the time required to solve problems of size 1 C  if n=1 2T(n/2) + cn  if n>1
A Recursion Tree for the Recurrence T(n) Cn T(n/2) T(n/2)
A Recursion Tree for the Recurrence Cn Cn/2 Cn/2 T(n/4) T(n/4) T(n/4) T(n/4)
A Recursion Tree for the Recurrence C(n) Cn/2 Cn/2 Cn/4 Cn/4 Cn/4 Cn/4 C C C C C C C cn cn cn cn lg n
Total Running Time The fully expanded tree has lg n +1 levels and each level contributes a total cost of cn. Therefore T(n)= cn lg n + cn =  θ (nlg n)
Growth of Functions We look at input sizes large enough to make only the order of growth of the running time relevant.
Asymptotic Notation Used to describe running time of an algorithm and defined in terms of functions whose domains are the set of natural numbers N={0,1,2--------------}
θ - Notation θ (g(n)) = { f(n) : there exist positive constants C 1 , C 2 , and n 0  such that  0 <= C 1 g(n)<=f(n) <=C 2 g(n) for all n>=n 0 }
θ - Notation n 0 n C 2 g(n) C 1 g(n) f(n) f(n)= θ (g(n))
θ - Notation For all n>=n 0 , the function f(n) is equal to g(n) to within a constant factor. So g(n) is asymptotically tight bound for f(n).
θ - Notation Let us show that 1/2n 2 - 3n= θ (n 2 ) To do so, we must determine C 1 , C 2 , and n 0  such that C 1 n 2 <=1/2n 2 -3n<=C 2 n 2  for all n>=n 0
θ - Notation Diving by n 2 C 1 <=1/2 – 3/n <= C 2 By choosing C 1 =1/14 , C 2 =1/2, and n 0 =7, we can verify that 1/2n 2 - 3n= θ (n 2 )
O - Notation O (g(n)) = { f(n) : there exist positive constants C and n 0  such that  0 <= f(n) <=Cg(n) for all n>=n 0 }
O - Notation n 0 n Cg(n) f(n) f(n)= O (g(n))
O - Notation O(n 2 )  bound on worst-case running time of insertion sort also applies to its running time on every input.
O - Notation θ (n 2 )  bound on worst-case running time of insertion sort, however, does not imply a  θ (n 2 )  bound on the running time of insertion sort on every input.
Ω - Notation Ω (g(n)) = { f(n) : there exist positive constants C and n 0  such that  0 <= Cg(n)<= f(n) for all n>=n 0 }
Ω - Notation n 0 n Cg(n) f(n) f(n)= Ω(g(n))
Ω - Notation Since Ω-notation describes a lower bound, we use it to bound the best-case running time of an algorithm, we also bound the running time of algorithm on arbitrary inputs
o - Notation We use  o- notation to denote a upper bound that is not asymptotically tight. The bound 2n 2 = O (n 2 ) is asymptotically tight, but the bound 2n= O (n 2 ) is not.
ω - Notation We use  ω - notation to denote a lower bound that is not asymptotically tight.

Weitere ähnliche Inhalte

Was ist angesagt?

Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
Ankit Katiyar
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysis
sumitbardhan
 
Performance analysis and randamized agoritham
Performance analysis and randamized agorithamPerformance analysis and randamized agoritham
Performance analysis and randamized agoritham
lilyMalar1
 
Programming language design and implemenation
Programming language design and implemenationProgramming language design and implemenation
Programming language design and implemenation
Ashwini Awatare
 
Introduction to data structures and Algorithm
Introduction to data structures and AlgorithmIntroduction to data structures and Algorithm
Introduction to data structures and Algorithm
Dhaval Kaneria
 

Was ist angesagt? (20)

Design and analysis of algorithms
Design and analysis of algorithmsDesign and analysis of algorithms
Design and analysis of algorithms
 
Analysis of algorithm
Analysis of algorithmAnalysis of algorithm
Analysis of algorithm
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
 
Fundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm EfficiencyFundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm Efficiency
 
Compiler Design Lecture Notes
Compiler Design Lecture NotesCompiler Design Lecture Notes
Compiler Design Lecture Notes
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms I
 
Computability - Tractable, Intractable and Non-computable Function
Computability - Tractable, Intractable and Non-computable FunctionComputability - Tractable, Intractable and Non-computable Function
Computability - Tractable, Intractable and Non-computable Function
 
Daa unit 1
Daa unit 1Daa unit 1
Daa unit 1
 
Introduction to Compiler design
Introduction to Compiler design Introduction to Compiler design
Introduction to Compiler design
 
Notion of an algorithm
Notion of an algorithmNotion of an algorithm
Notion of an algorithm
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysis
 
Data Structure and Algorithms.pptx
Data Structure and Algorithms.pptxData Structure and Algorithms.pptx
Data Structure and Algorithms.pptx
 
Performance analysis and randamized agoritham
Performance analysis and randamized agorithamPerformance analysis and randamized agoritham
Performance analysis and randamized agoritham
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of Algorithms
 
Programming language design and implemenation
Programming language design and implemenationProgramming language design and implemenation
Programming language design and implemenation
 
Asymptotic notation
Asymptotic notationAsymptotic notation
Asymptotic notation
 
Introduction to data structures and Algorithm
Introduction to data structures and AlgorithmIntroduction to data structures and Algorithm
Introduction to data structures and Algorithm
 
asymptotic notation
asymptotic notationasymptotic notation
asymptotic notation
 
Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis
 

Ähnlich wie Algorithm.ppt

lecture 1
lecture 1lecture 1
lecture 1
sajinsc
 
T2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxT2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptx
GadaFarhan
 

Ähnlich wie Algorithm.ppt (20)

Merge Sort
Merge SortMerge Sort
Merge Sort
 
Slide2
Slide2Slide2
Slide2
 
Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1
 
03 dc
03 dc03 dc
03 dc
 
5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquer
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
lecture 1
lecture 1lecture 1
lecture 1
 
Mergesort
MergesortMergesort
Mergesort
 
Asymptotic Notation and Complexity
Asymptotic Notation and ComplexityAsymptotic Notation and Complexity
Asymptotic Notation and Complexity
 
Introduction
IntroductionIntroduction
Introduction
 
T2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxT2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptx
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdf
 
Design and analysis of algorithm ppt ppt
Design and analysis of algorithm ppt pptDesign and analysis of algorithm ppt ppt
Design and analysis of algorithm ppt ppt
 
Cs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer keyCs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer key
 
Admission in india 2015
Admission in india 2015Admission in india 2015
Admission in india 2015
 
analysis.ppt
analysis.pptanalysis.ppt
analysis.ppt
 
01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptx
 
C1 - Insertion Sort
C1 - Insertion SortC1 - Insertion Sort
C1 - Insertion Sort
 
Algorithm Design and Analysis
Algorithm Design and AnalysisAlgorithm Design and Analysis
Algorithm Design and Analysis
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
 

Mehr von Tareq Hasan

08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
Tareq Hasan
 
02 c++ Array Pointer
02 c++ Array Pointer02 c++ Array Pointer
02 c++ Array Pointer
Tareq Hasan
 
01 c++ Intro.ppt
01 c++ Intro.ppt01 c++ Intro.ppt
01 c++ Intro.ppt
Tareq Hasan
 
Algorithm: priority queue
Algorithm: priority queueAlgorithm: priority queue
Algorithm: priority queue
Tareq Hasan
 

Mehr von Tareq Hasan (20)

Grow Your Career with WordPress
Grow Your Career with WordPressGrow Your Career with WordPress
Grow Your Career with WordPress
 
Caching in WordPress
Caching in WordPressCaching in WordPress
Caching in WordPress
 
How to Submit a plugin to WordPress.org Repository
How to Submit a plugin to WordPress.org RepositoryHow to Submit a plugin to WordPress.org Repository
How to Submit a plugin to WordPress.org Repository
 
Composer - The missing package manager for PHP
Composer - The missing package manager for PHPComposer - The missing package manager for PHP
Composer - The missing package manager for PHP
 
WordPress Theme & Plugin development best practices - phpXperts seminar 2011
WordPress Theme & Plugin development best practices - phpXperts seminar 2011WordPress Theme & Plugin development best practices - phpXperts seminar 2011
WordPress Theme & Plugin development best practices - phpXperts seminar 2011
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 
02 c++ Array Pointer
02 c++ Array Pointer02 c++ Array Pointer
02 c++ Array Pointer
 
01 c++ Intro.ppt
01 c++ Intro.ppt01 c++ Intro.ppt
01 c++ Intro.ppt
 
chapter22.ppt
chapter22.pptchapter22.ppt
chapter22.ppt
 
chapter - 6.ppt
chapter - 6.pptchapter - 6.ppt
chapter - 6.ppt
 
chapter-8.ppt
chapter-8.pptchapter-8.ppt
chapter-8.ppt
 
chapter23.ppt
chapter23.pptchapter23.ppt
chapter23.ppt
 
chapter24.ppt
chapter24.pptchapter24.ppt
chapter24.ppt
 
Algorithm: priority queue
Algorithm: priority queueAlgorithm: priority queue
Algorithm: priority queue
 
Java: GUI
Java: GUIJava: GUI
Java: GUI
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: Inheritance
 
Java: Exception
Java: ExceptionJava: Exception
Java: Exception
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
 
Java: Class Design Examples
Java: Class Design ExamplesJava: Class Design Examples
Java: Class Design Examples
 
Java: Objects and Object References
Java: Objects and Object ReferencesJava: Objects and Object References
Java: Objects and Object References
 

Algorithm.ppt

  • 1.
  • 2.
  • 3. Merge Sort Algorithm Divide: Divide the n-element sequence into two subsequences of n/2 elements each. Conquer: Sort the two subsequences recursively using merge sort. Combine: Merge the two sorted sequences.
  • 4.
  • 5.
  • 6.
  • 7. Merging Algorithm 8. L[n1 + 1]  ∞ 9. R[n2 + 1]  ∞ 10. i  1 11. j  1
  • 8.
  • 9. Merging Algorithm (a) … 6 3 2 1 7 5 4 2 A… 12 11 10 k 9 8 17 16 15 14 13 ∞ 7 5 4 2 L 4 3 2 i 1 5 ∞ 6 3 2 1 R 4 3 2 j 1 5
  • 10. Merging Algorithm (b) … 6 3 2 1 7 5 4 1 A… 12 11 k 10 9 8 17 16 15 14 13 ∞ 7 5 4 2 L 4 3 2 i 1 5 ∞ 6 3 2 1 R 4 3 j 2 1 5
  • 11. Merging Algorithm (c) … 6 3 2 1 7 5 2 1 A… 12 k 11 10 9 8 17 16 15 14 13 ∞ 7 5 4 2 L 4 3 i 2 1 5 ∞ 6 3 2 1 R 4 3 j 2 1 5
  • 12. Merging Algorithm (d) … 6 3 2 1 7 2 2 1 A… k 12 11 10 9 8 17 16 15 14 13 ∞ 7 5 4 2 L 4 3 i 2 1 5 ∞ 6 3 2 1 R 4 j 3 2 1 5
  • 13. Merging Algorithm (e) … 6 3 2 1 3 2 2 1 A… 12 11 10 9 8 k 17 16 15 14 13 ∞ 7 5 4 2 L 4 3 i 2 1 5 ∞ 6 3 2 1 R j 4 3 2 1 5
  • 14. Merging Algorithm (f) … 6 3 2 4 3 2 2 1 A… 12 11 10 9 8 k 17 16 15 14 13 ∞ 7 5 4 2 L 4 i 3 2 1 5 ∞ 6 3 2 1 R j 4 3 2 1 5
  • 15. Merging Algorithm (g) … 6 3 5 4 3 2 2 1 A… 12 11 10 9 8 k 17 16 15 14 13 ∞ 7 5 4 2 L i 4 3 2 1 5 ∞ 6 3 2 1 R j 4 3 2 1 5
  • 16. Merging Algorithm (h) … 6 6 5 4 3 2 2 1 A… 12 11 10 9 8 k 17 16 15 14 13 ∞ 7 5 4 2 L i 4 3 2 1 5 ∞ 6 3 2 1 R 4 3 2 1 j 5
  • 17. Merging Algorithm (i) … 7 6 5 4 3 2 2 1 A… 12 11 10 9 8 k 17 16 15 14 13 ∞ 7 5 4 2 L 4 3 2 1 i 5 ∞ 6 3 2 1 R 4 3 2 1 j 5
  • 18.
  • 19. Operation of Merge Sort 6 1 3 7 5 4 2 2 7 5 4 2 6 3 2 1 5 2 7 4 3 1 6 2 5 2 4 7 1 3 2 6
  • 20. Analyzing Divide-and-Conquer Algorithm When an algorithm contains a recursive call to itself, its running time can be described by a recurrence equation or recurrence which describes the running time
  • 21. Recurrence If the problem size is small enough, say n<=c for some constant c, the straightforward solution takes constant time, can be written as θ (1).
  • 22.
  • 23.
  • 24. Recurrence Divide: The divide step computes the middle of the subarray which takes constant time, D(n)= θ (1)
  • 25. Recurrence Conquer: We recursively solve two subproblems, each of size n/2, which contributes 2T(n/2) to the running time.
  • 26. Recurrence Combine: Merge procedure takes θ (n) time on an n-element subarray. C(n)= θ (n) The recurrence T(n)= θ (1) if n=1 2T(n/2) + θ (n) if n>1
  • 27. Recurrence Let us rewrite the recurrence T(n)= C represents the time required to solve problems of size 1 C if n=1 2T(n/2) + cn if n>1
  • 28. A Recursion Tree for the Recurrence T(n) Cn T(n/2) T(n/2)
  • 29. A Recursion Tree for the Recurrence Cn Cn/2 Cn/2 T(n/4) T(n/4) T(n/4) T(n/4)
  • 30. A Recursion Tree for the Recurrence C(n) Cn/2 Cn/2 Cn/4 Cn/4 Cn/4 Cn/4 C C C C C C C cn cn cn cn lg n
  • 31. Total Running Time The fully expanded tree has lg n +1 levels and each level contributes a total cost of cn. Therefore T(n)= cn lg n + cn = θ (nlg n)
  • 32. Growth of Functions We look at input sizes large enough to make only the order of growth of the running time relevant.
  • 33. Asymptotic Notation Used to describe running time of an algorithm and defined in terms of functions whose domains are the set of natural numbers N={0,1,2--------------}
  • 34. θ - Notation θ (g(n)) = { f(n) : there exist positive constants C 1 , C 2 , and n 0 such that 0 <= C 1 g(n)<=f(n) <=C 2 g(n) for all n>=n 0 }
  • 35. θ - Notation n 0 n C 2 g(n) C 1 g(n) f(n) f(n)= θ (g(n))
  • 36. θ - Notation For all n>=n 0 , the function f(n) is equal to g(n) to within a constant factor. So g(n) is asymptotically tight bound for f(n).
  • 37. θ - Notation Let us show that 1/2n 2 - 3n= θ (n 2 ) To do so, we must determine C 1 , C 2 , and n 0 such that C 1 n 2 <=1/2n 2 -3n<=C 2 n 2 for all n>=n 0
  • 38. θ - Notation Diving by n 2 C 1 <=1/2 – 3/n <= C 2 By choosing C 1 =1/14 , C 2 =1/2, and n 0 =7, we can verify that 1/2n 2 - 3n= θ (n 2 )
  • 39. O - Notation O (g(n)) = { f(n) : there exist positive constants C and n 0 such that 0 <= f(n) <=Cg(n) for all n>=n 0 }
  • 40. O - Notation n 0 n Cg(n) f(n) f(n)= O (g(n))
  • 41. O - Notation O(n 2 ) bound on worst-case running time of insertion sort also applies to its running time on every input.
  • 42. O - Notation θ (n 2 ) bound on worst-case running time of insertion sort, however, does not imply a θ (n 2 ) bound on the running time of insertion sort on every input.
  • 43. Ω - Notation Ω (g(n)) = { f(n) : there exist positive constants C and n 0 such that 0 <= Cg(n)<= f(n) for all n>=n 0 }
  • 44. Ω - Notation n 0 n Cg(n) f(n) f(n)= Ω(g(n))
  • 45. Ω - Notation Since Ω-notation describes a lower bound, we use it to bound the best-case running time of an algorithm, we also bound the running time of algorithm on arbitrary inputs
  • 46. o - Notation We use o- notation to denote a upper bound that is not asymptotically tight. The bound 2n 2 = O (n 2 ) is asymptotically tight, but the bound 2n= O (n 2 ) is not.
  • 47. ω - Notation We use ω - notation to denote a lower bound that is not asymptotically tight.