previous | start | next

Representing Graphs

There are two basic ways to represent directed graphs. Both ways also work for undirected graphs with no modification required (although some space could be eliminated).

Two Ways of Representing Graphs

 1. adjacency matrix. A |V| x |V| sized matrix, A

    For unweighted graph, 

        A[i][j] is 1 if there is an edge from vertex i to vetex j
                   0 otherwise.

   For weighted graph,

        A[i][j] is the weight of the edge if there is an edge from
                   vertex i to vetex j 
                   otherwise some chosen value not to be confused with
                   a weight (e.g., largest or smallest possible int
                   value)

   (Note for an undirected graph, the matrix A represents the same
    edge twice since A[i][j] will be equal to A[j][i].)



 2. adjacency list. An |V| sized array of lists. 

       A[i] = list of vertices j for which there is an edge from i to
              j.
  
    (Note for an undirected graph, every edge is represented twice.)



previous | start | next