‫So now we're going to write the code to remove a vertex.
‫And when we remove a vertex, let's say we're going to remove D in order to remove D, we have to remove
‫all of the edges that D has with the other vertices and the edges.
‫The other vertices have back to D.
‫It is only then that we can remove the vertex.
‫So let's put this back.
‫We'll put our edges back like that.
‫And this graph will be represented by this adjacency list.
‫So let's focus in on this.
‫So there's an efficiency that we have when we remove a vertex where we have all bi directional edges.
‫So what we can do is look in this unordered set and if DX has an edge with A, B, and C, we know that
‫those vertices have an edge back to DX.
‫So we can loop through this set starting with a and we know that A has an edge back to DX and we can
‫remove that and that B has an edge back to DX and remove that.
‫And the same is true for C, and it wouldn't matter if this graph had a thousand vertices in it.
‫We would only have to visit these three vertices and remove the edges back to DX.
‫And this only works if we have bidirectional edges.
‫And once we've removed all of the edges that go back to DX, we can come down here and remove the vertex
‫and that gives us a graph that looks like this.
‫So now let's start writing our code.
‫This will be called Remove Vertex and then we'll just pass it a string.
‫That is the vertex that we want to remove.
‫So the first thing we'll do is check to make sure that the vertex is in the adjacency list.
‫And if it is not, we will return false.
‫Otherwise, we know the vertex is in the graph.
‫So the next thing we'll do is create a for loop that loops through the unordered set that is associated
‫with the vertex that we're removing.
‫So this line here, adjacency list at the particular vertex, let's just focus in on the for loop here.
‫What that's going to do is return this unordered set and this is what the for loop is looping through.
‫So let's put this back in with the rest of our code and inside of the for loop.
‫We're going to say adjacency list at.
‫Other vertex and then we'll say dot arrays.
‫Vertex.
‫So now let's walk through this line of code.
‫So let's start with this part adjacency list at other vertex.
‫Well, this is the variable that's being used to loop through the unordered set down here.
‫We're starting with a so other vertex.
‫Is this what this part is going to return?
‫Is this unordered set?
‫Erase is one of the member functions associated with unordered sets.
‫The vertex were removing is D and that removes that.
‫And remember, this is inside of that for loop.
‫So we go to B and now that becomes other vertex.
‫And that removes D from that unordered set.
‫And then we go to see that is the other vertex and we remove D from here.
‫So let's bring back the rest of our code.
‫And now that we've removed all of the edges back to D, now we can do this adjacency list.
‫Race the vertex.
‫So that is going to look like this, which gives us a graph that looks like this.
‫Now the only thing left to do is to return true.
‫So that is all of the code for remove vertex.
‫We'll look at this code in a moment and vs code.
‫And when we do we'll build this graph and then we'll remove the DX vertex and then we'll print the graph
‫out and we should see something like this.
‫So now let's flip over and take a look at this.
‫So there is our remove vertex member function added to our graph class and I'll scroll up and in our
‫main function this creates our new graph and with these lines we create our vertices A, B, C and D
‫and with these lines we'll add the edges that we had in the diagram.
‫And with this line we'll print that out and I'll run this.
‫And that looks like the graph that we just saw in the diagram.
‫And now I'm going to come up here and I'm going to add a line.
‫And with this line we'll remove the D vertex and I will run this.
‫And you can see up here we have removed the D vertex and all of the edges that go back to D.
‫And that is our function for remove vertex.