Graph
Graph
Graphs
1843 ORD
SFO
802
43
17
337
1233
LAX DFW
Graph
A graph is a pair (V, E), where
V is a set of nodes, called vertices
E is a collection of edges (pairs of vertices)
Vertices and edges are positions and store elements
Graphs useful for representing real-world relationships:
vertex = airport
edge = flight route, storing mileage
Abstract real-world problems into problems on graphs
849 PVD
1843 ORD 2
SFO 14
802
43 LGA
17
337
7
HNL 2555 138 10
99
1233
LAX DFW 1120
MIA
Graphs version 1.3 3
1
Graphs 2/21/2003 9:27 AM
Applications
cslab1a cslab1b
Databases Paul
David
Entity-relationship diagram
Graphs version 1.3 4
Sample problems
What is cheapest way to fly from X to Y?
If airport X closes from bad weather, can I
still fly between every other pair of cities?
Many classes have prereqs; in what order can
I take the classes for my major?
How much traffic can flow between
intersection X and intersection Y
How can I minimize the amount of wiring
needed to connect some outlets together?
Edge Types
Directed edge
ordered pair of vertices (u,v)
flight
first vertex u is the origin ORD AA 1206 PVD
second vertex v is the destination
e.g., a flight
Undirected edge
unordered pair of vertices (u,v) 849
ORD PVD
2
Graphs 2/21/2003 9:27 AM
Terminology
End vertices (or endpoints) of an
edge
U and V are the endpoints of a V
Edges incident on a vertex a b
h j
a, d, and b are incident on V
Adjacent vertices U d X Z
U and V are adjacent
c e i
Degree of a vertex
X has degree 5 W g
Parallel edges (typically not used)
h and i are parallel edges f
Self-loop (typically not used) Y
j is a self-loop
Terminology (cont.)
Path
sequence of alternating vertices
and edges
V
begins and ends with some vertex a b
each edge is preceded and P1
followed by its endpoints d
Simple path
U X Z
P2 h
path such that all its vertices and c e
edges are distinct
Reachable W g
path exists
Examples
f
P1=(V,b,X,h,Z) is a simple path
Y
P2=(U,c,W,e,X,g,Y,f,W,d,V) is a
path that is not simple
Z is reachable from U
Graphs version 1.3 8
Terminology (cont.)
Cycle
circular sequence of alternating
vertices and edges
each edge is preceded and V
followed by its endpoints a b C3
edges traversed only in one
direction d
U X Z
Simple cycle C2 h
cycle such that all its vertices
c e
and edges are distinct
W g
Examples
C1=(V,b,X,g,Y,f,W,c,U,a,↵) is a C1
simple cycle f
C2=(U,c,W,e,X,g,Y,f,W,d,V,a,↵)
Y
is a cycle that is not simple
C3=(X,h,Z,h,X) is not a cycle
3
Graphs 2/21/2003 9:27 AM
Subgraphs
A subgraph S of a graph
G is a graph such that
The vertices of S are a
subset of the vertices of G
The edges of S are a Subgraph
subset of the edges of G
A spanning subgraph of G
is a subgraph that
contains all the vertices
of G
Spanning subgraph
Connectivity
A graph is
connected if there is
a path between
every pair of Connected graph
vertices
A connected
component of a
graph G is a
maximal connected
subgraph of G Non connected graph with two
connected components
4
Graphs 2/21/2003 9:27 AM
Properties
Property 1 Notation
Σv deg(v) = 2m n number of vertices
Proof: each edge is m number of edges
counted twice deg(v) degree of vertex v
Property 2
In an undirected graph Example
(with no self-loops or
parallel edges) n = 4
m ≤ n (n − 1)/2 m = 6
Proof: at most one edge deg(v) = 3
for every unique
combination of 2 for all
vertices vertices
What is the bound on m
for a directed graph?Graphs version 1.3 14
Vertex aVertex()
5
Graphs 2/21/2003 9:27 AM
6
Graphs 2/21/2003 9:27 AM
a b
7
Graphs 2/21/2003 9:27 AM
Asymptotic Performance
n vertices, m edges Edge Adjacency Adjacency
List List Matrix
Space O(n + m) O(n + m) O(n2 )
Iterating through
O(m) O( deg(v) ) O(n)
incidentEdges(v)
O( min(deg(v),
areAdjacent (v, w) O(m) O(1)
deg(w)) )
insertVertex(o) O(1) O(1) O(n2 )
insertEdge(v, w, o) O(1) O(1) O(1)
removeVertex(v) O(m) O( deg(v) ) O(n2 )
removeEdge(e) O(1) O(1) O(1)
Notes: Assuming no parallel edges or self-loops
Using Positions (for removeVertex and removeEdge)
Graphs version 1.3 22
Depth-First Search
A
B D E
8
Graphs 2/21/2003 9:27 AM
Depth-First Search
Depth-first search (DFS) is
general graph traversal technique
visits all the vertices and edges of G
with n vertices and m edges takes O(n + m ) time
a recursive traversal like Euler tour for binary trees
A DFS traversal of a graph G can be used to
Determines whether G is connected
Computes the connected components of G
Computes a spanning forest of G
Find and report a path between two given vertices
Find a cycle in the graph
Graphs version 1.3 25
Example
A
A unexplored vertex
A visited vertex
B D E
unexplored edge
discovery edge C
back edge
A A
B D E B D E
C C
Example (cont.)
A A
B D E B D E
C C
A A
B D E B D E
C C
9
Graphs 2/21/2003 9:27 AM
DFS Algorithm
The algorithm uses a mechanism
for setting and getting “labels” of Algorithm DFS(G, v)
vertices and edges Input graph G and a start vertex v of G
Algorithm DFS_Sweep(G) Output labeling of the edges of G
Input graph G in the connected component of v
Output labeling of the edges of G as discovery edges and back edges
as discovery edges and setLabel(v, VISITED)
back edges for all e ∈ G.incidentEdges(v)
for all u ∈ G.vertices() if getLabel(e) = UNEXPLORED
setLabel(u, UNEXPLORED) w ← G.opposite(v,e)
for all e ∈ G.edges() if getLabel(w) = UNEXPLORED
setLabel(e, UNEXPLORED) setLabel(e, DISCOVERY)
for all v ∈ G.vertices() DFS(G, w)
if getLabel(v) = UNEXPLORED else
DFS(G, v) setLabel(e, BACK)
3
5
7
4
10
Graphs 2/21/2003 9:27 AM
DFS Algorithm
The algorithm uses a mechanism
for setting and getting “labels” of Algorithm DFS(G, v)
vertices and edges Input graph G and a start vertex v of G
Algorithm DFS_Sweep(G) Output labeling of the edges of G
Input graph G in the connected component of v
Output labeling of the edges of G as discovery edges and back edges
as discovery edges and setLabel(v, VISITED)
back edges for all e ∈ G.incidentEdges(v)
for all u ∈ G.vertices() if getLabel(e) = UNEXPLORED
setLabel(u, UNEXPLORED) w ← G.opposite(v,e)
for all e ∈ G.edges() if getLabel(w) = UNEXPLORED
setLabel(e, UNEXPLORED) setLabel(e, DISCOVERY)
for all v ∈ G.vertices() DFS(G, w)
if getLabel(v) = UNEXPLORED else
DFS(G, v) setLabel(e, BACK)
Analysis of DFS
Setting/getting a vertex/edge label takes O(1) time
Each vertex is labeled twice
once as UNEXPLORED
once as VISITED
Each edge is labeled twice
once as UNEXPLORED
once as DISCOVERY or BACK
DFS(G, v) called once for each vertex v
Inner loop in DFS(G, v) runs in O(deg(v)) time
Not counting time inside recursive calls
Assuming adjacency list implementation
DFS runs in O(n + m) time
Recall that Σv deg(v)Graphs
= 2m
version 1.3 32
Properties of DFS
Property 1
DFS(G, v) visits all the
vertices and edges in the
connected component of v A
Property 2
The discovery edges labeled
by DFS(G, v) form a B D E
spanning tree of the
connected component of v
called DFS tree, rooted at v
C
Property 3
DFS_Sweep(G) visits all
vertices and edges of G
11
Graphs 2/21/2003 9:27 AM
Path Finding
Specialize DFS to find a path
between two given vertices v Algorithm pathDFS(G, v, z)
and z setLabel(v, VISITED)
S.push(v)
Call DFS(G, v, z) where
if v = z
G is the graph return S.elements()
v is the start vertex for all e ∈ G.incidentEdges(v)
z is the destination vertex if getLabel(e) = UNEXPLORED
Use a stack S to keep track w ← opposite(v,e)
of the path between the if getLabel(w) = UNEXPLORED
start vertex and the current setLabel(e, DISCOVERY)
vertex S.push(e)
As soon as destination pathDFS(G, w, z)
vertex z is encountered, we S.pop(e)
return the path as the else
contents of the stack setLabel(e, BACK)
S.pop(v)
Graphs version 1.3 36
12
Graphs 2/21/2003 9:27 AM
Cycle Finding
Algorithm cycleDFS(G, v)
Specialize DFS to find a setLabel(v, VISITED)
simple cycle S.push(v)
We use a stack S to for all e ∈ G.incidentEdges(v)
if getLabel(e) = UNEXPLORED
keep track of the path w ← opposite(v,e)
between the start vertex S.push(e)
and the current vertex if getLabel(w) = UNEXPLORED
setLabel(e, DISCOVERY)
As soon as a back edge cycleDFS(G, w)
(v, w) is encountered, S.pop(e)
we return the cycle as else
T ← new empty stack
the portion of the stack repeat
from the top to vertex w o ← S.pop()
T.push(o)
until o = w
return T.elements()
S.pop(v)
Graphs version 1.3 37
13