337
LAX
Graphs
3
4
17
1233
ORD
802
SFO
1843
DFW
Graphs
A graph is a pair (V, E), where
Example:
SFO
337
HNL
2555
LAX
1843
43
7
1
1233
ORD
802
DFW
Graphs
849
2
14
PVD
LGA
87
3
1
1120
10
9
MIA
2
Edge Types
Directed edge
Undirected edge
flight
ORD AA 1206 PVD
ORD
849
miles
PVD
Directed graph
Undirected graph
Graphs
Applications
cslab1a
Electronic circuits
cs.brown.edu
Highway network
Flight network
brown.edu
qwest.net
Computer networks
math.brown.edu
Transportation networks
cslab1b
Databases
att.net
cox.net
John
Paul
Entity-relationship diagram
Graphs
David
Terminology
X has degree 5
j
Z
i
g
h
X
Self-loop
Parallel edges
Degree of a vertex
Adjacent vertices
j is a self-loop
Graphs
Terminology (cont.)
Path
Simple path
sequence of alternating
vertices and edges
begins with a vertex
ends with a vertex
each edge is preceded and
followed by its endpoints
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
Importance of distinction ?
Graphs
a
U
c
d
P2
P1
X
g
f
Terminology (cont.)
Cycle
a
U
c
d
C2
X
e
Graphs
C1
g
W
f
Terminology (cont.)
Simple cycle
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
Graphs
a
U
c
d
C2
X
e
C1
g
W
f
Terminology (cont.)
Graphs
a
U
c
d
C2
X
e
C1
g
W
f
Terminology (cont.)
a
U
c
d
C2
X
e
Graphs
C1
g
W
f
10
Digraphs
A digraph is a
graph whose
edges are all
directed
E
D
Applications
one-way streets
flights
task scheduling
Directed Graphs
11
Graph Connectivity
An undirected graph is said to be connected
if there is a path between every pair of
nodes. Otherwise, the graph is disconnected
Informally, an undirected graph is
connected if it hangs in one piece
Connected
Disconnected
CS 103
12
Connectivity
Q: Which of the following graphs are
connected?
1
2
3
L24
13
Connectivity
A: First and second are disconnected.
Last is connected.
1
2
3
L24
14
Connectivity
Graphs
15
Connectivity in Directed
Graphs (I)
Definition: A directed graph is said to be
strongly connected
if for any pair of nodes there is a path
from each one to the other
A
16
Connectivity in Directed
Graphs (II)
Definition: A directed graph is said to be
weakly connected if the underlying
undirected graph is connected
A
Each unilaterally
connected graph is also
weakly connected
There is no path
between B and D
17
Directed Graphs
Graphs
18
Properties
Notation
Property 1
v deg(v) 2m
n
m
deg(v)
number of vertices
number of edges
degree of vertex v
Property 2
In an undirected graph
with no self-loops and
no multiple edges
m n (n 1)2
Proof: each vertex has
degree at most (n 1)
Example
n 4
m 6
deg(v) 3
Graphs
19
Digraph Properties
D
C
If G is simple, m n(n 1)
If we keep in-edges and out-edges in
separate adjacency lists, we can perform
listing of incoming edges and outgoing
edges in time proportional to their size
Directed Graphs
20
are positions
store elements
Accessor methods
e.endVertices(): a list of
the two endvertices of e
e.opposite(v): the vertex
opposite of v on e
u.isAdjacentTo(v): true iff
u and v are adjacent
*v: reference to element
associated with vertex v
*e: reference to element
associated with edge e
Graphs
insertVertex(o): insert a
vertex storing element o
insertEdge(v, w, o): insert
an edge (v,w) storing
element o
eraseVertex(v): remove
vertex v (and its incident
edges)
eraseEdge(e): remove
edge e
incidentEdges(v): list of
edges incident to v
vertices(): list of all
vertices in the graph
edges(): list of all edges in
the graph
21
Graph Representation
For graphs to be computationally useful,
they have to be conveniently represented in
programs
There are two computer representations of
graphs:
Adjacency matrix representation
Adjacency lists representation
CS 103
22
CS 103
23
2
4
3
25
1
0
0
0
0
0
1
1
0
0
1
0
0
0
1
0
0
0
0
0
2
4
3
26
Helen 1
Joe 3
John 2
Tom 4
Paul 5
27
0
0
1
1
1
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
1
0
0
Mary 0
0
0
1
John 2
1
0
Tom 4
0
CS 103
Helen 1
Joe 3
Paul 5
28
Representing Graphs
Definition: Let G = (V, E) be an undirected graph
with |V| = n. Suppose that the vertices and edges
of G are listed in arbitrary order as v1, v2, , vn and
e1, e2, , em, respectively.
The incidence matrix of G with respect to this
listing of the vertices and edges is the nm zeroone matrix with 1 as its (i, j) entry when edge ej is
incident with vi, and 0 otherwise.
In other words, for an incidence matrix M = [mij],
mij = 1
mij = 0
Representing Graphs
Example: What is the incidence
matrix M for the following
graph G based on the order of
vertices a, b, c, d and edges 1, 2,
3, 4, 5, 6?
1 1 0 0 1 0
Solution:
1 0 1 0 0 0
M
0 0 0 1 1 1
0 1 1 1 0 0
a
2
d
1
3
4
b
5
c
31
32
33
Adjacency Matrices
Can you determine if it is a directed graph ?
0
0
A=
0
1
0
1
0
0
0
0
0
1
1
0
0
1
0
0
0
1
0
0
0
0
0
34
Cons:
No matter how few edges the graph has, the
matrix takes O(n2) in memory
35
36
Mary 0
Helen 1
Joe
3
John 2
Tom 4
Paul 5
38
Cons:
It can take up to O(n) time to determine if a pair
of nodes (i,j) is an edge: one would have to
search the linked list L[i], which takes time
proportional to the length of L[i].
39
3
7
4
6
40
2
1
2
3
4
5
6
7
2
3
2
3
6
1
4
41
5
1
4
7
1
5
5
6
7
5
7
2
3
7
4
42
43
0
1
A
1
1
0
0
1
1
0
0
1
0
1
1
EXERCISE 3.3
EXERCISE
b
c
a
g
f
e
EXERCISE
EXERCISE
c
d
EXERCISE
49