 Welcome to this course on data structures and algorithms. We are currently discussing graphs and their representations. In the last session, we saw the basic operations on graphs like adding nodes and edges. Today, we shall see a C plus plus program to do the same thing using STL functions. First, we identify the data structures and functions that we need. We will use a vector list as a vector which is of the type list. We will have the following functions, one function to create graph nodes, so that means a, b, c, d, e, f, g whatever nodes we have, we need to create those nodes for the graph. Next is a function required to add an edge prescribed an edge from one source node to destination node, we should be able to add it. Similarly, if there is a mistake or for some reason we have to remove an edge, we should need a function to remove an existing edge from one node to another. We of course, will need ability to show the final graph, so we have two functions as we had discussed the two displays that we will create. One is showing outgoing edges and the other showing incoming edges. The program is really simple and straightforward if all the input data is correctly given. Let us look at the program. First, let us look at the representation of vector in some greater details. So, this is our vector list. There is an index 0, 1, 2, 3, 4 up to n minus 1 assuming there are n nodes in the graph. For each node, there will be a associated list. So, list 1 will be associated with index 0, list 2 will be associated with index 1 and so on. All the n lists are associated like this. Now, what should be contained in each list? That is very important. Each list will have first the source node identity. Now, obviously, if we have n nodes, it makes sense to include the node 1, node 2, node 3, etcetera in that order in that sequence index by 0, 1, 2, 3, 4, n minus 1. So, this shall be the first entry into each of the vector list. Once that is done, this shall be followed by a list of destination nodes for which the edges emanate from node 1. Similarly, here the edges which come from node 2, etcetera, etcetera. You can very clearly see that this represents completely the entire graph that we have in mind. Let us proceed towards the program. First, we define the template class t. So, class graph is what we are going to define. It is a vector list. As we said, we call it a vector list. The public components are 1, create graph nodes. You remember the function that we mentioned. We give the number of nodes and t star which is the pointer to the class. Then we have add edge, remove edge. Currently, we are describing them as void because there is nothing to be returned. We just either add an edge or remove an edge based on the prescription that is given through parameters. Similarly, we have a function to print outgoing edges and a function to print incoming edges. That is a simple description of the class. Let us look at creating graph nodes. Note that we assume nodes a, b, c, d, e are to be created and its destination nodes are not known as of now. So, what will happen? Our vector list will look like this. The first element in each vector list will be initialized to a, b, c, d, e which are the sample nodes that we have considered and there will be nothing else for the time being. So, the program is very simple. Create graph nodes function. Vector list dot resize a will actually initialize. Remember that when the a is given as a parameter, we know the exact number of nodes. This resize will ensure that the list is sized appropriately for the total number of nodes which are given as an input parameter. Now, we have to iterate and in each iteration, we have to insert one node which is given by this vertex array. See the for loop. It is exactly like a for loop except that we are going to use the vector iterator instead of an index variable i or j that we use in a for loop. So, the starting point of that vector iterator is vector list dot begin. The condition for continuation of the for loop is not equal to vector list dot end. So, from begin till end, we keep adding one to the vector iterator and we keep incrementing array index by one. This is a simple loop and the only action that we do inside that loop is we push back the current vertex depending upon the array index that we have. Please remember we are incrementing array index appropriately which starts with 0. So, initially 0th element then first element then second element etcetera will be pushed back. Note the simplicity of using the STL library facility. Push back is a simple function which pushes everything on to the vector list that we have created. Now, consider adding an edge. We want to add an edge from c to a. This is our existing vector list as of now where we have created all the nodes. What we will have to do is we will have to go through these vector list, find out the starting node. Is this c no? Is this c no? This is c yes. Then to this vector list we will add a to indicate that there is an edge from c to a. So, it is a simple mechanism. Every time we get an edge, we look for the source node in the vector list as the first element of the vector list. And whenever we find it, we add the edge to the corresponding node that is given to us. The program is very simple. We say void add edge, t source, t destination. These are the two things that are given. What we have to do is we have to iterate through the vector list to find out where exactly is the t source. So, notice the same iteration is put here. And all that we do is we set iterator equal to vector iterator dot begin initially. Now, if the list iterator is equal to source, that means we have found the source node. At this point, we will say push back destination. Please note that this push back will automatically insert the destination node subsequent to the original source or any previous age that you have. That is the property of the STL function that we use. So, a simple push back does multiple things. And in the instant case, we would have added the edge prescribed to us through the parameters t source to t destination. How do we remove an edge? Suppose we want to remove the edge from c to a. Again, we will be given a source and destination which edge is to be removed. So, we will have to search for the source which as usual we search amongst the first element of the respective vector list. So, we will iterate around these vector list. And whenever we find c, then we go to the edges. A happens to be the edge here. There could have been other edges as well. But as long as edge a exists here, c to a edge has to be removed. When we remove it, a will be removed from the vector list. So, simple operation again and the program or function to do this will appear very similar to adding an edge. Let us look at that. Again, we define the remove edge function as y. We have to iterate through the vector list. So, there is a for loop. We have already seen that for loop. We start with vector iterator dot begin. Now, if list iterator is equal to source, that means the initial element of that list happens to be equal to source. We have found the source node. At this juncture, we will have to write a code to remove the destination. Now, the destination node is given here t destination. Simply dot remove destination will remove the edge. So, this is as simple as that. We have now seen adding and removing edges and adding nodes. What we need more is functions to print outgoing edges and function to print incoming edges. Once again, the print outgoing edges function is simply written using the same iterator. For each visit of the particular list, we set this to begin of the vector iterator and we output the list iterator pointed thing and we add one to the list iterator. So, that is because within each list, there could be multiple edges. So, all those lists, all those edges have to be covered. Please note there is no initialization specification. All that we do is as long as list iterator is not at the end of vector iterator list. We keep doing this. We keep outputting the next element pointed out by the vector iterator. So, in short, we traverse all the lists. For each list, we travel to look at all the edges which are outgoing and print those edges. What about the incoming edges? The incoming edges is slightly different than outgoing edges. We have to iterate through all the nodes. What we do is we just put that same iterator, vector list dot begin, vector list up to vector list dot end and vlt plus plus. We now say we define an iterator llt where we set it to the begin. Now, we output that particular thing followed by a caller. What we need to do is we need to examine all nodes starting with this and display incoming edges for each. So, let us see how we do that. We will look at this code now. First and foremost, we have this iteration from beginning to the end of the vector list. We set this and we set the list iterator to begin. Now, we set another iteration. We look at each list in succession. So, this is looking at one particular list. Now, if the list iterator is star llt, remember we had noted the place where we are looking for the node in star llt. What this means is that in the outgoing list, there seems to be an element which is actually happening to be the source. If that is so, I will output the front of that because the front of that is place to which there is an edge. So, this is slightly tricky, but it should be obvious if you look at the code, it will be able to display all the nodes with incoming edges. The main program is now written very simply. In this case, we are not showing a demonstration of a very large graph. We are showing a graph which has only 5 vertices a, b, c, d and e. We define the graph as g. We create the 5 nodes. Now, we have to define the edges. We are simply defining all the edges as they come explicitly by using g dot add a. Please note that this is not the way in a real program we will do things because there could be hundreds of edges and those will have to be input. There is an exercise later which will help you to understand how to program them. Because that is done, we now have to output outgoing edges and incoming edges. We just invoke the two functions outgoing and incoming and we got the output representing the entire graph. We next demonstrate the removal of two edges by using remove edge function and after that we again invoke the outgoing and incoming edges to demonstrate that these edges are indeed removed. So, it is a very simple program. As an exercise, try to implement a program which takes a directed graph description from the user and modify that program to implement an undirected graph. There again try to write functions to determine whether an edge exists from one node to another or determine the out degree and in degree of a node or remove an existing node from the graph. So, these are simple exercises similar to what we have already done. Before ending I would like to point out certain real life issues that may crop up. So far we have assumed that all our input is perfect and correct and therefore, we did not bother to cross check while removing an edge for example, whether the edge existed or not. In problems in real life there are various errors that can occur. For example, because of the wrong input given by the user we might try to add an edge which already exists or we might try to add an edge where either the source node or destination node is simply not present in the graph. Similarly, we might try to remove a non present edge or we might try to remove an edge where either the source or destination node is not present. Even worse we might try to add a node in the graph which already exists. Now, these are the problems that crop up because of input errors that may happen particularly when user is trying to use our programs with very large graph. We need to address these and our programs will have to be written to take care of this. To conclude today we have seen a C plus plus program which creates the representation of a graph based on the input given by the user. For the demonstration we have assumed a simple graph with five nodes, demonstrated how nodes can be added, edges can be added and removed. Towards the end we also identified the problems that might crop up in real life because of the mistakes made in giving the input. We shall see some of these problems and how to address these in the next session. Thank you.