Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:08,310 --> 00:00:14,640
High in this video I'm going to go over an algorithm called Prim's algorithm and what Prim's algorithm
2
00:00:14,640 --> 00:00:22,630
is at a high level is a greedy algorithm for finding the minimum spanning tree of a graph.
3
00:00:22,710 --> 00:00:27,840
If you don't know what that means that's perfectly fine it actually is a lot easier to understand than
4
00:00:27,840 --> 00:00:38,430
that it's finding or whatever path has the lowest value in a graph.
5
00:00:38,430 --> 00:00:48,130
And so it means that right here is you can see on the graph we have values like 3 and 4 in 1 to 7.
6
00:00:48,240 --> 00:00:52,790
Every single one of the edges has a weight associated with it.
7
00:00:52,860 --> 00:01:01,800
And what we're looking for is the path that connects all of the nodes together that has the lowest number.
8
00:01:01,830 --> 00:01:07,530
And I'm going to go through and show you how that works on a step by step basis and you'll understand
9
00:01:07,530 --> 00:01:09,380
how Prim's algorithm works.
10
00:01:09,390 --> 00:01:10,710
Once that happens.
11
00:01:10,890 --> 00:01:19,540
So the very first thing to do is to pick a node in the graph and it can be completely arbitrary.
12
00:01:19,650 --> 00:01:29,250
And so if you're new to graph algorithms and digraphs in general nodes are the circles like that and
13
00:01:29,250 --> 00:01:38,400
then the edges are the lines that connect those nodes together and the nodes are also called vertexes
14
00:01:38,580 --> 00:01:40,280
as well.
15
00:01:40,290 --> 00:01:44,670
I'm going to call them nodes for the sake of this discussion because that's how I learned them.
16
00:01:44,700 --> 00:01:47,390
So we're going to start at A.
17
00:01:47,550 --> 00:01:49,830
So this is going to be our starting point right here.
18
00:01:49,860 --> 00:01:56,580
We could have started though at Or we could have started anywhere we wanted but we're doing it just
19
00:01:56,580 --> 00:01:59,020
because I think it's a little bit easier to read it this way.
20
00:01:59,220 --> 00:02:05,200
And the very first thing we're going to do is we're going to look at a and we're going to see what edges
21
00:02:05,210 --> 00:02:07,790
it has connected to what nodes.
22
00:02:07,860 --> 00:02:16,730
So we can see right here that we have a is connected to B and the value of A to B.
23
00:02:16,740 --> 00:02:32,620
So if we go A to B there as a way of 3 then we have another option A to see an ADA see has a weight
24
00:02:32,670 --> 00:02:38,100
of for because then all we have to do is we just compare these two.
25
00:02:38,110 --> 00:02:43,150
We see that three is less than four.
26
00:02:43,240 --> 00:02:51,490
And so we know three is going to be the option which means A to B is going to be our very first option
27
00:02:51,520 --> 00:02:54,920
and that is essentially as easy as it gets.
28
00:02:54,940 --> 00:03:01,120
And the only complicated thing will happen here in a second I'll show you how that works.
29
00:03:01,120 --> 00:03:08,620
So the very first thing that we're going to do is I'm just going to color this so you can see the way
30
00:03:08,620 --> 00:03:10,420
it actually works.
31
00:03:11,050 --> 00:03:17,040
So we're going to go to be and we're going to take up that line.
32
00:03:17,060 --> 00:03:19,520
And now this edge is taking care of.
33
00:03:19,520 --> 00:03:24,380
So we know we're going here now that we have that set up.
34
00:03:24,470 --> 00:03:29,230
Now all we have to do is add in the other options.
35
00:03:29,300 --> 00:03:36,560
So if we want to connect to different nodes we can see that B has a few different options we can go
36
00:03:36,920 --> 00:03:43,150
B to either we can go BTD we can go B to C.
37
00:03:43,430 --> 00:03:51,080
And because of the way the Prim's algorithm works we can actually still go Atassi.
38
00:03:51,230 --> 00:03:53,150
So that one is still available to us.
39
00:03:53,150 --> 00:03:58,850
So if you think of the algorithm in a certain real world can't sense it's almost like a snowball as
40
00:03:59,180 --> 00:04:05,030
you add new edges and new nodes every step of the way.
41
00:04:05,150 --> 00:04:08,300
You add in all of their possible connections.
42
00:04:08,360 --> 00:04:17,240
So we started off with just when we had just a in our set we only had two options but now that we have
43
00:04:17,870 --> 00:04:25,730
a and b in our set now we have a to be or I'm sorry we have to see which we solve that one.
44
00:04:25,730 --> 00:04:28,830
Then we have three other options.
45
00:04:28,850 --> 00:04:35,400
And so now we have a total of four different items that we're going to be comparing.
46
00:04:35,720 --> 00:04:37,380
So let's do that.
47
00:04:37,430 --> 00:04:43,230
So we'll just take a nice easy approach and just go from top to bottom with it.
48
00:04:43,640 --> 00:04:52,040
So B-D is 5 to DS 2 b c is 1 and A to C is 4.
49
00:04:52,130 --> 00:04:57,580
So we can see pretty easy B to C is switch colors.
50
00:04:57,710 --> 00:05:00,920
B c is going to be our best option.
51
00:05:00,920 --> 00:05:03,790
So we now have those points connected.
52
00:05:04,040 --> 00:05:12,500
Another important thing to know about Prim's algorithm is once once an edge is connected or once a node
53
00:05:12,500 --> 00:05:18,770
has been reached I should say then we don't have to worry about connecting it again.
54
00:05:18,770 --> 00:05:25,030
So in other words this a c is no longer necessary.
55
00:05:25,040 --> 00:05:33,560
So this for we can cross off because A is now in our set B's and our set in C is in our set.
56
00:05:33,590 --> 00:05:37,440
So there is no reason to go from A to C anymore.
57
00:05:37,450 --> 00:05:43,250
So we don't have to worry about that which makes it nice and that gets to be one item that gets crossed
58
00:05:43,250 --> 00:05:44,350
off our list.
59
00:05:44,420 --> 00:05:54,550
So now that we have these now we can see we still have BT which is five BDD which is two.
60
00:05:54,680 --> 00:06:00,530
Then we have seeded D which is 7 and C to F which is 8.
61
00:06:00,560 --> 00:06:05,410
It's pretty easy to see we have B to D is our best option.
62
00:06:05,540 --> 00:06:12,410
And the way Prim's works it's very easy you just keep on recursively going down adding different nodes
63
00:06:12,410 --> 00:06:15,610
and then adding in all of the items that they point to.
64
00:06:15,650 --> 00:06:24,620
So we need to get to E and F now and to do that we just compare all of the items whether it's from B
65
00:06:24,770 --> 00:06:31,920
D or C so we can see we have a five a 9 a three and a name.
66
00:06:31,970 --> 00:06:36,020
Obviously a three is our best option right there.
67
00:06:36,080 --> 00:06:46,560
And so now we have f covered and the last item is to connect the we have 5 9 and 6 5 is our best pick
68
00:06:47,330 --> 00:06:48,460
and there we go.
69
00:06:48,500 --> 00:06:57,440
So if I know the graphs a little bit messy so I'm going to actually write out what our best options
70
00:06:57,440 --> 00:06:58,390
are.
71
00:06:58,980 --> 00:07:00,200
So we have
72
00:07:02,950 --> 00:07:03,470
to be
73
00:07:08,440 --> 00:07:15,500
then we have to see and we have BTD
74
00:07:18,980 --> 00:07:23,040
then we have B E.
75
00:07:23,450 --> 00:07:24,800
And lastly we have deid
76
00:07:40,300 --> 00:07:41,060
And there you go.
77
00:07:41,170 --> 00:07:46,270
If you take each one of these items each one of these connections where are these nodes are connected
78
00:07:46,600 --> 00:07:51,830
you will have your minimum spanning tree for this graph and you use Prim's algorithm in order to get
79
00:07:51,830 --> 00:07:51,940
it.
80
00:07:51,930 --> 00:07:53,080
So good job.
81
00:07:53,080 --> 00:07:55,920
And let me know if you have any questions at all about that video.
8489
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.