Like this presentation? Why not share!

- Divide and Conquer - Part 1 by Amrinder Arora 1191 views
- Divide and conquer 1 by Kumar 544 views
- Divide and Conquer by Cagatay Yuksel 2301 views
- Divide and Conquer by swolgotissues 401 views
- Divide & conquer by Mehmet Süleyman Y... 923 views
- 02 Analysis of Algorithms: Divide a... by Andres Mendez-Vaz... 290 views

1,477

-1

-1

Published on

Divide-and-Conquer

No Downloads

Total Views

1,477

On Slideshare

0

From Embeds

0

Number of Embeds

0

Shares

0

Downloads

47

Comments

0

Likes

2

No embeds

No notes for slide

- 1. Divide-and-ConquerRepublic of YemenTHAMAR UNIVERSITYFaculty of Computer Science&Information System1 Eng: Mohammed Hussein
- 2. Divide-and-Conquer2 Eng: Mohammed Hussein
- 3. Divide-and-Conquer The whole problem we want to solve may too big to understand or solve atonce. We break it up into smaller pieces, solve the pieces separately, andcombine the separate pieces together. Divide-and conquer is a general algorithm design paradigm:1. Divide: divide the input data S in two or more disjoint subsets S1,S2, …2. Recur: solve the subproblems recursively3. Conquer: combine the solutions for S1, S2, …, into a solution for S The base case for the recursion are subproblems of constant size Analysis can be done using recurrence equations3 Eng: Mohammed Hussein
- 4. Recurrence Equation Analysis The conquer step of merge-sort consists of merging two sorted sequences, each withn/2 elements and implemented by means of a doubly linked list, takes at most bn steps,for some constant b. Likewise, the basis case (n < 2) will take at b most steps. Therefore, if we let T(n) denote the running time of merge-sort: We can therefore analyze the running time of merge-sort by finding a closed formsolution to the above equation. That is, a solution that has T(n) only on the left-hand side.2if)2/(22if)(nbnnTnbnT4 Eng: Mohammed Hussein
- 5. Solving recurrences Recurrences are like solving integrals, differential equations, etc. Should learn a few tricks. Three ways of solving recurrences1. Guess-and-Test Method(If we know the answer)2. RecursionTree Method (Very useful !)3. MasterTheorem (Save our effort)5 Eng: Mohammed Hussein
- 6. Guess-and-Test Method In the guess-and-test method, we guess a closed form solution and then try to prove it istrue by induction: Guess that:T(n) < c n log n. Wrong: we cannot make this last line be less than cn log nncnnbncnncnnbnncnnbnnncnbnnTnTloglogloglog)2log(loglog))2/log()2/((2log)2/(2)(2iflog)2/(22if)(nnbnnTnbnT6 Eng: Mohammed HusseinLog(x/y)= log x – log yLog 2 =1بالتعويض
- 7. Guess-and-Test Method, Part 2 Recall the recurrence equation: Guess #2 that:T(n) < cn log2 n. if c > b. So,T(n) is O(n log2 n). In general, to use this method, you need to have a good guess and you need to be good atinduction proofs.ncnnbncnncnncnnbnnncnnbnncnnbnnncnbnnTnT222222logloglog2loglog))2(log2loglog2(loglog)2log(loglog))2/(log)2/((2log)2/(2)(2iflog)2/(22if)(nnbnnTnbnT7 Eng: Mohammed Hussein(x-y) 2= x2 -2xy +y2Log 2 =1nj <= nd if j <= dBig-Oh of highest degree
- 8. Recurrences In Mathematic a recurrence is a function that is defined interms of one or more base cases, and itself, with smallerarguments Examples:8 Eng: Mohammed Hussein
- 9. Master Method (tree) The master theorem concerns the recurrence a≥1 , b> 1 n is the size of the problem. a is the number of subproblems in the recursion. ()الفزوع n/b is the size of each subproblem. (Here it is assumed thatall subproblems are essentially the same size.) f (n) is the cost of the work done outside the recursive calls)()/()( nfbnaTnT 9 Eng: Mohammed Husseinmerge-sort خوارسميةاالستدعاء عمليات عندالذاتيالدمج عملية تنفيذ مستويات مختلف في
- 10. The Tree Definition The root of this tree is node A. Definitions: Parent (A) Children (E, F) Siblings (C, D) Root (A) Leaf / Leaves K, L, F, G, M, I, J… The degree of a node is the number of sub-trees of the node. The level of a node: Initially letting the root be at level one the level of the node’s parent plus one. Level H=level(Parent (H)+1)); The height or depth of a tree is the maximum level of any node in the tree. height =Max(level(tree))10 Eng: Mohammed Hussein
- 11. Divide and conquer (tree) We analyze this in some generality: suppose we have a pieces, each of size n/b and merging takes time f(n). (In this example a=b=2 and f(n)=O(log n) but it will not always be true thata=b sometimes the pieces will overlap.) For simplicity, lets assume n is a power of b, and that the recursion stopswhen n is 1. Notice that the size of a node depends only on its level: size(i) = n/(bi). What is time taken by a node at level i? : time(i) = f(n/ bi) How many levels can we have before we get down to n=1? For bottom level, n/bi =1, so n=bi and i=(log n)/(log b). How many items at level i?: ai.11 Eng: Mohammed Hussein)()/()( nfbnaTnT time(i) = f(n/ bi)
- 12. The Recursion Tree Draw the recursion tree for the recurrence relation and look for a pattern: At each successive level of recursion the sub problems get halved in size (n/2). At the (log n) th level the sub problems get down to size 1, and so the recursion ends.Level/depthT’s size0 1 n1 2 n/2k 2k n/2k… … …2if)2/(22if)(nbnnTnbnTtimebnbnbn…Total time = bn + bn log n(last level plus all previous levels)2k =n k= log n for each level.log nNotice that the size of a node depends only on its level12 Eng: Mohammed Husseintime=b x size = bn
- 13. Steps using recursion tree. Height = k=log n Levels = log n + 1 Cost/level = cn Bottom node number n = 2log n forheight log n dividing problem by 2each time. Recall that a balanced binary tree ofheight k has k + 1 levels. Recall: alog an = n so 2log 2n = 2log n =n Bottom cost = T(1) * n = cn Note that T(1) is problem size n=1,there are n subproblems at bottom.13 Eng: Mohammed Hussein 2k =n k= log n for each level.
- 14. Merge Sort - Finding O Recall that a balanced binary tree of height k has k + 1 levels. A problem of size n=16 divided as described by the following recurrence equationwould then be represented by the tree. Use the implied coefficient c for arithmetic purposes:14 Eng: Mohammed Hussein16=24
- 15. Master Method Many divide-and-conquer recurrence equations have the form: The MasterTheorem:dnnfbnaTdncnTif)()/(if)(.1somefor)()/(provided)),((is)(then),(is)(if3.)log(is)(then),log(is)(if2.)(is)(then),(is)(if1.log1loglogloglognfbnafnfnTnnfnnnTnnnfnnTnOnfakakaaabbbbb15 Eng: Mohammed Husseinvery small f(n)moderate f(n)very large f(n)
- 16. Analysis of Merge SortAnalysis of Merge Sort Divide:Trivial. Conquer:Recursively sort 2subarrays. Combine:Linear-time merge. Using Master theorem16 Eng: Mohammed Hussein
- 17. Analysis of Binary Search Recurrence for the worst case
- 18. Master Method, Example 1 The form: The MasterTheorem: Example:dnnfbnaTdncnTif)()/(if)(.1somefor)()/(provided)),((is)(then),(is)(if3.)log(is)(then),log(is)(if2.)(is)(then),(is)(if1.log1loglogloglognfbnafnfnTnnfnnnTnnnfnnTnOnfakakaaabbbbb)()2/(4)( nOnTnT Solution: logba =log24=2, so case 1 says T(n) is Θ(n2).18 Eng: Mohammed Hussein
- 19. Master Method, Example 2 The form: The MasterTheorem: Example:dnnfbnaTdncnTif)()/(if)(.1somefor)()/(provided)),((is)(then),(is)(if3.)log(is)(then),log(is)(if2.)(is)(then),(is)(if1.log1loglogloglognfbnafnfnTnnfnnnTnnnfnnTnOnfakakaaabbbbb)log()2/(2)( nnnTnT Solution: logba=log22=1, so case 2 says T(n) is Θ(n log2 n).19 Eng: Mohammed Hussein
- 20. Master Method, Example 3 The form: The MasterTheorem: Example:dnnfbnaTdncnTif)()/(if)(.1somefor)()/(provided)),((is)(then),(is)(if3.)log(is)(then),log(is)(if2.)(is)(then),(is)(if1.log1loglogloglognfbnafnfnTnnfnnnTnnnfnnTnOnfakakaaabbbbb)log()3/()( nnnTnT Solution: logba=0, so case 3 says T(n) is Θ(n log n).20 Eng: Mohammed Hussein
- 21. Master Method, Example 4 The form: The MasterTheorem: Example:dnnfbnaTdncnTif)()/(if)(.1somefor)()/(provided)),((is)(then),(is)(if3.)log(is)(then),log(is)(if2.)(is)(then),(is)(if1.log1loglogloglognfbnafnfnTnnfnnnTnnnfnnTnOnfakakaaabbbbb)()2/(8)( 2nnTnT Solution: logba=3, so case 1 says T(n) is Θ(n3).21 Eng: Mohammed Hussein
- 22. Master Method, Example 5 The form: The MasterTheorem: Example:dnnfbnaTdncnTif)()/(if)(.1somefor)()/(provided)),((is)(then),(is)(if3.)log(is)(then),log(is)(if2.)(is)(then),(is)(if1.log1loglogloglognfbnafnfnTnnfnnnTnnnfnnTnOnfakakaaabbbbb)()3/(9)( 3nnTnT Solution: logba=2, so case 3 says T(n) is Θ(n3).22 Eng: Mohammed Hussein
- 23. Master Method, Example 6 The form: The MasterTheorem: Example:dnnfbnaTdncnTif)()/(if)(.1somefor)()/(provided)),((is)(then),(is)(if3.)log(is)(then),log(is)(if2.)(is)(then),(is)(if1.log1loglogloglognfbnafnfnTnnfnnnTnnnfnnTnOnfakakaaabbbbb)1()2/()( nTnTSolution: logba=0, so case 2 says T(n) is Θ(log n).(binary search)23 Eng: Mohammed Hussein
- 24. Master Method, Example 7 The form: The MasterTheorem: Example:dnnfbnaTdncnTif)()/(if)(.1somefor)()/(provided)),((is)(then),(is)(if3.)log(is)(then),log(is)(if2.)(is)(then),(is)(if1.log1loglogloglognfbnafnfnTnnfnnnTnnnfnnTnOnfakakaaabbbbb)(log)2/(2)( nnTnT Solution: logba=1, so case 1 says T(n) is Θ(n).(heap construction)24 Eng: Mohammed Hussein
- 25. Eng: Mohammed Hussein25
- 26. Eng: Mohammed Hussein26
- 27. Eng: Mohammed Hussein27

No public clipboards found for this slide

Be the first to comment