 Hello and welcome to this session in which we will be studying a new data type called graph. Graphs are a natural generalization of sequences and trees that we have studied so far. So, if you have seen in a sequence every node or element in the sequence has at most one next node and at most one previous node. So, the first node does not have a previous node, the last element does not have a next element. So, as every element has one previous element and one next element. In a tree every node has at most one previous node which we call the parent except the root node which does not have any parent. But a node can have any number of next nodes, they are called children in a tree. So, a node can have any number of children, but it will have at most one parent. So, graph is obtained by just relaxing all these conditions. In a graph a node can have any number of previous or next node. So, what is a graph? So, it consists of a finite set V of nodes also called vertices and a collection E of ordered pairs of nodes. So, an ordered pair N comma M is in the set E, then it is set to be an edge from the node N to the node M. So, E is a collection of edges which are essentially ordered pairs of nodes N, M and if N, M is an edge then we say node M is adjacent to node M or a successor of node M. So, node M is like a next node of N, but N can have any number of successor and they are defined by the set of edges in the graph G. Similarly, node N is set to be the predecessor of node M or the previous node of N. So, again M can have any number of nodes as previous nodes of predecessor and which nodes are predecessors or successors is defined by the set E of edges in the graph. So, the set V defines what are the nodes in the graph and set E defines the ordered pair which defines which nodes are successors of which nodes. The out degree of a node is the number of its successor and the in degree is the number of its predecessor. So, let us look at some examples of graph, a graph of say air connections. So, here the nodes are the cities in the country and there is an edge from node N to node M if say there is a direct flight from city N to city M. So, the airline can represent their flight by a graph depending on which cities the flight connect. So, there may be an edge from node N to node M if there is a plane flying from node city N to city M. So, if you look at courses offered in an institute, there is a graph which defines their structure and an edge indicates, so the nodes are the courses, set of all courses and edge N M indicates that course N is a prerequisite for course M. So, you can do course M only after completing course M and this relation between courses is indicated by the set of edges in the graph. Similarly, if you look at a graph of student registration, now here nodes can be of two types, they are students as well as courses in the institute and an edge N M indicates that student N has registered for course M. So, the registration data in an institute can also be represented as a graph where the nodes are students as well as courses. So, we will look at how a graph is represented. So, a graph is just defined by a sequence of nodes in the graph and a function called successor N M that returns to if and only if node M is a successor of node N in the graph. So, we have a sequence of nodes which represents what are the nodes in the graph and a single function successor that tells us whether a given node M is a successor of node N or not. So, this is also called adjacent matrix representation of a graph because for every pair of nodes it tells us whether a given node M is a successor of node N or not. So, any functions on graphs can be defined using just these two things, we need to know what are the nodes in the graph and we need to know which nodes are successors of which node. If this is defined any operation on graphs can be defined using this. Alternatively, sometimes we can define a graph is in a different way by defining a different function called successors of N that returns a sequence of successors of node N. So, rather than giving a pair of nodes and telling us whether M is a successor of N, this gives us the list of all successors of a given node N. So, you give a node N in the graph, it will tell you what are all its successors in the graph and this is sometimes called adjacent list representation. Both representations are equivalent, a graph can be represented in either way and we will choose whichever representation is more convenient depending on its use. So, there are some standard terms used in a graph which we will define here, a walk in a graph. So, walk is essentially motivated by some of the examples that we have seen. So, for example, we have a graph of direct flights from city i to where an edge represents a flight from city i to city j and we want to go from some city x to some city y. Now, there may not be a direct flight from x to y, but then how do you go from x to y? So, for problems like this we define these terms like walk and as we will say path and cycle. So, what is a walk? It is just a sequence of nodes and it is defined as the following. The empty sequence is not considered to be a walk in a graph, a walk containing a single node or a sequence of nodes containing a single node is a walk. So, walk of push n comma 5 is true and walk of push n comma push m comma s will be true only if m is a successor of n and walk of push m comma s is true. So, that is if I take a sequence that has first element n and second element m, then this sequence is a walk. If m the second element or second node is a successor of the first node and the remaining sequence obtained by removing the first node is also a walk. So, essentially a walk is a sequence of nodes where every node in the sequence is followed by one of its successor. A path is a similar thing. So, it is a sequence of nodes defined in a very similar way. It is false for the empty sequence for a sequence containing a single node n it is true. Now, here we have the extra condition. So, apart from the condition in a walk that m is a successor of n that is the second element must be a successor of the first element. The remaining sequence must be a path itself and we have this extra condition that occurs n comma remaining sequence must be false that is the first node in the sequence must not occur anywhere else in the sequence and this should hold for the remaining sequence also. So, essentially this is that is a path is a sequence of distinct nodes we do not come to the same node again in the sequence. So, path is a sequence of distinct nodes which is a walk also walk. So, the occurs function essentially tells us whether a given node occurs in a given sequence of nodes. So, occurs n comma phi is false occurs n comma push n comma s is true if n equals n otherwise it is true if n occurs in s itself. This will be useful later also and similarly another very useful concept in a graph is that of a cycle. So, it is again a sequence of node defined similarly if it is empty it is not a cycle on the other hand if it is non-empty it must be a path. So, the sequence must satisfy all the properties of a path and the additional condition that the last element of the sequence or the first element of the sequence must be a successor of the last element of the sequence. The last of push n comma s comma n successor should be true we just said that the first element of the sequence m must be a successor of the last element of the sequence. The last here is a function that returns the last element in a sequence the undefined for the empty sequence and it can be defined in a usual way for any non-empty sequence. So, here are some exercises so as you may have given the representation of a graph as a sequence of nodes and a function successor n m which tells us whether m is a successor of n or not. Using this we can write a function successor n that returns the sequence of all successors of a node n and we can also do the converse given a function that returns all successors of node n. We can write a function which tells us whether a given node m is a successor of n or not. And now we can use this to write a function called predecessors n that returns the sequence of nodes that are predecessors of a given node n or similarly we can write a function predecessors n m which tells us whether m is a predecessor of n or not. All functions on graphs can be used defined using just these two the understanding these will be crucial for understanding graphs thank you.