daa ppt 28th jan
Post on 05-Apr-2018
223 Views
Preview:
TRANSCRIPT
-
8/2/2019 Daa Ppt 28th Jan
1/35
BITS PilaniPilani Campus
Design and Analysis of
AlgorithmsDr. Maheswari Karthikeyan
28/01/2012, Lecture2
-
8/2/2019 Daa Ppt 28th Jan
2/35
BITS PilaniPilani Campus
Bubble Sort AnalysisMerge Sort AnalysisGrowth of Functions
Asymptotic notation
-
8/2/2019 Daa Ppt 28th Jan
3/35
BITS Pilani, Pilani Campus
Idea: Repeatedly pass through the array
Swaps adjacent elements that are out of order
Easier to implement, but slower than Insertion sort
Bubble Sort
1 2 3 ni
1329648
j
-
8/2/2019 Daa Ppt 28th Jan
4/35
BITS Pilani, Pilani Campus
Example
1329648
i = 1 j
3129648
i = 1 j
3219648
i = 1 j
3291648
i = 1 j
3296148
i = 1 j
3296418
i = 1 j
3296481
i = 2 j
3964821
i = 3 j
9648321
i = 4 j
9684321
i = 5 j
9864321
i = 6 j
9864321
i = 7
j
-
8/2/2019 Daa Ppt 28th Jan
5/35
BITS Pilani, Pilani Campus
Alg.:BUBBLESORT(A)
fori 1tolength[A]
do forj length[A]downtoi + 1
do ifA[j] < A[j -1]then exchange A[j] A[j-1]
Bubble Sort
1329648
i = 1 j
i
-
8/2/2019 Daa Ppt 28th Jan
6/35
BITS Pilani, Pilani CampusT(n) =
(n
2
)
Bubble-Sort Running Time
222
)1()(
1 1
2
2
1
nnnnninin
n
i
n
i
n
i
T(n) = c1(n+1) +
n
i
in
1
)1(c2 c3
n
i
in
1
)( c4
n
i
in
1
)(
= (n) + (c2 + c2 + c4)
n
i
in1
)(
Alg.: BUBBLESORT(A)fori 1tolength[A]
do forj length[A]downtoi + 1
do ifA[j] < A[j -1]
then exchange A[j] A[j-1]
Exchanges: n2/2Comparisons n2/2
do if A[j] < A[j -1]
-
8/2/2019 Daa Ppt 28th Jan
7/35BITS Pilani, Pilani Campus
Insertion sort Design approach:
Sorts in place:
Best case:
Worst case:
Bubble Sort
Design approach:
Sorts in place: Running time:
Sorting
Yes
(n)
(n2)
incremental
Yes(n2)
incremental
-
8/2/2019 Daa Ppt 28th Jan
8/35BITS Pilani, Pilani Campus
Selection sort Design approach:
Sorts in place:
Running time:
Merge Sort
Design approach:
Sorts in place:
Running time:
Sorting
Yes
(n2)
incremental
No
Lets see!!
divide and conquer
-
8/2/2019 Daa Ppt 28th Jan
9/35BITS Pilani, Pilani Campus
Divide the problem into a number of sub-problems
Similar sub-problems of smaller size
Conquer the sub-problems
Solve the sub-problems recursively
Sub-problem size small enough solve the problems in
straightforward manner
Combine the solutions to the sub-problems
Obtain the solution for the original problem
Divide-and-Conquer
-
8/2/2019 Daa Ppt 28th Jan
10/35BITS Pilani, Pilani Campus
To sort an array A[p . . r]:
Divide Divide the n-element sequence to be sorted into two subsequences of n/2
elements each
Conquer Sort the subsequences recursively using merge sort
When the size of the sequences is 1 there is nothing more to do
Combine
Merge the two sorted subsequences
Merge Sort Approach
-
8/2/2019 Daa Ppt 28th Jan
11/35BITS Pilani, Pilani Campus
Alg.: MERGE-SORT(A, p, r)
if p < r Check for base casethen q(p + r)/2 Divide
MERGE-SORT(A, p, q) ConquerMERGE-SORT(A, q + 1, r) Conquer
MERGE(A, p, q, r) Combine
Initial call:MERGE-SORT(A, 1, n)
Merge Sort
1 2 3 4 5 6 7 8
62317425
p rq
-
8/2/2019 Daa Ppt 28th Jan
12/35BITS Pilani, Pilani Campus
Examplen Power of 2
1 2 3 4 5 6 7 8
q = 462317425
1 2 3 4
7425
5 6 7 8
6231
1 2
25
3 4
74
5 6
31
7 8
62
1
5
2
2
3
4
4
7 1
6
3
7
2
8
6
5
Example
http://c/Documents%20and%20Settings/MCADEPARTMENT/Local%20Settings/Temporary%20Internet%20Files/Content.IE5/27PAKPBG/MergeSort.ppthttp://c/Documents%20and%20Settings/MCADEPARTMENT/Local%20Settings/Temporary%20Internet%20Files/Content.IE5/27PAKPBG/MergeSort.ppt -
8/2/2019 Daa Ppt 28th Jan
13/35BITS Pilani, Pilani Campus
Examplen Power of 2
1
5
2
2
3
4
4
7 1
6
3
7
2
8
6
5
1 2 3 4 5 6 7 8
76543221
1 2 3 4
7542
5 6 7 8
6321
1 2
52
3 4
74
5 6
31
7 8
62
-
8/2/2019 Daa Ppt 28th Jan
14/35BITS Pilani, Pilani Campus
Examplen Not a Power of 2
62537416274
1 2 3 4 5 6 7 8 9 10 11
q = 6
416274
1 2 3 4 5 6
62537
7 8 9 10 11
q = 9q = 3
274
1 2 3
416
4 5 6
537
7 8 9
62
10 11
74
1 2
2
3
16
4 5
4
6
37
7 8
5
9
2
10
6
11
4
1
7
2
6
4
1
5
7
7
3
8
-
8/2/2019 Daa Ppt 28th Jan
15/35BITS Pilani, Pilani Campus
Examplen Not a Power of 2
77665443221
1 2 3 4 5 6 7 8 9 10 11
764421
1 2 3 4 5 6
76532
7 8 9 10 11
742
1 2 3
641
4 5 6
753
7 8 9
62
10 11
2
3
4
6
5
9
2
10
6
11
4
1
7
2
6
4
1
5
7
7
3
8
74
1 2
61
4 5
73
7 8
-
8/2/2019 Daa Ppt 28th Jan
16/35BITS Pilani, Pilani Campus
Input: Array Aand indices p, q, rsuch that p q < r Subarrays A[p . . q] and A[q + 1 . . r] are sorted
Output: One single sorted subarray A[p . . r]
Merging
1 2 3 4 5 6 7 8
63217542
p rq
-
8/2/2019 Daa Ppt 28th Jan
17/35BITS Pilani, Pilani Campus
Idea for merging:
Two piles of sorted cards
Choose the smaller of the two top cards
Remove it and place it in the output pile Repeat the process until one pile is empty
Take the remaining input pile and place it face-down onto the output pile
Merging
-
8/2/2019 Daa Ppt 28th Jan
18/35BITS Pilani, Pilani Campus
MERGE(A, 9, 12, 16)
p r
-
8/2/2019 Daa Ppt 28th Jan
19/35BITS Pilani, Pilani Campus
Example: MERGE(A, 9, 12, 16)
-
8/2/2019 Daa Ppt 28th Jan
20/35BITS Pilani, Pilani Campus
Example (cont.)
-
8/2/2019 Daa Ppt 28th Jan
21/35BITS Pilani, Pilani Campus
Example (cont.)
-
8/2/2019 Daa Ppt 28th Jan
22/35BITS Pilani, Pilani Campus
Example (cont.)
Done!
-
8/2/2019 Daa Ppt 28th Jan
23/35
BITS Pilani, Pilani Campus
Alg.:MERGE(A, p, q, r)
1. Compute n1and n2
2. Copy the first n1 elements into L[1 . . n1 + 1] and the next n2elements into R[1 . . n2 + 1]
3. L[n1 + 1]; R[n2 + 1]
4. i 1; j 1
5. for k pto r
6. do if L[ i ] R[ j ]7. then A[k] L[ i ]
8. ii + 1
9. else A[k] R[ j ]
10. j j + 1
Merge - Pseudocode
p q
7542
6321
rq + 1
L
R
1 2 3 4 5 6 7 8
63217542
p rq
n1 n2
-
8/2/2019 Daa Ppt 28th Jan
24/35
BITS Pilani, Pilani Campus
Initialization (copying into temporary arrays):
(n1 + n2) = (n)
Adding the elements to the final array (the last for loop):
n iterations, each taking constant time (n)
Total time for Merge:
(n)
Running Time of Merge
-
8/2/2019 Daa Ppt 28th Jan
25/35
BITS Pilani, Pilani Campus
The recurrence is based on the three steps of theparadigm: T(n) running time on a problem of size n
Divide the problem into a subproblems, each of size n/b: takes D(n)
Conquer (solve) the subproblems aT(n/b)
Combine the solutions C(n)
(1) if n c
T(n) = aT(n/b) + D(n) + C(n) otherwise
Analyzing Divide-and ConquerAlgorithms
-
8/2/2019 Daa Ppt 28th Jan
26/35
BITS Pilani, Pilani Campus
Divide: compute qas the average of pand r:D(n) = (1)
Conquer:
recursively solve 2 subproblems, each of size n/22T (n/2)
Combine:
MERGE on an n-element subarray takes (n) timeC(n) = (n)(1) if n =1
T(n) = 2T(n/2) + (n) if n > 1
MERGE-SORT Running Time
-
8/2/2019 Daa Ppt 28th Jan
27/35
BITS Pilani, Pilani Campus
T(n) = c if n = 12T(n/2) + cn if n > 1
Use Masters Theorem:
Compare n with f(n) = cn
Case 2: T(n) = (nlgn)
Solve the Recurrence
-
8/2/2019 Daa Ppt 28th Jan
28/35
BITS Pilani, Pilani Campus
A way to describe behavior of functions in the limit
How we indicate running times of algorithms
Describe the running time of an algorithm as n grows to
O notation: asymptotic less than: f(n) g(n)
notation: asymptotic greater than: f(n) g(n)
notation: asymptotic equality: f(n) = g(n)
Asymptotic Notations
-
8/2/2019 Daa Ppt 28th Jan
29/35
BITS Pilani, Pilani Campus
For each of the following pairs of functions, either f(n) isO(g(n)), f(n) is (g(n)), or f(n) is (g(n)). Determine
which relationship is correct. f(n) = log n2; g(n) = log n + 5
f(n) = n; g(n) = log n2
f(n) = log log n; g(n) = log n
f(n) = n; g(n) = log2 n
Asymptotic Notations -Examples
f(n) = (g(n))
f(n) = (g(n))
f(n) = O(g(n))
f(n) = (g(n))
-
8/2/2019 Daa Ppt 28th Jan
30/35
BITS Pilani, Pilani Campus
Intuitively: O(g(n)) = the set of
functions with a smaller or
same order of growth as g(n)
Asymptotic notations
O-notation
-
8/2/2019 Daa Ppt 28th Jan
31/35
BITS Pilani, Pilani Campus
3n + 2 = O(n) ; 3n + 2 = 2
3n + 3 = O(n) ; 3n + 3 = 3
100n + 6 = O(n) ; 100n + 6 = 6
Examples
-
8/2/2019 Daa Ppt 28th Jan
32/35
BITS Pilani, Pilani Campus
3n + 2 = O(n) ; 3n + 2 = 2
3n + 3 = O(n) ; 3n + 3 = 3
100n + 6 = O(n) ; 100n + 6 = 6
10 n 2 + 4n + 2
Examples
= O(n2)
10 n 2 + 4n + 2 < = 11 n2 for n >= 5
-
8/2/2019 Daa Ppt 28th Jan
33/35
BITS Pilani, Pilani Campus
- notation
Asymptotic notations (cont.)
Intuitively: (g(n)) = the set of
functions with a larger or same
order of growth as g(n)
-
8/2/2019 Daa Ppt 28th Jan
34/35
BITS Pilani, Pilani Campus
3n + 2 = ?
3n + 3 = ?
100n + 6 = ?
Examples
3n + 2 >= 3n for all n >= 1
3n + 3 >= 3n for all n >= 1
100n + 6 >= 100n for all n >= 1
-
8/2/2019 Daa Ppt 28th Jan
35/35
-notationAsymptotic notations (cont.)
Intuitively(g(n)) = the set offunctions with the same order of
growth as g(n)
top related