Presentation is loading. Please wait.

Presentation is loading. Please wait.

UNIT- I Problem solving and Algorithmic Analysis

Similar presentations


Presentation on theme: "UNIT- I Problem solving and Algorithmic Analysis"— Presentation transcript:

1 UNIT- I Problem solving and Algorithmic Analysis
Problem solving principles: Classification of problem, problem solving strategies, classification of time complexities (linear, logarithmic etc) problem subdivision { Divide and Conquer strategy}. Asymptotic notations, lower bound and upper bound: Best case, worst case, average case analysis, amortized analysis. Performance analysis of basic programming constructs. Recurrences: Formulation and solving recurrence equations using Master Theorem.

2 Recurrences The expression: is a recurrence.
Recurrence: an equation that describes a function in terms of its value on smaller functions The expression: is a recurrence.

3 Recurrence Examples

4 Solving Recurrences Substitution method Iteration method Master method

5 The Master Theorem Given: a divide and conquer algorithm
An algorithm that divides the problem of size n into a subproblems, each of size n/b Let the cost of each stage (i.e., the work to divide the problem + combine solved subproblems) be described by the function f(n) Then, the Master Theorem gives us a cookbook for the algorithm’s running time:

6

7 The Master Theorem if T(n) = aT(n/b) + f(n) then

8 Divide and Conquer Recursive in structure
Divide the problem into sub-problems that are similar to the original but smaller in size Conquer the sub-problems by solving them recursively. If they are small enough, just solve them in a straightforward manner. Combine the solutions to create a solution to the original problem Comp 122

9 Control abstraction of D&C Method
1. Algorithm DAndC(P) 2. { if small(P) then return S(P); else { divide P into smaller instances P1, P2… Pk, k>=1; Apply DAndC to each of these sub problems; return combine (DAndC(P1), DAndC(P2),..,DAndC(Pk)); } 10. }

10 If size of P is n and the size of the k subproblems are n1, n2 …
If size of P is n and the size of the k subproblems are n1, n2 …..nk ten computing time of DandC is described by the recurrence relation T(n)= g(n) n small T(n1)+T(N2)+…+T(nk)+f(n) otherwise T(n) time for DAndC on any input of size n g(n) time to compute the answer directly for small input f(n) time for dividing P and combining the solution of subproblems.

11 The complexity of many divide and conquer algorithm is given by recurrence of the form
T(n)= T(1) n small aT(n/b)+f(n) otherwise Where a and b are known constants. We assume that T(1) is known and n is power of b that is n=bk

12 Example of Divide and Conquer
Binary Search Merge Sort Quick Sort

13 Binary Search Let ai, 1 ≤ i ≤ n be a list of elements which are sorted in nondecreasing order. Consider the problem of determining whether a given element x is present in the list. If x is present, we are to determine a value j such that aj = x. If x is not in the list then j is to be set to zero. Divide-and-conquer suggests breaking up any instance I = (n, a1, ... , an. x) of this search problem into subinstances.

14 Binary Search Algorithm BINSRCH(a, i, l, x) //given an array A(i:l) of elements in nondecreasing order, 1 ≤ i ≤ l determine if x is present, and if so, set j such that x = a[j] else return 0 { if (l=i) then if( x=a[i]) then return i; Else return 0; } Else { //reduce P into a smaller subproblem mid= ⌊ (i+l)/2 ⌋; if (x=a[mid] )then return mid; Else if (x<a[mid]) then Return BINSRCH(a, i, mid-1,x); Else Return BINSRCH(a, i, mid+1,x);

15 Binary Search Ex. Binary search for 33. 6 13 14 25 33 43 51 53 64 72
84 93 95 96 97 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lo hi

16 Binary Search Ex. Binary search for 33. 6 13 14 25 33 43 51 53 64 72
84 93 95 96 97 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lo mid hi

17 Binary Search Ex. Binary search for 33. 6 13 14 25 33 43 51 53 64 72
84 93 95 96 97 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lo hi

18 Binary Search Ex. Binary search for 33. 6 13 14 25 33 43 51 53 64 72
84 93 95 96 97 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lo mid hi

19 Binary Search Ex. Binary search for 33. 6 13 14 25 33 43 51 53 64 72
84 93 95 96 97 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lo hi

20 Binary Search Ex. Binary search for 33. 6 13 14 25 33 43 51 53 64 72
84 93 95 96 97 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lo mid hi

21 Binary Search Ex. Binary search for 33. 6 13 14 25 33 43 51 53 64 72
84 93 95 96 97 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lo hi

22 Binary Search Ex. Binary search for 33. 6 13 14 25 33 43 51 53 64 72
84 93 95 96 97 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lo hi mid

23 Binary Search Ex. Binary search for 33. 6 13 14 25 33 43 51 53 64 72
84 93 95 96 97 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lo hi mid

24 Successful Search Unsuccessful search Ө(1) Best case Ө(log n) Average Case Worst Case

25 Merge sort Given a sequence of n. elements (also called keys) A(l), ... , A(n) the general idea is to imagine them split into two sets A(1), . , A( n/2) and A( n/2 + 1), ... , A(n). Each set is individually sorted and the resulting sequences are merged to produce a single sorted sequence of n elements.

26 Algorithm MERGESORT(low, high) //A(low : high) is a global array containing high - low + 1 ≥0 //values which represent the elements to be sorted. if low < high then mid  ⌊(low + high)/2 ⌋ //find where to split the set// call MERGESORT(low, mid) //sort one subset// call MERGESORT(mid + 1, high) //sort the other subset// call MERGE(low, mid, high) //combine the results/ Endif end MERGESORT

27

28 T(n) = 2T(n/2) + (n) if n > 1
Analysis of Merge Sort Running time T(n) of Merge Sort: Divide: computing the middle takes (1) Conquer: solving 2 subproblems takes 2T(n/2) Combine: merging n elements takes (n) Total: T(n) = (1) if n = 1 T(n) = 2T(n/2) + (n) if n > 1  T(n) = (n log n) Comp 122

29 If the time for the merging operation is proportional to n then the computing time for mergesort is described by the recurrence relation T(n) = a n = 1, a constant 2T(n/2) + cn n > l, c constant By using recurrence we got T(n) = O(n log2n)

30 Merging The key to Merge Sort is merging two sorted lists into one, such that if you have two lists X (x1x2…xm) and Y(y1y2…yn) the resulting list is Z(z1z2…zm+n) Example: L1 = { } L2 = { } merge(L1, L2) = { }

31 Merging (cont.) X: 3 10 23 54 Y: 1 5 25 75 Result:

32 Merging (cont.) X: 3 10 23 54 Y: 5 25 75 Result: 1

33 Merging (cont.) X: 10 23 54 Y: 5 25 75 Result: 1 3

34 Merging (cont.) X: 10 23 54 Y: 25 75 Result: 1 3 5

35 Merging (cont.) X: 23 54 Y: 25 75 Result: 1 3 5 10

36 Merging (cont.) X: 54 Y: 25 75 Result: 1 3 5 10 23

37 Merging (cont.) X: 54 Y: 75 Result: 1 3 5 10 23 25

38 Merging (cont.) X: Y: 75 Result: 1 3 5 10 23 25 54

39 Merging (cont.) X: Y: Result: 1 3 5 10 23 25 54 75

40 Merge Sort Example 99 6 86 15 58 35 4

41 Merge Sort Example 99 6 86 15 58 35 4 99 6 86 15 58 35 86 4

42 Merge Sort Example 99 6 86 15 58 35 4 99 6 86 15 58 35 86 4 99 6 86 15 58 35 86 4

43 Merge Sort Example 99 6 86 15 58 35 4 99 6 86 15 58 35 86 4 99 6 86 15 58 35 86 4 99 6 86 15 58 35 86 4

44 Merge Sort Example 99 6 86 15 58 35 4 99 6 86 15 58 35 86 4 99 6 86 15 58 35 86 4 99 6 86 15 58 35 86 4 4

45 Merge Sort Example 99 6 86 15 58 35 86 4 Merge 4

46 Merge Sort Example 6 99 15 86 35 58 4 86 99 6 86 15 58 35 86 4 Merge

47 Merge Sort Example 6 15 86 99 4 35 58 86 6 99 15 86 58 35 4 86 Merge

48 Merge Sort Example 4 6 15 35 58 86 99 6 15 86 99 4 35 58 86 Merge

49 Merge Sort Example 4 6 15 35 58 86 99


Download ppt "UNIT- I Problem solving and Algorithmic Analysis"

Similar presentations


Ads by Google