You are on page 1of 41

Graph Representation

Instructor: Ashok Singh Sairam


Lecture Plan
• Graph
 Basic Terminology
 Representation – adjacency list and adjacency matrix
• Graph Traversal
 Breadth-First Search
 Depth-First Search

MA512: Data Structures and Algorithms


2
Graphs
Definition = a set of nodes (vertices) with
edges (links) between them.
• G = (V, E) - graph 1 2
• V = set of vertices, V = n
• E = set of edges, E = m
3 4
 Binary relation on V
 Subset of V x V ={(u,v): u V, v V}

MA512: Data Structures and Algorithms


3
Applications
• Applications that involve not only a set of items,
but also the connections between them

Computer networks Maps


Hypertext

MA512: Data Structures and Algorithms


4
Types of graphs
• Undirected
• Directed
• Dense
• Sparse
• Weighted

MA512: Data Structures and Algorithms


5
Undirected graph
• Undirected graph: Unordered pair of vertices
 degree(v): #edges incident on v
 v is adjacent to u and u is adjacent to v, if there is

an edge (u,v)

MA512: Data Structures and Algorithms


6
Directed graph
• Directed graph: Ordered pair of vertices
 in-degree(v): #edges entering v
 out-degree(v): #edges leaving v

 v is adjacent to u, if there is an edge (u,v)

MA512: Data Structures and Algorithms


7
Dense and Sparse graphs
• Dense graph: |E| is close to |V|2
• Sparse graph: |E| is much less than |V|2

MA512: Data Structures and Algorithms


8
Complete graph
• Complete graph
 A graph with an edge between each pair of vertices
• Subgraph
 A graph (V’, E’) such that V’V and E’E
• Path from v to w
1 2
 A sequence of vertices <v0, v1, …, vk>
such that v0=v and vk=w
• Length of a path 3 4
 Number of edges in the path
path from v1 to v4
<v1, v2, v4>
MA512: Data Structures and Algorithms
9
Path and Cycles
• w is reachable from v
 If there is a path from v to w
• Simple path
 All the vertices in the path are distinct 1 2
• Cycles
 A path <v0, v1, …, vk> forms a cycle
if v0=vk and k≥2 3 4
• Acyclic graph cycle from v1 to v1
 A graph without any cycles <v1, v2, v3,v1>

MA512: Data Structures and Algorithms


10
Graph Representation
• Two standard ways
 Adjacency lists

 Adjacency matrix

MA512: Data Structures and Algorithms


11
Graph Representation: Adjacency list
• Adjacency list representation of G = (V, E)
 An array of V lists, one for each vertex in V
 Each list Adj[u] contains all the vertices v that are
adjacent to u (i.e., there is an edge from u to v)
 Can be used for both directed and undirected graphs
1 2 5 /
1 2 2 1 5 3 4 /

3 3 2 4

5 4 4 2 5 3 /
5 4 1 2

Undirected graph
MA512: Data Structures and Algorithms
12
Adjacency list for directed graph
• Sum of “lengths” of all adjacency lists
 Directed graph: E 
• edge (u, v) appears only once (in the list of u)
 Undirected graph: 2 E 
• edge (u, v) appears twice (in the lists of both u and v)

MA512: Data Structures and Algorithms


13
Properties of Adjacency-List
Representation
• Memory required: (V + E)
• Preferred when
 The graph is sparse: E  << V 2
 We need to quickly determine the nodes adjacent to a

given node.
• Disadvantage
 No quick way to determine whether there is an edge

between node u and v


• Time to determine if (u, v)  E: O(degree(u))
• Time to list all vertices adjacent to u: (degree(u))

MA512: Data Structures and Algorithms


14
Graph Representation: Adjacency
matrix
• Adjacency matrix representation of G = (V, E)
 Assume vertices are numbered 1, 2, … V 
 The representation consists of a matrix A V x V :
 aij = 1 if (i, j)  E
0 otherwise

MA512: Data Structures and Algorithms


15
Adjacency matrix for directed graph

MA512: Data Structures and Algorithms


16
Properties of adjacency matrix
• Memory required: (V2), independent on the number of
edges in G
• Preferred when
 The graph is dense: E is close to V 2
 We need to quickly determine if there is an edge

between two vertices


 Time to determine if (u, v)  E: (1)
• Disadvantage
 Consumes a lot of memory for sparse graph
 redundant info. for undirected graph

• Time to list all vertices adjacent to u: (V)

MA512: Data Structures and Algorithms


17
Weighted graphs
• Graphs for each edge has an associated weight
w(u, v) 4
w: E  R, weight function 1 2 1
• Storing the weights of a graph 3 2 5 3
 Adjacency list:
4
• Store w(u,v) along with vertex 5 4
3
v in u’s adjacency list
 Adjacency matrix:

• Store w(u, v) at location (u, v) in the matrix

MA512: Data Structures and Algorithms


18
Review
• Given an adjacency-list representation, how long
does it take to compute the out-degree of every
vertex? Θ(E)
• Give an adjacency-list representation for a
complete binary tree on 7 vertices. Give an
equivalent adjacency-matrix representation.
Assume that vertices are numbered from 1 to 7 as
in a binary heap.

MA512: Data Structures and Algorithms


19
Graph Traversal
• Graph traversal
 Systematically follow the edges of a graph to visit
(discover) the vertices
 Application: Check whether graph is connected, find
shortest path b/w nodes, etc.
• There are two standard graph traversal
techniques:
 Breadth-First Search (BFS)
 Depth-First Search (DFS)

MA512: Data Structures and Algorithms


20
Breadth First Search
• Input: Graph G = (V, E), either directed or
undirected, and source vertex s  V.
• Output:
 d[v] = shortest path from s to v, for all v  V.
d[v] =  ⇒ v is not reachable from s.
 [v] = u such that (u, v) is last edge on shortest path
from s to v.
• u is v’s predecessor.
 Builds breadth-first tree with root s that contains all
reachable vertices.

MA512: Data Structures and Algorithms


21
Breadth First Search: Principle
• Expands frontier b/w discovered and undiscovered
vertices uniformly across the breadth of the frontier.
 A vertex is discovered the first time it is encountered during
the search.
 A vertex is finished if all vertices adjacent to it have been
discovered.
• Colors the vertices to keep track of progress.
 White – Undiscovered.
 Gray – Discovered but not finished.
 Black – Finished.
• Colors are required only to reason about the algorithm. Can be
implemented without colors.
MA512: Data Structures and Algorithms
22
BFS(G,s)
1. for each vertex u in V[G] – {s}
2 do color[u]  white
BFS: Algo. 3
4
d[u]  
[u]  nil
5 color[s]  gray
6 d[s]  0
7 [s]  nil
Q: a queue 8 Q
of discovered 9 enqueue(Q,s)
vertices 10 while Q  
11 do u  dequeue(Q)
12 for each v in Adj[u]
13 do if color[v] = white
14 then color[v]  gray
15 d[v]  d[u] + 1
16 [v]  u
17 enqueue(Q,v)
18 color[u]  black

MA512: Data Structures and Algorithms


23
Example (BFS)

r s t u
 0   10 while Q  
11 do u  dequeue(Q)
12 for each v in Adj[u]
13 do if color[v] = white
14 then color[v]  gray
    15 d[v]  d[u] + 1
16 [v]  u
v w x y 17 enqueue(Q,v)
18 color[u]  black

Q: s
0
(Courtesy of Prof. Jim Anderson)
Example (BFS)

r s t u
1 0   10 while Q  
11 do u  dequeue(Q)
12 for each v in Adj[u]
13 do if color[v] = white
14 then color[v]  gray
 1   15 d[v]  d[u] + 1
16 [v]  u
v w x y 17 enqueue(Q,v)
18 color[u]  black

Q: w r
1 1
Example (BFS)

r s t u
1 0   10 while Q  
11 do u  dequeue(Q)
12 for each v in Adj[u]
13 do if color[v] = white
14 then color[v]  gray
 1   15 d[v]  d[u] + 1
16 [v]  u
v w x y 17 enqueue(Q,v)
18 color[u]  black

Q: w r
1 1
Example (BFS)

r s t u
1 0 2  10 while Q  
11 do u  dequeue(Q)
12 for each v in Adj[u]
13 do if color[v] = white
14 then color[v]  gray
 1 2  15 d[v]  d[u] + 1
16 [v]  u
v w x y 17 enqueue(Q,v)
18 color[u]  black

Q: r t x
1 2 2
Example (BFS)

r s t u
1 0 2  10 while Q  
11 do u  dequeue(Q)
12 for each v in Adj[u]
13 do if color[v] = white
14 then color[v]  gray
 1 2  15 d[v]  d[u] + 1
16 [v]  u
v w x y 17 enqueue(Q,v)
18 color[u]  black

Q: r t x
1 2 2
Example (BFS)

r s t u
1 0 2 

2 1 2 
v w x y

Q: t x v
2 2 2
Example (BFS)

r s t u
1 0 2 3

2 1 2 
v w x y

Q: x v u
2 2 3
Example (BFS)

r s t u
1 0 2 3

2 1 2 3
v w x y

Q: v u y
2 3 3
Example (BFS)

r s t u
1 0 2 3

2 1 2 3
v w x y

Q: u y
3 3

Comp 122, Fall 2004


Example (BFS)

r s t u
1 0 2 3

2 1 2 3
v w x y

Q: y
3
Example (BFS)

r s t u
1 0 2 3

2 1 2 3
v w x y

Q: 
Example (BFS)

r s t u
1 0 2 3

2 1 2 3
v w x y

The procedure builds a Breadth-First Tree


BFS(G,s)
BFS: 1. for each vertex u in V[G] – {s}
2 do color[u]  white
Initialization
Analysis 3
4
d[u]  
[u]  nil
5 color[s]  gray
6 d[s]  0
7 [s]  nil
8 Q
9 enqueue(Q,s)
10 while Q  
11 do u  dequeue(Q)
12 for each v in Adj[u]
13 do if color[v] = white
Loop 14 then color[v]  gray
15 d[v]  d[u] + 1
16 [v]  u
17 enqueue(Q,v)
18 color[u]  black

MA512: Data Structures and Algorithms


36
Analysis
• Initialization takes O(V).
• Traversal Loop
 The adjacency list of each vertex is scanned at most
once. The sum of lengths of all adjacency lists is (E).
 Each vertex is enqueued and dequeued at most once,
and each operation takes O(1). So, total time for
queuing is O(V).
• Summing up over all vertices => total running time
of BFS is O(V+E),
 linear in the size of the adjacency list representation of
graph.

MA512: Data Structures and Algorithms


37
Breadth First Tree
• For a graph G = (V, E) with source s, the predecessor
subgraph of G is G = (V , E) where
 V ={vV : [v]  NIL}{s}

 E ={([v],v)E : v  V - {s}}

r s t u
1 0 2 3

2 1 2 3
v w x y
MA512: Data Structures and Algorithms
38
Breadth First Tree
• For a graph G = (V, E) with source s, the predecessor
subgraph of G is G = (V , E) where
 V ={vV : [v]  NIL}{s}

 E ={([v],v)E : v  V - {s}}

[t]=w [u]=t
r s t u
[r]=s 1 0 2 3

[w]=s [x]=w [y]=x

[v]=r 2 1 2 3
v w x y
MA512: Data Structures and Algorithms
39
Breadth First Tree
• The predecessor subgraph G is a breadth-first tree
if:
 V consists of the vertices reachable from s and
 for all vV , there is a unique simple path from s to v in G
that is also a shortest path from s to v in G.
• The edges in E are called tree edges.
|E | = |V | - 1.

MA512: Data Structures and Algorithms


40
Acknowledgement
David A. Plaisted
Department of Computer Science
University of North Carolina at Chapel Hill

Analysis of Algorithms
41

You might also like