Graph Representations

COMP2521 20T2 ♢ Graph Representations [0/24]
❖ Graph Representations

Describing graphs:

E.g. four representations of the same graph:

[Diagram:Pics/graph-reps.png]

COMP2521 20T2 ♢ Graph Representations [1/24]
❖ ... Graph Representations


We discuss three different graph data structures:

  1. Array of edges
    • explicit representation of edges as (v,w) pairs
  2. Adjacency matrix
    • edges defined by presence value in VxV matrix
  3. Adjacency list
    • edges defined by entries in array of V lists
COMP2521 20T2 ♢ Graph Representations [2/24]
❖ Array-of-edges Representation

Edges are represented as an array of Edge values (= pairs of vertices)

[Diagram:Pics/graph-array-edges.png]

For simplicity, we always assume vertices to be numbered 0..V-1

COMP2521 20T2 ♢ Graph Representations [3/24]
❖ ... Array-of-edges Representation

Graph initialisation

newGraph(V):
|  Input  number of nodes V
|  Output new empty graph (no edges)
|
|  g.nV = V   // #vertices (numbered 0..V-1)
|  g.nE = 0   // #edges
|  allocate enough memory for g.edges[]
|  return g


Assumes ≅ struct Graph { int nV; int nE; Edge edges[]; }

COMP2521 20T2 ♢ Graph Representations [4/24]
❖ ... Array-of-edges Representation

Edge insertion

insertEdge(g,(v,w)):
|  Input  graph g, edge (v,w)
|  Output graph g containing (v,w)
|
|  i=0
|  while i < g.nE ∧ g.edges[i] ≠ (v,w) do
|     i=i+1
|  end while
|  if i=g.nE then      // (v,w) not found
|     g.edges[i]=(v,w)
|     g.nE=g.nE+1
|  end if

We "normalise" edges so that e.g  (v < w) in all (v,w)

COMP2521 20T2 ♢ Graph Representations [5/24]
❖ ... Array-of-edges Representation

Edge removal

removeEdge(g,(v,w)):
|  Input  graph g, edge (v,w)
|  Output graph g without (v,w)
|
|  i=0
|  while i < g.nE ∧ g.edges[i] ≠ (v,w) do
|     i=i+1
|  end while
|  if i < g.nE then  // (v,w) found
|     g.edges[i]=g.edges[g.nE-1]
|         // replace by last edge in array
|     g.nE=g.nE-1
|  end if

COMP2521 20T2 ♢ Graph Representations [6/24]
❖ ... Array-of-edges Representation


Print a list of edges

showEdges(g):
|  Input graph g
|
|  for all i=0 to g.nE-1 do
|  |  (v,w)=g.edges[i]
|  |  print v"—"w
|  end for

COMP2521 20T2 ♢ Graph Representations [7/24]
❖ Array-of-edges Cost Analysis

Storage cost: O(E)

Cost of operations:

If array is full on insert If we maintain edges in order
COMP2521 20T2 ♢ Graph Representations [8/24]
❖ Adjacency Matrix Representation

Edges represented by a V × V matrix

[Diagram:Pics/adjmatrix.png]

COMP2521 20T2 ♢ Graph Representations [9/24]
❖ ... Adjacency Matrix Representation


Advantages

Disadvantages:
COMP2521 20T2 ♢ Graph Representations [10/24]
❖ ... Adjacency Matrix Representation


Graph initialisation

newGraph(V):
|  Input  number of nodes V
|  Output new empty graph
|
|  g.nV = V   // #vertices (numbered 0..V-1)
|  g.nE = 0   // #edges
|  allocate memory for g.edges[][]
|  for all i,j=0..V-1 do
|     g.edges[i][j]=0   // false
|  end for
|  return g

COMP2521 20T2 ♢ Graph Representations [11/24]
❖ ... Adjacency Matrix Representation


Edge insertion

insertEdge(g,(v,w)):
|  Input  graph g, edge (v,w)
|  Output graph g containing (v,w)
|
|  if g.edges[v][w] = 0 then  // (v,w) not in graph
|     g.edges[v][w]=1    // set to true
|     g.edges[w][v]=1
|     g.nE=g.nE+1
|  end if

COMP2521 20T2 ♢ Graph Representations [12/24]
❖ ... Adjacency Matrix Representation


Edge removal

removeEdge(g,(v,w)):
|  Input  graph g, edge (v,w)
|  Output graph g without (v,w)
|
|  if g.edges[v][w] ≠ 0 then  // (v,w) in graph
|     g.edges[v][w]=0       // set to false
|     g.edges[w][v]=0
|     g.nE=g.nE-1
|  end if

COMP2521 20T2 ♢ Graph Representations [13/24]
❖ ... Adjacency Matrix Representation


Print a list of edges

showEdges(g):
|  Input graph g
|
|  for all i=0 to g.nV-1 do
|  |  for all j=i+1 to g.nV-1 do
|  |     if g.edges[i][j] ≠ 0 then
|  |        print i"—"j
|  |     end if
|  |  end for
|  end for

COMP2521 20T2 ♢ Graph Representations [14/24]
❖ Adjacency Matrix Cost Analysis


Storage cost: O(V2)

If the graph is sparse, most storage is wasted.

Cost of operations:

COMP2521 20T2 ♢ Graph Representations [15/24]
❖ ... Adjacency Matrix Cost Analysis

A storage optimisation: store only top-right part of matrix.

[Diagram:Pics/graphRep2.png]

New storage cost: V-1 int ptrs + V(V+1)/2 ints   (but still O(V2))

Requires us to always use edges (v,w) such that v < w.

COMP2521 20T2 ♢ Graph Representations [16/24]
❖ Adjacency List Representation

For each vertex, store linked list of adjacent vertices:

[Diagram:Pics/adjlists.png]

COMP2521 20T2 ♢ Graph Representations [17/24]
❖ ... Adjacency List Representation


Advantages

Disadvantages:
COMP2521 20T2 ♢ Graph Representations [18/24]
❖ ... Adjacency List Representation

Graph initialisation

newGraph(V):
|  Input  number of nodes V
|  Output new empty graph
|
|  g.nV = V   // #vertices (numbered 0..V-1)
|  g.nE = 0   // #edges
|  allocate memory for g.edges[]
|  for all i=0..V-1 do
|     g.edges[i]=newList()  // empty list
|  end for
|  return g

COMP2521 20T2 ♢ Graph Representations [19/24]
❖ ... Adjacency List Representation


Edge insertion:

insertEdge(g,(v,w)):
|  Input  graph g, edge (v,w)
|  Output graph g containing (v,w)
|
|  if not ListMember(g.edges[v],w) then
|     // (v,w) not in graph
|     ListInsert(g.edges[v],w)
|     ListInsert(g.edges[w],v)
|     g.nE=g.nE+1
|  end if

COMP2521 20T2 ♢ Graph Representations [20/24]
❖ ... Adjacency List Representation


Edge removal:

removeEdge(g,(v,w)):
|  Input  graph g, edge (v,w)
|  Output graph g without (v,w)
|
|  if ListMember(g.edges[v],w) then
|     // (v,w) in graph
|     ListDelete(g.edges[v],w)
|     ListDelete(g.edges[w],v)
|     g.nE=g.nE-1
|  end if

COMP2521 20T2 ♢ Graph Representations [21/24]
❖ ... Adjacency List Representation


Print a list of edges

showEdges(g):
|  Input graph g
|
|  for all i=0 to g.nV-1 do
|  |  for all v in g.edges[i] do
|  |     if i < v then
|  |        print i"—"v
|  |     end if
|  |  end for
|  end for

COMP2521 20T2 ♢ Graph Representations [22/24]
❖ Adjacency List Cost Analysis


Storage cost: O(V+E)

Cost of operations:

Could sort vertex lists, but no benefit  (although no extra cost)
COMP2521 20T2 ♢ Graph Representations [23/24]
❖ Comparison of Graph Representations

Summary of operations above:

 array
of edges
adjacency
matrix
adjacency
list
space usageEV2V+E
initialise1V2V
insert edgeE1E
remove edgeE1E

Other operations:

 array
of edges
adjacency
matrix
adjacency
list
disconnected(v)?EV1
isPath(x,y)?E·log VV2V+E
copy graphEV2V+E
destroy graph1VV+E

COMP2521 20T2 ♢ Graph Representations [24/24]


Produced: 29 Mar 2021