Uploaded by Usman Arshad

graphs

advertisement
Graphs
SFO
LAX
© 2010 Goodrich, Tamassia
Graphs
ORD
DFW
1
Graphs


way of representing relationships that exist between pairs of objects
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
DUB
PSH
LHR
ISB
HNL
© 2010 Goodrich, Tamassia
SHR
KHR
QTA
Edge Types

Directed edge







unordered pair of vertices (u,v)
e.g., a flight route
ISB
849
miles
LHR
all the edges are directed
e.g., route network
Undirected graph



LHR
Directed graph


ISB
flight
AA 1206
Undirected edge


ordered pair of vertices (u,v)
first vertex u is the origin
second vertex v is the destination
e.g., a flight
all the edges are undirected
e.g., flight network
Mixed graph

Combination of directed and undirected graph
© 2010 Goodrich, Tamassia
Graphs
3
Applications
cslab1a

cslab1b
General
math.brown.edu
Social networks
 Google map
 Electronic Circuits


cs.brown.edu
Transportation networks
Highway network
 Flight network
brown.edu


qwest.net
att.net
Computer networks
Local area network
 Internet
 Web


cox.net
John
Paul
David
Databases

Entity-relationship diagram
Graphs
© 2010 Goodrich, Tamassia
4
Terminology

End vertices (or endpoints) of an edge


Edges incident on a vertex


U
X has degree 5
h and i are parallel edges
d
Graphs
h
X
e
j
Z
i
g
f
j is a self-loop
© 2010 Goodrich, Tamassia
b
W
Self-loop

V
c
Parallel edges


U and V are adjacent
Degree of a vertex


a
a, d, and b are incident on V
Adjacent vertices


U and V are the endpoints of a
Y
5
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


Examples



path such that all its vertices and edges are
a
distinct
U
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
c
not simple
Simple Graph


No parallel edges or self-loops
edges of a simple graph are a et of vertex pairs
(and not just a collection).
© 2010 Goodrich, Tamassia
Graphs
V
b
d
P2
P1
X
e
W
h
g
f
Y
6
Z
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

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
© 2010 Goodrich, Tamassia
U
c
Examples

a
Graphs
V
b
d
C2
X
e
C1
g
W
f
h
Z
Y
7
Properties
Property 1
Notation
Sv deg(v) = 2m
Proof: each edge is counted
twice
Property 2
In an undirected simple graph
m  n (n - 1)/2
Proof: each vertex has degree
at most (n - 1)
© 2010 Goodrich, Tamassia
Graphs
n
m
deg(v)
number of vertices
number of edges
degree of vertex v
Example
 n = 4
 m = 6
 deg(v) = 3
8
Main Methods of the Graph ADT
As an ADT, a graph is a collection of elements that are stored at the
graph’s positions—its vertices and edges
Vertic v: *v: reference to element associated with vertex v
Edge e: *e: reference to element associated with edge e

Access methods



e.endVertices():

e.opposite(v):


 true if u and v are adjacent


 Test whether e is incident on v.

incidentEdges(v):



list of edges incident to v
vertices():

© 2010 Goodrich, Tamassia
remove edge e
Iterable collection methods

Graphs
remove vertex v (and its incident edges)
eraseEdge(e):

e.isIncidentOn(v):
insert an edge (v,w) storing element o
eraseVertex(v):

u.isAdjacentTo(v):
insert a vertex storing element o
insertEdge(v, w, o):

 the vertex opposite of v on e

insertVertex(o):

 a list of the two endvertices of e

Update methods
list of all vertices in the graph
edges():

list of all edges in the graph
9
Data Structures for graphs

Graph Representation
Edge List
 Adjacency List
 Adjacency Matrix

© 2010 Goodrich, Tamassia
Graphs
10
Edge List Structure




element
reference to position in
vertex sequence



element
origin vertex object
destination vertex object
reference to position in edge
sequence
Vertex sequence

v
u
Unsorted
Space O(n)
b
d
w
z
z
w
v
a
Edge sequence

c
sequence of vertex objects



a
Edge object


u
Vertex object
b
c
d
sequence of edge objects


Unsorted
Space O(m)
© 2010 Goodrich, Tamassia
Graphs
11
Edge List Structure

Advantages


easy to implement
 e.endVertices()
Disadvantages

Have to examine the entire edge sequence O(m) for the
following operations
 v.incidentEdges()
 v.isAdjacentTo(w)
 eraseVertix(v)
© 2010 Goodrich, Tamassia
Graphs
12
How to Improve Edge List
Structure?

The problem with edge list structure
v.incidentEdges() takes O(m)

 Each vertex does not know which edges are incident on it

Solution: put p pointers (reference) from each vertex
to the edge object incident on this vertex
Each vertex can have lots of edges incident on it

 Thus we need a sequence of incident vertices
this sequence is called Adjacency List, because it’s usually
implemented as a list

u
a
v
c
b
w
© 2010 Goodrich, Tamassia
d
z
Graphs
13
Adjacency List Structure


Edge list structure
Incidence sequence for each
vertex I(u):

sequence of references to
edge objects of incident
edges
 Example: I(v) consists of
References to edges a and b

Augmented edge objects:

references to associated
positions in incidence
sequences of end vertices
 Example: Edge b has references
to associated positions in I(w)
and I(v)
© 2010 Goodrich, Tamassia
Graphs
14
Adjacency List Structure

Complexity

Space Complexity
 Vertex sequence: O(n)
 Edge sequence: O(m)
 Adjacency lists: O(Σv deg(v) ) = 2m = O(m)
 For vertex v, O(deg(v)) for the adjacency list of v.
 Thus total space is O(n + m)
© 2010 Goodrich, Tamassia
Graphs
15
Adjacency List Structure

Complexity

Time Complexity
© 2010 Goodrich, Tamassia
Graphs
16
Adjacency List Structure
Advantages:
 adjacency list structure provides improved running
times for the following functions:


Method v.incidentEdges() takes time proportional to the
number of incident vertices of v, that is, O(deg(v))
time.

Method v.isAdjacentTo(w) can be performed by inspecting
either the incidence collection of v or that of w.
 By choosing the smaller of the two, we get O(min(deg(v),deg(w)))
running time.


Method eraseVertex(v) takes O(deg(v)) time.
Disadvantages:

Implementation complexity
© 2010 Goodrich, Tamassia
Graphs
17
Adjacency Matrix Structure


Edge list structure
Augmented vertex objects (V)


2D-array adjacency array (A)



Integer key (index) associated
with vertex
two-dimensional n × n array A
A[i, j] holds a reference to the
edge (v,w), where v is the vertex
with index i and w is the vertex
with index j. if it exists, If there is
no such edge, then A[i, j] = null.
Cost adjacency matrix

Weighted graphs


Directed graphs
Undirected graphs
© 2010 Goodrich, Tamassia
Graphs
18
Adjacency Matrix Structure

Complexity:
Space: O(n2)
 Time:

© 2010 Goodrich, Tamassia
Graphs
19
Graph Data Structures: Performance
 n vertices, m edges
 no parallel edges
 no self-loops
Edge
List
Adjacency
List
Adjacency
Matrix
Space
n+m
n+m
n2
v.incidentEdges()
m
deg(v)
n
u.isAdjacentTo (v)
m
min(deg(v), deg(w))
1
insertVertex(o)
1
1
n2
insertEdge(v, w, o)
1
1
1
eraseVertex(v)
m
deg(v)
eraseEdge(e)
1
1
n2
1
© 2010 Goodrich, Tamassia
Graphs
20
Summary of Graph Data Structures

Edge list structure


To be used only out of laziness
Any lazy Student here ?



It’s a good starting point (as well as part of) for the other data structures
Adjacency list



Why did we study it?
Good performance for any graph
Most complicated to implement
2D adjacency array, to be used when:



Good choice only for dense graphs (m is O(n2), that is it has a lot of edges)
And used only in case when the set of vertices stays fixed (no vertex
insertion or deletion)
In cases when the above 2 conditions hold, adjacency array is the best
choice
© 2010 Goodrich, Tamassia
Graphs
21
Download