Professional Documents
Culture Documents
Raphs: - Definitions - The Graph ADT - Data Structures For Graphs
Raphs: - Definitions - The Graph ADT - Data Structures For Graphs
Definitions
The Graph ADT
Data structures for graphs
PVD
LAX
HNL
STL
DFW
FTL
Graphs
What is a Graph?
A graph G = (V,E) is composed of:
V: set of vertices
E: set of edges connecting the vertices in V
An edge e = (u,v) is a pair of vertices
Example:
V= {a,b,c,d,e}
a
b
c
Graphs
E=
{(a,b),(a,c),(a,d),
(b,e),(c,d),(c,e),
(d,e)}
Applications
electronic circuits
CS16
start
find the path of least resistance to CS16
networks (roads, flights, communications)
PVD
LAX
HNL
STL
DFW
FTL
Graphs
mo better examples
A Spike Lee Joint Production
wake up
eat
cs16 meditation
work
more cs16
play
cs16 program
cxhextris
make cookies
for cs16 HTA
sleep
dream of cs16
Graphs
Graph Terminology
adjacent vertices: connected by an edge
degree (of a vertex): # of adjacent vertices
vV
3
3
abedc
Graphs
c
e
d
bedc
b
bec
c
e
b
acda
c
d
Graphs
connected
not connected
Graphs
Caramba! Another
Terminology Slide!
(free) tree - connected graph without cycles
forest - collection of trees
tree
tree
forest
tree
tree
Graphs
Connectivity
Let n = #vertices
m = #edges
- complete graph - all pairs of vertices are adjacent
vV
n=5
m = (5 4)/2 = 10
Graphs
More Connectivity
n = #vertices
m = #edges
For a tree m = n - 1
n=5
m=4
Graphs
10
Spanning Tree
A spanning tree of G is a subgraph which
- is a tree
- contains all vertices of G
spanning tree of G
Graphs
11
TA
TA
But Plant-Ops
accidentally cuts
a phone cable!!!
TA
TA
TA
Graphs
12
Gilligans Isle?
Pregal River
A
B
Graphs
13
C
A
D
B
14
isEmpty()
elements()
positions()
swap()
replaceElement()
Graphs
15
16
17
Update Methods
- makeUndirected(e)
Set e to be an undirected edge.
- reverseDirection(e)
Switch the origin and destination
vertices of e.
- setDirectionFrom(e, v)
Sets the direction of e away from v, one
of its end vertices.
- setDirectionTo(e, v)
Sets the direction of e toward v, one of
its end vertices.
- insertEdge(v, w, o)
Insert and return an undirected edge
between v and w, storing o at this
position.
- insertDirectedEdge(v, w, o)
Insert and return a directed edge
between v and w, storing o at this
position.
- insertVertex(o)
Insert and return a new (isolated)
vertex storing o at this position.
- removeEdge(e)
Remove edge e.
Graphs
18
35
BOS
LAX
AA 49
NW
AA
DFW
AA
AA 411
523
247
8
13
DL
35
JFK
AA 903
A
U
DL 3
SFO
0
2
1
UA 8
77
ORD
MIA
Graphs
19
Edge List
The edge list structure simply stores the vertices and
the edges into unsorted sequences.
Easy to implement.
Finding the edges incident on a given vertex is
inefficient since it requires examining the entire
edge sequence
E
NW 35 DL 247
BOS
AA 49
LAX
DFW
JFK
MIA
ORD
SFO
Graphs
20
Graphs
Time
O(1)
O(1)
O(n)
O(m)
O(n+m)
O(1)
O(m)
O(1)
O(m)
21
Adjacency List
(traditional)
adjacency list of a vertex v:
sequence of vertices adjacent to v
represent the graph by the adjacency lists of all the
vertices
a
b
c
e
d
a
Space = (N +
Graphs
deg(v)) = (N + M)
22
Adjacency List
(modern)
The adjacency list structure extends the edge list
structure by adding incidence containers to each
vertex.
NW 35 DL 247
in
BOS
out
AA 49
LAX
in
out
DFW
in
out
JFK
in
out
MIA
in
out
ORD
in
out
NW 35
AA 49 UA 120
AA1387 DL335
NW 35 AA1387
DL 247 AA523
UA 120 UA 877
DL 247
AA 411
UA 877 AA 49
AA 903
AA 903 AA 411
DL 335
AA 523
TW 45
SFO
in
out
TW 45
23
Time
O(1)
O(1)
O(n)
O(m)
O(n+m)
O(1)
O(deg(v))
O(min(deg(u),
deg(v)))
insertVertex, insertEdge, insertDirected- O(1)
Edge, removeEdge, makeUndirected,
reverseDirection,
removeVertex(v)
O(deg(v))
Graphs
24
Adjacency Matrix
(traditional)
a
b
a
b
c
d
e
c
d
F
T
T
T
F
T
F
F
F
T
T
F
F
T
T
T
F
T
F
T
F
T
T
T
F
Graphs
25
Adjacency Matrix
(modern)
The adjacency matrix structures augments the edge
list structure with a matrix where each row and
column corresponds to a vertex.
0
NW
35
DL
247
AA
49
DL
335
AA
1387
AA
903
TW
45
UA
120
AA
523
AA
411
UA
877
26
Graphs
Time
O(1)
O(1)
O(n)
O(m)
O(n+m)
O(1)
O(n)
O(1)
O(1)
O(n2)
27