1
00:00:16,840 --> 00:00:24,440
So, let us discuss some of other types of
paradigms as you of on limited discussing
2
00:00:24,440 --> 00:00:31,420
these paradigms with the assumption that you
have the basic knowledge of paradigms. And
3
00:00:31,420 --> 00:00:38,420
here, we like to like to mention about the
tangent problem we handle on the different
4
00:00:43,160 --> 00:00:43,629
types of paradigms.
5
00:00:43,629 --> 00:00:50,629
So the second paradigm what would like to
discuss today is greedy method. Now here let
6
00:00:53,059 --> 00:01:00,059
us assure that here N possible, there are
n possible entity, and you want find out the
7
00:01:13,000 --> 00:01:20,000
best solution are using the form this n n
p k. Now you see you wanted to be it is a,
8
00:01:22,380 --> 00:01:28,259
now you have to see select 1 by 1, so that
you get the best possible solution for your
9
00:01:28,259 --> 00:01:35,259
problem. So, initially you have say if I write
the algorithm greedy, genetic algorithm for
10
00:01:40,460 --> 00:01:47,460
greedy and A and n parameters, then we will
be write it that your solution initially is
11
00:01:49,170 --> 00:01:56,170
phi, which is null solution. And then you
have to select one after another so for i
12
00:01:56,280 --> 00:02:03,280
equals to 1 to n, 1 to n, and then x equals
to select 1 parameter out of n and you check
13
00:02:19,180 --> 00:02:26,180
you check with a by select x you that solution
this will lead to a feasible solution on not.
14
00:02:27,180 --> 00:02:34,180
If x leads to feasible solution to a feasible
solution of your problem solution of the problem
15
00:02:45,330 --> 00:02:52,330
then you update your solution by solution
u will in x otherwise, you discard x otherwise
16
00:03:01,380 --> 00:03:08,380
discard else discard x. That means that this
x will not lead to the feasible solution this
17
00:03:12,570 --> 00:03:18,950
is the simple study we follow we follow under
the greedy strategy.
18
00:03:18,950 --> 00:03:25,950
Now one example is there is I do not know
whether you have seen the tests magnitude
19
00:03:30,540 --> 00:03:37,540
test these are large twice and is to store
the data of phi into this magnitude test.
20
00:03:43,220 --> 00:03:50,220
And suppose you have the files F 1, F 2, F
3 and F n and files and the cycle of this
21
00:03:57,160 --> 00:04:04,160
phi is S 1,S 2,S 3 and S n. Now, let know
store this phi the magnetic in such a way
22
00:04:07,710 --> 00:04:14,710
that it takes the minimum amount of phi so
suppose I have the 3 files F 1, F 2, F 3 and
23
00:04:21,539 --> 00:04:28,539
the size of the phi is say 20,10 and say 5,
or 20,10 and that 15. So, if I starts saving
24
00:04:35,789 --> 00:04:42,789
that first phi if F 1 then F 2 then F 3 then
the total number of decode movements in that
25
00:04:46,249 --> 00:04:53,249
magnitude is that first time it will be saving
through F phi. F 1 of size 20 records and
26
00:04:54,199 --> 00:05:01,199
then it will rewind that so second time if
I strokes F 2. Say first to move up to this
27
00:05:02,879 --> 00:05:09,879
and then it will write again 10. So, here
you need that 30 record movements and third
28
00:05:13,710 --> 00:05:20,710
type so it will do not worry and then 15.
So, you need 45 record movements the total
29
00:05:24,080 --> 00:05:31,080
number of record movement becomes 20 plus,
30 plus, 45 which is the 95.
30
00:05:34,689 --> 00:05:41,689
Now if I think the other way that know 1 will
store first F 2 then, F 1 then F 3. What happens
31
00:05:42,539 --> 00:05:49,539
first time 10. Second time is 10 and 10, 20.
So, this becomes 30 and the third time this
32
00:05:55,499 --> 00:06:02,499
30 movements plus 15. So, this gives you 45
that mean 10 plus 30 plus 45 this gives you
33
00:06:12,999 --> 00:06:16,009
85 record movements.
34
00:06:16,009 --> 00:06:23,009
Now, what happens if I say the files in the
form F 2, F 3 and then F 1. First F 2, which
35
00:06:28,770 --> 00:06:35,770
is the 10 next times it is 10 plus 15. It
is 25 and then next time it is 45. So, it
36
00:06:38,809 --> 00:06:45,809
becomes 10 plus 25 plus 45. It becomes 80
and this is the minimum number of rdreco records
37
00:06:51,059 --> 00:06:55,460
to be moved to store all this 3 files into
the magnitude
38
00:06:55,460 --> 00:07:02,460
So, what is the strategy in follow here that
you have n files F 1, F 2, F n and with the
39
00:07:06,529 --> 00:07:13,529
size S 1, S 2 S n respectively. The x initially
solution is phi and for I equals to 1 to n.
40
00:07:14,860 --> 00:07:21,860
Now will be selecting x for from the set of
n you will be selecting the phi, which is
41
00:07:25,699 --> 00:07:32,699
the smallest 3 files. So S 1 is the solution
and is if lifts to F it will lift to the solution.
42
00:07:34,449 --> 00:07:41,449
So, you have storing and you solution solve
become solve in x 1 x 1 is x which is a smallest
43
00:07:43,589 --> 00:07:50,589
phi size and then next time again you take
grade the next smallest 1 and you add it and
44
00:07:52,270 --> 00:07:59,270
so on. So, this is the one strategy this is
the greedy method to solve the problem of
45
00:08:04,460 --> 00:08:06,860
storing the files into the magnitude.
46
00:08:06,860 --> 00:08:13,860
Now, on the next problem is well known to
you who is knap sack problem. Do know what
47
00:08:17,059 --> 00:08:23,919
is knap sack problem right? Is it clear? Yes.
Now, let me tell you what is knap sack problem.
48
00:08:23,919 --> 00:08:30,919
Let us a show direct you have that n n t ts
x 1 x 2 x 3 x n and to select x I you want
49
00:08:40,200 --> 00:08:47,200
the profit p i. So, you have the profit p
2, p 3, p n. To select p i you want the profit
50
00:08:53,320 --> 00:09:00,320
p i to select x i you want the profit p i
and each n t t has started weights say w 1
51
00:09:01,269 --> 00:09:07,370
is the weight at x 2, x 1 weight x 3 and weight
n.
52
00:09:07,370 --> 00:09:14,370
Now, there is existing a beam of capacity
a bean capacity M. Now from m is to select
53
00:09:20,889 --> 00:09:27,889
to select some of xs and put them into this
bean whose capacity is M such that you on
54
00:09:32,410 --> 00:09:39,410
the maximum profit. So, what is the problem
is to select some the xs and put them into
55
00:09:41,449 --> 00:09:48,449
the capacity bean of capacity m in such a
way that you want the maximum profit right.
56
00:09:49,250 --> 00:09:56,250
So, this problem is that if I shall w 3 weight
w y weights of item of x i then i are the
57
00:10:00,779 --> 00:10:07,779
profit p i and i want to maximize by profit
but, with the condition ways that that my
58
00:10:08,370 --> 00:10:12,990
bean can take up to the load of M to you need
of x.
59
00:10:12,990 --> 00:10:19,990
Now, 1 thing should be clear to you that my
bean, if it is not full then I should take
60
00:10:21,069 --> 00:10:28,069
some more items from here to get i into the
to put into the bean so that i n little more
61
00:10:29,899 --> 00:10:36,899
profits. So, I can write that maximize this
problem i can write maximize p i x i i is
62
00:10:45,990 --> 00:10:52,990
equals to 1. To n such that or subject to
the condition subject to the condition summation
63
00:10:56,569 --> 00:11:03,569
over w i x i i is 1 to n is less than equals
to m and x i is lying between 0 and 1 and
64
00:11:18,199 --> 00:11:25,199
ways of possibility right? So, what happens
the problem is that i have x 1 x, 2 x n are
65
00:11:25,279 --> 00:11:30,990
the n t ts p i is w i is the weight attacks
x i and p i is the profit attacks the item
66
00:11:30,990 --> 00:11:37,990
x i and you a bean of capacity M. You want
to select some of the x i such that your bean
67
00:11:42,380 --> 00:11:48,709
is full and also you are in the maximum profit.
So, the problem can be read if I maximize
68
00:11:48,709 --> 00:11:55,709
summation p i x i x i i equals to 0 i equals
to 1 to n subject to the condition that summation
69
00:11:55,959 --> 00:12:02,959
over w i x i less then equals to n and x i
is 0. And 1 what does it mean x is like by
70
00:12:02,970 --> 00:12:09,160
0 and 1 that it is 0. If you do not select
if it is 1 you select the whole item whole
71
00:12:09,160 --> 00:12:16,160
item means that you select w i o a ts total
right, or you can select you can select small
72
00:12:18,490 --> 00:12:25,490
amount of small amount of the w i of the x
i col x i ith comp1nt where ith i 10
73
00:12:27,259 --> 00:12:34,259
Now, how to solve this problem? So, let us
consider 1 small example suppose I have 3
74
00:12:37,339 --> 00:12:44,339
items item 1 item 2 and item 3 and the item
weight is 1 is having the weight 5. This weight
75
00:12:46,509 --> 00:12:53,509
is having 10 and this have weight is having
say 4 and I earn the profit is say 10. Here
76
00:12:57,350 --> 00:13:04,350
it can be 15 and it can be 7 6. No, it can
say 7. So, this is your weight and this is
77
00:13:26,600 --> 00:13:33,600
your profit. So, there that means that if
I shall 5 k unit of item 1 I earn the profit
78
00:13:36,190 --> 00:13:43,190
10 and if I shall 10 unit of item 2 I earn
the profit 14 for unit of item 3 I earn the
79
00:13:45,350 --> 00:13:52,350
profit of 7.
Now, I want as I have a capacity I have a
80
00:13:53,029 --> 00:14:00,029
capacity of say 8. Now, how to select this
8 unit of materials that 1 possible thing
81
00:14:08,939 --> 00:14:15,939
that I select item 1 completely and 3, 3 unit
from item 2; that means 5 3 and 0 another
82
00:14:26,170 --> 00:14:33,170
1 can be 2 6 and not 6. Say 2 4, 2 may be
1 possibility there of another possibility
83
00:14:40,610 --> 00:14:47,610
could be 3, 3 2 and so on. If it is the case
that how much I earned the profit or what
84
00:14:53,060 --> 00:15:00,060
is the profit amount? So I earned 5. So i
earned 10. Then i 3 that means 15 divided
85
00:15:02,819 --> 00:15:09,819
by 10 into 3. So, this will gives you so 14
point 5.
86
00:15:10,529 --> 00:15:17,529
Now, in this case 10 divided by 5 into 2 plus
15, divided by 10 into 4 plus 7, divided by
87
00:15:26,899 --> 00:15:33,899
4 into 2. So, this is 1 this 4 plus 1 to 5
6 plus 3 point 5. So, it is 13 point 5. Here
88
00:15:43,879 --> 00:15:50,879
10 by 5 into 3 plus 15 by 10 into 3 plus 7
by 4 into 3 you get 6 plus 4 point 4 plus
89
00:16:08,300 --> 00:16:15,300
3 point 5 is 14 and so on. So, you observe
that your this gives the better profit among
90
00:16:20,439 --> 00:16:27,439
them but, how to how to select or what should
be the best possible way to select weights
91
00:16:28,680 --> 00:16:35,680
so that so that your bean is 4 and I earn
the maximum profit. So, 1 possibility that
92
00:16:37,029 --> 00:16:43,930
because here you observe that we have proceeded
that weight as a factor weight is related
93
00:16:43,930 --> 00:16:50,199
with by profit so I should obtain what is
the per unit profit.
94
00:16:50,199 --> 00:16:57,199
Now, let us find out the per unit profit that
is p by w. So, it is2 it 1 point 5 and this
95
00:17:08,030 --> 00:17:15,030
is 1 point 7 5. This is 1 point 7 5 so, if
I that means that I must, I must there, I
96
00:17:18,819 --> 00:17:25,819
must I put as much as I can from the item
1 into by beans because I am getting final
97
00:17:28,730 --> 00:17:35,730
profit is maximum on item 1 and if it is the
case if it is the case. So, I will take 5
98
00:17:36,400 --> 00:17:43,400
unit, 5 unit of item 1, then next item is
coming this 1. Because this gives the possibility
99
00:17:46,190 --> 00:17:53,190
profit is maximum. So, what I should do? I
should take 3 unit directly from here 3 is
100
00:17:53,450 --> 00:17:59,659
directly from here. So, that and 0 unit from
here. So, that you get the maximum profit.
101
00:17:59,659 --> 00:18:05,929
So, if it is it is your selection is like
that then you are on profit 10 plus 0 plus.
102
00:18:05,929 --> 00:18:12,929
So, you earned the profit 15, 20, 5 which
is the maximum profit you can earn.
103
00:18:22,190 --> 00:18:27,990
So in terms of in terms of generally in term
if I consider the generalize problem that
104
00:18:27,990 --> 00:18:34,990
you have have the p 1, p 2, p n profit with
reference to item i you have the weight w
105
00:18:36,770 --> 00:18:43,640
y then the safest procedure is there you first
obtained the per unit profit for each item
106
00:18:43,640 --> 00:18:50,640
and after training the per unit for profile
in you. Select first the unit having the maximum
107
00:18:53,860 --> 00:18:59,820
profit and as much as you can into the bean.
108
00:18:59,820 --> 00:19:06,820
Now, after putting that maximum profit here
you grade the next highest profit 1 and try
109
00:19:07,350 --> 00:19:14,350
to put as much as you can into the bean and
so on. Finally, you will get some fraction
110
00:19:15,140 --> 00:19:22,140
some fraction will be there of the ith 1 to
put here. And if I add this you will be getting
111
00:19:25,730 --> 00:19:32,520
the maximum profit. So, that is the greedy
strategy for finding the knapsack problem
112
00:19:32,520 --> 00:19:36,700
finding for solving the knap sack problem.
113
00:19:36,700 --> 00:19:43,700
Now, let us consider another interesting problem
that is optimal merge patterns. Now what is
114
00:19:57,029 --> 00:20:04,029
the problem here problem is that suppose I
have files F1, F2, F3, Fn and the size is
115
00:20:11,600 --> 00:20:18,600
S1, S2, S3 Sn. So, you have n files and and
and phi i has the size or has the records
116
00:20:24,789 --> 00:20:30,110
number s i.
Now, the problem is that we want to merge
117
00:20:30,110 --> 00:20:37,110
all this files and to make into 1 phi. Do
you observed in the last class we discussed
118
00:20:37,840 --> 00:20:44,840
about the merging of 2 files, then there is
a need of data to patterns form that you know
119
00:20:51,080 --> 00:20:58,080
from 1 file to another file. So, if I have
a file A and you have another B and you want
120
00:20:59,120 --> 00:21:03,770
to merge you this 2 file.
121
00:21:03,770 --> 00:21:10,770
Suppose this is of size n, this is size m
and first what you do first you do what you
122
00:21:12,740 --> 00:21:19,740
do that this you need n plus m record movements
from for merging this 2 files.
123
00:21:26,510 --> 00:21:33,510
So, for this case also that how I if I merge
a F 1 with F 2 then, I need S 1 plus S 2 the
124
00:21:36,730 --> 00:21:43,110
record movement and then if I take this 1
and you need again a S 1 plus S 2 plus S 3.
125
00:21:43,110 --> 00:21:50,110
There are record book and so on now well that
you know our target should be such or our
126
00:21:50,590 --> 00:21:57,590
problem should be like that that you merge
this files into 1 file in such way that number
127
00:21:57,710 --> 00:22:04,710
of records movement is minimum. In minimum
there is 1 way could be that you first merge
128
00:22:05,980 --> 00:22:12,980
this 2 then you merge item with this 1 merge
item with this 1 and then merge item with
129
00:22:13,690 --> 00:22:20,690
this 1 and so on right. Another could be that
you merge this 2 this 2 and this 2 and this
130
00:22:20,720 --> 00:22:27,140
2 then merge with this 1 this 1 and so on.
There is several ways you can merge you can
131
00:22:27,140 --> 00:22:29,370
merge this n files.
132
00:22:29,370 --> 00:22:36,370
Let you have to understand the problem say
let us shown them I have five files F 1, F
133
00:22:38,020 --> 00:22:45,020
2 F 3 F 4 and F 5 and size of the files could
be 5 10 20 50 and may be this 1 is 30. So,
134
00:22:55,549 --> 00:23:02,549
if I merge for this 1, then this 1 and then
merge these 4 files and then these 4 pages.
135
00:23:07,169 --> 00:23:14,169
Then let us see a how many record movements
will be there to merge this 2 files you need
136
00:23:18,140 --> 00:23:25,140
25 record movements to merge this again 25.
Now to merge this 2 you need 50 and to merge
137
00:23:33,049 --> 00:23:40,049
this will be you need 80. So, the total number
of record movements becomes 25 50 and then
138
00:23:46,149 --> 00:23:49,429
80. So, it is becoming 108 record movement.
139
00:23:49,429 --> 00:23:56,429
Now let us think about the other way. You
have 10 15 5 20 30 and suppose I merge this
140
00:23:57,590 --> 00:24:04,590
1 first then this 1 then this 1 and then finally,
this 1 in that case you have 25 record movements
141
00:24:06,370 --> 00:24:13,370
here, 30 record movements, here 50 record
movements and here 80 record movements. In
142
00:24:14,360 --> 00:24:21,360
that case total becomes 25 plus 30 plus 50
plus 80. So, you get 185 record movements
143
00:24:25,320 --> 00:24:26,890
is it 195.
144
00:24:26,890 --> 00:24:33,890
Now what happens file first merge this 2 then
that merge item is merge to this 1 then whole
145
00:24:37,570 --> 00:24:44,570
thing is merge with this 1 and finally, merge
with this in that case here it is 15 here
146
00:24:51,549 --> 00:24:58,549
15 plus 15, 30, here it is 20 and here it
is 30. So, number of record movement becomes
147
00:25:01,419 --> 00:25:08,419
15 30 20 30. For 30 this is that 15, then
this is 30 and this is our 30 plus 20 50 and
148
00:25:20,179 --> 00:25:27,179
this becomes 80 50 and this becomes 80 so
this is 40 plus 95 175.
149
00:25:31,330 --> 00:25:38,330
Now can you tell me what is the best way to
do it? Yes what you have to do is that you
150
00:25:41,610 --> 00:25:48,610
first try to get the file with this smaller
size and you try to merge them, then you select
151
00:25:48,960 --> 00:25:55,309
again its file with the smallest 1 and you
try to merge say for example, suppose this
152
00:25:55,309 --> 00:26:02,309
is instead of 15 it is 25 in that case instead
of finding this 1 so merge this 1 you.
153
00:26:14,269 --> 00:26:21,269
Should have merging this 1 then it becoming
is becoming file size becoming 30 five now
154
00:26:24,120 --> 00:26:31,120
that this file this file and this file among
this 3 that you consist the 2 of smallest
155
00:26:32,750 --> 00:26:39,610
1 with towards the smallest 1. So, this size
becomes 55 and then you merge this 1, ok.
156
00:26:39,610 --> 00:26:42,260
So basically I finally, present in the form
of tree. So, what I will do I’ll be putting
157
00:26:42,260 --> 00:26:49,260
first 5 and I merge with the 10, I get 15
and then I get 20 you get 30 5 and then this
158
00:27:03,919 --> 00:27:10,919
side you get 25 and 30 you get 55 you get
90. So, there will number of record movements
159
00:27:25,549 --> 00:27:32,549
will be 3 times of file 3 times with this
five raise to move up to this so 3 times five
160
00:27:37,990 --> 00:27:44,990
plus 3 times of 10 plus 20 into 2 times plus
20 into 2 plus 30 into 2. So, the effect is
161
00:27:54,159 --> 00:28:01,159
15 plus 30 plus 40 plus 50 plus 60. This gives
you 150 150 195. So, that is the way you can
162
00:28:14,320 --> 00:28:21,320
solve this. So, what you do here the first
thing is that you have pick up that 2 files
163
00:28:23,409 --> 00:28:30,409
with th smaller in size at the bottom most
level and then merge them and you fix up the
164
00:28:32,059 --> 00:28:39,059
2 smallest files again merge them and so on,
so, formula becomes d.
165
00:28:42,730 --> 00:28:49,730
i into wi where wi is the wi is the number
of records in the i-th file and bi is the
166
00:28:54,409 --> 00:28:59,389
distance of the i-th record from the root.
167
00:28:59,389 --> 00:29:06,389
Now the next type of problem which we can
solve through greedy method is finding the
168
00:29:06,470 --> 00:29:13,470
minimum spanning tree. Suppose give me the
weighted graph say this is the weighted graph
169
00:29:24,299 --> 00:29:31,299
you have and you want obtain the tree from
these weighted graph solve the some of the
170
00:29:58,710 --> 00:30:05,710
ways minimum and first thing is that if I
forget about this, I would looking for the
171
00:30:15,889 --> 00:30:21,779
spanning tree then from this tree from this
graph you have to find out that the tree with
172
00:30:21,779 --> 00:30:28,779
n minus 1 edges and n minus 1 edges that will
give you the spanning tree. So, for this graph
173
00:30:29,659 --> 00:30:36,659
1 spanning tree could be this 1. So, this
is 10 5 and 8. Another spanning tree could
174
00:30:46,200 --> 00:30:53,200
be this 1 which is weight is 10 7 and 5, another
spanning tree could be 5 7 and 8 sorry not
175
00:31:10,139 --> 00:31:17,139
7 it is 12 and the cost of the spanning in
case the some of this weights. So, the spanning
176
00:31:20,610 --> 00:31:22,080
tree having the minimum cost is not a minimum
spanning tree.
177
00:31:22,080 --> 00:31:28,830
Now the 2 well known, if you remember correctly
there are 2 well known algorithm exist 1 is
178
00:31:28,830 --> 00:31:35,830
the fixed algorithm, another 1 is kruskal’s
algorithm. So, let us discuss kruskal’s
179
00:31:40,330 --> 00:31:44,049
algorithm and a.
180
00:31:44,049 --> 00:31:51,049
Let see me the graph, we write the set of
vertices v and e is the number of edges and
181
00:31:56,080 --> 00:32:03,080
you have to assume that number of vertices
is n and w i or weight is assigned w i is
182
00:32:09,710 --> 00:32:16,710
the weight ascend to the eage between v i
and v j. w i j is the weight assigned to v
183
00:32:23,919 --> 00:32:30,919
i and v j.
Now since I have to obtain 1 tree, so, the
184
00:32:33,110 --> 00:32:40,110
tree becomes tree initial it is not tree and
we have to finally, obtain the possible the
185
00:32:47,059 --> 00:32:54,059
tree was having number of the vertices varies
number of varies as n minus 1. Now, initially
186
00:32:55,029 --> 00:33:02,029
this weights here n is the raise in increasing
order initially it is a raise increasing order.
187
00:33:04,070 --> 00:33:11,070
This with respect to the weights right edges
are a raise in the order of in the increasing
188
00:33:13,299 --> 00:33:20,299
order of its weight.
Next now the algorithm will be like that,
189
00:33:20,860 --> 00:33:27,860
while the number of edges here in the tree
is less than n minus 1 and e is not empty
190
00:33:45,630 --> 00:33:52,630
t is relevant number of edges in the tree
is less than not at n minus 1 and e is not
191
00:34:01,789 --> 00:34:08,789
empty. Select edge u v with minimum cost with
you select an edge from e having the minimum
192
00:34:24,359 --> 00:34:31,359
cost from e, ok.
Now you see after selecting this u v you have
193
00:34:38,409 --> 00:34:45,409
to say can I put it in my tree? What it means
that if I put it in my tree you should not
194
00:34:48,089 --> 00:34:55,089
create a cycle, that is
the thing you have to see. So, the check whether
tree union u v creates a cycle or not, if
195
00:35:23,060 --> 00:35:29,030
yes then you cannot considered u for your
minimum spanning tree you. Just discard u
196
00:35:29,030 --> 00:35:36,030
v form e, else it is not delete you acts as
tree union u v and you delete u v form e.
197
00:35:58,280 --> 00:36:05,280
So, of that kruskal’s algorithm, so, what
it does initial t is 5 while number of edges
198
00:36:10,520 --> 00:36:17,520
with less than n minus 1 and is not empty,
selecting each u v from e that is must at
199
00:36:24,490 --> 00:36:30,890
the minimum cost and then you see if I add
this 1 we are in t it should not creates a
200
00:36:30,890 --> 00:36:36,980
cycle. If it creates cycle then you will be
discard u v from e and otherwise you add it
201
00:36:36,980 --> 00:36:43,609
into the tree and then result will be form
e. Now after coming out from this while loop
202
00:36:43,609 --> 00:36:50,609
you check with the number of edges in tree
is n minus 1 if not it is n minus 1. You got
203
00:36:51,290 --> 00:36:56,640
the minimum for spanning tree, so, that is
the kruskal’s algorithm.
204
00:36:56,640 --> 00:37:03,640
Now next part able is Dynamic program. This
is also 1 of the most we known paradigms what
205
00:37:10,260 --> 00:37:17,260
we used in different places 1, simple 1 is
that finding the all pairs for this first
206
00:37:17,770 --> 00:37:24,770
problem and here the strategy is like that
suppose you have a graph with a graph g and
207
00:37:31,119 --> 00:37:38,119
you have to find the all what you do that
given the cost matrix o a t matrix that you
208
00:37:43,230 --> 00:37:50,230
see by inclusion of k-th node whether k-th
node ,whether your cost or path becomes shorter
209
00:37:53,940 --> 00:37:57,859
or not if it is shorter you consider that
is a strategy is feasible that suppose you
210
00:37:57,859 --> 00:37:58,930
have i.
211
00:37:58,930 --> 00:38:05,930
At any instant of j you have the shorted path,
you have the paths or solve this paths by
212
00:38:11,150 --> 00:38:18,150
a some nodes or by a some vertices lying between
1 to k minus 1. Now by if you have that the
213
00:38:22,930 --> 00:38:29,930
k-th stage what we do that we like to introduce
the node k and you check the path weight of
214
00:38:38,000 --> 00:38:45,000
the path from i to k and k to j. If you find
the sum of this weight is less than this the
215
00:38:46,079 --> 00:38:53,079
original weight whatever you obtained from
i to j then instead of using this path you
216
00:38:53,750 --> 00:39:00,750
know, use this path that is the idea. So,
dynamically you are updating your shortest
217
00:39:02,839 --> 00:39:04,079
this path.
218
00:39:04,079 --> 00:39:11,079
Suppose you have written this algorithm, suppose
c i c j is the weight of first of the a i
219
00:39:18,390 --> 00:39:25,390
j. Now I do not want to talk so for i equal
to 1 to n for j is equals to 1 to n. Say a
220
00:39:33,030 --> 00:39:40,030
i j is your c i c j and will up by this 1.
a i j is a matrix for all path the algorithm
221
00:39:45,440 --> 00:39:52,440
is for k equals to 1 to n for i equals to
1 to n, for a is equals to 1 to n a i j in
222
00:40:11,490 --> 00:40:18,490
a replace by minimum of a i j and a i k plus
a k j what is feasible meaning of that I am
223
00:40:25,450 --> 00:40:27,589
consider. Whatever the shortest path I have
using I have it will i and j using the vertices
224
00:40:27,589 --> 00:40:34,589
1 2 3 up to k minus 1 and I have the shortest
path to perform i to k using the vertices
225
00:40:34,849 --> 00:40:41,849
1 to k minus 1. At last the shortest path
using the vertices 1 to k but, in between
226
00:40:45,119 --> 00:40:52,119
k and using the vertices 1 to k minus 1 and
that will replace by the . This is the 1 thing
227
00:40:53,790 --> 00:41:00,730
and similar type of a program we will stable
such type of program with using the binary,
228
00:41:00,730 --> 00:41:02,569
using the dynamic programming.
229
00:41:02,569 --> 00:41:09,569
The another program is where we solve using
the binary using the dynamic program is optimal
230
00:41:12,650 --> 00:41:19,650
binary search tree. Here what happens the
program is that suppose we have the several
231
00:41:37,730 --> 00:41:44,730
identifiers say id 1 id 2 and id 3 say id.
There are n identifiers and you know this
232
00:41:54,410 --> 00:42:01,410
identifiers like to search several time for
example, in the case compiler you have started
233
00:42:03,569 --> 00:42:10,380
the if then else this are the identifiers
and we have retain suppose if 1, now I want
234
00:42:10,380 --> 00:42:17,380
to check for the equal reason, I am defined
not, so, we will be searching it right say
235
00:42:17,410 --> 00:42:24,410
for example, I have if another 1 then another
1 is end another 1 is while right.
236
00:42:29,829 --> 00:42:36,829
Say I have these 4 identifiers; so, I can
arrange this or put there in the form of binary
237
00:42:38,130 --> 00:42:45,130
tree. Say for example, if I write if is the
identify first and this side it will be end,
238
00:42:46,359 --> 00:42:53,359
this side it may be while and this side may
be else, then another 1 could be you have
239
00:42:58,819 --> 00:43:05,819
know I want to put here, then this side it
is n, this side it is if this side it is your
240
00:43:11,849 --> 00:43:18,849
while. Another way I can put is that can be,
say then this side it is if this side it is
241
00:43:34,980 --> 00:43:41,980
end, this side it is while. There are several
ways, all the way you can have. Now suppose
242
00:43:44,609 --> 00:43:51,609
I want to check there whether if exist, if
1 exist so, what I will do? I will search
243
00:43:52,329 --> 00:43:59,329
here. Now I will come here, if now will be
looking whether there exists anything or not.
244
00:44:00,109 --> 00:44:05,930
If it is got then you will tell this is not
there. Now similarly, in the case with suppose
245
00:44:05,930 --> 00:44:12,930
while exist or not 1 will come here then it
has exist and it becomes successful also.
246
00:44:15,400 --> 00:44:22,400
So, there is question of coming that number
of comparison you will be performing. Now
247
00:44:29,640 --> 00:44:36,640
find out with that can identify exist or not.
Now for this case are if it is a successful
248
00:44:39,630 --> 00:44:46,630
search a then very number of comparison required
for than is 1 for, if it is 2 comparisons
249
00:44:46,800 --> 00:44:53,800
for end, 3 comparisons for while. This 3 comparison
so this is 6 plus 6 plus 5 is 11 number divided
250
00:44:59,250 --> 00:45:06,250
by 4. So, that is your while number of comparison.
Ok but, for this case it is also 1 2 plus
251
00:45:12,849 --> 00:45:19,849
2 plus 3 and it becomes 5 plus 3 8 divided
by 4. So, it becomes 2 so this is that I assume
252
00:45:28,970 --> 00:45:35,970
that there are equal lightly than for successful
search it becomes 2 point 75 algorithm numbers
253
00:45:36,260 --> 00:45:43,260
of comparisons. In this case it is 2 in that
case also it will become the 2 point 75 end.
254
00:45:44,380 --> 00:45:51,240
So 1 our target should draw at the tree such
way that there algorithm number of comparison
255
00:45:51,240 --> 00:45:58,240
is minimum. Now the here we assume that all
of them are equal lightly and also we assume
256
00:45:58,490 --> 00:46:05,490
that that it is only the but, what happens
really this suppose I go for if 1, that it
257
00:46:05,869 --> 00:46:09,450
becomes failure 1, right. So, there is not
only that we are assuming that equal lightly
258
00:46:09,450 --> 00:46:16,450
which is not Correct because, so, I depend
upon the program or program i has…
259
00:46:17,380 --> 00:46:24,380
So, 1 part is that the question is coming
for the identifier i d1 the probability of
260
00:46:26,960 --> 00:46:33,960
successful saw this p i and there will be.
You observe that if I have the 4 n identifier
261
00:46:35,589 --> 00:46:42,589
than there will be n plus 1 failure such that,
if i have 1 item say d 2 and in the case become
262
00:46:45,180 --> 00:46:52,180
here to the does not exist. So, it becomes
failure search. So, there will be q 0 q 1
263
00:46:52,420 --> 00:46:59,420
q 2 q q i. q 0 q 1 and q n that many they
this are the probabilities for unsuccessful
264
00:47:08,380 --> 00:47:14,980
search. The face this will lead to this class
of identifier this will lead to this i this
265
00:47:14,980 --> 00:47:19,869
unsuccessful search leads to this class of
items and so on right.
266
00:47:19,869 --> 00:47:26,869
So, since such that summation over p i plus
q i plus q 0 is 1 p i is the probability for
267
00:47:37,069 --> 00:47:44,069
a i-th identifier successful search and q
1 q 0 q 1 q 2 q n are the probability for
268
00:47:44,290 --> 00:47:51,290
unsuccessful searches. Then you have q0 plus
summation over p i q of plus q i is equal
269
00:47:55,890 --> 00:48:02,890
to 1. Now what is the problem? Now problem
is that you have to put the identifier in
270
00:48:04,140 --> 00:48:11,140
such way that in such a way that
the cost is minimum cost means that if I put
i-th identify in the level t than t i then
271
00:48:22,859 --> 00:48:28,780
you will be writing as p i and t i and so
on.
272
00:48:28,780 --> 00:48:35,780
So, the cost should be minimum. So, this can
be solved in using the dynamic programming.
273
00:48:39,950 --> 00:48:43,630
Now the next type of probable you have parallel
is back techniques and ensuring form I think
274
00:48:43,630 --> 00:48:50,630
this also I do not want to discuss merge on
this but, what I want to tell that say 1 problem
275
00:48:55,240 --> 00:49:02,240
is that graph coloring problems. So, what
you do that you have any tree of the in quotation,
276
00:49:08,579 --> 00:49:14,099
you want to color 1 by vertex by 1 color and
then will go to the next vertex and put the
277
00:49:14,099 --> 00:49:18,500
color another type of color and you check
that if by putting that color it fails to
278
00:49:18,500 --> 00:49:25,500
satisfy the criteria of graph coloring than
if it is then go back and then go off and
279
00:49:27,670 --> 00:49:32,079
previous note you just to put different type
of color.
280
00:49:32,079 --> 00:49:38,589
So, that is 1 example of back techniques similarly,
the case n queen problems that to problem
281
00:49:38,589 --> 00:49:45,589
is to put the queen in the chess board that
is cost n chess board in such way that no
282
00:49:48,069 --> 00:49:50,619
queen attach the Other queen.
283
00:49:50,619 --> 00:49:57,619
For example, I have a chess board of cost
4 and I have the 4 queens q 1 q 2 q 3 q 4.
284
00:50:08,619 --> 00:50:15,619
So, 2 queens at each other diagonally or if
I place them diagonally or what is row algorithm
285
00:50:16,049 --> 00:50:23,049
just fitting the saving for us. I would put
this incident in such way that they are not
286
00:50:26,280 --> 00:50:33,280
on the same diagonal. Not 1 the same row and
not on that same column. So, if I put q 1
287
00:50:35,809 --> 00:50:42,809
here q 2 cannot be put here q 2 so I can put
q 2 here. Once I put the q 2 here which like
288
00:50:48,319 --> 00:50:55,319
put q 3 I cannot put here I cannot put here
I cannot put here I cannot cannot I cannot
289
00:50:56,410 --> 00:51:02,280
put here I cannot put here I cannot put here
I cannot put here I cannot put here. Once
290
00:51:02,280 --> 00:51:09,280
I cannot put here q 2 q 3 and so what shall
I do with the assume for 2 simplify and under
291
00:51:12,130 --> 00:51:19,130
the assume that q i is placed with the ith
row. q i is placed in the ith row. So, q 3
292
00:51:22,839 --> 00:51:28,619
should be place here now. Once I know that
q 3 position cannot be this 1, cannot be this
293
00:51:28,619 --> 00:51:35,490
1, cannot be this 1, cannot be this 1, what
shall I do? I back track. Now I tried to put
294
00:51:35,490 --> 00:51:42,490
the position of q 2 so cannot be here. Let
us assume that q 3 is here. Now what happens
295
00:51:44,579 --> 00:51:51,579
q 3 cannot be here q 3 yes q 3 can be here
so, I put q 3 is here right because it is
296
00:51:52,480 --> 00:51:57,750
not attacking by q 1 is attacking by q 2 now
what happens what shall I put what can I put
297
00:51:57,750 --> 00:52:04,750
by q 4.
Cannot put q 4 here q 4 no q 4 no q 4 no q
298
00:52:05,079 --> 00:52:12,079
4 here. Now what shall I do know now you back
track cannot put q 3 here so cannot put q
299
00:52:13,579 --> 00:52:20,579
3 here no, now cannot put q 2, q 2 has g1
so, I have to put q 1 independently so I can
300
00:52:24,780 --> 00:52:31,780
put q 1 here. Now if I put q 1 here what shall
I put p 2? I cannot put here here here, I
301
00:52:32,579 --> 00:52:39,579
can put here. Now if it is case when can I
put q 3 q 3 I can put here if I put q 3 here
302
00:52:44,069 --> 00:52:51,069
q 4 I can put here. So, you observe that you
got a solution where they do queen is attacking
303
00:52:53,089 --> 00:52:58,520
each other, now there may be several. It is
not the unique solution. You may have the
304
00:52:58,520 --> 00:53:05,520
defined cost depend way is to put this values
for queens in the chess board to get the solution.
305
00:53:07,500 --> 00:53:14,500
Get so, this is all about the back tracking.
Similarly, you can solve you can find out
306
00:53:16,880 --> 00:53:23,880
the branch in found and the other thing from
it. 1 thing! Want to discuss is the traversal
307
00:53:26,010 --> 00:53:33,010
algorithm. This we need to know for a word
which is studied. Now first problem is that
308
00:53:41,130 --> 00:53:48,130
suppose I am a binary tree and I want to traverse
is binary tree. How you can traverse? Now
309
00:53:53,700 --> 00:54:00,690
we tell this is the root and you have the
left of tree and you have the right side,
310
00:54:00,690 --> 00:54:05,010
ok.
Now you want to traverse is binary tree with
311
00:54:05,010 --> 00:54:12,010
nodes. There are several ways you can traverse.
1 way could be I traverse first root than
312
00:54:14,730 --> 00:54:21,730
lefts of tree than rights of tree and recovers
ably. I go for the left of the tree root left
313
00:54:22,819 --> 00:54:29,390
side and so on another 1 could be roots right
side tree left side tree another 1 could be
314
00:54:29,390 --> 00:54:36,390
left side tree roots right side tree another
1 could be right side tree root left side
315
00:54:36,859 --> 00:54:43,460
tree. Another 1 could be left side tree root
right side tree root for right side tree left
316
00:54:43,460 --> 00:54:50,460
side tree. These are the 6 possible ways you
can traverse binary tree. Now see this is
317
00:54:50,900 --> 00:54:57,900
the basically the mirror of this saw clearly
we do not consider this thing. So basically
318
00:54:59,099 --> 00:55:04,539
we have the 3 ways to traverse a binary tree
1 is the root.
319
00:55:04,539 --> 00:55:10,930
First then right left sub tree then right
sub tree. Another 1 is the left sub tree root
320
00:55:10,930 --> 00:55:17,930
right sub tree another 1 is left sub tree
root right sub tree. Say for example, if I
321
00:55:18,079 --> 00:55:25,079
have a b c d e f g h suppose. You have this
binary tree now this traversal is known as
322
00:55:59,440 --> 00:56:04,369
pre order traversal.
323
00:56:04,369 --> 00:56:11,369
This is known as in order and this is known
as post order. So, if I traverse pre order
324
00:56:20,480 --> 00:56:27,480
using the preorder traversal then pre order
tells the root first the left sub tree then
325
00:56:33,270 --> 00:56:35,440
right sub tree now left.
326
00:56:35,440 --> 00:56:42,440
Sub tree. this 1 so if i traverse then bs
,then left sub tree, b then g then e then
327
00:56:47,069 --> 00:56:54,069
h then l then k then c and then f now. If
I traverse in order then first is left sub
328
00:57:05,690 --> 00:57:12,690
tree then root right sub tree. Now you have
come here first is left sub tree then note
329
00:57:13,130 --> 00:57:17,900
the right sub tree. Now you have to first
left sub tree, then root. So, it becomes that
330
00:57:17,900 --> 00:57:24,900
g d b then h l e k a c f and if I traverse
post order then left sub tree right sub tree
331
00:57:52,140 --> 00:57:59,140
and then root. Now left sub tree right sub
tree then root then left sub tree right sub
332
00:58:01,530 --> 00:58:08,530
tree. So g d right sub tree left sub tree
right sub tree is l h k e b f c a. So, I think
333
00:58:30,150 --> 00:58:36,599
this is remember we discuss all this things
in your undergraduate curriculum? The 1 thing
334
00:58:36,599 --> 00:58:43,599
you remember that if I know 1 of the design
the preorder the post order and in order,
335
00:58:45,339 --> 00:58:52,339
I can generate the binary tree you need to
but, this is of the case if I know. Preorder
336
00:58:52,980 --> 00:58:59,250
post order are the not the in order you have
to see that. Suppose I know that preorder
337
00:58:59,250 --> 00:59:06,250
and in order let us see how we can generate
the binary tree.
338
00:59:11,059 --> 00:59:18,059
See from the preorder I can get the, who is
the root of this node, which is a now from
339
00:59:19,890 --> 00:59:26,890
the in order. I can find out what are the
elements in the left sub tree are and what
340
00:59:30,670 --> 00:59:36,470
are the elements in the right sub tree. So,
this gives you that this much is 1 the left
341
00:59:36,470 --> 00:59:43,470
sub tree this much is on the right sub tree.
Now from preorder again I can find out who
342
00:59:44,579 --> 00:59:50,609
is the root which is b again. From here I
can find out this is the left sub tree and
343
00:59:50,609 --> 00:59:57,609
this is the right sub tree of d. So, g d is
here and this is here now from the d g g d
344
01:00:01,730 --> 01:00:08,730
i get d. So, I know the d is the root and
g is d is the root and from here you get the
345
01:00:13,700 --> 01:00:20,700
g is the left of d. So, it is g now once you
know d. So, I have d1. This I have d1 this
346
01:00:30,660 --> 01:00:37,660
1 this 1 this 1. Now I have this part is the
root, so, I write here e once I know the e
347
01:00:43,270 --> 01:00:50,270
is here h and l is the left sub tree of e
h and l so h is the root and l is the right
348
01:00:53,180 --> 01:01:00,180
sub tree
and e k k is the right of e. So, k then you
have c and f c is the root and f is the right.
349
01:01:15,549 --> 01:01:20,910
So, similarly, if I have the preorder and
I have if I have the post order in order,
350
01:01:20,910 --> 01:01:26,819
I can generate the binary tree uniquely. But,
in the case I if I have the preorder and post
351
01:01:26,819 --> 01:01:33,750
order I am in problem. I know from the preorder
I can get the root of the tree which is here
352
01:01:33,750 --> 01:01:40,440
but, after that I do not know which are elements
in the left side and right side. So, as the
353
01:01:40,440 --> 01:01:44,020
result you cannot go out the conclusion of
it.
354
01:01:44,020 --> 01:01:49,930
Now the next 1 is that traversing the graph.
If I complete this 1 then you have the preliminary
355
01:01:49,930 --> 01:01:56,930
of algorithms particle but, so, then you can
go for the parallel algorithm materials. So,
356
01:01:57,099 --> 01:02:04,099
there are 2 way we can traverse the graph,
1 is known as breadth first search in short.
357
01:02:08,160 --> 01:02:15,160
We tell bfs another 1 is known as depth first
search in short dfs. So, difference between
358
01:02:31,609 --> 01:02:38,609
these 2 is then suppose
359
01:02:47,970 --> 01:02:54,970
you have a graph suppose and what I have it
does that 1 is that you first go to select
360
01:02:55,900 --> 01:03:02,900
this, visit this slope and then you visit
all vertices of its set. Then for each adjacent
361
01:03:06,109 --> 01:03:11,809
then you go to the next 1 and so on so it
is the level wise basically you have search
362
01:03:11,809 --> 01:03:17,980
The other method is that you saw first search
is to come to this, then you come to this,
363
01:03:17,980 --> 01:03:22,660
come to this, then you finish this, 1 then
you finish, 1 that is known as depth first
364
01:03:22,660 --> 01:03:27,380
search. Hopefully, you remember those things
but if I have to write the algorithm for breadth
365
01:03:27,380 --> 01:03:30,289
first search say we can.
366
01:03:30,289 --> 01:03:37,289
Write bfs and v is the starting node then
visited v is 1 and I write u equals to v now
367
01:03:52,900 --> 01:03:59,900
for all vertices in also initialize q is empty,
for all vertices u adjacent from u do the
368
01:04:31,920 --> 01:04:38,920
following. If visited w is 0 that means not
here visited then add w in q and visited w
369
01:05:00,099 --> 01:05:07,099
in set to 0 set to 1 set to 1. So, this comes
under follow and
if q is not empty not empty delete u from
370
01:05:35,329 --> 01:05:42,329
q
371
01:06:00,140 --> 01:06:07,140
and repeats and repeat this ok.
So, what it does basically for all vertices
372
01:06:14,059 --> 01:06:21,059
w and adjacent from u do the following if
visited w. w is not at visited then we visit
373
01:06:21,770 --> 01:06:28,770
that 1 and you add w into the q and you repeat.
This if for all adjacent vertices that is
374
01:06:30,400 --> 01:06:37,400
the same level takes and if find that q is
not empty you take the 1 delete 1 element
375
01:06:39,920 --> 01:06:46,920
from q from the top and repeat this process
because you have to find out w and so on.
376
01:06:48,020 --> 01:06:55,020
But, in the case of that depth first in the
case of depth first the is little you go into
377
01:06:57,190 --> 01:07:02,630
the depth of the graph and the algorithm becomes
different.
378
01:07:02,630 --> 01:07:09,630
Little different dfs u and visited algorithm
becomes like that v is equal to 1 and for
379
01:07:20,510 --> 01:07:27,510
all vertices w adjacent to u adjacent to v
if you have consider v do the following if
380
01:07:51,089 --> 01:07:58,089
visited be visited w is equals to 0. Then
dfs w here it is that you check what we are
381
01:08:04,849 --> 01:08:11,849
doing that you check with the visited. This
dfs v that visited v is 1 I am setting it
382
01:08:11,910 --> 01:08:18,190
first. Now for all vertices of w adjacent
to v, you see whether w is visited or not
383
01:08:18,190 --> 01:08:25,190
if not then you call dfs w otherwise, you
go to the next adjacent vertex of v and you
384
01:08:27,210 --> 01:08:34,180
check again and so on. So, that is the dfs
of depth first search and this algorithm is
385
01:08:34,180 --> 01:08:41,140
where the unit it prevent the q.
So, with these we like to stop or leave the
386
01:08:41,140 --> 01:08:47,950
here today and we assume that we are discuss
the different paradigms on sequential algorithms.
387
01:08:47,950 --> 01:08:55,970
So, next class will be discussing about that
need of parallel algorithms, the 5 types of
388
01:08:55,970 --> 01:09:02,970
parallel models and will start designing.
So, other differences. Thank you.