﻿1
00:00:00,500 --> 00:00:08,210
‫So now we're going to create our add vertex function and add vertex is just going to create one of these

2
00:00:08,570 --> 00:00:14,870
‫and the next video will learn how to add edges and then we can create a graph like this.

3
00:00:15,230 --> 00:00:18,410
‫But for now, we're just creating this.

4
00:00:18,710 --> 00:00:23,150
‫And when we create that, it's going to look like this.

5
00:00:23,950 --> 00:00:29,860
‫So I'm going to move this down to the bottom and we need to start writing our class, which is called

6
00:00:29,860 --> 00:00:30,880
‫graph.

7
00:00:31,360 --> 00:00:37,540
‫And before we actually get into the add vertex function, we need to create that unordered map that

8
00:00:37,540 --> 00:00:38,980
‫will store our graph.

9
00:00:38,980 --> 00:00:45,730
‫N And then once we have that created, we can create vertices like this.

10
00:00:46,300 --> 00:00:49,990
‫So that unordered map will be created with this line.

11
00:00:50,350 --> 00:00:54,910
‫So this is an unordered map where the key is a string.

12
00:00:54,910 --> 00:01:02,860
‫That's the A in this case, and then the value is an unordered set of strings.

13
00:01:03,100 --> 00:01:07,840
‫So this would be the value in this key value pair.

14
00:01:08,350 --> 00:01:12,460
‫And then we will call this unordered map adjacency list.

15
00:01:13,190 --> 00:01:18,740
‫So this line of code is going to give us an empty unordered map, and then we need to write the function

16
00:01:18,740 --> 00:01:22,340
‫to put this into the unordered map.

17
00:01:22,490 --> 00:01:29,450
‫So for now, I'm going to remove this and now we'll start writing the function to add the vertex.

18
00:01:30,020 --> 00:01:33,380
‫So let's focus in just on this code.

19
00:01:33,680 --> 00:01:41,090
‫In order to add an item to this unordered map, we can just do this and say into the adjacency list,

20
00:01:41,090 --> 00:01:46,100
‫we're going to pass this vertex and that does this.

21
00:01:46,640 --> 00:01:53,270
‫So one of the things I want to point out here is if you change this Boolean to a void.

22
00:01:53,450 --> 00:01:57,980
‫This is a complete function just like this.

23
00:01:58,370 --> 00:02:02,380
‫But I'm going to write my function a little bit differently.

24
00:02:02,390 --> 00:02:10,400
‫What I'm going to do is check to see is that vertex already in the adjacency list and I'm going to do

25
00:02:10,400 --> 00:02:12,740
‫that with this code.

26
00:02:13,010 --> 00:02:21,410
‫So we're going to say if adjacency list count for that vertex equals zero.

27
00:02:21,770 --> 00:02:26,420
‫In other words, this vertex is not already in the adjacency list.

28
00:02:26,420 --> 00:02:31,370
‫If that is the case, only then are we going to run this line.

29
00:02:31,730 --> 00:02:39,020
‫And then after we do this, we'll return true, true that we were able to add a vertex.

30
00:02:39,380 --> 00:02:47,030
‫And then if the conditional in the if statement is false, we'll skip the if statement and return false.

31
00:02:47,450 --> 00:02:51,140
‫And like I said, all of this is not necessary.

32
00:02:51,140 --> 00:02:56,330
‫We could change this to void and just run this one line.

33
00:02:56,690 --> 00:03:03,680
‫But doing it this way where it returns true or false, gives us a little bit more visibility into what's

34
00:03:03,680 --> 00:03:04,790
‫going on.

35
00:03:05,300 --> 00:03:08,490
‫So that is all of our code for ADD Vertex.

36
00:03:08,510 --> 00:03:12,470
‫Now let's flip over to VS Code and take a look at this.

37
00:03:13,100 --> 00:03:19,370
‫So to start off with, we'll need these two lines because we're using an unordered map and an unordered

38
00:03:19,370 --> 00:03:20,180
‫set.

39
00:03:20,270 --> 00:03:22,610
‫And I'm going to scroll up a little bit here.

40
00:03:22,880 --> 00:03:26,960
‫And this is the code here that creates that unordered map.

41
00:03:27,200 --> 00:03:31,580
‫And then I created a function called print graph so we could print this out.

42
00:03:32,950 --> 00:03:36,760
‫And this is the add vertex function that we just created.

43
00:03:36,760 --> 00:03:38,320
‫And I'll scroll up again.

44
00:03:38,890 --> 00:03:45,220
‫And in our main function this creates our new graph and with this line we're adding that new vertex

45
00:03:45,220 --> 00:03:52,030
‫with a value of a and with this line we'll run that print graph function and I'll run this.

46
00:03:52,780 --> 00:04:00,220
‫And you can see up here that we've been able to add that vertex with the value of A and it has an empty

47
00:04:00,220 --> 00:04:06,670
‫unordered set and that is our function for ADD Vertex.

