You are on page 1of 3

Graphs 3/17/2005 2:11 AM

Outline and Reading


Graphs (§6.1)
Graphs „

„
Definition
Applications
„ Terminology
1843 ORD „ Properties
SFO
ADT

802
„
43
17
337
Data structures for graphs (§6.2)
1233 Edge list structure
LAX DFW
„

„ Adjacency list structure


„ Adjacency matrix structure

Graphs 1 Graphs 2

Graph Edge Types


A graph is a pair (V, E), where Directed edge
V is a set of nodes, called vertices ordered pair of vertices (u,v)
„ „
flight
„ E is a collection of pairs of vertices, called edges „ first vertex u is the origin ORD AA 1206 PVD
„ Vertices and edges are positions and store elements „ second vertex v is the destination
Example: „ e.g., a flight
„ A vertex represents an airport and stores the three-letter airport code Undirected edge
An edge represents a flight route between two airports and stores the unordered pair of vertices (u,v) 849
ORD PVD
„ „
mileage of the route „ e.g., a flight route miles
849 PVD Directed graph
1843 ORD 2
SFO 14 „ all the edges are directed
802

LGA e.g., flight network


43
„

17
337

7 Undirected graph
HNL 2555 138 10
99 all the edges are undirected
1233 „
LAX DFW 1120 „ e.g., route network
MIA
Graphs 3 Graphs 4

Applications Terminology
cslab1a cslab1b

Electronic circuits math.brown.edu


End vertices (or endpoints) of
„ Printed circuit board an edge
Integrated circuit
„ U and V are the endpoints of a V
„
cs.brown.edu Edges incident on a vertex a b
Transportation networks h j
„ a, d, and b are incident on V
„ Highway network brown.edu Adjacent vertices U d X Z
Flight network U and V are adjacent
„
qwest.net
„
c e i
Degree of a vertex
Computer networks att.net
W
„ X has degree 5 g
„ Local area network Parallel edges
„ Internet cox.net „ h and i are parallel edges f
„ Web John Self-loop Y
Databases Paul „ j is a self-loop
David

„ Entity-relationship diagram
Graphs 5 Graphs 6

1
Graphs 3/17/2005 2:11 AM

Terminology (cont.) Terminology (cont.)


Path Cycle
„ sequence of alternating „ circular sequence of alternating
vertices and edges vertices and edges
V each edge is preceded and
V
„ begins with a vertex a b „
a b
„ ends with a vertex P1 followed by its endpoints
each edge is preceded and d Simple cycle d
„
followed by its endpoints
U X Z U X Z
P2 h „ cycle such that all its vertices C2 h
Simple path c e and edges are distinct e C1
path such that all its vertices Examples
c
W W
„
and edges are distinct g g
„ C1=(V,b,X,g,Y,f,W,c,U,a,↵) is a
Examples simple cycle
„ P1=(V,b,X,h,Z) is a simple path f f
P2=(U,c,W,e,X,g,Y,f,W,d,V) is a
Y „ C2=(U,c,W,e,X,g,Y,f,W,d,V,a,↵) Y
„
is a cycle that is not simple
path that is not simple

Graphs 7 Graphs 8

Properties Main Methods of the Graph ADT


Property 1 Notation
Vertices and edges Update methods
Σv deg(v) = 2m n number of vertices
„ are positions „ insertVertex(o)
Proof: each edge is m number of edges „ store elements „ insertEdge(v, w, o)
counted twice deg(v) degree of vertex v insertDirectedEdge(v, w, o)
Accessor methods „
Property 2 „ aVertex() „ removeVertex(v)
In an undirected graph Example „ incidentEdges(v) „ removeEdge(e)
with no self-loops and Generic methods
no multiple edges „ n = 4 „ endVertices(e)
isDirected(e) numVertices()
„ m = 6
„ „
m ≤ n (n − 1)/2
„ origin(e) „ numEdges()
Proof: each vertex has „ deg(v) = 3
degree at most (n − 1) „ destination(e) „ vertices()
„ opposite(v, e) „ edges()
What is the bound for a „ areAdjacent(v, w)
directed graph?
Graphs 9 Graphs 10

Edge List Structure Adjacency List Structure


Vertex object u Edge list structure a v b
„ element a c Incidence sequence u w
„ reference to position in b d for each vertex
vertex sequence v w z „ sequence of
Edge object references to edge
„ element objects of incident
„ origin vertex object edges u v w
„ destination vertex object Augmented edge
„ reference to position in u v w z objects
edge sequence „ references to
associated
Vertex sequence positions in
„ sequence of vertex a b c d incidence
objects sequences of end a b
Edge sequence vertices
„ sequence of edge objects

Graphs 11 Graphs 12

2
Graphs 3/17/2005 2:11 AM

Adjacency Matrix Structure Asymptotic Performance


Edge list structure a v b n vertices, m edges
Augmented vertex u w no parallel edges Edge Adjacency Adjacency
objects no self-loops List List Matrix
„ Integer key (index) Bounds are “big-Oh”
associated with vertex
2D adjacency array
Space n+m n+m n2
0 u 1 v 2 w
„ Reference to edge incidentEdges(v) m deg(v) n
object for adjacent
vertices areAdjacent (v, w) m min(deg(v), deg(w)) 1
0 1 2
Null for non
„
nonadjacent vertices 0 ∅ ∅ insertVertex(o) 1 1 n2
The “old fashioned” 1 ∅ insertEdge(v, w, o) 1 1 1
version just has 0 for a b
no edge and 1 for edge
2 ∅ ∅ removeVertex(v) m deg(v) n2
removeEdge(e) 1 1 1
Graphs 13 Graphs 14

You might also like