You are on page 1of 36

GRAPHS

1
Outline
2

 Undirected Graphs and Directed Graphs


 Depth-First Search
 Breadth-First Search
Graphs
3

 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
849 PVD
1843 ORD 2
SFO 14
802
7 43 LGA
337

1 8 7 10
2555 13
HNL 1233 9 9
LAX DFW 1120
MIA
4
Edge Types
 Directed edge
 ordered pair of vertices (u,v)
 first vertex u is the origin flight
ORD AA 1206 PVD
 second vertex v is the destination
 e.g., a flight
 Undirected edge
 unordered pair of vertices (u,v) 849
 e.g., a flight route ORD PVD
miles
 Directed graph
 all the edges are directed
 e.g., route network
 Undirected graph
 all the edges are undirected
 e.g., flight network
Applications
5

 Electronic circuits cslab1a cslab1b

 Printed circuit board math.brown.edu


 Integrated circuit
 Transportation networks cs.brown.edu
 Highway network
 Flight network brown.edu
 Computer networks qwest.net
att.net
 Local area network
 Internet
 Web
cox.net
 Databases John
 Entity-relationship diagram Paul
David
6
Terminology (1/3)

 End vertices (or endpoints) of an


edge
 U and V are the endpoints of a V
 Edges incident on a vertex a b
h j
 a, d, and b are incident on V
 Adjacent vertices U d X Z
 U and V are adjacent c e i
 Degree of a vertex
W g
 X has degree 5
 Parallel edges f
 h and i are parallel edges Y
 Self-loop
 j is a self-loop
Terminology (2/3)
7

 Path
 sequence of alternating vertices and
edges
 begins with a vertex V
ends with a vertex
a b

P1
 each edge is preceded and followed d
by its endpoints U X Z
 Simple path P2 h
 path such that all its vertices and
c e
edges are distinct W g
 Examples
 P1=(V,b,X,h,Z) is a simple path f
 P2=(U,c,W,e,X,g,Y,f,W,d,V) is a Y
path that is not simple
Terminology (3/3)
8

 Cycle
 circular sequence of alternating
vertices and edges
V
 each edge is preceded and followed a b
by its endpoints
 Simple cycle U d X Z
 cycle such that all its vertices and C2 h
edges are distinct e C1
c
 Examples W g
 C1=(V,b,X,g,Y,f,W,c,U,a,) is a
simple cycle f
 C2=(U,c,W,e,X,g,Y,f,W,d,V,a,) is Y
a cycle that is not simple
Properties
9

Property 1 Notation
v deg(v) 2m n number of vertices
Proof: each edge is counted m number of edges
twice deg(v) degree of vertex v
Property 2
In an undirected graph with no Example
self-loops and no multiple
 n 4
edges
m n (n  1)2  m 6

Proof: each vertex has degree  deg(v) 3


at most (n  1)

What is the bound for a


directed graph?
Main Methods of the Graph ADT
10

 Update methods
 Vertices and edges  insertVertex(o): insert a vertex
 are positions storing element o
 store elements  insertEdge(v, w, o): insert an
edge (v,w) storing element o
 Accessor methods  removeVertex(v): remove
 endVertices(e): an array of the vertex v (and its incident edges)
two endvertices of e
 removeEdge(e): remove edge e
 opposite(v, e): the vertex
opposite of v on e  Iterator methods
 areAdjacent(v, w): true iff v and  incidentEdges(v): edges
w are adjacent incident to v
 replace(v, x): replace element at  vertices(): all vertices in the
vertex v with x graph
 replace(e, x): replace element at  edges(): all edges in the graph
edge e with x
Edge List Structure
11

 Vertex object u
 element a c
 reference to position in b d
vertex sequence v w z
 Edge object
 element
 origin vertex object
 destination vertex object
u v w z
 reference to position in edge
sequence
 Vertex sequence
 sequence of vertex objects a b c d
 Edge sequence
 sequence of edge objects
Adjacency List Structure
12

 Edge list structure a v b


 Incidence sequence for u w
each vertex
 sequence of
references to edge
objects of incident
edges u v w
 Augmented edge
objects
 references to
associated positions in
incidence sequences
of end vertices
a b
Adjacency Matrix Structure
13

 Edge list structure a v b


 Augmented vertex objects u w
 Integer key (index)
associated with vertex
 2D-array adjacency array
 Reference to edge object 0 u 1 v 2 w
for adjacent vertices
 Null for non nonadjacent
vertices 0 1 2
 The “old fashioned” 0  
version just has 0 for no
1 
edge and 1 for edge
a 2   b
DEPTH-FIRST
SEARCH
A

B D E

14
Subgraphs
15

 A subgraph S of a graph G
is a graph such that
 The vertices of S are a subset
of the vertices of G
 The edges of S are a subset of Subgraph
the edges of G
 A spanning subgraph of G is
a subgraph that contains all
the vertices of G

Spanning subgraph
Connectivity
16

 A graph is connected
if there is a path
between every pair of
vertices
 A connected Connected graph
component of a graph
G is a maximal
connected subgraph of
G

Non connected graph with two


connected components
17
Trees and Forests
 A (free) tree is an
undirected graph T such
that
 T is connected
 T has no cycles Tree
This definition of tree is
different from the one of a
rooted tree
 A forest is an undirected
graph without cycles
 The connected components
of a forest are trees
Forest
Spanning Trees and Forests
18

 A spanning tree of a
connected graph is a spanning
subgraph that is a tree
 A spanning tree is not unique
unless the graph is a tree
 Spanning trees have Graph
applications to the design of
communication networks
 A spanning forest of a graph
is a spanning subgraph that is
a forest

Spanning tree
19
Depth-First Search
 Depth-first search (DFS)  DFS on a graph with n
is a general technique for vertices and m edges takes
traversing a graph O(nm ) time
 A DFS traversal of a graph  DFS can be further
G extended to solve other
 Visits all the vertices and graph problems
edges of G  Find and report a path
 Determines whether G is between two given vertices
connected  Find a cycle in the graph
 Computes the connected  Depth-first search is to
components of G
graphs what Euler tour is
 Computes a spanning forest
of G to binary trees
DFS Algorithm
20

 The algorithm uses a mechanism for


setting and getting “labels” of Algorithm DFS(G, v)
vertices and edges Input graph G and a start vertex v of G
Output labeling of the edges of G
Algorithm DFS(G) in the connected component of v
Input graph G as discovery edges and back edges
Output labeling of the edges of G { setLabel(v, VISITED);
as discovery edges and for all e  G.incidentEdges(v)
back edges
if ( getLabel(e) UNEXPLORED )
{ for all u  G.vertices()
{ w = opposite(v, e);
setLabel(u, UNEXPLORED);
if ( getLabel(w) UNEXPLORED )
for all e  G.edges()
{ setLabel(e, DISCOVERY);
setLabel(e, UNEXPLORED);
DFS(G, w);
for all v  G.vertices()
}
if ( getLabel(v) UNEXPLORED )
else
DFS(G, v);
setLabel(e, BACK);
}
}
}
Example (1/2)
21

A
A unexplored vertex
A visited vertex
B D E
unexplored edge
discovery edge C
back edge

A A

B D E B D E

C C
Example (2/2)
22

A A

B D E B D E

C C

A A

B D E B D E

C C
Properties of DFS
23

Property 1
DFS(G, v) visits all the
vertices and edges in the A
connected component of v
Property 2 B D E
The discovery edges
labeled by DFS(G, v)
form a spanning tree of the C
connected component of v
24
Analysis of DFS

 Setting/getting a vertex/edge label takes O(1) time


 Each vertex is labeled twice
 once as UNEXPLORED
 once as VISITED
 Each edge is labeled twice
 once as UNEXPLORED
 once as DISCOVERY or BACK
 Method incidentEdges is called once for each vertex
 DFS runs in O(n  m) time provided the graph is
represented by the adjacency list structure

Recall that v deg(v) 2m
25
Path Finding
 We can specialize the DFS Algorithm pathDFS(G, v, z)
algorithm to find a path { setLabel(v, VISITED);
between two given vertices v S.push(v);
and z using the template if ( v z )
return S.elements();
method pattern
for all e  G.incidentEdges(v)
 We call DFS(G, v) with v as if ( getLabel(e) UNEXPLORED )
the start vertex { w = opposite(v,e);
if ( getLabel(w)
 We use a stack S to keep UNEXPLORED )
track of the path between the { setLabel(e, DISCOVERY);
start vertex and the current S.push(e);
vertex pathDFS(G, w, z);
S.pop(e);
 As soon as destination vertex }
z is encountered, we return else
the path as the contents of setLabel(e, BACK);
the stack }
S.pop(v);
BREADTH-FIRST
SEARCH L 0
A

L1
B C D

L2
E F

26
Breadth-First Search
27

 Breadth-first search (BFS)  BFS on a graph with n


is a general technique for vertices and m edges takes
traversing a graph O(nm ) time
 A BFS traversal of a graph  BFS can be further
G
extended to solve other
 Visits all the vertices and
edges of G graph problems
 Determines whether G is  Find and report a path with
connected the minimum number of
 Computes the connected edges between two given
components of G vertices
 Computes a spanning forest  Find a simple cycle, if there
of G is one
BFS Algorithm
28

 The algorithm uses a Algorithm BFS(G, s)


mechanism for setting and { L0 = new empty sequence;
getting “labels” of vertices and L0.insertLast(s);
edges setLabel(s, VISITED);
i = 0;
Algorithm BFS(G) while ( Li.isEmpty() )
Input graph G { Li 1 = new empty sequence;
Output labeling of the edges for all v  Li.elements()
and partition of the for all e  G.incidentEdges(v)
vertices of G if ( getLabel(e) UNEXPLORED )
{ { w = opposite(v,e);
for all u  G.vertices() if ( getLabel(w)
setLabel(u, UNEXPLORED); UNEXPLORED )
for all e  G.edges() { setLabel(e, DISCOVERY);
setLabel(w, VISITED);
setLabel(e, UNEXPLORED);
Li 1.insertLast(w); }
for all v  G.vertices() else
if ( getLabel(v) UNEXPLORED ) setLabel(e, CROSS);
BFS(G, v); }
} i = i 1;
}
Example (1/3)
29
L0
A
A unexplored vertex
A visited vertex L1
B C D
unexplored edge
discovery edge E F
cross edge

L0 L0
A A

L1 L1
B C D B C D

E F E F
Example (2/3)
30

L0 L0
A A

L1 L1
B C D B C D

L2
E F E F

L0 L0
A A

L1 L1
B C D B C D

L2 L2
E F E F
Example (3/3)
31

L0 L0
A A

L1 L1
B C D B C D

L2 L2
E F E F

L0
A

L1
B C D

L2
E F
Properties
32

Notation A
Gs: connected component of s
Property 1 B C D
BFS(G, s) visits all the vertices and
edges of Gs E F
Property 2
The discovery edges labeled by
BFS(G, s) form a spanning tree Ts of L0
Gs A
Property 3 L1
For each vertex v in Li B C D
 The path of Ts from s to v has i edges L2
 Every path from s to v in Gs has at least i E F
edges
Analysis
33

 Setting/getting a vertex/edge label takes O(1) time


 Each vertex is labeled twice
 once as UNEXPLORED
 once as VISITED
 Each edge is labeled twice
 once as UNEXPLORED
 once as DISCOVERY or CROSS
 Each vertex is inserted once into a sequence Li
 Method incidentEdges is called once for each vertex
 BFS runs in O(n  m) time provided the graph is represented by
the adjacency list structure

Recall that v deg(v) 2m
34
DFS vs. BFS (1/2)

Applications DFS BFS


Spanning forest, connected
 
components, paths, cycles
Shortest paths 

Biconnected components 

L0
A A

L1
B C D B C D

L2
E F E F

DFS BFS
DFS vs. BFS (2/2)
35

Back edge (v,w) Cross edge (v,w)


 w is an ancestor of v in the  w is in the same level as v or
tree of discovery edges in the next level in the tree
of discovery edges

L0
A A

L1
B C D B C D

L2
E F E F

DFS BFS
36
References

1. Chapter 13, Data Structures and Algorithms by


Goodrich and Tamassia.

You might also like