1
00:00:16,600 --> 00:00:29,780
So in the last class we discuss about that
certain situation the structure and also we
2
00:00:29,780 --> 00:00:44,360
have told what is algorithm and today, I let
to consider the different paradise consequential
3
00:00:44,360 --> 00:00:51,210
or paradise we use are you assume this or
knowledge basic knowledge on these penalties
4
00:00:51,210 --> 00:00:56,530
however for completeness .I will be covering
with some of these paradise and I will be
5
00:00:56,530 --> 00:01:06,010
completed first because this is you can consider
it is a warm-up class ,so they do not pay
6
00:01:06,010 --> 00:01:12,450
any problem in understanding the parallel
algorithms.
7
00:01:12,450 --> 00:01:20,890
We will be discussing similar the type of
problems know that the different paradigms
8
00:01:20,890 --> 00:01:33,810
we use for sequential algorithms are 1/10.
9
00:01:33,810 --> 00:01:47,250
All clear this is one way little paradise
we use it the next one is so Greedy method
10
00:01:47,250 --> 00:02:11,970
third one is dynamic programming for bunnies
tracking fifth one is branch and bound.
11
00:02:11,970 --> 00:02:52,970
Then you have linear programming in integer
programming and so on, we will try to discuss
12
00:02:52,970 --> 00:03:05,740
in detail at least these three paradigms right
.we can also discuss about this back end backtracking
13
00:03:05,740 --> 00:03:14,670
and branch-and-bound paradise time partners
now let us consider the first paradise which
14
00:03:14,670 --> 00:03:26,950
is divide and conquer what happens here actually
suppose you have a problem P.
15
00:03:26,950 --> 00:03:37,170
And that you want to solve this problem P
what we will do in divide and conquer you
16
00:03:37,170 --> 00:03:52,170
divide this P problem P into several sub problems
P 1, P 2, P 3, P K ,okay there are K sub problems
17
00:03:52,170 --> 00:04:00,490
we can divide this P problem into several
sub problems and these sub problems are having
18
00:04:00,490 --> 00:04:12,511
the similar properties as we have in the case
of P so now I solve this problem P 1 problem
19
00:04:12,511 --> 00:04:20,630
p2 problem P3 and problem PK and then I combine
the results to get the solution of P is it
20
00:04:20,630 --> 00:04:26,630
okay that what we do that we have the K sub
problems this half problem each sub problem
21
00:04:26,630 --> 00:04:34,020
is pollination with 2p.
Now we try to solve this P 1 P 2 P K and if
22
00:04:34,020 --> 00:04:39,810
I can solve this then you combine this SL
to get the solution of P now it may, so happen
23
00:04:39,810 --> 00:04:46,500
that are into PK they are all so large in
size the problem is not that simple problem
24
00:04:46,500 --> 00:04:57,060
then you divide these sub problems into further
sub problems where we can have P 1 P 1 1 P
25
00:04:57,060 --> 00:05:06,600
1 2 P 1 3 and P 1 suppose n 1 similarly the
case with P 2 and so on now you solve this
26
00:05:06,600 --> 00:05:15,030
problems p11 p12 p13 and P 1and I will combine
this to get the solution of P 1 and so on
27
00:05:15,030 --> 00:05:23,800
okay know say for example.
We want to find out the sum of N numbers now
28
00:05:23,800 --> 00:05:38,820
by observation process now look at the problem
that I have X 1 X 2 X X3 Xn.
29
00:05:38,820 --> 00:05:51,370
This n elements and I want to point out the
sum of N numbers these numbers so one way
30
00:05:51,370 --> 00:06:08,210
I can think that I divide this two part right
see I want to find out the sum know that instead
31
00:06:08,210 --> 00:06:17,600
of finding the sum of N numbers what I do
here is now if I sum (A 1.,n/2) this side
32
00:06:17,600 --> 00:06:37,860
I find sum (A,n/2+1) you really end by two
elements now I got the numbers okay so if
33
00:06:37,860 --> 00:06:40,980
I have to write this in the form of algorithms.
34
00:06:40,980 --> 00:07:25,320
I can write sum (A (l,u)) if l = u _ 1 then
return.
35
00:07:25,320 --> 00:08:28,620
A(l) + A(u) else returned sum(A(l, M =(l+u)/2
and then the return sum A(l) + m + sum (A(m)+1
36
00:08:28,620 --> 00:08:50,180
okay so basically you are computing this l
+ u and then you return this sum and then
37
00:08:50,180 --> 00:09:02,949
+ this one so if I have to compute the time
complexity for that then I have T n = 0 if
38
00:09:02,949 --> 00:09:28,630
n is =l 1 ,1 if n = 2 otherwise 2 TN / 2 +
1 otherwise, so you have to solve this when
39
00:09:28,630 --> 00:09:47,490
this one is coming that one addition and this
is sizes half-size so 2 TN / 2 so Tn comes
40
00:09:47,490 --> 00:10:13,110
Tn/2 + one 2tn/4 +1 so it gives you basically
Tn /4 +2+1 is it correct next time.
41
00:10:13,110 --> 00:10:56,430
I had to square tn/8 +1+2+1 so I get 23 tn/8
+2 +2 +1 so if n =2 K K I get here 2 K _ 1+Tn-2+1
42
00:10:56,430 --> 00:11:17,649
now this is t2 is 1 K _ 1 so this gives you
2 k _ 1+ 2 K _ .
43
00:11:17,649 --> 00:11:29,260
So this gives you 2k -1 2-1which is nothing
but n _ 1 okay, because 2 K is n so you get
44
00:11:29,260 --> 00:11:35,300
n _ 1 and which is the reality is also true
that to find the sum of N numbers you need
45
00:11:35,300 --> 00:11:46,209
n _ 1 okay so this is the first simple problem
no you move the binary touch let us think
46
00:11:46,209 --> 00:11:56,160
about the binary touch what happens in the
bind the case of binary search we assume that
47
00:11:56,160 --> 00:12:02,800
element either in descending order or decreasing
order.
48
00:12:02,800 --> 00:12:21,509
Suppose you have an A1.A2 A3, Al and an argument
K you have your interest with element K exists
49
00:12:21,509 --> 00:12:27,249
in this sequence of n elements or not now
it is a show the elements are in increasing
50
00:12:27,249 --> 00:12:32,560
order so in the case of binary search what
we do.
51
00:12:32,560 --> 00:12:44,319
We divide this n elements into we compare
the K with the middle elements it has the
52
00:12:44,319 --> 00:12:51,899
property that if it is in increasing order
then this element is less than equals to this
53
00:12:51,899 --> 00:12:58,269
distinguished and so on so we will be comparing
the middle element with the K now if I find
54
00:12:58,269 --> 00:13:12,300
that K 00:13:19,920
if they exist at all then it will be lying
here so in that case in that case my searching
56
00:13:19,920 --> 00:13:27,629
loan will be reduced to this part and we can
easily discard all the elements of my life
57
00:13:27,629 --> 00:13:34,970
from my searching area.
Now the searching reduced to this and again
58
00:13:34,970 --> 00:13:43,170
I take the middle element and I compare whether
K < equal to or greater than if it is equal
59
00:13:43,170 --> 00:13:52,860
to M or the middle element we tell that element
exists and you got the value m. Now if we
60
00:13:52,860 --> 00:14:01,920
find that K is greater than this side then
K lies in this job so my searching you will
61
00:14:01,920 --> 00:14:10,440
be reduced to this and so on till we process
this one till I come I find that there exists
62
00:14:10,440 --> 00:14:19,339
only one element and you compare and I found
either exist equal to or not equal okay now
63
00:14:19,339 --> 00:14:23,480
in that case if I have to estimate the time
complexity for that then.
64
00:14:23,480 --> 00:14:45,540
It becomes TN is you will be comparing1 if
n is >1 and if n=1 then only one now solution
65
00:14:45,540 --> 00:14:57,269
of this becomes order log n now if I have
to do or I write the genetic algorithms for
66
00:14:57,269 --> 00:15:03,569
divide and conquer and with the understanding
that we will be dividing the problem into
67
00:15:03,569 --> 00:15:07,290
the two sub problems.
68
00:15:07,290 --> 00:15:24,879
So I write D and C the problem P and size
is a small (P (p,q) ) so this is the problem
69
00:15:24,879 --> 00:15:59,319
from the size of P and Q now if problem P(p,q))
is small then solve else M = p+ q /2 and then
70
00:15:59,319 --> 00:16:19,329
you call divide and conquer P(p,m) x =y=/
conquer P(m + 1 q) and then combine the result
71
00:16:19,329 --> 00:16:38,119
for combine the result (x,y) so this is the
generic algorithm for divide and conquer if
72
00:16:38,119 --> 00:16:41,199
I have to see the time complexity Tn.
73
00:16:41,199 --> 00:17:03,459
Is = say F (n) if n is small else this 2 T
n / 2 + some another function g(l) this g(l)
74
00:17:03,459 --> 00:17:15,390
is for the time you need to combine is to
realize.
75
00:17:15,390 --> 00:17:24,459
This is an example of divided paradigms suppose
you have x1,x2,xn are M elements and they
76
00:17:24,459 --> 00:17:33,500
are not in sorted order you want to arrange
them in ascending order or descending order
77
00:17:33,500 --> 00:17:38,330
look at the issue without any loss of generality
we want to arrange them in ascending order
78
00:17:38,330 --> 00:17:46,389
so the merge sort is a technique to arrange
this n elements in either in ascending order
79
00:17:46,389 --> 00:17:56,889
or descending order this C algorithm becomes
very simple see if I write merge sort and
80
00:17:56,889 --> 00:18:22,169
you have X (L.. ,u)) then you can write M
=(l n + u / 2 then you write merge sort (x(l,m))
81
00:18:22,169 --> 00:18:53,990
merge sort (x(m+1.u)).
Then you have merge (x,(l,m+ 1,u) what it
82
00:18:53,990 --> 00:19:00,841
means that basically you have n elements you
have n elements and you are dividing into
83
00:19:00,841 --> 00:19:02,130
the two parts.
84
00:19:02,130 --> 00:19:05,810
You are recursively you are doing the master
of this accessible you are doing the master
85
00:19:05,810 --> 00:19:14,889
of this and then this is in increasing order
this is also increasing order you are then
86
00:19:14,889 --> 00:19:23,389
combining these two results to get the result
is in increasing order okay, so M is the middle
87
00:19:23,389 --> 00:19:28,460
of this we are dividing this n elements into
the two equal part we are making the merge
88
00:19:28,460 --> 00:19:32,309
sort algorithm recursively you are calling
this part mark sort algorithm calling this
89
00:19:32,309 --> 00:19:41,390
side then you are merging these two to get
the results. We will be discussing how to
90
00:19:41,390 --> 00:19:47,520
merge okay, if it is the case then my time
complexity becomes.
91
00:19:47,520 --> 00:20:03,240
Tn = 2 times Tn/ 2 + the time you need time
you need 2merge time you need to much agree
92
00:20:03,240 --> 00:20:06,120
now let us think about how to merge.
93
00:20:06,120 --> 00:20:24,070
You have a1 2 a 3 a X and you have b 1 b 2
b 3 suppose you want merge this two to sequence
94
00:20:24,070 --> 00:20:32,610
with the understanding that this is satisfying
this criteria and you want to merge these
95
00:20:32,610 --> 00:20:42,009
two sorted sequence so what I do I compared
a 1 we take to b 1 if you find that you want
96
00:20:42,009 --> 00:20:49,840
is smaller than b 1 then even is the smallest
element which is I write a 1 now once you
97
00:20:49,840 --> 00:20:56,850
have selected a walk then pointer moves to
this area and I know I compared it to it b1
98
00:20:56,850 --> 00:20:59,950
you are and if you find estimate we smaller
than b 1.
99
00:20:59,950 --> 00:21:09,200
Then you write in the position of C 1 and
the point opposed to it please know it is
100
00:21:09,200 --> 00:21:19,110
comparable b 1 and if you find that b 1 is
smaller than smaller than a 3 then b 1 is
101
00:21:19,110 --> 00:21:28,830
coming and pointer moves to this place okay,
and proceed till one of the array already
102
00:21:28,830 --> 00:21:36,080
one of the sequence is already transferred
to C sequence and the remaining elements take
103
00:21:36,080 --> 00:21:53,880
you to see final mark sequence now here. I
have basically I have one sequence I have
104
00:21:53,880 --> 00:21:55,000
one sequence.
105
00:21:55,000 --> 00:22:14,809
X 1 x 2 x XL XL + 1 X M then you have XM +
1 XM + 2 you have you this is satisfying this
106
00:22:14,809 --> 00:22:22,139
property that this is an increasing order
this is also increasing order now we have
107
00:22:22,139 --> 00:22:50,409
to model it so I can write merge (x ,l,m+1
,u) you or for me mid and then you write
108
00:22:50,409 --> 00:23:16,820
I write I = l,j=mid and K the pointer for
merge sequence which is L so I will be moving
109
00:23:16,820 --> 00:23:28,259
this pointer I and J and we will see as long
as long as the whole sequence is considered
110
00:23:28,259 --> 00:23:58,650
you have to is not yet considered we have
to do these operations.
111
00:23:58,650 --> 00:24:14,411
So while (i 00:24:28,059
found < X < (j) then this sequence contains
the smaller elements and that has to remove
113
00:24:28,059 --> 00:24:58,630
two sequence say Y( K) =x (i);i++; is k
is > this so you will be writing is Y(K) okay
114
00:24:58,630 --> 00:25:11,110
and in both the cases your k will be moved
into by1 okay this is the things you have
115
00:25:11,110 --> 00:25:21,259
to do that if you find that X (i)< X(j ) then
Y(K) is updated by X (a) X (i) I is increasing
116
00:25:21,259 --> 00:25:31,659
by 1 then otherwise YK is updated by anyway
that you have to update the YC k to get the
117
00:25:31,659 --> 00:25:37,250
next position of Y. Now in that process what
yet may happens that one sequence.
118
00:25:37,250 --> 00:25:48,570
Is already entered into Y sequence and some
part of other sequence y sequence the remaining
119
00:25:48,570 --> 00:25:59,379
part is yet to be shifted to Y okay so that
has to be taken into account now this cannot
120
00:25:59,379 --> 00:26:01,759
turn like that.
121
00:26:01,759 --> 00:26:21,710
If you find that I > mid ) then there exists
some elements of the second sub sequence to
122
00:26:21,710 --> 00:27:16,940
be shifted that is for L = j to u y (l) is
your x YK =Xl increasing by 1 for l=I to mid
123
00:27:16,940 --> 00:27:30,629
- 1 and (YK) =x (l) K++; okay, so basically
you have merge now x equals x sub sequence
124
00:27:30,629 --> 00:27:41,050
and this subsequence but in order to get the
result back to X you have to write a small
125
00:27:41,050 --> 00:27:42,050
statement.
126
00:27:42,050 --> 00:28:03,259
That is for I = u you have X(i) =y (i) okay
so store back to X now if I have to find out
127
00:28:03,259 --> 00:28:14,889
the complexity of this algorithm then you
observe that it takes basically here what
128
00:28:14,889 --> 00:28:24,429
missing at you what will be the worst case
scenario worst-case scenario could be that
129
00:28:24,429 --> 00:28:25,429
one element.
130
00:28:25,429 --> 00:28:32,860
You have this sub sequence and this subsequent
so you are taking one element here then next
131
00:28:32,860 --> 00:28:38,320
element then next elements ,next elements,
next elements , next element and so on so
132
00:28:38,320 --> 00:28:43,850
that many comparisons this loop will be compared
every time and which will be taking order
133
00:28:43,850 --> 00:28:55,519
L _ U basically order n u _ L basically order
and this also will take otherwise also data
134
00:28:55,519 --> 00:29:03,159
movement time also basically we fine order
routine will take order any time module it
135
00:29:03,159 --> 00:29:11,100
will take order n time. So in that case merge
sort takes the following type of or needs
136
00:29:11,100 --> 00:29:16,720
the following time tools sort.
137
00:29:16,720 --> 00:29:27,710
Then you have T n / 2 + order n this is for
merge what is the complexity now it is coming
138
00:29:27,710 --> 00:29:44,789
2 times 2 times Tn / 4 so let me write the
Tn O n is Cn
139
00:29:44,789 --> 00:30:03,809
can write CN here and here I write CN /2 agreed
so this gives you 2 2 Tn / 4 + Cn + Cn here
140
00:30:03,809 --> 00:30:25,059
which gives me 2 22 times Tn / 8 + C + / 4
+ CN + here right so this gives me 2 cube
141
00:30:25,059 --> 00:30:57,470
TN by a + 3 times C n now if n equals 2 2
to the +Cn+ Cn so this gives me 23 . Tn /a
142
00:30:57,470 --> 00:31:01,850
+3 Cn.
143
00:31:01,850 --> 00:31:07,860
Now if any considered n=2k Tn =0 okay 1 elements
nothing to be sorted okay and t2 is in that
144
00:31:07,860 --> 00:31:37,120
case Tn=2k T , 1 + K times C n this is zero
so you get C n log n times so this is matching
145
00:31:37,120 --> 00:31:46,429
methods this message with the lower order
of sorting algorithms this takes order log
146
00:31:46,429 --> 00:31:56,470
n time complexity to sort n elements using
my sort technique now another example of divide
147
00:31:56,470 --> 00:32:10,720
and conquer is Quick sort.
So you observe they in the case of merge sort
148
00:32:10,720 --> 00:32:20,450
what we do we divided in sequence and recursively
call merge sort the first part and the second
149
00:32:20,450 --> 00:32:24,760
part then you merge now in the case of Quick
Sort.
150
00:32:24,760 --> 00:32:35,169
It is little different suppose you have x
1 x2 x n now what we do that we take the first
151
00:32:35,169 --> 00:32:41,779
element or any one of them random look and
select but you call first element as the partitioning
152
00:32:41,779 --> 00:32:51,620
element and what it does this element is used
to find this position with reference to x
153
00:32:51,620 --> 00:32:56,919
n.
So if we divide or we get the exact position
154
00:32:56,919 --> 00:33:06,789
of x 1 exact position of x 1 and if I solve
if the sequence is sorted by some technique
155
00:33:06,789 --> 00:33:15,419
we partition these sequence into the two parts
in such a way that x 1 gets its position in
156
00:33:15,419 --> 00:33:26,700
the sequence satisfying the property of sorting
agreed so I get the position of x 1 here in
157
00:33:26,700 --> 00:33:32,029
such way that all these elements here are
smaller than this elements and all these elements
158
00:33:32,029 --> 00:33:40,490
here are larger than these elements okay that
means what we select 1 element say first element
159
00:33:40,490 --> 00:33:46,679
which we term as a partitioning element .
And this element gets not only gets its position
160
00:33:46,679 --> 00:33:54,610
in the sorted sequence also all the elements
towards it right are of its right are larger
161
00:33:54,610 --> 00:34:00,190
than this element and of his level smaller
than this elements then you recursively call
162
00:34:00,190 --> 00:34:10,180
the quick sort of this part finally when you
have one element know how to partition.
163
00:34:10,180 --> 00:34:38,560
I have a sequence partition say A, m and u
what is being that you have I put a pointer
164
00:34:38,560 --> 00:34:53,460
here now a m is compared with the first element
if you find that this is smaller than or equal
165
00:34:53,460 --> 00:34:57,330
you retain it as it is we move the pointer
here basically this element is compared with
166
00:34:57,330 --> 00:35:02,740
this you find that these elements smaller
than here you go to the next points literally
167
00:35:02,740 --> 00:35:13,890
if you find that this element is smaller than
this element we go to the next one now if
168
00:35:13,890 --> 00:35:19,820
you find that this element is larger than
this element then you stop here .
169
00:35:19,820 --> 00:35:24,860
Now you start searching from the side now
if you find that this element is larger than
170
00:35:24,860 --> 00:35:29,450
a go to the next one if you find that this
element is larger than m then go to the next
171
00:35:29,450 --> 00:35:35,210
one and so on finally if you will be getting
a position where this element will be you
172
00:35:35,210 --> 00:35:41,570
may get the position but this element is smaller
than this one so you interchange this and
173
00:35:41,570 --> 00:35:51,600
then you proceed again okay so finally you
will get the position where this has crossed
174
00:35:51,600 --> 00:35:54,210
over.
So you will get it this element with this
175
00:35:54,210 --> 00:36:03,090
one and so you will find at the end and all
these elements are smaller than this element
176
00:36:03,090 --> 00:36:15,720
and since if I consider the size is m then
I need n + 1 okay so this is the idea of partitioning
177
00:36:15,720 --> 00:36:28,080
element but there is idea of partitioning
algorithms okay which takes n +1 unit of time
178
00:36:28,080 --> 00:36:30,770
if I have to write the algorithm .
179
00:36:30,770 --> 00:36:51,380
Then I can write this way say v=A(m) and i
= m okay
180
00:36:51,380 --> 00:37:32,220
then do I is increased by 1 until A (i) = v
do u is decreased by one until A(u) = V if
181
00:37:32,220 --> 00:38:06,770
i < u then interchange A(i),A(u) after doing
this you repeat till there is a crossover
182
00:38:06,770 --> 00:38:13,760
.
Okay that is I
183
00:38:13,760 --> 00:38:40,110
is while I = n you do all these things okay
and then you have A(m) = A(u), A(n)= v okay
184
00:38:40,110 --> 00:38:52,640
so this is your partitioning elements partitioning
algorithms this takes order n + 1 unit time
185
00:38:52,640 --> 00:38:59,410
to do the part to get the partitioning possible
to get the position of x1 in a subsequence
186
00:38:59,410 --> 00:39:04,180
of n now if I have to write the quick sort
algorithm .
187
00:39:04,180 --> 00:39:23,501
So you have quick sort (A, p, q) that is we
are looking for to sort A(p) here one do you
188
00:39:23,501 --> 00:39:40,540
remember I forgot to mention that a u is u
-1 that is we are partitioning the sequence
189
00:39:40,540 --> 00:40:17,480
from a.m. to a u-1 one okay if p< q then j=q+1
and then I call partition A, p and j then
190
00:40:17,480 --> 00:40:34,760
we call quick sort A, p and j-1 because that
means the position 1st position then you get
191
00:40:34,760 --> 00:41:25,580
with quick sort A, j+1 q that as long as queue
you do these operations .
192
00:41:25,580 --> 00:41:35,950
So this quick sort algorithm is worst can
you tell me why you observe that this complexity
193
00:41:35,950 --> 00:41:48,940
is dependent on the partition technique so
we are assuming that assuming that the j is
194
00:41:48,940 --> 00:42:00,200
here and it so happens all the elements of
this part are larger than this then this quick
195
00:42:00,200 --> 00:42:16,240
sort will not have any meaning because the
elements to be sorted by this so again next
196
00:42:16,240 --> 00:42:24,330
you may have in all these elements.
197
00:42:24,330 --> 00:42:37,080
So in that case what happens if Tn is the
time complexity then you need order n times
198
00:42:37,080 --> 00:42:50,000
to partition and size is not reviews only
one element is one side and the other side
199
00:42:50,000 --> 00:43:02,260
there is nothing no elements right this side
no element you have n -1 elements so you have
200
00:43:02,260 --> 00:43:15,950
Cn + T n- 1+ T 0 is 0 so you get next time
T n-2 + C (n-1)+ C n right next time again
201
00:43:15,950 --> 00:43:57,010
no elements in one side so you get T n-3 +
C (n-2)+ C (n-1)+ C n so you have n -1 this
202
00:43:57,010 --> 00:44:14,040
is 1 means T1 is 0 so you get C 2 + 3 + then
which is order n square .
203
00:44:14,040 --> 00:44:20,950
Okay so in the worst case it becomes order
n square now in reality what it means that
204
00:44:20,950 --> 00:44:27,580
when the elements are in increasing order
or decreasing order you will find that partition
205
00:44:27,580 --> 00:44:34,220
routine will give you will not give you the
good partition it will find that one side
206
00:44:34,220 --> 00:44:39,440
you have n -1 elements the other side you
do not have one any element so the complexity
207
00:44:39,440 --> 00:44:46,520
in the worst way becomes order n square now
what is the best case can you tell me the
208
00:44:46,520 --> 00:45:02,890
best possible case is when the partitioning
element parts in that case.
209
00:45:02,890 --> 00:45:20,000
The time complexity becomes C n into 2 times
T n/2 if it is CN + 2 T n/ 2 so I can write
210
00:45:20,000 --> 00:45:40,860
CN plus 2 times C n/2 + 2 times T n/4 and
this becomes C n+ C n +2 2 T n/4 right and
211
00:45:40,860 --> 00:45:55,120
you can show this is nothing but C n log n
so in the best case it is order n log n and
212
00:45:55,120 --> 00:46:07,650
in the worst case it is order N squared .Now
let us try to find out the average time complexity
213
00:46:07,650 --> 00:46:14,450
whatever is number of comparisons you need
in the push sort.
214
00:46:14,450 --> 00:46:25,300
Let us assume that T A (n) is the average
time complexity this is nothing but C n is
215
00:46:25,300 --> 00:46:54,200
the time for the partitioning + 1/n actually
to be exact it is n +1 and it is 1 +? T A
216
00:46:54,200 --> 00:47:13,220
(k) it should be K -1 T A (n-k) this is K
- 1 K is 1 to n what it means that you are
217
00:47:13,220 --> 00:47:22,650
partitioning it this is K so if it is K you
have n elements so this side is n - K this
218
00:47:22,650 --> 00:47:32,930
side you have K -1 and K can be 1 K can be
1 2 and K can be 1 to n okay so that is the
219
00:47:32,930 --> 00:47:39,720
time complexity average why 1 / a that we
are assuming they are equally likely so that
220
00:47:39,720 --> 00:47:50,770
is why it is 1 /n.
So this gives you basically n times T A (n)
221
00:47:50,770 --> 00:48:18,550
= n (n+1)+ TA + TA(1) +TA (n- 1) + T n- 1
this
222
00:48:18,550 --> 00:49:26,830
is nothing but n (n +1) 2 times ? TA (i) I
is 1 to n-1 because TA is zero does not have
223
00:49:26,830 --> 00:49:43,160
any meaning not only TA also does not have
any TA(1) does not have any meaning
224
00:49:43,160 --> 00:49:55,130
so I take i=n-1 now I want to solve this you
have to solve these let us replace n by n
225
00:49:55,130 --> 00:50:19,870
- 1 what I get n - 1 TA n - 1 = n - 1 + 2
times I= 2 to n - 2 T A (i) .
226
00:50:19,870 --> 00:50:49,810
Now if I subtract this I will get n TA(n)
- n - 1 TA (n- 1) here I
227
00:50:49,810 --> 00:51:13,720
get n square get cancel 2 n and here I get
+ 2 times TA (n- 1) okay.
228
00:51:13,720 --> 00:51:38,250
So if I bring this element this side n TA(n)
= 2 n + (n +1) TA (n- 1) now dividing both
229
00:51:38,250 --> 00:51:56,530
sides by n 2 n - 1 n + 1 that is I get TA(n)
/ n + 1 + 2 n -1 this gives me 2 ? over 1
230
00:51:56,530 --> 00:52:29,130
/k is three times so +TA so if I
231
00:52:29,130 --> 00:52:47,240
write here 3 this becomes 1 this becomes 2
okay so TA(1) is 0 so this is equal to 2 times
232
00:52:47,240 --> 00:53:07,410
? K = to 3 to n + 1 1 /k this is very first
2 times integration 2 to n+1 1 /k d k .
233
00:53:07,410 --> 00:53:31,980
And that is equal to 2 times log k and n +
2 that is 2 times log (n+1)- log 2 so that
234
00:53:31,980 --> 00:53:50,140
is TA(n) =
= n + 1 into 2 times log (n+1)- log 2 which
235
00:53:50,140 --> 00:54:01,850
is order n log n so every time complexity
for using the quick sort technique is on an
236
00:54:01,850 --> 00:54:12,740
longer see this I could have avoided it but
I felt that I should tell you how to compute
237
00:54:12,740 --> 00:54:16,750
the average time complexity of an algorithm
.
238
00:54:16,750 --> 00:54:25,460
Now let us consider another example the last
example today and hopefully you have been
239
00:54:25,460 --> 00:54:48,860
taught that finding the kth smallest element
of n element this is basically due to blums
240
00:54:48,860 --> 00:55:02,600
technique and I think do you know any algorithm
of such no right so let us consider this algorithm
241
00:55:02,600 --> 00:55:12,570
and let us try to understand how this has
been designed right see one way is that you
242
00:55:12,570 --> 00:55:19,470
find when you find a second minimum element
then you find a third minimum element and
243
00:55:19,470 --> 00:55:21,040
so on.
244
00:55:21,040 --> 00:55:26,990
Let us think from this point of view that
you have the n elements this n elements are
245
00:55:26,990 --> 00:55:46,130
divided into n by seven groups and each group
is having the seven element
246
00:55:46,130 --> 00:55:57,410
seven and you can pad it by a large numbers
so you divided
247
00:55:57,410 --> 00:56:24,150
n by seven groups
248
00:56:24,150 --> 00:56:33,470
and each group is having seven elements so
this side you have n by seven groups okay
249
00:56:33,470 --> 00:56:46,640
and the seven elements know you can solve
these seven elements of each group right so
250
00:56:46,640 --> 00:57:00,390
you solve each group in constant time so they
are n by seven
251
00:57:00,390 --> 00:57:12,920
no this is your median element okay now find
out instead of finding the kth element you
252
00:57:12,920 --> 00:57:22,340
find the median of this n by seven medians.
And recursively you can call that one finding
253
00:57:22,340 --> 00:57:32,300
the median of the end by seven medians right
as you are calling now this median let us
254
00:57:32,300 --> 00:57:42,290
call it m this is the median of median now
this median is used to partition this whole
255
00:57:42,290 --> 00:57:50,140
sequence into three groups one all the elements
smaller than this another group is all the
256
00:57:50,140 --> 00:58:02,200
elements equal to this and another elements
larger than this okay so basically you will
257
00:58:02,200 --> 00:58:14,880
be partitioning three groups one group all
the elements all the elements smaller than
258
00:58:14,880 --> 00:58:23,130
this .
Now if you observe that if the number of elements
259
00:58:23,130 --> 00:58:28,050
smaller than this smaller than this median
element if you find is more than k then kth
260
00:58:28,050 --> 00:58:37,250
element is lying here and if you find no all
the element this size of the size of this
261
00:58:37,250 --> 00:58:47,080
number of elements this is smaller than K
but if I find the number of elements equal
262
00:58:47,080 --> 00:58:58,280
to median number of elements such that this
element is equal to K is equal to the mid
263
00:58:58,280 --> 00:59:03,750
number of elements which are equal to the
median elements + number of elements less
264
00:59:03,750 --> 00:59:17,480
than equals to the median we find that is
greater than K .
265
00:59:17,480 --> 00:59:26,890
So you reduce the searching zone accordingly
now one thing I can assure you that at least
266
00:59:26,890 --> 00:59:38,420
25% of the size of the end can be discarded
at any instant of time at any iterations this
267
00:59:38,420 --> 00:59:45,200
is because of the fact that because of the
fact that all these elements will be smaller
268
00:59:45,200 --> 00:59:52,030
than this element and all these elements will
be larger than this element now here this
269
00:59:52,030 --> 01:00:00,940
is the median so this is you have the half
of the that is n /14 elements smaller than
270
01:00:00,940 --> 01:00:07,140
this and this side is n /14 larger than this
.
271
01:00:07,140 --> 01:00:12,310
Now this side again that half of this side
will be larger than the smaller than this
272
01:00:12,310 --> 01:00:19,560
half of this smaller than this half of this
so this is giving you the 25% element smaller
273
01:00:19,560 --> 01:00:28,500
at this at least larger than this and you
cannot draw any conclusion on this part so
274
01:00:28,500 --> 01:00:38,340
it can give you the reference that at least
25% of the of n elements will be destroyed
275
01:00:38,340 --> 01:00:45,460
at any instant in order to find the complexity
if I have the T and K is the time complexity
276
01:00:45,460 --> 01:00:48,250
to find the K element from the N elements
.
277
01:00:48,250 --> 01:00:54,380
(Refer Slide Time: 1:00:48)
So first you may by doing the sorting of each
278
01:00:54,380 --> 01:01:03,610
group there are there n /7 groups so time
needs order n /7 and after that you will be
279
01:01:03,610 --> 01:01:13,910
calling the median of medians elements so
basically you have n /7 and K is n / 14 and
280
01:01:13,910 --> 01:01:23,230
after finding that you will be discarding
25% so sizing becomes T 3 n/4 and then K okay
281
01:01:23,230 --> 01:01:33,520
and
the solution of this is alright I am not discussing
282
01:01:33,520 --> 01:01:39,650
how to solve it because it is a two dimensional
recurrence relation which I do not want to
283
01:01:39,650 --> 01:01:43,210
discuss in this class .
Another example for the divide-and-conquer
284
01:01:43,210 --> 01:01:50,760
which you can try yourselves which is matrix
here I will not discuss in details suppose
285
01:01:50,760 --> 01:01:56,950
you have the 2 matrix and you partition it
to the two parts so this is suppose you have
286
01:01:56,950 --> 01:02:13,480
A11 this is sub matrix of A1 2 A2 1 and you
have B11 B12 B 2 1 and B 2 2 then C11 is compute
287
01:02:13,480 --> 01:02:36,790
can be computed by computing that A11 B12
and soon you can try it at home in the next
288
01:02:36,790 --> 01:03:01,110
class we will be discussing about dynamic
programming.