Week 06: Graph Data Structures PDF
Week 06: Graph Data Structures PDF
Graph Definitions
2/67
Graphs
Many applications require
Examples:
A graph G = (V,E)
V is a set of vertices
E is a set of edges (subset of V×V)
Example:
Notes: vertices are cities, edges are distance between cities, symmetric
7/67
Properties of Graphs
Terminology: |V| and |E| (cardinality) normally written just as V and E.
The edges in a graph represent pairs of connected vertices. A graph with V has V2 such pairs.
… because
(v,w) and (w,v) denote the same edge (in an undirected graph)
we do not consider loops (v,v)
10/67
Graph Terminology
For an edge e that connects vertices v and w
Degree of a vertex v
Synonyms:
vertex = node, edge = arc = link (Note: some people use arc for directed edges)
#edges
Connected graph
Complete graph KV
Note: The entire graph has no spanning tree; what is shown in green is a spanning tree of the third connected component
1. 2
5⋅4
2. − 2 = 8 spanning trees (no spanning tree if we remove {e1,e2} or {e3,e4})
2
Undirected graph
Directed graph
Examples:
... Graph Terminology 18/67
Weighted graph
Multi-graph
20/67
Graph Representations
Defining graphs:
1. Array of edges
2. Adjacency matrix
3. Adjacency list
22/67
Array-of-edges Representation
Edges are represented as an array of Edge values (= pairs of vertices)
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 enough memory for g.edges[]
| return g
Edge insertion
insertEdge(g,(v,w)):
| Input graph g, edge (v,w)
|
| i=0
| while i<g.nE ∧ (v,w)≠g.edges[i] 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
Edge removal
removeEdge(g,(v,w)):
| Input graph g, edge (v,w)
|
| i=0
| while i<g.nE ∧ (v,w)≠g.edges[i] 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
26/67
Cost Analysis
Storage cost: O(E)
Cost of operations:
initialisation: O(1)
insert edge: O(E) (assuming edge array has space)
delete edge: O(E) (need to find edge in edge array)
allocate space for a bigger array, copy edges across ⇒ still O(E)
show(g):
| Input graph g
|
| for all i=0 to g.nE-1 do
| print g.edges[i]
| end for
29/67
Adjacency Matrix Representation
Edges represented by a V × V matrix
... Adjacency Matrix Representation 30/67
Advantages
Disadvantages:
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
Edge insertion
insertEdge(g,(v,w)):
| Input graph g, edge (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
Edge removal
removeEdge(g,(v,w)):
| Input graph g, edge (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
show(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
New storage cost: V-1 int ptrs + V(V+1)/2 ints (but still O(V2))
39/67
Adjacency List Representation
For each vertex, store linked list of adjacent vertices:
Advantages
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]=NULL // empty list
| end for
| return g
Edge insertion:
insertEdge(g,(v,w)):
| Input graph g, edge (v,w)
|
| if inLL(g.edges[v],w) then // (v,w) not in graph
| insertLL(g.edges[v],w)
| insertLL(g.edges[w],v)
| g.nE=g.nE+1
| end if
Edge removal:
removeEdge(g,(v,w)):
| Input graph g, edge (v,w)
|
| if inLL(g.edges[v],w) then // (v,w) in graph
| deleteLL(g.edges[v],w)
| deleteLL(g.edges[w],v)
| g.nE=g.nE-1
| end if
Cost of operations:
46/67
Comparison of Graph Representations
array adjacency adjacency
of edges matrix list
space usage E V2 V+E
initialise 1 V2 V
insert edge E 1 1
remove edge E 1 E
Other operations:
48/67
Graph ADT
Data:
Things to note:
// operations on graphs
Graph newGraph(int V); // new graph with V vertices
void insertEdge(Graph, Edge);
void removeEdge(Graph, Edge);
bool adjacent(Graph, Vertex, Vertex); /* is there an edge
between two vertices */
void freeGraph(Graph);
50/67
Graph ADT (Array of Edges)
Implementation of GraphRep (array-of-edges representation)
Graph newGraph(int V) {
assert(V >= 0);
Graph g = malloc(sizeof(GraphRep)); assert(g != NULL);
g->nV = V; g->nE = 0;
// allocate enough memory for edges
g->n = Enough;
g->edges = malloc(g->n*sizeof(Edge)); assert(g->edges != NULL);
return g;
}
How much is enough? … No more than V(V-1)/2 … Much less in practice (sparse graph)
Implement a function to check whether two vertices are directly connected by an edge
55/67
Graph ADT (Adjacency Matrix)
Implementation of GraphRep (adjacency-matrix representation)
Graph newGraph(int V) {
assert(V >= 0);
int i;
Implement a function to check whether two vertices are directly connected by an edge
60/67
Graph ADT (Adjacency List)
Implementation of GraphRep (adjacency-list representation)
typedef struct GraphRep {
Node **edges; // array of lists
int nV; // #vertices
int nE; // #edges
} GraphRep;
Graph newGraph(int V) {
assert(V >= 0);
int i;
return g;
}
inLL, insertLL, deleteLL are standard linked list operations (as discussed in week 3)
Implement a function to check whether two vertices are directly connected by an edge
#include <stdio.h>
#include "Graph.h"
#define NODES 4
#define NODE_OF_INTEREST 1
int main(void) {
Graph g = newGraph(NODES);
Edge e;
e.v = 0; e.w = 1; insertEdge(g,e);
e.v = 0; e.w = 3; insertEdge(g,e);
e.v = 1; e.w = 3; insertEdge(g,e);
e.v = 3; e.w = 2; insertEdge(g,e);
int v;
for (v = 0; v < NODES; v++) {
if (adjacent(g, v, NODE_OF_INTEREST))
printf("%d\n", v);
}
freeGraph(g);
return 0;
}
67/67
Summary
Graph terminology
vertices, edges, vertex degree, connected graph, tree
path, cycle, clique, spanning tree, spanning forest
Graph representations
array of edges
adjacency matrix
adjacency lists
Suggested reading:
Sedgewick, Ch.17.1-17.5