1
00:00:16,129 --> 00:00:25,830
So in the last class we are discussing about
the median parallel right that mean the median
2
00:00:25,830 --> 00:00:31,070
pair idea is that you have to sequence.
3
00:00:31,070 --> 00:00:39,120
And you find out the median pair in such a
way that it divides exactly two equal half's
4
00:00:39,120 --> 00:00:45,059
right so that next time this is done by one
processor next time I employ two processor
5
00:00:45,059 --> 00:00:50,920
to divide one processor is to divide this
part into two parts and this part into two
6
00:00:50,920 --> 00:01:00,750
parts and the size becomes safe with the condition
is right the size it becomes size becomes
7
00:01:00,750 --> 00:01:08,280
safe and all these elements will be smaller
than this element this element will be smaller
8
00:01:08,280 --> 00:01:16,100
than elements.
So now I can employ in process to match each
9
00:01:16,100 --> 00:01:25,530
sub part right, and they will take the same
amount of time and then there is no need of
10
00:01:25,530 --> 00:01:30,030
concurrent read part right so only that if
I do can do this partition that will take
11
00:01:30,030 --> 00:01:40,409
care so the issue is how to divide or how
to get the median pair right initially the
12
00:01:40,409 --> 00:01:44,130
process is like this.
13
00:01:44,130 --> 00:01:55,009
Initially I take the middle value, so I can
assume that these two elements are the problem
14
00:01:55,009 --> 00:02:05,789
candidate for my median pair okay now if this
is the case yes or no this is this is the
15
00:02:05,789 --> 00:02:15,150
because if I tell this is n /2 and this is
m /2 so if I take some of this then it becomes
16
00:02:15,150 --> 00:02:22,030
one of them will be + M divided /2 that the
median means that is the element yes so that
17
00:02:22,030 --> 00:02:27,060
is a probable candidate he's a pro you will
candidate right.
18
00:02:27,060 --> 00:02:34,850
But as I told yesterday this cannot be a probable
candidate for the median. Now what it does
19
00:02:34,850 --> 00:02:42,610
he checks is there any element is there any
element in this zone which is larger than
20
00:02:42,610 --> 00:02:51,580
this element right or is there any element
of this zone which is larger than this element
21
00:02:51,580 --> 00:03:00,070
right suppose there exists one element here
,which is larger than this element suppose
22
00:03:00,070 --> 00:03:07,620
what it means these all these elements will
be smaller than this elements right.
23
00:03:07,620 --> 00:03:16,610
There is this one element 1 element here which
if I find that this is smaller than this element
24
00:03:16,610 --> 00:03:21,910
right then all these elements will be smaller
than this element and what it means suppose
25
00:03:21,910 --> 00:03:32,460
this is K then n / 2 +M / 2 + K that many
elements smaller than this elements so this
26
00:03:32,460 --> 00:03:39,871
cannot be the median element right, so if
you want to make this the middle element you
27
00:03:39,871 --> 00:03:44,920
want to find out median relevant on this then
this has to be shifted by something as many
28
00:03:44,920 --> 00:03:51,300
five you have shifted this side.
So you have to shift it by five this side
29
00:03:51,300 --> 00:03:59,590
so what happens that now the probable candidate
is that these and these are the member of
30
00:03:59,590 --> 00:04:05,910
this so if you increase this side that should
be decreases right so that M + in M / n M
31
00:04:05,910 --> 00:04:17,660
/ 2 + n / 2 that relationship will maintain
okay now since it is a sorted order so you
32
00:04:17,660 --> 00:04:27,261
can use the binary search right to find it
out so you know to find out the median pair
33
00:04:27,261 --> 00:04:28,261
suppose.
34
00:04:28,261 --> 00:04:49,810
You have a sequence so as usual you have LA
=1 I; HA= M 1 low index of B is 1 and height
35
00:04:49,810 --> 00:05:10,110
of B is your n then number of elements in
a is M number of elements in B so in a points
36
00:05:10,110 --> 00:05:22,840
to the total number of elements you are considering
for finding the median pair n a in this L
37
00:05:22,840 --> 00:05:29,030
n a gives you the number of elements in the
Na which you are considering for finding the
38
00:05:29,030 --> 00:05:37,530
median pair element and then we give the total
number of elements in the Na B which are under
39
00:05:37,530 --> 00:05:43,870
consideration for median pair right.
Now this once I am doing the first time I
40
00:05:43,870 --> 00:05:49,500
am considering this one second at my searching
dude maybe this one of this size because in
41
00:05:49,500 --> 00:05:54,870
the binary stars in having it all the time
and you know there may be must be a committing
42
00:05:54,870 --> 00:06:19,620
condition what is that well n a > 1 and a
and B > 1 do you have to find out the middle
43
00:06:19,620 --> 00:06:52,290
element middle element middle element is La
+ HA - LA - 1/2 V=LB.
44
00:06:52,290 --> 00:06:59,449
And this situation you will be able to discard
some part right suppose I find out I find
45
00:06:59,449 --> 00:07:07,910
out that this element is smaller than, this
smaller than this so you have to shift right
46
00:07:07,910 --> 00:07:15,400
this reaction so such changes will be reduced
no such as you do in the case of binary search
47
00:07:15,400 --> 00:07:19,820
right what you doing the binary search then
you search is here if it is larger than you
48
00:07:19,820 --> 00:07:25,620
go to this side and this part is reduce right
you discard it same thing we are doing here
49
00:07:25,620 --> 00:07:34,060
we would not discourage certain parts to find
out the appropriate pair of elements right.
50
00:07:34,060 --> 00:07:49,880
So NA will be next time it will be half and
N B is also NB /1/2 right and you W you will
51
00:07:49,880 --> 00:08:01,940
be able to discard is minimum of NA and NB
will discuss through example this one I think
52
00:08:01,940 --> 00:08:12,320
I should not change NA,NB now then I may be
little problem here you put it by 2/2 and
53
00:08:12,320 --> 00:08:33,329
then you put na is equal to na - W NB=NB-W
there is no guarantee difference is that see
54
00:08:33,329 --> 00:08:47,670
1/2 if I write first N equals to na by too
first right if I write n = na / 2 and B = B
55
00:08:47,670 --> 00:08:59,120
/2 and the w= define that minimum of this
to what it and then in this size will be reduced
56
00:08:59,120 --> 00:09:07,000
by I have already reduced by size by half
but this side there is no guarantee.
57
00:09:07,000 --> 00:09:16,180
I can reduce by half exactly. I can reduce
maximum this side here right not more than
58
00:09:16,180 --> 00:09:22,600
that because I have to get that guarantee
as I told that this is not a probable candidate
59
00:09:22,600 --> 00:09:27,690
so those guarantee also you have to take care
right so you got in it.
60
00:09:27,690 --> 00:09:40,740
Now if you
61
00:09:40,740 --> 00:09:53,750
if a u > BV what it means that the element
I have to find out the element from this zone
62
00:09:53,750 --> 00:10:01,941
Au is > I have to find out the element from
this zone sorry a limit from this zone and
63
00:10:01,941 --> 00:10:12,830
this side is your bu so I have to find out
this side right au if you find that > V V
64
00:10:12,830 --> 00:10:23,120
that means the median pair must be lying somewhere
here and some hail here agreed so that means
65
00:10:23,120 --> 00:10:29,010
in this zone means height should be high index
should be reduce.
66
00:10:29,010 --> 00:11:15,990
So your HA= becomes U-1 LB becomes B+1 okay,
and it becomes this one yes is the reverse
67
00:11:15,990 --> 00:11:39,589
one only becomes LA + 1 and H B becomes u
-1 it will be become LB + 1 LA becomes u +
68
00:11:39,589 --> 00:11:50,080
1 H becomes so once this will be directed
till there exist at least two elements in
69
00:11:50,080 --> 00:12:02,310
na and so when it is one of them it fails
you will be getting what you will be getting
70
00:12:02,310 --> 00:12:08,970
that U and V right so you be getting some
U.
71
00:12:08,970 --> 00:12:18,390
And some V this is U and V right but as you
know that this may not give you through an
72
00:12:18,390 --> 00:12:28,430
example we have seen that this is the most
probable candidate to be the median pair so
73
00:12:28,430 --> 00:12:39,770
what it has been observed because of that
u - 1 and V + 1 that these are the three probable
74
00:12:39,770 --> 00:12:47,130
candidates can be the member of your median
pair.
75
00:12:47,130 --> 00:13:17,360
So consider AU - 1 AU + AU + 1 + Bb -,Bb ,Bb+
1 right so how many eight pairs will be getting
76
00:13:17,360 --> 00:13:35,420
would refers to this you will get it
you will get 9 and you select one we satisfy
77
00:13:35,420 --> 00:13:43,570
the your definition of median pair that can
be done in constant time now you know the
78
00:13:43,570 --> 00:14:00,829
definition of median pair so nine elements
you see select for this element for this constant
79
00:14:00,829 --> 00:14:22,779
over five right so you have from the nine
element you have to get the 5th let us take
80
00:14:22,779 --> 00:14:26,790
first this three element.
81
00:14:26,790 --> 00:14:43,339
3 7 11 4 6 & 8 right so you get (3,4), (3,6),(3,8)
(7,4) (7,6) (7,8) (11,4) (11,6) (11,8) and
82
00:14:43,339 --> 00:15:04,279
these are the 9 pairs we are get right now
we are should be ready right now which one
83
00:15:04,279 --> 00:15:13,089
is your begins here for this time 3 4 6 7
8 this so 7th is your median element right
84
00:15:13,089 --> 00:15:25,370
seven is a median element agreed 6 also possible
middle element right so one possible candidate
85
00:15:25,370 --> 00:15:32,470
is this one another possible candidate is
this one another possible is this one another
86
00:15:32,470 --> 00:15:45,400
possible candidate is this one yes 6,8,6,11
cannot be can it be a possible candidate because
87
00:15:45,400 --> 00:15:58,610
6,7 is lying in there right.
So this is the fourth yes,3,6 also not a possible
88
00:15:58,610 --> 00:16:21,360
candidate 3,6 is also not a possible candidate
4,7 6,7, 7,8 is it way 4,7, 7,4 r what is
89
00:16:21,360 --> 00:16:32,220
the definition we mean what one of them one
of them is median the other element is the
90
00:16:32,220 --> 00:16:47,269
yes other element is that just either smaller
then .I just smaller than this element or
91
00:16:47,269 --> 00:16:55,100
just higher than this element okay, so if
seven is median pair median element then all
92
00:16:55,100 --> 00:17:04,089
the elements tell me the element so 6 is a
probable candidate and 7,8 is a probable candidate
93
00:17:04,089 --> 00:17:16,260
in the case 6 is the middle element right
yes is
94
00:17:16,260 --> 00:17:23,459
it okay.
Yes from the definition now you will be selecting
95
00:17:23,459 --> 00:17:35,720
the one who is some of the index is the smallest
right so here it is 1 2 3 so this will be
96
00:17:35,720 --> 00:17:52,300
the 3,6 actually 3,6 in the your return Oh,
No the same median pair definition that they
97
00:17:52,300 --> 00:18:02,740
find definition was made like that that one
of these quelling rentals with them again
98
00:18:02,740 --> 00:18:18,470
anywhere the other element of other sequence
is that element we is the either the largest
99
00:18:18,470 --> 00:18:26,250
elements smaller than the middle element or
the smallest element larger.
100
00:18:26,250 --> 00:18:34,030
And if you get more than one return then you
select the one to sum up in businesses there
101
00:18:34,030 --> 00:18:43,990
is a definition define right raise up that
only we are finding this now this can be done
102
00:18:43,990 --> 00:18:55,730
in constant amount of time but is 9p running
manipulate always agree and here theoretically
103
00:18:55,730 --> 00:19:04,600
third legally that every time we are dividing
into half of all in reality that is different
104
00:19:04,600 --> 00:19:13,410
but theoretically that they are of equal size
and every time you are reducing by half agree.
105
00:19:13,410 --> 00:19:23,880
So loss after login iterations I must be able
to come here which takes constant amount of
106
00:19:23,880 --> 00:19:34,390
time so the complexity of this algorithm becomes
longer write or log off.
107
00:19:34,390 --> 00:19:51,120
Maximum Na and or Nb m,n so let us consider
one example I think too that you will be able
108
00:19:51,120 --> 00:19:52,120
to.
109
00:19:52,120 --> 00:20:40,060
So you really is one and it is your HA is
8 initial LB 1,HB =6 NA 8 NB 6 right that
110
00:20:40,060 --> 00:21:07,990
is initialization this is the first iteration
U= 1 + 6 /2 so this 4 v = 1 + 4y 2 3 w = your
111
00:21:07,990 --> 00:21:33,380
minimum of 4 , 3 NA modified is 5 and NB is
0 if au a 4 which is 8 >a 3 b3, b3 10 answer
112
00:21:33,380 --> 00:22:19,780
is no if it is no so this is compare if it
is no LA only becomes 5 and HB is 2 now you
113
00:22:19,780 --> 00:23:41,720
becomes 1+8 -5 -1 .I do not know yes.
So divided / 2 5 + 1 is 6 V is 2 + a 1 + 0
114
00:23:41,720 --> 00:24:43,840
so B is 1 W 1 so NA is 4 NB is 2 then a 6
6 is 13 is it > there is it less than or > B
115
00:24:43,840 --> 00:25:18,110
what is B 1 B1 is 2- yes it is then H A has
to be updated HA is 5-5 and LB is 4 now U
116
00:25:18,110 --> 00:26:03,000
is LA,LA is still 5 5 + 0 which is 5 5 - 5
0 so it is 0 you take this index so this 5
117
00:26:03,000 --> 00:26:24,310
U 5 these V is 5 - 5 is 0 - 1 / 2 is 0 you
take the lower index, upon index want if you
118
00:26:24,310 --> 00:26:52,409
have to make it 0.
Because the integer part of it V what know
119
00:26:52,409 --> 00:27:16,090
these LB 2+2 men so this is also 0 so it is
2 W is what NA So W is 1 NA is 3 N B is 1
120
00:27:16,090 --> 00:27:47,440
and now if you find a 5 a 5 is what 9 >= 6
answer is yes if yes then HA is 4 LB is 3
121
00:27:47,440 --> 00:28:19,620
right NA / 2 N B / 2 so here your return value
is here A5 and B 2 because now NA has come
122
00:28:19,620 --> 00:28:37,990
and NB has come = 1 so it will stop so basically
then you get the sequence 1,2,3,4 ,5 so 8
123
00:28:37,990 --> 00:28:55,770
9 13 and 2 6 10 this is the from the A sequence
you get this from the B sequence you get this
124
00:28:55,770 --> 00:29:07,650
and your median pair is lying here.
Let us see 1, 2,3,4,6 7, 8 totally 14 elements.
125
00:29:07,650 --> 00:29:32,070
I have to find out the seventh 1 2 3 4 5 6
7 this type of so it will force you to consider
126
00:29:32,070 --> 00:29:51,710
one of them 2,6 yes 2,6,8 6 elements for 3rd
or 4th element whether this of this and immediately
127
00:29:51,710 --> 00:29:58,390
so this is considered here this will be possible
right you will be getting from there right
128
00:29:58,390 --> 00:30:08,200
8,9 are the middle pair million elements so
and it will be selected now if I find what
129
00:30:08,200 --> 00:30:14,169
are the problem can they tell me from here
median element.
130
00:30:14,169 --> 00:30:33,660
1 can be 8t if you 8 then it can be 2,8or
8,6 right if it is 9 oh 6,8 yes 8,6 and another
131
00:30:33,660 --> 00:30:49,410
one is 8,10 you can tell and in case if it
is 9 9 6 9 10 right and the return will be
132
00:30:49,410 --> 00:30:57,120
1 which is having the minimum index either
this one this is one 8,6, 8 is the one of
133
00:30:57,120 --> 00:31:07,530
the element is the middle so that is idea
so no this is here right and then what it
134
00:31:07,530 --> 00:31:18,580
I actual did here he has used the idea of
binary search on two so two arrays in such
135
00:31:18,580 --> 00:31:30,039
way that if this moves aside the other pointer
moves in this side that is the only thing.
136
00:31:30,039 --> 00:31:40,809
It is not a very big achievement but observed
to that problem can be solved that I the only
137
00:31:40,809 --> 00:31:49,070
thing then once I knew the pointer from here
to here this point is moved from yet so that
138
00:31:49,070 --> 00:31:54,730
then NY to half off I have to get immediate
path with a relevant must be there so that
139
00:31:54,730 --> 00:32:02,231
properties mainly now once I know this medians
there and you observe this medium pair is
140
00:32:02,231 --> 00:32:09,270
a situational thought that I have used of
caramel.
141
00:32:09,270 --> 00:32:16,390
So once I know this sequential algorithm for
finding the median pair this can be used for
142
00:32:16,390 --> 00:32:19,220
here you do rotate.
143
00:32:19,220 --> 00:32:24,860
So in the first iteration suppose you have
the true sequence in the first iteration.
144
00:32:24,860 --> 00:32:31,169
I have used one processor to find the median
pairs in the second iteration. I use two processor
145
00:32:31,169 --> 00:32:37,730
to find out the median pair and thirdly, I
use for process to find the median pairs and
146
00:32:37,730 --> 00:32:44,520
so on and we divide this paired in such a
way that one element is the median element
147
00:32:44,520 --> 00:32:51,929
the other element is selected in such a way
that all these elements are smaller than these
148
00:32:51,929 --> 00:32:57,890
elements so that the property is maintained
that this cluster is smaller than this cluster
149
00:32:57,890 --> 00:33:14,390
this cluster is smaller than this so on.
150
00:33:14,390 --> 00:33:56,590
E
So ER, EW MERGE processor p1 receives so one
151
00:33:56,590 --> 00:34:04,120
m1m n what it with that he there is the first
index of a sequence last index of a sequence
152
00:34:04,120 --> 00:34:36,899
first index of be sequenced now for J =1n
to log if n is the total number of processors
153
00:34:36,899 --> 00:35:08,670
or I = 1 to 2 J -1 parallel so what I am Telling
the first time one processor. I am activating
154
00:35:08,670 --> 00:35:23,670
second time to process and so on VI gets (a
b c d) A is the first B is the second of the
155
00:35:23,670 --> 00:35:38,650
first occurrence first index VI
calls
156
00:35:38,650 --> 00:36:28,969
median pair A now if ax is median.
Then if ax is median then I will be transmitting
157
00:36:28,969 --> 00:36:39,239
from here to here and here to here to the
next process p1 and from here to here and
158
00:36:39,239 --> 00:36:47,559
here to here to the other processor, so that
next iteration he gets this bound and he divides
159
00:36:47,559 --> 00:36:57,920
and so on is it clear ,so from here to here
this I have to set the index X so let us assume
160
00:36:57,920 --> 00:37:10,989
that p1 is the starting index p1 ,p1 is x
q1 is the starting index for the next processor
161
00:37:10,989 --> 00:37:22,990
which is x + 1 ,right up to this for the first
processor and from here onwards.
162
00:37:22,990 --> 00:37:47,130
To the next processor and here you have to
find out now if is ax < B Y if X < B Y then
163
00:37:47,130 --> 00:37:53,589
what B Y is the larger than this that means
this element will be smaller than ax this
164
00:37:53,589 --> 00:38:02,940
is the smallest element larger than this so
up to this I have to send it to first processor
165
00:38:02,940 --> 00:38:39,019
so p2 y-1 and q2 is 1 else P2=Y,Q=Y+1 so I
got p1 I got P2 got q1 I got q2 else P1 Is
166
00:38:39,019 --> 00:39:05,779
not p1 else b y b is the media so if by is
median p2 is your y u2 is your Y + 1 if by
167
00:39:05,779 --> 00:39:36,980
> B Y < X then p1 x-1q1 is your x else p1
is your x q1, your x+1.
168
00:39:36,980 --> 00:39:47,109
Right there is now next is I have to broadcast
bi shoots communicate to the next processor
169
00:39:47,109 --> 00:39:59,789
right do you want to send the value 2 p1 and
with this part and this part right this is
170
00:39:59,789 --> 00:40:08,999
so next iteration you observe so first is
p1 does the one next one P 1 and P 2 then
171
00:40:08,999 --> 00:40:23,849
P 1 P 2 P 3 P 4 so P2 sends the information
to this P 1 sends this information to this
172
00:40:23,849 --> 00:40:37,469
right next time p1 p2 p3 p4 p5 p6 p7 p8 8
processors you have to transmits this right
173
00:40:37,469 --> 00:40:47,049
that is the way you do broadcasting.
So if I in p3 p2 broadcast is data to whom
174
00:40:47,049 --> 00:41:09,989
p5 and p6 how to get that P I sense since
if ( a ,P1C,P2,)(q1,b,q2,d) A to the last
175
00:41:09,989 --> 00:42:10,369
index p1 C and the last index you have defined
which is p2 and first index just immediate
176
00:42:10,369 --> 00:42:28,559
this is X + 1 to the last index is B and because
it was initially A B now you will be sending
177
00:42:28,559 --> 00:42:53,630
A p1 and p2 and you want you want to be you
want to be similarly c p2 and decide to go
178
00:42:53,630 --> 00:43:06,789
into sent to 2I - 1.
So P sense if PI senses to this next iteration
179
00:43:06,789 --> 00:43:14,509
P I P I is nothing but the new P I guess this
four parameters he computes this and so on.
180
00:43:14,509 --> 00:43:30,130
So after login iterations (a, b, c, d) what
is A either starting in this PI a,b c,d PI
181
00:43:30,130 --> 00:43:47,069
has got this point right this is
A sequence and this is B sequence now you
182
00:43:47,069 --> 00:43:57,999
have to merge it right so you need to compute
in the C sequence what is the A starting index
183
00:43:57,999 --> 00:44:07,900
starting index right and the end index yes
or no know how to get the starting index starting
184
00:44:07,900 --> 00:44:17,529
in races every processor is doing now you
are dividing equal parts every processor is
185
00:44:17,529 --> 00:44:27,759
having that much size right because equal
part.
186
00:44:27,759 --> 00:44:34,969
So that if I multiplied by n then I get M
+ N and total number of elements yes so every
187
00:44:34,969 --> 00:44:42,819
processor is naming M + n divided by n block
so I processor starting index will be starting
188
00:44:42,819 --> 00:45:04,309
in this is if it is W it is this is the starting
index yes and what is your end index n index
189
00:45:04,309 --> 00:45:20,420
is minimum of items m + in your divided by
or M + s because last one may not be exactly
190
00:45:20,420 --> 00:45:27,339
our M + n divided by n last one may not be
exactly that size.
191
00:45:27,339 --> 00:45:33,529
So there may be little one too because you
are dividing by n so this maximum index is
192
00:45:33,529 --> 00:46:03,150
M + M so PI merge sequence uses sequential
merge algorithm too merge in a , b b c,d to
193
00:46:03,150 --> 00:46:21,859
store in c w ,z and to store it in c.
194
00:46:21,859 --> 00:46:33,940
Now if I have to compute the time complexity
this takes constant amount of time this takes
195
00:46:33,940 --> 00:46:41,239
constant amount of times this takes constant
amount of time this also takes constant amount
196
00:46:41,239 --> 00:46:56,729
of time.
Now what is the time needed here but what
197
00:46:56,729 --> 00:47:18,630
is the login here so and in the I, J up stage
at the J upstage what is the size you want
198
00:47:18,630 --> 00:47:29,799
to login whatever the J up at the first stage
if the first digit is M + n right so time
199
00:47:29,799 --> 00:47:42,029
needs log of M + n in the second stage M +
n divided / 2 right at the J up stage it is
200
00:47:42,029 --> 00:47:56,920
log of M + n divided /2 j- - 1 it is first
stage it is M + n same class M + n /2 and
201
00:47:56,920 --> 00:48:04,799
so on agreed.
So this is your inside the loop but summation
202
00:48:04,799 --> 00:48:12,930
if you I take then it becomes 1 so this becomes
whole algorithm becomes ordered log capital
203
00:48:12,930 --> 00:49:04,249
n log M + n what about this one sequential
Merge order why long it takes M + M + n / n
204
00:49:04,249 --> 00:49:14,359
because that is the maximum size you have
M + n / n because if there is no maximum size
205
00:49:14,359 --> 00:49:20,279
you have agreed so the total time complexity
becomes.
206
00:49:20,279 --> 00:49:44,210
So tm,n becomes order log N log m+n + m+n
/n now if M = n then this becomes order log
207
00:49:44,210 --> 00:50:26,609
in log small n + n / M so the cost becomes
in O (N log n,log n +n) so it is cost optimal
208
00:50:26,609 --> 00:50:55,690
if
small n is equals to order n log n log that
209
00:50:55,690 --> 00:51:33,130
is if n < = n / log 2 that is if n < = / log
2 so it is cost optimal if so we have discussed
210
00:51:33,130 --> 00:51:57,789
CIW merge algorithm so the next class we discuss
how can solve on CW and CRW.