‫So now we're going to create our add vertex function and add vertex is just going to create one of these
‫and the next video will learn how to add edges and then we can create a graph like this.
‫But for now, we're just creating this.
‫And when we create that, it's going to look like this.
‫So I'm going to move this down to the bottom and we need to start writing our class, which is called
‫graph.
‫And before we actually get into the add vertex function, we need to create that unordered map that
‫will store our graph.
‫N And then once we have that created, we can create vertices like this.
‫So that unordered map will be created with this line.
‫So this is an unordered map where the key is a string.
‫That's the A in this case, and then the value is an unordered set of strings.
‫So this would be the value in this key value pair.
‫And then we will call this unordered map adjacency list.
‫So this line of code is going to give us an empty unordered map, and then we need to write the function
‫to put this into the unordered map.
‫So for now, I'm going to remove this and now we'll start writing the function to add the vertex.
‫So let's focus in just on this code.
‫In order to add an item to this unordered map, we can just do this and say into the adjacency list,
‫we're going to pass this vertex and that does this.
‫So one of the things I want to point out here is if you change this Boolean to a void.
‫This is a complete function just like this.
‫But I'm going to write my function a little bit differently.
‫What I'm going to do is check to see is that vertex already in the adjacency list and I'm going to do
‫that with this code.
‫So we're going to say if adjacency list count for that vertex equals zero.
‫In other words, this vertex is not already in the adjacency list.
‫If that is the case, only then are we going to run this line.
‫And then after we do this, we'll return true, true that we were able to add a vertex.
‫And then if the conditional in the if statement is false, we'll skip the if statement and return false.
‫And like I said, all of this is not necessary.
‫We could change this to void and just run this one line.
‫But doing it this way where it returns true or false, gives us a little bit more visibility into what's
‫going on.
‫So that is all of our code for ADD Vertex.
‫Now let's flip over to VS Code and take a look at this.
‫So to start off with, we'll need these two lines because we're using an unordered map and an unordered
‫set.
‫And I'm going to scroll up a little bit here.
‫And this is the code here that creates that unordered map.
‫And then I created a function called print graph so we could print this out.
‫And this is the add vertex function that we just created.
‫And I'll scroll up again.
‫And in our main function this creates our new graph and with this line we're adding that new vertex
‫with a value of a and with this line we'll run that print graph function and I'll run this.
‫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
‫unordered set and that is our function for ADD Vertex.