1
00:00:16,600 --> 00:00:23,120
So in the last class, we discuss about that
certain sequential data structure and also
2
00:00:23,120 --> 00:00:30,120
you have told, what is the algorithm and data
structures. So, today I like to consider,
3
00:00:33,070 --> 00:00:38,820
the different paradigms on sequential or paradigms
we use for sequential are going to consider.
4
00:00:38,820 --> 00:00:40,000
I assume that you have the base knowledge,
basic knowledge on these paradigms however,
5
00:00:40,000 --> 00:00:47,000
for completeness saying I will be covering
this, some of this paradigms. And I will be
6
00:00:48,199 --> 00:00:55,199
little fast because this is an why, you can
consider it is an warm up or class, so that
7
00:00:57,489 --> 00:01:04,489
do not take any problem in understanding the
parallel algorithms you will be discussing
8
00:01:05,850 --> 00:01:09,700
similar type of problems.
9
00:01:09,700 --> 00:01:16,700
Now, the
the different paradigms we use for sequential
algorithms are one divide and, divide and
10
00:01:29,439 --> 00:01:36,439
conquer, this is one when notepad is, we use
it. The next one is the greedy method, third
11
00:01:50,039 --> 00:01:57,039
one is dynamic programming, fourth one is
back tracking fifth one is branch and bound.
12
00:02:19,470 --> 00:02:26,470
Then, we have linear programming, integer
programming and so on, we try to discuss in
13
00:02:52,980 --> 00:02:59,980
detail at least the these three paradigms
right. And if we can also then discuss about
14
00:03:05,100 --> 00:03:10,540
this back, back tracking and, branch and bound
paradigms, if time permits.
15
00:03:10,540 --> 00:03:17,540
So, now let us consider our first paradigm
divide and conquer, what happens here actually
16
00:03:23,430 --> 00:03:30,430
suppose, here a problem P and that, you want
to solve this problem P.
17
00:03:33,930 --> 00:03:40,930
What we will do in divide and conquer, you
divide this P, problem P into several sub
18
00:03:43,120 --> 00:03:50,120
problems P 1, P 2, P 3, P k. There are k sub
problems, you can divide this P problem into
19
00:03:54,060 --> 00:04:01,060
several sub problems and these sub problems
are having the similar properties, as we have
20
00:04:03,510 --> 00:04:10,510
in the case of P. Now, I will solve this problem
P 1, problem P 2 and problem P 3 and problem
21
00:04:14,640 --> 00:04:21,640
P k, and then I combine the results to get
the solution of P, is it ok. Then, what we
22
00:04:21,970 --> 00:04:26,710
do, that we have the k sub problems, these
sub problems, each sub problem is parallel
23
00:04:26,710 --> 00:04:33,710
in nature with reference to P. Now, we try
to solve this P 1, P 2, P k and if I can solve
24
00:04:34,750 --> 00:04:38,010
this, then you combine result to get the solution
of P.
25
00:04:38,010 --> 00:04:45,010
Now, it may so happen that, P 1, P 2 and P
k they are also large in size, the problem
26
00:04:45,070 --> 00:04:52,070
is not that simple problem. Then, you divide
these sub problems into further sub sub problems,
27
00:04:53,810 --> 00:05:00,810
where we can have P 1, P 1 1, P 1 2, P 1 3,
P 1 suppose, n 1 similarly, the case with
28
00:05:03,030 --> 00:05:10,030
P 2 and so on. Now, you solve this problems
P 1 1, P 1 2, P 1 3, P 1 n and combine to
29
00:05:10,210 --> 00:05:14,830
get the solution of P 1 and so on.
30
00:05:14,830 --> 00:05:21,830
Now, say for example, we have to find out
the sum of n numbers now, by observation you
31
00:05:23,120 --> 00:05:30,120
can tell, the finding the sum of n numbers
is a sequential process. Now, look at the
32
00:05:30,520 --> 00:05:37,520
problem that I have x 1, x 2, x n this n elements
I have, these are the n elements x 1, x 2,
33
00:05:46,760 --> 00:05:47,389
x n.
34
00:05:47,389 --> 00:05:52,730
And I want to find out the sum of n numbers,
this n numbers so, one we, I can think that
35
00:05:52,730 --> 00:05:59,730
I divide this into two parts right say, I
want to find out the sum of n numbers. Now,
36
00:06:07,639 --> 00:06:11,320
that instead of, finding the sum of n numbers,
what I do here now, I will find sum of 1 to
37
00:06:11,320 --> 00:06:18,320
n by 2, and this side I find sum of remaining
n by 2 elements. Now, I got the result of
38
00:06:31,550 --> 00:06:37,310
this sum of this sum of this session and I
combined this two, to get the sum of n numbers.
39
00:06:37,310 --> 00:06:44,310
So, if I have to write this in the form of
algorithms, I can write sum A say, I want
40
00:06:47,919 --> 00:06:54,919
to find out the sum of l to u.
41
00:06:56,590 --> 00:07:03,590
If l equals to u then, return A l else if,
l equals to u minus 1 then, return A l plus
42
00:07:35,780 --> 00:07:42,780
A u else, return sum A l. Return else m equals
to l plus u by 2 and then, return sum A l
43
00:08:21,990 --> 00:08:28,990
m plus sum A m plus 1 u. So, basically you
are computing this l plus u and then, you
44
00:08:48,930 --> 00:08:51,759
return this sum and then, plus this one.
45
00:08:51,759 --> 00:08:58,759
So, if you have to compute the time complex
in for that then, I have T n is equal to Zero
46
00:09:01,459 --> 00:09:08,459
if n is equal to 1, 1 if n equal to 2 otherwise,
2 T n by 2 plus 1, otherwise.
47
00:09:23,029 --> 00:09:30,029
So, you have to solve this when this one is
coming under one condition and this is size
48
00:09:33,639 --> 00:09:40,639
is half, size is 2 T n by 2. So, T n becomes
2 T n by 2 plus 1, which I can write 2 times
49
00:09:58,220 --> 00:10:05,220
2 T n by 4 plus 1 plus 1. So, it gives you
basically, 2 square T n by 4 plus 2 plus 1,
50
00:10:07,110 --> 00:10:14,110
is it correct? Next time I write, 2 square
T n by 8 2 times plus 1 plus 2 plus 1 so,
51
00:10:23,110 --> 00:10:30,110
I get put cube T n by 8 plus 2 square plus
2 plus 1.
52
00:10:34,410 --> 00:10:41,410
So, if n equal to 2 to the power of k, I get
here 2 to the power of k minus 1 T 2 plus
53
00:10:57,279 --> 00:11:00,309
2 to the power of k minus 2.
54
00:11:00,309 --> 00:11:07,309
Now, this is T 2 is 1 so, here 2 to the power
of k minus 1 so, here 2 to the power of k
55
00:11:09,639 --> 00:11:14,429
minus 1 plus 2 to the power of k minus 2.
56
00:11:14,429 --> 00:11:21,429
So, this keeps you 2 to the power k minus
1, 2 minus 1, which nothing but, n minus 1
57
00:11:24,350 --> 00:11:31,350
because, 2 to the power of k is n so, you
get n minus 1. And which is the, reality is
58
00:11:32,009 --> 00:11:39,009
also true that, to find the sum of n numbers,
you need n minus 1 an each other. So, this
59
00:11:39,519 --> 00:11:46,519
is our first simple problem now, you know
that binary search let us think about the
60
00:11:47,839 --> 00:11:54,839
binary search, what happens. In the case of
the binary search we assume that the elements
61
00:11:57,679 --> 00:11:59,279
are either .
62
00:11:59,279 --> 00:12:06,279
So, suppose you have n elements, you have
A 1, A 2, A n and n argument k you have. Your
63
00:12:17,079 --> 00:12:23,600
interest is to find whether the element k,
exists in this sequence of an elements or
64
00:12:23,600 --> 00:12:27,819
not now, it is assume the elements are in
increasing order.
65
00:12:27,819 --> 00:12:34,819
So, in the case of binary search, what we
do, we divide these n elements into, you compare
66
00:12:39,949 --> 00:12:46,699
the k with the middle elements. It has the
property that is, it is in increasing order
67
00:12:46,699 --> 00:12:53,699
then, these elements is less than equal to
this, less than equal to this and so on. So,
68
00:12:53,759 --> 00:13:00,759
we will be comparing the middle element with
the k now, we should find that, k is for find
69
00:13:02,670 --> 00:13:09,670
that, k is less than a m where, a m is the
middle element of the sequence.
70
00:13:10,670 --> 00:13:17,670
Then, k if k exists at all then, it will lie
here so, in that case in that case, my searching
71
00:13:19,420 --> 00:13:26,420
zone will be reduce to this part and you can
easily discard all the elements of so, my
72
00:13:28,769 --> 00:13:35,769
searching area. Now, the searching zone is
reduce to this and again, I take the middle
73
00:13:35,790 --> 00:13:42,790
element and I compare with that k, is less
than equal to, all greater than, if it is
74
00:13:43,069 --> 00:13:50,069
equal to m or the middle element, you tell
the element exist and you got the below m.
75
00:13:50,779 --> 00:13:57,779
Now, if find that k is greater than this side,
the k lies in this zone. So, my searching
76
00:14:00,629 --> 00:14:07,629
zone will be reduce to this and so on, till
you possess this one, till I find that, there
77
00:14:10,410 --> 00:14:15,579
exists only one element. And we compare and
you have compare and found either exists equal
78
00:14:15,579 --> 00:14:18,670
to or not equal to.
79
00:14:18,670 --> 00:14:23,649
Now, in that case, if I have to estimate the
time complexity for that, there it is comes
80
00:14:23,649 --> 00:14:30,649
T n is, you will comparing 1 and the size
increases to half, if n is greater than 1.
81
00:14:40,649 --> 00:14:46,369
And if n equals to 1 then, only one component
will be required. Now, the solution of this
82
00:14:46,369 --> 00:14:53,369
becomes order log n now, if I have to do or
I have to write the generic algorithm for
83
00:14:57,269 --> 00:15:04,269
divide and conquer, and with the addition,
we will be dividing the problem into the two
84
00:15:04,769 --> 00:15:06,850
sub problems.
85
00:15:06,850 --> 00:15:13,850
So, i write D and C, the problem P and size
is small p and q so, this the problem from
86
00:15:25,449 --> 00:15:32,449
the size of p and q. Now, if problem p q is
small then, solve else, m equal to say, p
87
00:15:50,859 --> 00:15:57,859
plus q divided by 2 and then, you call divide
and conquer P p m x equals to, y equals to
88
00:16:13,720 --> 00:16:20,720
divide and conquer P m plus one, q. And then,
combine the result combine the result x with
89
00:16:28,679 --> 00:16:35,679
y that is the required so, this is the generic
algorithm for divide and conquer.
90
00:16:38,029 --> 00:16:45,029
If i have to see the time complexity T n is
equal to say, f n if n is small else, this
91
00:16:52,049 --> 00:16:59,049
2 T n by 2 plus, some another function g n,
otherwise right. So, this g n
is for the time you need to combined this
92
00:17:09,540 --> 00:17:10,770
together.
93
00:17:10,770 --> 00:17:17,770
This is an example of divided and conquer
paradigms suppose, you have x 1, x 2, x n
94
00:17:22,139 --> 00:17:29,139
or that n elements and they are not in sorted
order, you want to arrange it in ascending
95
00:17:31,299 --> 00:17:36,549
order or descending order. Let us assume without
the laws are generated, you want to arrange
96
00:17:36,549 --> 00:17:43,549
it in ascending order. So, this merge sort
is a technique to arrange this n elements
97
00:17:44,529 --> 00:17:51,529
either in ascending order or descending order.
So, algorithm becomes very simple, if I write
98
00:17:53,630 --> 00:18:00,630
merge sort and you have x l dot dot u then,
you can write m equals to l plus u divided
99
00:18:12,580 --> 00:18:19,580
by 2. Then, you write merge sort x l m merge
sort x m plus 1 u then, you have merge x l,
100
00:18:48,019 --> 00:18:53,779
m plus 1,u.
101
00:18:53,779 --> 00:19:00,779
What it means, that basically you have n elements,
you have n elements and you are dividing into
102
00:19:01,049 --> 00:19:08,049
two parts, you are recursively you bring the
merge of this, recursively merge of this.
103
00:19:08,090 --> 00:19:15,090
And then, this is in increasing order, this
is also increasing order, you are that combining
104
00:19:15,460 --> 00:19:22,460
this two results, to get the result is in
the increasing order. So, m is the middle
105
00:19:23,389 --> 00:19:28,500
of this, you are diving this n elements into
the two equal part, you are making the merge
106
00:19:28,500 --> 00:19:32,840
of algorithm recursively you are calling this
part, merge of algorithm calling this side.
107
00:19:32,840 --> 00:19:39,380
Then, you are merging this two to get the
results, we will be discussed how to merge
108
00:19:39,380 --> 00:19:41,850
this one.
109
00:19:41,850 --> 00:19:48,850
If it is the case then, my time complexity
becomes T n equal to 2 times T n by 2 plus
110
00:19:53,580 --> 00:20:00,580
the time you need, time you need time you
need to merge.
111
00:20:02,630 --> 00:20:09,630
So, let us think about how to merge, you have
a 1, a 2, a 3, a x and you have b 1, b 2,
112
00:20:15,149 --> 00:20:22,149
b 3, b y. Suppose, you want to merge this
two, the sequence with the understanding that, this
113
00:20:25,860 --> 00:20:32,860
is satisfying this criteria, this satisfy
this criteria and we want to merge these two
114
00:20:33,070 --> 00:20:40,070
sorted sequence. So, what I do, I compared
a 1 with b 1, if you find a 1 is smaller than
115
00:20:43,120 --> 00:20:49,120
b 1, then a 1 is the smallest element, which
is I write c 1.
116
00:20:49,120 --> 00:20:55,919
Now, once you have selected a 1 that pointer
moves to this area and I know to compare a
117
00:20:55,919 --> 00:21:02,919
2 with b 1 and if you find still a 2 is smaller
than b 1 then, you write in the position of
118
00:21:02,950 --> 00:21:09,950
c 1 and the pointer moves to a 3. Now, a 3
is compared with b 1 and if you find that
119
00:21:12,019 --> 00:21:19,019
b 1 is smaller than smaller than a 3 then,
b 1 is come here and pointer moves to this
120
00:21:21,970 --> 00:21:28,970
place. And proceed till till one of the array,
already one of the sequence is, already task
121
00:21:32,250 --> 00:21:39,250
for to c sequence and that in any elements
c 2 is elements is to c to get the final merge
122
00:21:40,970 --> 00:21:41,730
sequence.
123
00:21:41,730 --> 00:21:48,730
Now, here I have, basically I have one sequence
i have one sequence x 1, x 2, x l, x l plus
124
00:21:59,029 --> 00:22:06,029
1, x m then, we have x m plus 1, x m plus
2 you have, x u, this is satisfying this property,
125
00:22:16,990 --> 00:22:23,309
this is increasing order and this is also
increasing order. Now, move to merge sort
126
00:22:23,309 --> 00:22:30,309
so, I can write merge x l, m plus 1, u or
be mid and then, you I write, I equal to l,
127
00:22:53,830 --> 00:23:00,830
j equal to mid. And k is the pointer for merge
sequence, which is l which is l so, I will
128
00:23:16,130 --> 00:23:23,130
be moving this pointer i j and j i and j.
And we will see as long as as long as whole
129
00:23:26,380 --> 00:23:33,380
sequence is considered, we have to, is not
yet considered, we have to do these operations.
130
00:23:36,289 --> 00:23:43,289
So, while i is less than mid mid and j is
less than u, you have to do the following
131
00:24:01,940 --> 00:24:08,940
that is, both the sequences as are not existence.
If x of i is found less than x of j then,
132
00:24:21,789 --> 00:24:26,059
this sequence contains the smaller element.
133
00:24:26,059 --> 00:24:33,059
And that has to be moved to sequence say,
y of k is your x of i and i is increased by
134
00:24:44,679 --> 00:24:51,679
1. Otherwise is this greater than this. So,
will be writing if y of k is your x of j and
135
00:25:02,799 --> 00:25:08,519
j is incremented by 1 and in both the case,
your k will be moved incremented by 1. This
136
00:25:08,519 --> 00:25:15,519
is the things you have to do that, if you
find that x of i less than x of j then y of
137
00:25:16,730 --> 00:25:23,730
k is updated by x of a x of i and i is increased
by 1. Otherwise, k is updated by x of j and
138
00:25:24,970 --> 00:25:31,970
j is incremented by 1 anyway, that you have
to update the y of k to get the next position
139
00:25:32,259 --> 00:25:33,230
of y.
140
00:25:33,230 --> 00:25:40,230
Now, in that process what it may so happen
that, one subsequence is already enter into
141
00:25:42,289 --> 00:25:49,289
y sequence and some parts of other sequence
enter into y sequence. The remaining part
142
00:25:51,379 --> 00:25:58,039
is, a 2 shifted to y so, that has to be taken
into account.
143
00:25:58,039 --> 00:26:05,039
So, this can be done like that, if you find
that, i is greater than equal to mid then,
144
00:26:17,100 --> 00:26:24,100
their exist some elements of the second sub
sequence to be shifted. That is, for l equal
145
00:26:27,090 --> 00:26:34,090
to j to u, y of l is your x y of k equal to
x of l at k is increased by 1 else, for l
146
00:26:55,240 --> 00:27:02,240
equal to i to mid i to mid minus one equals
i to mid.
147
00:27:14,519 --> 00:27:21,519
And then, y of k is your x of l and k is increased
by 1 so, basically your merging of x sequence,
148
00:27:29,539 --> 00:27:34,929
x subsequence and this sub sequence and merge
sequence you put it y.
149
00:27:34,929 --> 00:27:41,929
But, in order to, get the result back to x,
you have write a small statement that is,
150
00:27:42,919 --> 00:27:45,360
for i equals to one for l to u i equal to
l to u, you have x of i equals to y of i so,
151
00:27:45,360 --> 00:27:52,360
then result will be stored by to x. When you
up to find out the complexity of this algorithm
152
00:28:05,269 --> 00:28:12,269
then, you observed that it takes basically,
here worst case scenario, what will be the
153
00:28:16,549 --> 00:28:17,360
worst case scenario.
154
00:28:17,360 --> 00:28:24,360
Worst case scenario could be that, one element
you have this subsequence and this subsequence.
155
00:28:29,289 --> 00:28:35,740
So, you are taking one element here then,
next element then next element, next element,
156
00:28:35,740 --> 00:28:41,480
next element, next element and so on. So,
that my comparisons, this will be compared
157
00:28:41,480 --> 00:28:48,480
every time and which, will be taking order
l minus u basically, order n, u minus l that
158
00:28:49,220 --> 00:28:56,220
basically, order n. And this is also we will
take, otherwise also, better moment time also
159
00:28:56,580 --> 00:29:03,440
basically will find order n. So, merge routine
will take order n time merge routine will
160
00:29:03,440 --> 00:29:04,690
take order n time.
161
00:29:04,690 --> 00:29:11,690
So, it that case, merge sort take the following
time needs the following time if T n is the
162
00:29:17,759 --> 00:29:24,759
time complexity to solve then, you have T
T n by 2 plus order n, this is for merging.
163
00:29:25,649 --> 00:29:32,649
What is the complexity now is having, 2 times
2 times T n by 4 so, let me write the T n
164
00:29:34,710 --> 00:29:41,710
is O n is c n, can write c n here. And here,
I write c n by 2 so, this gives you 2 square
165
00:29:55,480 --> 00:30:02,480
T n by 4 plus c n plus c n, which gives me
2 square 2 times T n by 8 plus c n by 4 plus
166
00:30:17,820 --> 00:30:21,389
c n plus c n.
167
00:30:21,389 --> 00:30:28,389
So, this gives me 2 cube T n by 8 plus 3 times
c n.
168
00:30:32,570 --> 00:30:39,570
Now, if n equals 2 to the power k and T 1
is 0, one element nothing to be sorted and
169
00:30:49,519 --> 00:30:56,519
T 2 is T 2 is 1 because, only one comparison
we require so T 2 is 1. In that case, T n
170
00:31:07,429 --> 00:31:14,429
becomes 2 to the power k T 1 plus k times
c n, this is zero so, you get c n log n times,
171
00:31:28,450 --> 00:31:35,450
this is called as order n log n. So, this
is matching matches, this matches in the lower
172
00:31:41,200 --> 00:31:48,200
bound or lower bound of sorting algorithms,
this takes what all, log n time complexity
173
00:31:48,460 --> 00:31:54,009
to solve n element using merge sort technique.
174
00:31:54,009 --> 00:32:01,009
Now, another example for divide and conquer
is quick sort so, you observe that in the
175
00:32:09,289 --> 00:32:16,289
case of merge sort, what do you did, we divided
the sequence into two equal parts. And the
176
00:32:17,799 --> 00:32:23,340
first one we call merge of the first part
and the merge of the second part of algorithm
177
00:32:23,340 --> 00:32:29,940
Now, in the case of quick sort, it is little
difference suppose, you have x 1, x 2, x n
178
00:32:29,940 --> 00:32:35,639
now, what we do that we take the first element
or one of them, random you can select. But,
179
00:32:35,639 --> 00:32:40,529
for simplicity, you select the first element
as the partition element. And what it does?
180
00:32:40,529 --> 00:32:47,529
This element is huge, to find its position
with reference to x-axis; so if we divide
181
00:32:53,259 --> 00:33:00,259
or we get the exact position of x 1, exact
position of x 1, if I sort, if this sequence
182
00:33:03,580 --> 00:33:10,580
is sorted, by some, by some technique, we
partition this sequence into the two parts
183
00:33:10,899 --> 00:33:17,899
in such way that, x 1 gets its position in
the sequence, satisfying the property of sorting,
184
00:33:21,470 --> 00:33:28,470
agreed. So, I get the position of x 1 here
in such a way that, all these elements here
185
00:33:29,029 --> 00:33:34,990
are smaller than these elements and all these
elements here are larger than these elements.
186
00:33:34,990 --> 00:33:41,990
That means what, we select one element say
first element which we termed as a partition
187
00:33:42,529 --> 00:33:49,529
element and this element gets not only gets
position in the sorting sequence, also all
188
00:33:49,639 --> 00:33:55,590
the elements to where of this side or of this
side are larger than this elements and none
189
00:33:55,590 --> 00:34:01,039
of them is smaller than this elements. Then,
you require some equal, quick sort of this
190
00:34:01,039 --> 00:34:08,039
part and quick sort of this part finally,
you have one element because, know how to
191
00:34:08,800 --> 00:34:15,800
partition say, I have a sequence partition
say, partition
192
00:34:24,610 --> 00:34:28,870
A m and u.
193
00:34:28,870 --> 00:34:35,870
What it means that, you have a m a m plus
1 a u, I put a pointer here and I put pointer
194
00:34:44,160 --> 00:34:51,160
here. Now, a m is compared a m is compared
with this first element, if you find that
195
00:34:52,440 --> 00:34:57,620
this is smaller than or equal, you retain
as it is. You move the pointer here now, this
196
00:34:57,620 --> 00:35:04,620
element is compared with this, if you find
that this element smaller than a m, you go
197
00:35:06,550 --> 00:35:10,110
to the next pointer, next element.
You find this element is smaller than this
198
00:35:10,110 --> 00:35:17,110
element, you go to the next one now, if you
find this element is larger than this element
199
00:35:17,240 --> 00:35:23,700
then, you stop here now, you starts searching
from this side. Now, if you find it this element
200
00:35:23,700 --> 00:35:27,270
is larger than a m, you go to the next one
you find that this element is larger than
201
00:35:27,270 --> 00:35:31,530
a m, go to the next one and so on. Finally,
to get into a position where, this element
202
00:35:31,530 --> 00:35:38,260
will be, you make at a position where, this
element is smaller, then this one.
203
00:35:38,260 --> 00:35:45,260
So, you will touch this two you will touch
these two and then, you proceed again so,
204
00:35:46,400 --> 00:35:51,600
finally, you will get a position here where,
it is crossed over. So, you will touch this
205
00:35:51,600 --> 00:35:55,780
element to this one and so, you will find
that a m position. And all this elements,
206
00:35:55,780 --> 00:36:02,050
the small of these elements, all this elements
should be smaller than larger than this element.
207
00:36:02,050 --> 00:36:09,050
Now, since there if I consider the size is
n then, I need n plus 1 comparisons so, this
208
00:36:11,620 --> 00:36:18,620
is the idea of partitioning element then,
the idea of partition algorithm, which takes
209
00:36:22,640 --> 00:36:28,160
n plus 1 you need of time.
210
00:36:28,160 --> 00:36:35,160
If I have to write the algorithm then, I can
write this way say, v equals to A of m and
211
00:36:42,730 --> 00:36:49,730
I equal to m then, do i is increase by one
until A of i is greater than equal to v, do
212
00:37:14,430 --> 00:37:21,430
u is decrease by 1 until A of u is less than
equal to v. If i is less than u then interchange
213
00:37:38,130 --> 00:37:45,130
interchange a of i be a of u.
214
00:37:49,600 --> 00:37:56,600
After doing this, you repeat till there is
a cross over that is, i is while while i greater
215
00:38:16,270 --> 00:38:23,270
than i less than n, less than equal to n,
you do all these things. And then, you have
216
00:38:26,430 --> 00:38:33,430
A of m is equals to A of u and A of u is equal
to your v. So, this is your partitioning elements,
217
00:38:43,300 --> 00:38:50,300
partitioning algorithms and obviously than
this takes order n plus 1 unit time to do
218
00:38:53,010 --> 00:38:59,550
the to get the partitioning position to get
the position of x 1, it is sub sequence of
219
00:38:59,550 --> 00:38:59,890
n element.
220
00:38:59,890 --> 00:39:06,890
Now, if you have to write the quick sort algorithm
so, you have quick sort A p q that is, we
221
00:39:15,570 --> 00:39:22,570
looking part to sort A of p to A of q.
222
00:39:23,150 --> 00:39:30,150
Here, one to remember, I forgot to mention
that that instead of, u it is u minus 1 instead
223
00:39:35,500 --> 00:39:42,500
of u, it is u minus 1 that is, we are partitioning
the sequence from a m to a u minus 1.
224
00:39:45,180 --> 00:39:52,180
If p is less than q if p is less than q then,
j equals to q plus 1 q plus 1 and then, i
225
00:40:02,190 --> 00:40:09,190
call partition partition A p and j, then you
call quick sort A, p, j minus 1. Because that
226
00:40:28,700 --> 00:40:35,700
means, the position, first element first position
that is, p at position has got its own position,
227
00:40:35,930 --> 00:40:42,930
which is j. And then, you get quick sort A,
j plus 1, q.
228
00:40:49,630 --> 00:40:56,630
So, idea is very simple that, what happens
that, this is p and this is q so, i get a
229
00:41:10,440 --> 00:41:17,440
j, this A p moves here all this elements fall
of the p of the and as long as p is less than
230
00:41:21,820 --> 00:41:28,820
q, due to the some partitions. Now, this quick
sort algorithm is worst, can you tell me when,
231
00:41:33,910 --> 00:41:40,910
you observe that this complexity is depended
on that partition, take this. So, we are assuming
232
00:41:45,300 --> 00:41:52,300
that assuming that the j is here but, is so
happens that, all the elements all the elements
233
00:41:54,810 --> 00:42:01,810
of this part are larger than this edge then
this quick sort will not have any meaning,
234
00:42:04,040 --> 00:42:11,040
because because that does not exist any elements
in this side and you have remaining n minus
235
00:42:12,350 --> 00:42:19,350
1 elements to be sorted by this. So, again
next intention, we may have if all this elements
236
00:42:20,440 --> 00:42:24,510
are larger than these elements and so on.
237
00:42:24,510 --> 00:42:31,510
So, in that case what happens, if T n is the
time complexity that, you need order in n
238
00:42:36,420 --> 00:42:43,420
times to partition and size is not reused,
only one element is one side and the other
239
00:42:49,150 --> 00:42:56,150
side there is nothing, no elements right.
This side no element, this side you have n
240
00:42:58,570 --> 00:43:05,570
minus 1 elements so, we have T n plus T n
minus 1 plus T 0, T 0 is 0 so, you get next
241
00:43:07,050 --> 00:43:14,050
time T n minus 2 plus c n minus 1 plus c n
right. Next time again no elements in one
242
00:43:22,740 --> 00:43:29,740
side so, you get so, we have n n minus 1 c
2 c 3 c n, this is one; one means T 1 T 1
243
00:44:03,220 --> 00:44:03,890
is 0.
244
00:44:03,890 --> 00:44:10,890
So, you get c, 2 plus 3 plus n, which is order
n square so, in the worst case, it comes order
245
00:44:18,000 --> 00:44:23,810
n square. Now, invalid what it means, that
when the elements are in increasing order
246
00:44:23,810 --> 00:44:30,360
or decreasing order, you will find that partition
routine will give you, not give you that good
247
00:44:30,360 --> 00:44:36,310
partitions, it will find that one side you
have n minus 1 elements, the other side you
248
00:44:36,310 --> 00:44:39,780
would not have any other elements so, the
complexity in the worst becomes order n square.
249
00:44:39,780 --> 00:44:46,780
Now, what is the best case, can you tell me,
the best possible case is when the partitioning
250
00:44:51,300 --> 00:44:57,950
elements divides the two sequence divides
the whole sequence into two equal parts.
251
00:44:57,950 --> 00:45:04,950
In that case in that case, the time complexity
becomes becomes c n into 2 times T n by 2,
252
00:45:13,930 --> 00:45:20,930
if it is c n plus 2 T n by 2 so, I can write
c n plus 2 times, c n by 2 plus 2 times T
253
00:45:26,150 --> 00:45:33,150
n by four. And this becomes c n plus c n plus
2 square T n by 4 right and you can show,
254
00:45:42,380 --> 00:45:49,380
this is nothing but, c n log n, which is order
n log n. So, in the best case, it is order
255
00:45:53,920 --> 00:45:59,360
n log n in the worst case, it is order n square.
256
00:45:59,360 --> 00:46:06,360
Now, let us try to find out the average time
complexity or average number of comparisons
257
00:46:11,400 --> 00:46:18,400
you need in the quick sort. Let us assume
that, T A n is the average time complexity
258
00:46:21,390 --> 00:46:28,390
this is the nothing but, c n is a time for
the partitioning, plus 1 by n. Actually to
259
00:46:36,720 --> 00:46:43,720
be in exact, it is n plus 1 and it is 1 plus
n summation say, T A of k it should be k minus
260
00:46:56,400 --> 00:47:03,400
1, T A n minus k, this is k minus 1.
k is 1 to n, what it means, that you are partitioning
261
00:47:13,830 --> 00:47:20,830
it, this is k so, if it is k, your value remains
so so, this side it is n minus k, this side
262
00:47:22,740 --> 00:47:29,740
you have k minus one and k can be 1, k can
be 1 2 n k can be 1 2 n. So, that is the time
263
00:47:33,030 --> 00:47:40,030
complexity, average time why 1 by n you are
assuming, they are equally likely so, that
264
00:47:40,950 --> 00:47:44,260
is why, it is 1 by n.
265
00:47:44,260 --> 00:47:51,260
So, this gives you basically n times T A n
equal to n into, n plus 1 plus 1 by no plus
266
00:47:59,760 --> 00:48:06,760
T 0 T 1 T n minus 1 plus T n minus 1 T 0 this
is nothing but n into n plus 1, 2 times
summation t, this is T A 1 T A 1 plus T A
267
00:48:53,350 --> 00:49:00,350
2 T A n minus 1, T A i, i is 1 to n minus
1.
268
00:49:22,260 --> 00:49:29,260
Because T A 0 does not have any meaning, not
only T A 0 does not have any meaning, T A
269
00:49:30,180 --> 00:49:37,180
2 also does not have any, T A 1 does not have
any meaning. T A 0 is equal to T A 1 is equal
270
00:49:41,560 --> 00:49:47,670
to 0 so, i take, i equal to 2 to n minus 1.
271
00:49:47,670 --> 00:49:54,670
Now, I want to solve this, we have to solve
this, let us replace n by n minus 1, what
272
00:49:56,260 --> 00:50:03,260
i get n minus 1, T A, n minus 1 equal to n,
n minus 1 plus 2 times, i equal to 2 to n
273
00:50:14,300 --> 00:50:21,300
minus 2, T A i.
274
00:50:22,250 --> 00:50:29,250
When you if subtract is it, i get n T A n
minus, n minus 1 T A, n minus 1 here, we get
275
00:50:50,530 --> 00:50:57,530
n square n square get cancel, 2 n 2 n and
here, i get plus 2 times T A n minus 1.
276
00:51:11,450 --> 00:51:18,450
So, if i bring this element this side, I get
T A n equal to 2 n plus n plus 1 T A n minus
277
00:51:35,500 --> 00:51:42,500
1 now, dividing both sides by n into n minus
1 n plus one that is, i get T A n by n plus
278
00:51:48,970 --> 00:51:55,970
1 plus 2 by n plus, T A n minus 2 by n minus
1.
279
00:52:01,060 --> 00:52:08,060
This gives the 2 summation over 1 by k, k
equal to 3 times so, plus T A so, if i write
280
00:52:30,400 --> 00:52:37,400
here 3 if i write here 3, this becomes 1,
this becomes 2 so, T A 1 is 0. So, this is
281
00:52:45,130 --> 00:52:52,130
equal to 2 times summation k equal to 3 to
n plus 1, i by k
this is less than equal to 2 times integration
282
00:53:00,790 --> 00:53:07,590
2 to n plus 1, 1 by k d k.
283
00:53:07,590 --> 00:53:14,590
Then, that equal to 2 times log k and n plus
1, 2 that is, 2 times log of n plus 1, minus
284
00:53:26,880 --> 00:53:33,880
log two. So, that is T A n is less than equal
to n plus 1, into 2 times log of n plus 1
285
00:53:46,820 --> 00:53:53,820
minus log 2, which is order n log n. So, average
time complexity to solve n elements using
286
00:53:59,640 --> 00:54:06,640
the quick sort technique is order n log n.
See this am could have above in it, just to
287
00:54:06,720 --> 00:54:13,720
just for the completely say, I felt that I
should tell you, how to compute the average
288
00:54:14,390 --> 00:54:16,560
time complexity of n algorithm.
289
00:54:16,560 --> 00:54:23,560
Now, let us consider another example, which
will be the last example today and actually
290
00:54:23,750 --> 00:54:30,750
you have you have been taught algorithm, which
is finding the k th element, k th smallest
291
00:54:33,240 --> 00:54:40,240
element of n element. This is disagree to
blum’s technique and I think I do rub it,
292
00:54:53,180 --> 00:55:00,180
do you know any algorithm search? No right.
So, let us consider this algorithm and let
293
00:55:02,910 --> 00:55:08,750
us try to understand of it is been designed
right.
294
00:55:08,750 --> 00:55:15,750
See one way is that, you first find the minimum
elements then, you find the second minimum
295
00:55:17,240 --> 00:55:22,080
element then, you find the third minimum element
and so on. Instead of doing that, let us change
296
00:55:22,080 --> 00:55:29,080
from this point of view that, you have the
n element, these n elements are divided into
297
00:55:30,090 --> 00:55:36,810
n by 7 groups. And each group is having the
7 elements except first and the last group,
298
00:55:36,810 --> 00:55:43,810
which may have one or two sorting may be may
not, if divisible by seven and you can padding
299
00:55:48,170 --> 00:55:50,300
it by a large number.
300
00:55:50,300 --> 00:55:57,300
So, you divided is given by seven groups
301
00:56:24,140 --> 00:56:26,840
and each group is having 7 elements.
302
00:56:26,840 --> 00:56:33,840
So, this side we have n by 7 groups and the
7 elements is those now, in the constant,
303
00:56:37,900 --> 00:56:44,620
we will use the constant amount of time, you
can solve these 7 elements of each groups
304
00:56:44,620 --> 00:56:51,620
right. So, you solve each group in constant
time so, there n by 7 so, your type of order
305
00:56:57,020 --> 00:57:04,020
becomes order n by 7. Now, this is your median
element, these are all median elements of
306
00:57:08,170 --> 00:57:14,880
this groups now, find that we say, find the
k th element, you find the median of this
307
00:57:14,880 --> 00:57:21,880
n by 7 elements.
Because, you can call that, one finding the
308
00:57:22,420 --> 00:57:29,420
median of the n by 7 medians right, as you
are surely finding the k th element now, these
309
00:57:30,720 --> 00:57:37,720
median let us quality n, this is the median
of median. Now, this median of used to partition
310
00:57:40,600 --> 00:57:47,600
to partition these whole sequence into 3 groups,
one all the elements smaller than these, another
311
00:57:48,960 --> 00:57:55,960
group is all the elements equal to these,
and another is all the elements larger than
312
00:57:56,700 --> 00:58:03,700
these.
So basically, you will partition 3 groups,
313
00:58:09,060 --> 00:58:14,840
one is all the elements all the elements smaller
than these, of the all the elements equal
314
00:58:14,840 --> 00:58:21,840
to this and all the elements equal to this.
When you observe that, if the number of elements
315
00:58:23,130 --> 00:58:27,500
smaller than these, smaller than these median
element, if your find this more than k then,
316
00:58:27,500 --> 00:58:34,500
k eth element is lying here. And if you find
no, are the element this size of, the size
317
00:58:36,430 --> 00:58:43,220
of this number of elements of number of smaller
than this is smaller than k, the smaller than
318
00:58:43,220 --> 00:58:46,710
k.
But, in but, if i find the number of elements
319
00:58:46,710 --> 00:58:53,710
equal to median number of elements such that,
this element is equal to, k is equal to the
320
00:58:54,690 --> 00:59:01,690
need no of element. And which are equal to
the median elements plus number of elements
321
00:59:03,480 --> 00:59:09,930
less than equal to the median elements. If
you find that, is greater than k greater than
322
00:59:09,930 --> 00:59:16,470
k then, the median element is the k eth element
otherwise, k th element is lying in this zone.
323
00:59:16,470 --> 00:59:23,470
So, you reduce the searching zone accordingly
now, one thing again assured that, at least
324
00:59:26,770 --> 00:59:33,770
25 percent of the, size of the n may be discarded
at any instant of time or at any iterations.
325
00:59:38,320 --> 00:59:44,760
This is because of the fact that because of
the fact that, all these elements will be
326
00:59:44,760 --> 00:59:51,760
smaller than this element and all these elements
will be larger than this element. Now, here
327
00:59:51,870 --> 00:59:58,870
since this is the median so, this is you have
the half of the, there is n by 14 elements
328
01:00:00,040 --> 01:00:07,040
smaller than these and this side also n by
14 larger than these. Now, this side again
329
01:00:08,190 --> 01:00:13,490
that half of this side will be larger than
the is smaller than this, half of these smaller
330
01:00:13,490 --> 01:00:19,250
than this, half of these smaller than this.
So, this is giving the 25 percent elements
331
01:00:19,250 --> 01:00:25,100
smaller, atleast smaller than this and the
25 percent will be atleast larger than this
332
01:00:25,100 --> 01:00:32,100
and you can draw a conclusion on this part.
So, it can giving you the assurance that,
333
01:00:33,250 --> 01:00:39,750
atleast 25 percent of the half n elements,
you will be discarded at any instant of .
334
01:00:39,750 --> 01:00:45,300
You know the point of complexity, if I had
the T n k is the time complexity, to find
335
01:00:45,300 --> 01:00:52,300
the k eth element from the n elements. So,
first you may bring the sorting of each group
336
01:00:53,410 --> 01:01:00,410
there are there are n by 7 groups so, time
is order n by 7 and after that, you will be
337
01:01:03,610 --> 01:01:10,610
calling the median of median elements. So,
basically, you have n by 7 and k is n by 14
338
01:01:13,610 --> 01:01:20,610
and after finding that, you will be discarding
25 percent, your size becomes T, 3 n by 4
339
01:01:22,760 --> 01:01:29,760
and then, k and the solution of this is order
n.
340
01:01:31,560 --> 01:01:37,900
I am not discussing how to solve it because,
it is two dimensional recurrence relation,
341
01:01:37,900 --> 01:01:44,170
which I do not want discuss in this class.
Another example for the divided and conquer
342
01:01:44,170 --> 01:01:51,170
, which is matrix multiplication here, I will
not discussion in detail. Suppose, here the
343
01:01:51,360 --> 01:01:57,780
two matrices and you partition it into two
parts so, this is suppose, you have A 1 1,
344
01:01:57,780 --> 01:02:04,780
this is sub matrix A 1 2, 2 1 and A 2 2.
You have B 1 1, B 1 2, B 2 1 and B 2 2 then,
345
01:02:12,260 --> 01:02:19,260
C 1 1 is compute can be computed, by computing
that A 1 1 dot B 1 1 and plus A 1 2 and B
346
01:02:20,710 --> 01:02:27,710
2 1 right. Similarly, you can computing C
1 2 and C 2 1 and C 2 2 so, this can be done
347
01:02:29,940 --> 01:02:36,940
recursively using the divide and conquer strategy
that you can try at home. In the next class,
348
01:02:37,170 --> 01:02:44,170
we will be discussing about the other strategies,
if possible in the next class, we will be
349
01:02:44,200 --> 01:02:51,200
talking about greedy method, dynamic programming and if time permits, little about the back back tracking.