 Welcome to this course on data structures and algorithms. Last time we had seen some basic operations on graphs and written a C plus plus program. At that time, we had observed that in real life there could be problems particularly because of error in inputting data. Today, we shall see how to address some of those errors and in the process how to write real life programs. To recapitulate, we had used a data structure which was actually a vector list and there was a list associated with every node. We had agreed that the first element of each vector list will be the starting node and then subsequent elements of that vector list will be the destination nodes from each of these nodes. So, this way we will be able to represent all edges which emanate from node 1, node 2, node 3, etcetera, etcetera. We had also written functions to create graph nodes, to add an edge, to remove an edge and also two functions to print outgoing and incoming edges. We had said at that time that there could be problems in real life such that we may try to add an edge which already exists or we might add an edge where either the source node or destination node is not present in the graph. Exactly similar problems could occur during the removal operation. We shall look at two of the problems today and we will modify our computer program to address these. The one problem that we are addressing is when somebody is trying to add an edge which already exists. Other problem is its inverse we are trying to remove an edge which is not present at all. So, let us look at how we will have to modify the functions. The class graph remains as it is with a important difference. You remember that add edge and remove edge where functions which were declared as void function. We now change these to bool function. Why? Because each of these functions we will have to now return true or false depending upon whether they succeed in doing the normal operation or not and why would they not succeed? Well it is because of the real life problem that we just mentioned. For example, if we want to try and add edge which already exists then add edge function will fail and it will return false if we declare it as bool. So, this is the modification that we do and of course, the functions themselves will have to be rewritten appropriately. So, let us look at the add edge function. We begin with the assumption that the vector destination is not found. So, we set false to the bool found destination. Now, we have the same iteration that we had seen earlier using the iterator vector iterator. Again for each iteration we start with the vector iterator dot begin for that particular vector list and now we will look at all the elements of that vector. First we see the source. If the iterator is equal to source, we have found the source node otherwise we will have to go to the next list. Now, if we have found the source this is where we have to write code to add the edge. Let us see how that code will look different from our earlier code. First we increment the list iterator pointing to the second element in the list which represents the first edge. We set up a for loop till the end of this particular list incrementing the list iterator one every time. All that we check is whether we find a destination. If we find a destination that means the edge exists. If edge exists we set found destination to be true and break because we cannot add the edge if it already exists. So, we get out. If not we continue this entire iteration. If we do not find throughout this test whether the destination node exists already then the found destination is set to check whether it is false. Please remember we may come out of this loop either because the normal completion of the loop or because this break. So, we have to test whether the found destination is true or false. If it is false then we know that no edge exists and we have to add a edge. This is where we use that simple function pushback which was there in the earlier program also and we return true that we have added the edge. Otherwise we simply return false saying that edge already exists. So, please know that for a real life problem where somebody is trying to add an edge which already exists. Instead of this simple statement that we had used in our last program we have to write so much of code. This is the travesty of fate in real life you have to perpetually check for errors that may happen and write lot of code to ensure that those errors do not cause any malfunctioning of the program. Exactly the same thing will have to be done in the remove edge. So, we start with a bull set false to the found destination as before. Again we iterate through the vector list and for each list we identify whether we find the source. When the source node is found now please note what we have to do we have to remove an edge. So, if the source node is found we must locate the edge to be removed in that particular list. That is where we will write code to remove the edge. Let us look at that code. You will recall the last time the only statement we had was vector iterator dot remove destination and that is because we presume that when we find the destination that is to be removed and we were sure that we will find the destination. But now we are not so sure. So, we will iterate and when we iterate through this particular list if there is a destination we have found the edge from source to destination. We set found destination to be true and remove the edge and return true. But if found destination is false that means somebody was trying to remove an edge which did not exist that is why this function will return false because the edge is not found. The main program will look exactly like what we had earlier except for the fact that whenever we call functions for adding or removing edges these functions are now Boolean function. That means every time we invoke a function to add edge for example, we will have to check whether this returns true or false. If it returns true we say edge added from it to be otherwise we say edge already present. We will have to do it for each of the edges that we add in exactly the same way after printing outgoing and incoming edges when we try to demonstrate removal of edge. For example, the removal also will return a Boolean value and we will have to check that whether if it is true that means the edge has been removed we will say the so otherwise we will say edge not found. We will repeat the same statement for the demonstration on second edge that we had added in our program. In real life the program will not be as long as this because I would typically use an iteration just as I to collect all the vertices for example and to collect each edge from the user through an iteration. Similarly, when I do that the edge addition or edge removal will be called iteratively. The program will not be very long the point being made is that introduction of the Boolean function requires that for adding or removing edge when we want to display an appropriate message we will have to check the Boolean value returned by that function. After this we again have the print outgoing and print incoming as a part of the demonstration and that is all the main program is. As in exercise I would like to suggest that try to modify functions to handle the problems if for example the source or destination does not exist while adding an edge or the source or destination does not exist while removing an edge. More important and this is a harder problem the node being added to the graph already exists right at the beginning you remember we added nodes 1 2 3 4 5 arbitrarily because these were fixed nodes that we have. But assume that you are getting the nodes to be added from the user in an iterative fashion and by mistake a user prescribes the node which already exists. So, the node being added to the graph if it already exists well we must not add it. As I mentioned these are the kind of problems which make the real life code for our program a bit longer, but always much safer and that is the sign of a professional program. In this session we have looked at some of the real life problems that we may face while capturing a graph both for adding edges and removing edges. As an exercise we request you to try and write code which will address more such problems which have been listed in the graph. Thank you.