 Welcome back, in the previous segment we discussed implementation of the classes vector and set and also a little bit about maps. In this segment I am going to talk about how you use these classes or data structures in programming and in particular in programming you will need to compose them together and how do we do that. So, let me take an example, suppose I am writing a program in which I want to keep track of friends of different people and let us say for each person I want to make a list of his or her friends in the order oldest to youngest and then maybe I might want to do some processing on those lists. So, what data structure do we use? Well, first of all what is it that I require this data structures for all people, lots of people. So, let me begin by asking what is it that I want for a single individual. So, for a single individual I want list of friends in oldest to youngest order. So, naturally what I can use is a vector of strings. So, the string will hold the name of the friend and I have a vector so I can have I can put lots of names in it and I can put the names in the order oldest to youngest. So, this is for one individual, so if I want it for all what do I want? Well, I should build a map, so for every person whose name is here I want this data structure, so I should have map and let me call that map the friends map. So, just to clarify this let me give an example of how we are going to use this map. So, I might say for example friends of amitabh dot and this will get me all the friends of amitabh and on that I am pushing back Dharmendra. So, if this is the very first statement then initially this will get me a vector which is empty and into that will be added Dharmendra. I can do that again. So, again what does this do? This gives me, this first part gives me all the friends of amitabh, so what are the friends of amitabh? Well, friends is a map, so friends of amitabh gets me this element, so it gets me a vector of strings and onto that I am pushing back another string which is what I should do so I am pushing back Vinodh, so what does friends of amitabh contain right now? Well, it contains Dharmendra followed by Vinodh. I could write one more command friends of Dharmendra dot push back amitabh for example. So, then friends will contain a pair for amitabh and one pair for Dharmendra and I can put friends of different people inside this friends map and the second element or the, this map is going to take a name and it is going to return a list or a vector of friends names and to get to that, how do I get to that? Well, a vector is going to be indexed by numbers, so for example if I can now write something like this, friends of amitabh 0, so give me the zeroth friend of amitabh, so again let us look at what this means, so friends of amitabh is going to give me a vector and since this is a vector, it is legal to take the zeroth indexed element of it and what is the zeroth indexed element, well the push backs happened in this order and therefore I am going to get the zeroth indexed element which is Dharmendra, so Dharmendra is going to get printed as a result of this. So, this is one example of composing data structure, so what have we composed? Well, we have composed map and of course there is string and we have vector, so we have made a complicated looking data structure but if you look at it closely, it is actually not that complicated because vector of strings is a list of strings or it is a list of my friends and then for every person I am storing such a list, so the trick is you read it inside out or I guess you can read it outside in as well, so you can say that look for every string something is being stored over here and what it is, let me take another example, so again this is about keeping track of friends but now I want to determine if a and b are friends, so I want to determine if Dharmendra and Amitabh are friends or Jitendra and Dharmendra are friends and things like that, so I want to have a data structure which can quickly answer queries like this, so what should I use? Well, let me give you the answer, so here it turns out that it is useful to use this a map from string to a set of strings, so again I am going to call it friends, so let me read this, so the first thing is going to be the name of the person whose friends I am thinking of and then this set is going to contain the set of all the friends, so how do I use this? So earlier I pushed back Dharmendra, now I am going to insert, why insert? Because friends of Amitabh gets me a set of strings, not a vector of strings and into a set you insert, you do not push back, so again I am going to insert Vinod as before and now I can query, so here is how I am going to query, so I am going to ask, give me friends of Amitabh and what does that give me? It gives me a set of the friends and inside that count how many times Jitendra appears, so if it appears, if this appears once then that means yes Jitendra is a friend, if this appears, if this returns a 0 then it means Jitendra is not a friend, so what is going to happen now? Well into the set we inserted Dharmendra and we inserted Vinod and therefore Jitendra is not there and therefore this is going to print out a 0 because we have not made Jitendra a friend, but if you insert even now friends of Amitabh insert Jitendra and then call this again, then you will see that the count is has become 1 and by the way these are sets, so if you reinsert it is not going to change anything, so sets as you know from your study of mathematics a set cannot contain the same element more than once, so C++ actually does have something called a multi-set which does contain which allows you to store several copies of the same element into the set, so as I said look at the online documentation if you feel you need to learn what a multi-set is, but we are not going to look at this interesting class in this course because already we are doing a lot. Now I just want to point out that this kind of a query to quickly decide whether Jitendra is a friend of Amitabh would not be easy with factors, we would have to write a little bit more code, so what code would we write? So you would say look at that vector compare Jitendra to every element in that vector, that kind of code we would have to write, but here since it is a set and on the set we have this count operation we can get the answer in exactly in just a very small amount of code over here. So what is the important point that is coming out of this example? Well the data we are storing is really the same, we are storing information about who is a friend of whom, but depending upon what data structure we use certain kinds of queries are easier to answer, so here what is easy to answer whether or not two people are friends is easy to answer. What is not easy to answer? Well with this data structure you cannot tell who is the oldest friend, on the other hand if you had vectors and if you insert friends in order of how long you have known them then you could quickly tell who is the oldest friend, just look at the 0th friend in that vector. So again this is a very important point you should know how to compose data structures but just because you are storing the same information doesn't mean that exactly one kind of data structure is the right data structure, it really depends upon what you want to do with that information. I want to do one more example. So let us say we want to create a fair table, so we want to represent bus fares between cities. So once we have that table we could ask for ask questions like what is the fair between city A and city B, assuming of course we stored it in our table in the first place. So if there is a bus connection from city A to city B and somebody gives us a query tell me the fair from A to B we should be able to look at our table and answer this very quickly. Alright how do we do this? Well let us sort of try to build it bottom up or inside out so to say. So let us ask what do we need to remember for each city? What do we need to remember? Well if we need to remember what cities can be reached directly from that city and what fares are there for each of those journeys. So this is what we want to remember for the first city, second city, third city whatever it is for Mumbai, Pune, Nakpur, Kolhapur whatever cities you are talking about we need to remember this information. Well what is this information? So for every city that can be reached we want to keep track of the respective fare. So this is really just a map from city to fare payable. And so what kind of map is it going to be? So again let us say the reachable city is represented as strings by strings so it is going to be a map from strings to fare, fare could be double. So it could be a map from strings to double. So this is just for one city and what else do we really want? We want this for all cities. So if you want it for all cities what should we do? Well we want this is going to be inside something and what is that outer thing? Well we want a map from a city to a map. So for Mumbai I want a map of all cities reachables and what fare are payable. So it is a map from a city name to such a map. So here is what it is going to look like. So map from a string to a map from a string to a double. So this is really this part and this map, the city over here is this part. So I have to supply a city, I have to supply another city and out will come the double or I can store into this. So for example once I declare this I can write a statement like this. So fare from Mumbai to Pune is 500 rupees. That is what this statement is saying. But you should really understand what this is doing. So this is saying there is a variable or there is a data structure called fare. What is that? It is a map. So I can give you an index which does not have to be a number. It could be, it is a string as it is mentioned over here. So I get, if I give you Mumbai what do I get? I get a map and what is that map? That map is going to tell me what is the price to go from Mumbai to various cities. So for every city that is reachable from Mumbai I am going to get the name of the city and the fare. But once I have that map if I supply where I want to go I can store into it the fare or if I say print this out I will get the fare get printed. So what did we discuss? So we discussed that sometimes we need to compose data structures. The exact choice of data structures depends on what information we want to store but also on what operations we need to perform on that information. And then we also said something about how do you do this composition? So we said that the way we do this composition is look at a small part of the entire picture and build it up from there. So what do we need to do for one individual? What do we need to do for a single city? And therefore what do we need to do for all individuals or for all cities together? So next I am going to talk about something called a type def and then I am going to conclude this lecture sequence. But before that let me take a quick break.