Algorithm Analysis Divide and Conquer Chung-Ang University, Jaesung Lee
Introduction 2
Divide and Conquer Paradigm 3
Advantages of Divide and Conquer Solving Difficult Problems Algorithm Efficiency Parallelism Memory Access Roundoff Control 4
Implementation Issues Recursion Explicit Stack Stack Size Choosing the Base Cases Sharing Repeated Subproblems 5
Divide and Conquer Overview General method Design strategy Large improvement in Time Complexity 6
Divide and Conquer Overview Divide and Conquer Strategy Divide the problem Solve smaller instances recursively Assemble the solutions Recursion stops when the division becomes impossible 7
Divide and Conquer Overview Divide and Conquer Steps Divide Conquer 8
Control Abstraction of D/C 9
Control Abstraction of D/C 10
Binary Search records, instances, datum, Keys: < < < < < Objective of Binary Search Given a key, find corresponding element from the list Successful search: = Unsuccessful search: = 0 where 0 = 1 11
Binary Search Jump into the middle of the array: [ ] Compare and [ ] If = then has been found If < then must be in the former portion of If > then must be in the latter portion of 12
Binary Search Algorithm 13
Binary Search Algorithm 14
Example for Binary Search Index 1 2 3 4 5 6 7 8 9 Elements -15-6 0 7 9 23 54 82 101 15
Example for Binary Search Index 1 2 3 4 5 6 7 8 9 Elements -15-6 0 7 9 23 54 82 101 16
Example for Binary Search low mid high Index 1 2 3 4 5 6 7 8 9 Elements -15-6 0 7 9 23 54 82 101 17
Example for Binary Search low mid high Index 1 2 3 4 5 6 7 8 9 Elements -15-6 0 7 9 23 54 82 101 18
Example for Binary Search Index 1 2 3 4 5 6 7 low 8 high 9 Elements -15-6 0 7 9 23 54 82 101 mid 19
Example for Binary Search high Index 1 2 3 4 5 6 7 8 9 Elements -15-6 0 7 9 23 54 82 101 mid low 20
Example for Binary Search Index 1 2 3 4 5 6 7 8 9 Elements -15-6 0 7 9 23 54 82 101 21
Example for Binary Search high Index 1 2 3 4 5 6 7 8 9 Elements -15-6 0 7 9 23 54 82 101 mid low 22
Example for Binary Search Index high 1 low 2 3 4 5 6 7 8 9 Elements -15-6 0 7 9 23 54 82 101 mid 23
Example for Binary Search Index 1 2 3 4 5 6 7 8 9 Elements -15-6 0 7 9 23 54 82 101 Comp. 3 2 3 4 1 3 2 3 4 24
Example for Binary Search If is not presented, for example < 1 1 < < [2] 2 < < [3] 3 < < [4] 4 < < [5] 5 < < [6] 6 < < [7] 7 < < [8] 8 < < [9] 9 < Requires maximum 4 comparisons 25
Example for Binary Search Average comparisons for an unsuccessful search is = = 3.4 The time complexity of Binary Search is log Let s prove this! 26
Analysis for the worst case ( ): the time complexity of Binary search 0 = 0 ( ) = 1 = [ ] = 1 + 1 < [ ] = 1 + > [ ] 27
Analysis for the worst case Assume = 2 1 where is non-negative integer, then Array Algebraically, = = 2 for 1. 28
Analysis for the worst case Thus, we can simplify as 0 = 0 2 1 = 1 = [ ] = 1 + 2 1 < [ ] = 1 + 2 1 > [ ] 29
Analysis for the worst case In the worst case, the comparison always fails. 0 = 0 2 1 = 1 + 2 1 is time complexity for worst case! 30
Analysis for the worst case 2 1 = 1 + 2 1 2 1 = 1 + 1 + 2 1 2 1 = 1 + 1 + 1 + 2 1 2 1 = 2 1 = + 2 1 31
Analysis for the worst case 2 1 = + 2 1 For, 2 1 = + 0 = Because 2 1 =, = log + 1 = log + 1 = log 32
External and Internal Path Length An illustration of Binary tree ( ): external (square) nodes of tree ( ): internal (round) nodes 33
External and Internal Path Length h( ): the height of node 34
External and Internal Path Length ( ): the depth of node 35
External and Internal Path Length Internal path length: External path length: = = = 4 and = 4 = 12 and = 4 36
External and Internal Path Length Internal path length: External path length: = = Theorem: = + 2 where is a binary tree. 37