Adjacency Matrix for Directed Graphs Adjacency Lists for Directed Graphs

J.E.D.I Figure 1.85 Weighted Graph Figure 1.86 A spanning tree is a subgraph that connects all the vertices of a graph. The cost of a spanning tree, if it is weighted, is the sum of the weights of the branches edges in the spanning tree. A spanning tree that has the minimum cost is referred to as minimum cost spanning tree. This is not necessarily unique for a given graph. Figure 1.87 Spanning Tree

6.4 Graph Representations

There are several ways to represent a graph, and there are some factors that have to be considered: • Operations on the graph • Number of edges relative to the number of vertices in the graph

6.4.1 Adjacency Matrix for Directed Graphs

A directed graph may be represented using a two dimensional matrix, say A, with dimension n x n, where n is the number of vertices. The elements of A are defined as: Ai,j =1 if the edge i,j exists, 1 ≤ i, j ≤ n = 0 otherwise The adjacency matrix A can be declared as a boolean matrix if the graph is not weighted. If the graph is weighted, Ai, j is set to contain the cost of edge i, j, but if there is no edge i, j in the graph, Ai, j is set to a very large value. The matrix is then called a cost-adjacency matrix. For example, the cost-adjacency matrix representation of the following graph is shown below it: Data Structures 103 J.E.D.I 1 2 3 4 5 1 1 ∞ 9 ∞ 2 ∞ 2 5 10 3 ∞ ∞ ∞ 3 4 ∞ ∞ 4 8 5 6 ∞ ∞ ∞ Figure 1.88 Cost Adjacency Matrix Representation for Directed Graph No self-referencing is allowed, hence, the diagonal elements are always zero. The number of nonzero elements in A ισ less than or equal to nn-1, which is the upper limit if the digraph is complete. The outdegree of vertex i, i.e. the number of arrow emanating from it, is the same as the number of nonzero elements in row i. The case is similar for the indegree of vertex j, wherein the number of arrows pointing to it is the same as the number of nonzero elements in column j. With this representation, telling if there is an edge i, j takes O1 time. However, even if the digraph has fewer than n 2 edges, the representation implies space requirement of On 2 .

6.4.2 Adjacency Lists for Directed Graphs

A sequential table or list may also be used to represent a digraph G on n vertices, say LIST. The list is maintained such that for any vertex i in G, LISTi points to the list of vertices adjacent from i. For example, the following is the adjacency list representation of the previous graph: LIST INFO COST NEXT 1 4 9 2 1 Λ 2 5 10 4 5 3 2 Λ 3 5 3 Λ Data Structures 104 J.E.D.I LIST INFO COST NEXT 4 5 8 3 4 Λ 5 1 6 Λ Figure 1.89 Cost Adjacency List Representation for Directed Graph

6.4.3 Adjacency Matrix for Undirected Graphs