outline and reading - site.uottawa.ca
TRANSCRIPT
Graphs 9/14/2004 2:50 PM
1
Graphs 1
Graphs
ORD
DFW
SFO
LAX
80
2
1743
1843
1233
33
7
Graphs 2
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 3
GraphA 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
ORDPVD
MIADFW
SFO
LAX
LGA
HNL
849
80
2
13871743
1843
10991120
1233
33
7
2555
142
Graphs 4
Edge TypesDirected edge� ordered pair of vertices (u,v)
� first vertex u is the origin
� second vertex v is the destination
� e.g., a flight
Undirected edge� unordered pair of vertices (u,v)
� e.g., a flight route
Directed graph
� all the edges are directed
� e.g., route network
Undirected graph� all the edges are undirected
� e.g., flight network
ORD PVDflight
AA 1206
ORD PVD849
miles
Graphs 5
John
DavidPaul
brown.edu
cox.net
cs.brown.edu
att.net
qwest.net
math.brown.edu
cslab1bcslab1a
ApplicationsElectronic circuits
� Printed circuit board
� Integrated circuit
Transportation networks
� Highway network
� Flight network
Computer networks
� Local area network
� Internet
� Web
Databases
� Entity-relationship diagram
Graphs 6
TerminologyEnd vertices (or endpoints) of an edge
� U and V are the endpoints of a
Edges incident on a vertex� a, d, and b are incident on V
Adjacent vertices
� U and V are adjacent
Degree of a vertex� X has degree 5
Parallel edges� h and i are parallel edges
Self-loop
� j is a self-loop
XU
V
W
Z
Y
a
c
b
e
d
f
g
h
i
j
Graphs 9/14/2004 2:50 PM
2
Graphs 7
P1
Terminology (cont.)
Path� sequence of alternating
vertices and edges
� begins with a vertex
� ends with a vertex
� each edge is preceded and followed by its endpoints
Simple path� path such that all its vertices
and edges are distinct
Examples� P1=(V,b,X,h,Z) is a simple path
� P2=(U,c,W,e,X,g,Y,f,W,d,V) is a path that is not simple
XU
V
W
Z
Y
a
c
b
e
d
f
g
hP2
Graphs 8
Terminology (cont.)Cycle
� circular sequence of alternating vertices and edges
� each edge is preceded and followed by its endpoints
Simple cycle
� cycle such that all its vertices and edges are distinct
Examples
� C1=(V,b,X,g,Y,f,W,c,U,a,↵) is a
simple cycle
� C2=(U,c,W,e,X,g,Y,f,W,d,V,a,↵)is a cycle that is not simple
C1
XU
V
W
Z
Y
a
c
b
e
d
f
g
hC2
Graphs 9
PropertiesNotation
n number of vertices
m number of edges
deg(v) degree of vertex v
Property 1
ΣΣΣΣv deg(v) = 2m
Proof: each endpoint is counted twice
Property 2In an undirected
graph with no self-loops and no multiple edges
m ≤≤≤≤ n (n −−−− 1)////2Proof: each vertex
has degree at most (n −−−− 1)
Example
� n = = = = 4
� m = = = = 6
� deg(v) = 3
Graphs 10
Connected GraphsA (non-directed) graph is connected if there exists a path
∀ u, v ∈ V.
u
Connected components
G
Graphs 11
Main Methods of the Graph ADT
Vertices and edges
� are positions
� store elements
Accessor methods
� aVertex()
� incidentEdges(v)
� endVertices(e)
� isDirected(e)
� origin(e)
� destination(e)
� opposite(v, e)
� areAdjacent(v, w)
Update methods
� insertVertex(o)
� insertEdge(v, w, o)
� insertDirectedEdge(v, w, o)
� removeVertex(v)
� removeEdge(e)
Generic methods
� numVertices()
� numEdges()
� vertices()
� edges()
There could be other methods …..
Graphs 12
Representations
Edge ListAdjacency ListAdjacency MatrixIncidence Matrix
Graphs 9/14/2004 2:50 PM
3
Graphs 13
Edge List Structure (example)
l9l8l7l6l5l4l3l2l1
v2
v1
v6
v3
v4
v5
l1
l2
l3
l4 l
5l6
l7 l
8l9
Space: n + m
v6v5v4v3v2v1
Graphs 14
Edge List StructureVertex object
� element
� reference to position in vertex sequence
Edge object
� element
� origin vertex object
� destination vertex object
� reference to position in edge sequence
Vertex sequence
� sequence of vertex objects
Edge sequence� sequence of edge objects
v
u
w
a c
b
a
zd
u v w z
b c d
Graphs 15
5
1
4
2
3
5
4
3
2
1 2 4
2
5
1 2
Adjacency List (example)
Graphs 16
Adjacency List Structure Edge list structure
Incidence sequence for each vertex
� sequence of references to edge objects of incident edges
Augmented edge objects� references to
associated positions in incidence sequences of end vertices
u
v
w
a b
a
u v w
b
Graphs 17
5
1
4
2
3 000115
100004
000103
000002
010101
54321
G
5
1
4
2
3
G
If G is not-directed
010115
100014
000103
101012
110101
54321
symmetric matrix
Adjacency Matrix (examples)
Graphs 18
Space: n x n
Lots of waste space if the matrix is SPARSE …
0000100100
0000000110
0001000000
0010000010
1000000000
0
0
0
0
0
0
0
0
1
0
1
0
0
0
0
0
0
0
1
0
0
1
0
0
1
10000
01101
10000
00100
00001
Adjacency Matrix (observation)
Graphs 9/14/2004 2:50 PM
4
Graphs 19
Adjacency Matrix StructureEdge list structure
Augmented vertex objects
� Integer key (index) associated with vertex
2D-array adjacency array� Reference to edge
object for adjacent vertices
� Null for non nonadjacent vertices
u
v
w
a b
2
1
0
210
∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅
a
u v w0 1 2
b
Graphs 20
Incidence Matrix (1)
100-100000v6
-1-11000000v5
0
0
0
0
l9
1
0
0
0
l8
0
-1
0
0
l7
0
0
1
0
l6
-1
1
0
0
l5
0-100v4
-10-10v3
1001v2
011-1v1
l4l3l2l1
v2
v1
v6
v3
v4
v5
l1
l2
l3
l4 l
5l6
l7 l
8l9
Space: n x m
Graphs 21
Is (vi, vj) an edge?
Adjacency
Matrix:
Adjacency
List:
Edge
List:
[ ]i
j
O(1)
i …
O(deg(i))
l9l8l
7
l
6
l
5
l
4
l
3
l2l1
v6
v5
v4
v3
v2
v1 O(m)
Graphs 22
Which nodes are adjacent to vi?
Adjacency
Matrix:
Adjacency
List:
Edge
List:
[ ]i O(n)
i
O(deg(i))
l9l8l
7
l
6
l
5
l
4
l
3
l2l1
v6
v5
v4
v3
v2
v1 O(m)
Graphs 23
Mark all Edges
Adjacency
Matrix:
Adjacency
List:
[ ] O(n2)
1 2 ………… n
1
2
M
M
n
O(m)
1
n
l9l8l
7
l
6
l
5
l
4
l
3
l2l1
v6
v5
v4
v3
v2
v1
Edge
List: O(m)
Graphs 24
Add an Edge (vi, vj)
Adjacency
Matrix:
Adjacency
List (linked):
[ ] O(1)
O(1)i
i
j
1
l9l8l
7
l
6
l
5
l
4
l
3
l2l1
v6
v5
v4
v3
v2
v1
Edge
List: O(1)
Graphs 9/14/2004 2:50 PM
5
Graphs 25
Remove an Edge (vi, vj)
Adjacency
Matrix:
Adjacency
List :
[ ] O(1)
O(deg(i))ji
i
j
0
l9l8l
7
l
6
l
5
l
4
l
3
l2l1
v6
v5
v4
v3
v2
v1
Edge
List: O(1)
Graphs 26
O(deg(i))O(1)Remove edge
(vi, vj)
O(1)O(1)Add edge
(vi, vj)
O(m)O(n2)Mark all edges
O(deg(i))O(n)Which nodes are
adjacent to vi?
O(deg(i))O(1)Is (vi, vj) an
edge?
Adjacency List
Adjacency Matrix
O(deg(i)) = OUT-degree of node vi
G is directed
What are the
predecessors of vi??
Graphs 27
Performance
n2n + mn + mSpace
n2deg(v)mremoveVertex(v)
111insertEdge(v, w, o)
n211insertVertex(o)
111removeEdge(e)
1min(deg(v), deg(w))mareAdjacent (v, w)
ndeg(v)mincidentEdges(v)
Adjacency Matrix
AdjacencyList
EdgeList
n vertices
m edges
no parallel edges
no self-loops
Graphs 28
Special GraphsRegular Graphs
∀vi, vj ∈ V d(vi) = d(vj)
Bipartite Graphs Planar Graphs
Cannot have
Graphs 29
n – 1 ≤ m ≤
1 ≤ deg(i) ≤ n – 1
2
)1( −nn
connected,
non-directed
degree
n – 1 ≤ m ≤ n(n – 1)
1 ≤ deg(i) ≤ n – 1
connected,
directed
OUT-degree
n = | V | m = | E |
Graphs 30
—Ring —
— Complete Graph —
— Hypercube —
m = n
di = 2 ∀i
Tree
m = O(n)
m =
di = n – 1 ∀i
m = O(n2)2
)1( −nn
dim 0 dim 1 dim 2
Some Regular Graphs
dim 3 dim 4
Graphs 9/14/2004 2:50 PM
6
Graphs 31
Hypercube
12x2+8 = 3216h4
4x2+4 = 128h3
1x2+2 = 44h2
12h1
01h0
mn
n0 = 1 ni = 2i
ni = 2 ni–1 mi = i•2i–1
hi:
m =
d = log n
m = O(n log n)2
lognn
Graphs 32
—Grid —
— Torus —
Not regular
m = O(n)
deg(i) = 4 vi = internal
deg(i) = 3 vi = border
deg(i) = 2 vi = corner
m = O(n)
deg(i) = 4 ∀i