Outline and Reading Graphs
43 17
802
Graphs (6.1)
Definition Applications Terminology Properties ADT
SFO
337
1843
ORD
Data structures for graphs (6.2)
Edge list structure Adjacency list structure Adjacency matrix structure
1 5/3/2002 7:41 AM Graphs 2
LAX
1233
DFW
5/3/2002 7:41 AM
Graphs
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
Edge Types
Directed edge
ordered pair of vertices (u,v) first vertex u is the origin second vertex v is the destination e.g., a flight
ORD
flight AA 1206 849 miles
PVD
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
Undirected edge
unordered pair of vertices (u,v) e.g., a flight route
ORD
PVD
802
SFO
337
1843
43 17
ORD
849
2 14
PVD
Directed graph
all the edges are directed e.g., route network
HNL
2555
LAX
1233
DFW
7 138 1120
LGA
10 99
Undirected graph
all the edges are undirected e.g., flight network
3 5/3/2002 7:41 AM Graphs 4
MIA
5/3/2002 7:41 AM
Graphs
Applications
cslab1a cslab1b math.brown.edu
Terminology
End vertices (or endpoints) of an edge
U and V are the endpoints of a
cs.brown.edu
Electronic circuits
Printed circuit board Integrated circuit
Transportation networks
Highway network Flight network
a U c
V d
b X e
Edges incident on a vertex
brown.edu qwest.net att.net
h Z i g
a, d, and b are incident on V
Adjacent vertices
U and V are adjacent
Computer networks
Local area network Internet Web
Degree of a vertex
X has degree 5
cox.net John
W f
Parallel edges
h and i are parallel edges
David
Databases
Entity-relationship diagram
5/3/2002 7:41 AM
Paul
Self-loop
j is a self-loop
5 5/3/2002 7:41 AM Graphs 6
Graphs
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
Terminology (cont.)
Cycle
a U c
V d P2 W f
P1 X h Z
circular sequence of alternating vertices and edges each edge is preceded and followed by its endpoints
a U c
V d C2 W f
b X e h C1 g Y Z
Simple cycle
cycle such that all its vertices and edges are distinct
Simple path
path such that all its vertices and edges are distinct
e g Y
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
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
5/3/2002 7:41 AM
Graphs
5/3/2002 7:41 AM
Graphs
Properties
Property 1 Notation
n m deg(v) number of vertices number of edges degree of vertex v
Main Methods of the Graph ADT
Vertices and edges
are positions store elements
v deg(v) = 2m
Proof: each endpoint is counted twice
Update methods
insertVertex(o) insertEdge(v, w, o) insertDirectedEdge(v, w, o) removeVertex(v) removeEdge(e)
Property 2
In an undirected graph with no selfloops and no multiple edges m n (n 1)/2 Proof: each vertex has degree at most (n 1)
5/3/2002 7:41 AM Graphs
Accessor methods
aVertex() incidentEdges(v) endVertices(e) isDirected(e) origin(e) destination(e) opposite(v, e) areAdjacent(v, w)
Example n=4 m=6 deg(v) = 3
Generic methods
numVertices() numEdges() vertices() edges()
5/3/2002 7:41 AM
Graphs
10
Edge List Structure
Vertex object
element reference to position in vertex sequence v u a b c w d z
Adjacency List Structure
Edge list structure Incidence sequence for each vertex
sequence of references to edge objects of incident edges z a u v b w
Edge object
element origin vertex object destination vertex object reference to position in edge sequence
Augmented edge objects
references to associated positions in incidence sequences of end vertices
Vertex sequence
sequence of vertex objects a b c d
Edge sequence
sequence of edge objects
5/3/2002 7:41 AM Graphs 11
5/3/2002 7:41 AM
Graphs
12
Adjacency Matrix Structure
Edge list structure Augmented vertex objects
Integer key (index) associated with vertex a u v b w
Performance
n vertices m edges no parallel edges no self-loops
Edge List n+m m m 1 1 m 1
Graphs
Adjacency List n+m deg(v) min(deg(v), deg(w)) 1 1 deg(v) 1
Adjacency Matrix n2 n 1 n2 1 n2 1
14
Space
0 u 1 0 0 1 a 2 b v 1 2 2 w
2D-array adjacency array
Reference to edge object for adjacent vertices Null for non nonadjacent vertices
incidentEdges(v) areAdjacent (v, w) insertVertex(o) insertEdge(v, w, o) removeVertex(v) removeEdge(e)
5/3/2002 7:41 AM
5/3/2002 7:41 AM
Graphs
13