Graph
Graph
1843 ORD
SFO
802
4 3
17
337
1233
LAX DFW
Graphs 1
Outline and Reading
Graphs (§6.1)
Definition
Applications
Terminology
Properties
ADT
Data structures for graphs (§6.2)
Edge list structure
Adjacency list structure
Adjacency matrix structure
Graphs 2
Graph
A graph is a pair (V, E), where
V is a set of nodes, called vertices
E is a collection of pairs of vertices, called edges
Vertices and edges are positions and store elements
Example:
A vertex represents an airport and stores the three-letter airport code
An edge represents a flight route between two airports and stores the
mileage of the route
849 PVD
1843 ORD 2
SFO 14
7 43 802 LGA
337
1 8 7 10
2555 13
HNL 1233 99
LAX DFW 1120
MIA
Graphs 3
Edge Types
Directed edge
ordered pair of vertices (u,v)
first vertex u is the origin flight
second vertex v is the
ORD AA 1206 PVD
destination
e.g., a flight
Undirected edge
unordered pair of vertices (u,v) 849
ORD PVD
Graphs 4
Applications
cslab1a cslab1b
Databases Paul
David
Entity-relationship diagram
Graphs 5
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 (or multiple
edges) f
h and i are parallel edges Y
Self-loop
j is a self-loop
Graphs 6
Terminology (cont.)
Path
sequence of alternating
vertices and edges
V
begins with a vertex a b
ends with a vertex P1
each edge is preceded and
U d X Z
followed by its endpoints
P2 h
Simple path c e
path such that all its vertices
and edges are distinct W g
Examples
P1=(V,b,X,h,Z) is a simple path f
P2=(U,c,W,e,X,g,Y,f,W,d,V) is a
Y
path that is not simple
Graphs 7
Terminology (cont.)
Cycle
circular sequence of alternating
vertices and edges
each edge is preceded and
V
a b
followed by its endpoints
Simple cycle
U d X Z
cycle such that all its vertices C2 h
and edges are distinct
c e C1
Examples W g
C1=(V,b,X,g,Y,f,W,c,U,a,) is a
simple cycle
f
C2=(U,c,W,e,X,g,Y,f,W,d,V,a,) Y
is a cycle that is not simple
Graphs 8
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
Example
In an undirected graph
with no self-loops and n 4
Graphs 9
Main Methods of the Graph ADT
Vertices and edges Update methods
are positions insertVertex(o)
store elements insertEdge(v, w, o)
Accessor methods insertDirectedEdge(v, w, o)
aVertex() removeVertex(v)
incidentEdges(v) removeEdge(e)
endVertices(e) Generic methods
isDirected(e) numVertices()
origin(e) numEdges()
destination(e) vertices()
opposite(v, e) edges()
areAdjacent(v, w)
Graphs 10
Edge List Structure
Vertex object u
element a c
reference to position in b d
vertex sequence v w z
Edge object
element
origin vertex object
destination vertex object
reference to position in u v w z
edge sequence
Vertex sequence
sequence of vertex a b c d
objects
Edge sequence
sequence of edge objects
Graphs 11
Edge List Structure
It provides direct access from edges to the vertices
they are incident on.
Simple implementation of edged-based methods:
endVertices(e)
origin(e)
destination(e)
opposite(v, e)
However, accessing edges that are incident on a
vertex requires an exhaustive inspection of all the
edges
O(n+m) space
Graphs 12
Adjacency List Structure
Edge list structure a v b
Incidence sequence u w
for each vertex
sequence of
references to edge
objects that are
incident on the
u v w
vertex
Augmented edge
objects
references to
associated positions
in incidence
sequences of end
a b
vertices
Graphs 13
Adjacency List Structure
Direct access from the edges to the vertices and from
the vertices to their incident edges.
Easy implementation of more functions:
areAdjacent (u,v) O(min{deg(u), deg(v)}.
…
O(n+m) space
Graphs 14
Adjacency Matrix Structure
Edge list structure a v b
Augmented vertex u w
objects
Integer key (index)
associated with vertex
2D adjacency array
Reference to edge 0 u 1 v 2 w
object for adjacent
vertices
0 1 2
Null for non
nonadjacent vertices 0
The “old fashioned” 1
version just has 0 for a b
2
no edge and 1 for edge
Graphs 15
Adjacency Matrix Structure
Easy implementation of more functions:
areAdjacent (u,v) O(1)
Space O(nm)
Graphs 16
Asymptotic Performance
n vertices, m edges
no parallel edges Edge Adjacency Adjacency
no self-loops List List Matrix
Bounds are “big-Oh”
Space nm nm n2
incidentEdges(v) m deg(v) n
areAdjacent (v, w) m min(deg(v), deg(w)) 1
insertVertex(o) 1 1 n2
insertEdge(v, w, o) 1 1 1
removeVertex(v) m deg(v) n2
removeEdge(e) 1 1 1
Graphs 17