 Hello, welcome to the lecture on representation of graphs in computer memory. In this lecture, we will be discussing about how graph data can be stored or represented into computer memory. The learning outcome of this video lecture is that the student is able to explain and write adjacency matrix and adjacency list representation for graph data structure. There are two common ways of storing graphs in the computer memory. The first way is sequential representation using adjacency matrix. And second way is linked representation by using an adjacency list. Let us see in detail this representation of graph data structure. Adjacency matrix representation. An adjacency matrix is used to represent the nodes that are adjacent to one another. In graph, two nodes are said to be adjacent if there is an edge connecting them. In an adjacency matrix, the rows and columns are labeled by graph vertices, as shown here. If n nodes are present in the graph, then n by n matrix is needed to represent an adjacency matrix of that graph. As shown here, an entry in adjacency matrix G will contain one at ith row and jth column if the vertices vi and vj are adjacent to each other. For example, here in the graph G, there is an edge between vertices a and b. Therefore, at 0th row and first column, the matrix cell value is set to 1. Whereas, there is no edge between vertex a and e. Therefore, the 0th and 4th column matrix cell value is set to 0, as shown here. In this way, for every edge between two vertices, the value is set to 1 in respect to cell of matrix and graph is represented using adjacency matrix. The matrix G is adjacency matrix representation for graph G. We have seen the adjacency matrix representation for undirected graph. Now, let us see an adjacency matrix representation for directed graph. Here, graph G1 is directed graph. Here, each edge direction is considered while representing an edge in the adjacency matrix. Here, adjacency matrix G1 is representation of graph G1. As we can see here, there is an outgoing edge from vertex p to vertex q. Therefore, the value 1 is set at 0th row and first column matrix cell. But, there is no edge from vertex q to vertex p. So, the 0th value, 0 value is set at the first row and 0th column of column cell of matrix. Similarly, to represent the remaining edges in the graph G1, the value 1 is set to the respective cell of a matrix. To represent an outgoing edge from vertex p to q. To represent an outgoing edge from vertex p to vertex s, the value 1 is set to cell. To represent an outgoing edge from vertex q to vertex s, the value set to cell is 1. Similarly, to represent an outgoing edge from vertex r to vertex q, the value 1 is set to the cell. Similarly, to represent an outgoing edge from vertex s to vertex t, the value 1 is set to that cell. To represent an outgoing edge from vertex T to vertex R, the value 1 is set to cell. The remaining all cell is set to 0 because there is no edge present between these vertices. The adjacency matrix for a weighted graph contains the weight of edge containing the vertices. Here is an example of adjacency matrix for a weighted graph G2. Instead of a value 1, the weight of an edge is stored in the matrix cell representing the edge between the vertices. And if there is no edge, then zero value is used to represent no edge between these two vertices. Here is the time to reflect on the content what we have learned. Pause the video for some time and write the adjacency matrix for given graph G3. Here is the adjacency matrix for our graph G3. There is an edge from vertex P to Q and P to S. Therefore one value is used to represent an edge. Similarly, there is an edge from vertex Q to P and vertex Q to S. Therefore one value is used to represent an edge. There is an edge from vertex R to Q. So one value is used to represent that edge. Similarly, there is an edge from vertex S to T and from vertex T to R. So, there is one value is used to represent that edge in the respective cell. The adjacency list is another way to represent graph in computer memory. Here the link list used to store node information of graph. This representation consists of a list of all nodes in graph and every node is linked to its its own list in which adjacent node information is stored. In this example, undirected graph G is represented using adjacency list. First array of all nodes is created with link. Now node A is adjacent to node B and D. Therefore, node A is linked to node B and D. Here the last node link field consists of null value which is indicated here by cross sign. This indicates that the end of the list of nodes linked with the node A. Node B is adjacent to the node A, C and D. Therefore, node B is linked to the node A, C and D. Node C is adjacent to node B and E. Therefore, node C is limit to node B and E. Node D is adjacent to the node A, B and E. Therefore, node D is link to the nodes A, B and E. Node E is adjacent to node C, D. Therefore, node E is linked to node C and D. Here is directed graph G1 is represented using adjacency list. First array of all five nodes P, Q, R, S and T is created along with link and outgoing age is present from node P to node Q as shown in the diagram and from node P to node S. Therefore node P is linked to node Q and node S and outgoing age is present from node Q to node S. Therefore node Q is linked to node S. Here the last link field consist of null value and outgoing age is present from node R to node Q. Therefore R is linked to node Q and outgoing age is present from node S to node T. Therefore S is linked to node T and outgoing age is present from node T to node R. Therefore node T is linked to node R. Now let us see the adjacency list representation for weighted graph. Here weighted graph G2 is shown in this diagram. In weighted graph representation one more field is added in each node that is weight of that particular age. First array of all five nodes A, B, C, D and E is created along with link and outgoing age is present from node A to node B with weight 3. So from node A to node E with weight 2. So there is a node link from A to B and A to E with respect to weight values. Therefore node A is linked to node B and E where node B is holding age weight value 3 and node E is holding age weight value 2. There is no further node connected to node A. Therefore link field of node E is holding null value indicated by cross symbol and outgoing age is present from node B to node E with weight 10. Therefore node B is linked to node E where node E is holding age weight value 10 and outgoing age is present from node C to node B with weight value 4. Therefore node C is linked to node B where node B is holding age weight value 4 and outgoing age is present from node D to node C with weight value 5. Therefore node D is linked to node C where node C is holding age weight value 5 and outgoing age is present from node E to node D with weight value 6. Therefore node E is linked to node D where node D is holding the age weight value 6. Here is the time to reflect on the content what we have learned. Pause the video for some time and write the adjacency list for given graph G4. Here is the adjacency list for graph G4. There is an age from vertex P to Q and P to S. Therefore node P is linked to node Q and S. Similarly there is an age from vertex Q to P. Therefore node Q is linked to node P. There is an age from vertex R to Q and R to T. So node R is linked to node Q. There is an age from vertex R to Q and R to T. So node R is linked to node Q and node T. Similarly there is an age from node S to R and from node S to T. So node S is linked to node R and node T. Finally there is an age from node T to node R. So node T is linked with the node R. Thank you for watching this video.