Sei sulla pagina 1di 29

Graphs

Graphs

ORD

SFO
Graphs

LAX

DFW

2
Graphs

Outline and Reading


Graphs Definition

Applications
Terminology
Properties
ADT

Data structures for graphs


Edge list structure
Adjacency list structure
Adjacency matrix structure

Graphs

3
Graphs

Graph
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

SFO

PVD

ORD
LGA

HNL

LAX

DFW

MIA

Edge Types

Graphs

(u,v)
Directed edge

ordered pair of vertices (u,v)


first vertex u is the origin
second vertex v is the destination
e.g., a flight

u
ORD

flight
AA 1206 DFW
802 miles

ORD

flight
DFW
route
802 miles

Undirected edge

unordered pair of vertices (u,v)


e.g., a flight route

Weighted edge
Directed graph (Digraph)
all the edges are directed
e.g., route network

Undirected graph

all the edges are undirected


e.g., flight network

Weighted graph

all the edges are weighted

Graphs

5
Graphs

Applications
cslab1a

cslab1b

Electronic circuits

math.brown.edu

Printed circuit board


Integrated circuit

Transportation networks

cs.brown.edu

Highway network
Flight network

Computer networks

brown.edu
qwest.net
att.net

Local area network


Internet
Web

cox.net
John

Databases
Entity-relationship diagram

Paul

David

6
Graphs

Terminology
End vertices (or endpoints) of
an edge
U and V are the endpoints of a

Edges incident on a vertex

a, d, and b are incident on V

Adjacent vertices
U and V are adjacent

Degree of a vertex
X has degree 5

Parallel edges
h and i are parallel edges

Self-loop
j is a self-loop

e
W

j
Z

g
f

Graphs

7
Graphs

Terminology (cont.)
Outgoing edges of a
vertex

h and b are the outgoing


edges of X

Incoming edges of a
vertex

X
e

e, g, and i are incoming


edges of X

In-degree of a vertex
X has in-degree 3

g
f

Out-degree of a vertex

X has out-degree 2

8
Graphs

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
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

a
U
c

d
P2

P1
X

g
f

Graphs

9
Graphs

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

Examples

d
C2

U
c

X
e
f

C1
g

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

10
Graphs

Exercise on Terminology

# of vertices?
# of edges?
What type of the graph is it? ANS: undirected weighted graph
Show the end vertices of the edge with largest weight
Show the vertices of smallest degree and largest degree
Show the edges incident to the vertices in the above question
Enumerate pairs of adjacent vertices
Identify the shortest simple path from HNL to PVD
Identify the simple cycle with the most edges

SFO

PVD

ORD
LGA

HNL

LAX

DFW

MIA

Graphs

11
Graphs

Properties of Undirected Graphs


Property 1 Total degree

Notation

Sv deg(v) = ?
Property 2 Total number
of edges

n
m
deg(v)

In an undirected graph with


no self-loops and no
multiple edges
m Upper bound?
Lower bound? m

number of vertices
number of edges
degree of vertex v

Example
n = ?
m = ?
deg(v) = ?
A graph with given number of
vertices (4) and maximum
number of edges

12
Graphs

Properties of Undirected Graphs


Property 1 Total degree

Notation

Sv deg(v) = 2m

n
m
deg(v)

Proof: each edge is counted


twice

Property 2 Total number


of edges
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)

number of vertices
number of edges
degree of vertex v

Example
n = 4
m = 6
deg(v) = 3

A graph with given number of


vertices (4) and maximum
number of edges

Graphs

13
Graphs

Properties of Directed Graphs


Property 1 Total indegree and out-degree

Notation
n
m
deg(v)

Sv in-deg(v) = ?
Sv out-deg(v) = ?
Property 2 Total number
of edges

number of vertices
number of edges
degree of vertex v

Example
n = ?
m = ?
deg(v) = ?

In an directed graph with no


self-loops and no multiple
edges
m Upper bound?
Lower bound? m

A graph with given number of


vertices (4) and maximum
number of edges

14
Graphs

Properties of Directed Graphs


Property 1 Total indegree and out-degree

Sv in-deg(v) = m
Sv out-deg(v) = m
Property 2 Total
number of edges
In an directed graph with
no self-loops and no
multiple edges
m n (n - 1)

Notation
n
m
deg(v)

number of vertices
number of edges
degree of vertex v

Example
n = 4
m = 12
deg(v) = 6
A graph with given number of
vertices (4) and maximum
number of edges

Graphs

15

Main Methods of the Graph ADT


Graphs

Vertices and edges

Update methods

are positions
store elements

Accessor methods

aVertex()
incidentEdges(v)
adjacentVertices(v)
degree(v)
endVertices(e)
opposite(v, e)
areAdjacent(v, w)

isDirected(e)
origin(e)
destination(e)

insertVertex(o)
insertEdge(v, w, o)
insertDirectedEdge(v, w, o)
removeVertex(v)
removeEdge(e)

numVertices()
numEdges()
vertices()
edges()

Generic methods

Specific to
directed
edges

16
Graphs

Exercise on ADT

aVertex()
incidentEdges(ORD)
adjacentVertices(ORD)
degree(ORD)
endVertices((LGA,MIA))
opposite(DFW, (DFW,LGA))
areAdjacent(DFW, SFO)

insertVertex(IAH)
insertEdge(MIA, PVD, 1200)
removeVertex(ORD)
removeEdge((DFW,ORD))

isDirected((DFW,LGA))
origin ((DFW,LGA))
destination((DFW,LGA)))

SFO

PVD

ORD
LGA

HNL

LAX

DFW

MIA

Graphs

17
Graphs

Edge List Structure


Edge List

Vertex Sequence

(ORD, PVD) 849

ORD

(ORD, DFW) 802

LGA

(LGA, PVD)

142

PVD

(LGA, MIA) 1099

DFW

(DFW, LGA) 1387

MIA

An edge list can be


stored in a sequence,
a vector, a list or a
dictionary such as a
hash table
PVD

ORD
LGA
DFW

MIA

(DFW, MIA) 1120

18

Asymptotic Performance
Graphs

Vertices and edges


are positions
store elements

Accessor methods
Accessing vertex sequence
aVertex() O(1)
degree(v) O(1)

Accessing edge list

Generic methods

numVertices() O(1)
numEdges() O(1)
vertices() O(n)
edges() O(m)
Edge List

Vertex Sequence

Weight Directed

endVertices(e) O(1)
opposite(v, e) O(1)

849

False

ORD

(ORD, DFW) 802

False

LGA

isDirected(e) O(1)

(LGA, PVD)

142

False

PVD

(LGA, MIA)

1099 False

DFW

(DFW, LGA) 1387 False

MIA

Specific to origin(e) O(1)


directed destination(e) O(1)
edges

(ORD, PVD)

Degree

(DFW, MIA) 1120 False

Graphs

Asymptotic Performance of
Edge List Structure
n vertices, m edges
no parallel edges
no self-loops
Bounds are big-Oh

Edge
List

Space

n+m

incidentEdges(v)
adjacentVertices(v)
areAdjacent (v, w)
insertVertex(o)

insertEdge(v, w, o)

removeVertex(v)

m
1

Vertex Sequence

Weight Directed

(ORD, PVD)

m
m
1

removeEdge(e)

Edge List

Degree

849

False

ORD

(ORD, DFW) 802

False

LGA

(LGA, PVD)

142

False

PVD

(LGA, MIA)

1099 False

DFW

(DFW, LGA) 1387 False

MIA

(DFW, MIA) 1120 False

Graphs

19

20

Edge List Structure

Graphs

Vertex object
a

element
reference to position in
vertex sequence

Edge object

element
origin vertex object
destination vertex object
reference to position in
edge sequence

c
b

Vertex sequence
sequence of vertex objects

Edge sequence

sequence of edge objects

10

Graphs

21
PVD

ORD

Adjacency List Structure


Graphs

Edge List

LGA
DFW

Adjacency List

MIA

849 (ORD, PVD) ORD (ORD, PVD) (ORD, DFW)


802 (ORD, DFW) LGA (LGA, PVD)
142 (LGA, PVD)

(LGA, MIA) (LGA, DFW)

PVD (PVD, ORD) (PVD, LGA)

1099 (LGA, MIA) DFW (DFW, ORD) (DFW, LGA) (DFW, MIA)
1387 (DFW, LGA) MIA

(MIA, LGA) (MIA, DFW)

1120 (DFW, MIA)

Asymptotic Performance of
Adjacency List Structure
n vertices, m edges
no parallel edges
no self-loops
Bounds are big-Oh

Space
incidentEdges(v)
adjacentVertices(v)
areAdjacent (v, w)
insertVertex(o)
insertEdge(v, w, o)

removeVertex(v)
removeEdge(e)

Adjacency
List

Edge List
849 (ORD, PVD)
802 (ORD, DFW)

n+m

142 (LGA, PVD)


1099 (LGA, MIA)

deg(v)

1387 (DFW, LGA)

min(deg(v), deg(w))
1
ORD
1
deg(v)*
1*

1120 (DFW, MIA)


Adjacency List
(ORD, PVD) (ORD, DFW)

LGA (LGA, PVD) (LGA, MIA) (LGA, DFW)


PVD (PVD, ORD) (PVD, LGA)
DFW (DFW, ORD) (DFW, LGA) (DFW, MIA)
(MIA, LGA) (MIA, DFW)
MIA Graphs

22

11

Graphs

23

Adjacency List Structure


Graphs

Edge list structure


Incidence sequence
for each vertex

sequence of
references to edge
objects of incident
edges

Augmented edge
objects
references to
associated positions
in incidence
sequences of end
vertices

2:PVD24

0:ORD
1:LGA

Graphs

Adjacency Matrix
Structure
Edge List

3:DFW

Vertex Sequence

4:MIA

849 (ORD, PVD)

0 ORD

802 (ORD, DFW)

LGA

142 (LGA, PVD)

PVD

1099 (LGA, MIA)

3 DFW

1387 (DFW, LGA)

MIA

1120 (DFW, MIA)

12

Graphs

Asymptotic Performance of
Adjacency Matrix Structure
n vertices, m edges
no parallel edges
no self-loops
Bounds are big-Oh
Space

802 (ORD, DFW)


142 (LGA, PVD)

Adjacency
Matrix

1099 (LGA, MIA)


1387 (DFW, LGA)
1120 (DFW, MIA)

n2

incidentEdges(v)
adjacentVertices(v)
areAdjacent (v, w)

Edge List
849 (ORD, PVD)

Adjacency Matrix
0 1 2 3 4

Vertex
Sequence

0 ORD 0

1 LGA

insertVertex(o)

1
n2

insertEdge(v, w, o)

2 PVD

removeVertex(v)

n2
1

3 DFW 3

removeEdge(e)

4 MIA
Graphs

25

26

Adjacency Matrix Structure


Graphs

Edge list structure


Augmented vertex
objects

Integer key (index)


associated with vertex

2D-array adjacency
array

Reference to edge
object for adjacent
vertices
Null for non
nonadjacent vertices

The old fashioned


version just has 0 for
no edge and 1 for edge

0
0
2

1
a

13

Graphs

Asymptotic Performance
n vertices, m edges
no parallel edges
no self-loops
Bounds are big-Oh

Edge
List

Adjacency
List

Adjacency
Matrix

n+m

n+m

n2

deg(v)

insertVertex(o)

m
1

min(deg(v), deg(w))
1

1
n2

insertEdge(v, w, o)

removeVertex(v)

m
1

deg(v)
1

n2
1

Space
incidentEdges(v)
adjacentVertices(v)
areAdjacent (v, w)

removeEdge(e)

Graphs

27

28

Depth-First Search
A
B

Graphs

14

Graphs

29
Graphs

Outline and Reading


Definitions (12.1)
Subgraph
Connectivity
Spanning trees and forests

Depth-first search (12.3.1)

Algorithm
Example
Properties
Analysis

Applications of DFS
Path finding
Cycle finding

30
Graphs

Subgraphs
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 the edges of G

Subgraph

A spanning subgraph of
G is a subgraph that
contains all the vertices
of G
Spanning subgraph

15

Graphs

31
Graphs

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

Connected graph

Non connected graph with two


connected components

32
Graphs

Trees and Forests


A (free) tree is an
undirected graph T such
that
T is connected
T has no cycles
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

Tree

Forest

16

Graphs

33
Graphs

Spanning Trees and Forests


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
applications to the design
of communication
networks
A spanning forest of a
graph is a spanning
subgraph that is a forest

Graph

Spanning tree

34
Graphs

Depth-First Search
Depth-first search (DFS)
is a general technique
for traversing a graph
A DFS traversal of a
graph G
Visits all the vertices and
edges of G
Determines whether G is
connected
Computes the connected
components of G
Computes a spanning
forest of G

DFS on a graph with n


vertices and m edges
takes O(n + m ) time
DFS can be further
extended to solve other
graph problems
Find and report a path
between two given
vertices
Find a cycle in the graph

Depth-first search is to
graphs what Euler tour
is to binary trees

17

Graphs

35
Graphs

Example

A(B) = {A, C, F}
A(B) = {A, C, F}

unexplored vertex
visited vertex
unexplored edge
discovery edge
back edge

A
A

B
F

D
C

A(A) = {B, C, D, E}

A(C) = {A, B, D, E}

E
G

A(C) = {A, B, D, E}
A(C) = {A, B, D, E}

36

Example (cont.)
A(C) = {A, B, D, E}

A
B
F

A
B
F

A(D) = {A, C}
D

Graphs

A(D) = {A, C}
A(D) = {A, C}

A(E) = {A, C}
D

E
G

A(C) = {A, B, D, E}

E
G

A(E) = {A, C}
A(E) = {A, C}

18

Graphs

37
Graphs

Example (cont.)
A

A(C) = {A, B, D, E}
A(B) = {A, C, F}

A
B
F

D
C

E
G

A(F) = {B}
D

A(G) =

E
G

A(B) = {A, C, F}
A(A) = {A, B, C, D}

38
Graphs

DFS and Maze Traversal


The DFS algorithm is
similar to a classic
strategy for exploring a
maze
We mark each
intersection, corner and
dead end (vertex) visited
We mark each corridor
(edge ) traversed
We keep track of the path
back to the entrance (start
vertex) by means of a
rope (recursion stack)

19

Graphs

39
Graphs

DFS Algorithm
The algorithm uses a mechanism
for setting and getting labels of
vertices and edges
Algorithm DFS(G)
Input graph G
Output labeling of the edges of G
as discovery edges and
back edges
for all u G.vertices()
setLabel(u, UNEXPLORED)
for all e G.edges()
setLabel(e, UNEXPLORED)
for all v G.vertices()
if getLabel(v) = UNEXPLORED
DFS(G, v)

Algorithm DFS(G, v)
Input graph G and a start vertex v of G
Output labeling of the edges of G
in the connected component of v
as discovery edges and back edges
setLabel(v, VISITED)
for all e G.incidentEdges(v)
if getLabel(e) = UNEXPLORED
w opposite(v,e)
if getLabel(w) = UNEXPLORED
setLabel(e, DISCOVERY)
DFS(G, w)
else
setLabel(e, BACK)

40
Graphs

Exercise on DFS Algorithm


A
B

C
E

D
F

20

Graphs

41
Graphs

Properties of DFS
Property 1

DFS(G, v) visits all the


vertices and edges in
the connected
component of v

v1
A

Property 2

The discovery edges


labeled by DFS(G, v)
form a spanning tree of
the connected
component of v

v2

42

Analysis of DFS

Graphs

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

D
C

E
G

Function DFS(G, v) and the method incidentEdges


are called once for each vertex

21

Graphs

43

Analysis of DFS
DFS runs in O(n + m) time
provided the graph is
represented by the adjacency
list structure
Recall that v deg(v) = 2m

Algorithm DFS(G)
Input graph G
Output labeling of the edges of G
as discovery edges and
back edges
for all u G.vertices()
O(n)
setLabel(u, UNEXPLORED)
for all e G.edges()
O(m)
setLabel(e, UNEXPLORED)
for all v G.vertices() O(n +m)
if getLabel(v) = UNEXPLORED
DFS(G, v)

Graphs

Algorithm DFS(G, v)
Input graph G and a start vertex v of G
Output labeling of the edges of G
in the connected component of v
as discovery edges and back edges
setLabel(v, VISITED)
for all e G.incidentEdges(v) O(deg(v))
if getLabel(e) = UNEXPLORED
w opposite(v,e)
if getLabel(w) = UNEXPLORED
setLabel(e, DISCOVERY)
DFS(G, w)
else
setLabel(e, BACK)

44
Graphs

Path Finding
We can specialize the DFS
algorithm to find a path
between two given
vertices u and z using the
template method pattern
We call DFS(G, u) with u
as the start vertex
We use a stack S to keep
track of the path between
the start vertex and the
current vertex
As soon as destination
vertex z is encountered,
we return the path as the
contents of the stack

Algorithm pathDFS(G, v, z)
setLabel(v, VISITED)
S.push(v)
if v = z
return S.elements()
for all e G.incidentEdges(v)
if getLabel(e) = UNEXPLORED
w opposite(v,e)
if getLabel(w) = UNEXPLORED
setLabel(e, DISCOVERY)
S.push(e)
pathDFS(G, w, z)
S.pop(e)
else
setLabel(e, BACK)
S.pop(v)

22

Graphs

45
Graphs

Cycle Finding
Algorithm cycleDFS(G, v, z)
setLabel(v, VISITED)
S.push(v)
for all e G.incidentEdges(v)
if getLabel(e) = UNEXPLORED
w opposite(v,e)
S.push(e)
if getLabel(w) = UNEXPLORED
setLabel(e, DISCOVERY)
pathDFS(G, w, z)
S.pop(e)
else
T new empty stack
repeat
o S.pop()
T.push(o)
until o = w
return T.elements()
S.pop(v)

We can specialize the


DFS algorithm to find a
simple cycle using the
template method pattern
We use a stack S to keep
track of the path
between the start vertex
and the current vertex
As soon as a back edge
(v, w) is encountered,
we return the cycle as
the portion of the stack
from the top to vertex w

46

Breadth-First Search
L0
L1

L2

CGraphs
E

D
F

23

Graphs

47
Graphs

Outline and Reading


Breadth-first search Algorithm

Example
Properties
Analysis
Applications

DFS vs. BFS


Comparison of applications
Comparison of edge labels

48
Graphs

Breadth-First Search
Breadth-first search
(BFS) is a general
technique for traversing
a graph
A BFS traversal of a
graph G
Visits all the vertices and
edges of G
Determines whether G is
connected
Computes the connected
components of G
Computes a spanning
forest of G

BFS on a graph with n


vertices and m edges
takes O(n + m ) time
BFS can be further
extended to solve other
graph problems
Find and report a path
with the minimum
number of edges between
two given vertices
Find a simple cycle, if
there is one

24

Graphs

49
Graphs

Example

L0

unexplored vertex
visited vertex
unexplored edge
discovery edge
cross edge

A
A

L0
L1

L1

L0

C
E

L1

L1

A
A

L0

C
E

L0
L1

L2

L1

L0

C
E

D
F

L1

50
unexplored edge
discovery edge
cross edge

A
C
E

D
F

L2

unexplored vertex
visited vertex

L2

Example (cont.)

Graphs

L0

C
E

C
E

D
F

25

Graphs

A
A

Graphs

Example (cont.)
L0
L1

L2

L0
L1

C
E

L1

L2

unexplored edge
discovery edge
cross edge

C
E

D
F

L2

L0

51
unexplored vertex
visited vertex

C
E

D
F

52

BFS Algorithm
The algorithm uses a
mechanism for setting and
getting labels of vertices
and edges
Algorithm BFS(G)
Input graph G
Output labeling of the edges
and partition of the
vertices of G
for all u G.vertices()
setLabel(u, UNEXPLORED)
for all e G.edges()
setLabel(e, UNEXPLORED)
for all v G.vertices()
if getLabel(v) = UNEXPLORED
BFS(G, v)

Graphs

Algorithm BFS(G, s)
L0 new empty sequence
L0.insertLast(s)
setLabel(s, VISITED)
i0
while Li.isEmpty()
Li +1 new empty sequence
for all v Li.elements()
for all e G.incidentEdges(v)
if getLabel(e) = UNEXPLORED
w opposite(v,e)
if getLabel(w) = UNEXPLORED
setLabel(e, DISCOVERY)
setLabel(w, VISITED)
Li +1.insertLast(w)
else
setLabel(e, CROSS)
i i +1

26

Graphs

53
Graphs

Properties
Notation

Gs: connected component of s

Property 1

BFS(G, s) visits all the vertices and


edges of Gs

Property 2

The discovery edges labeled by


BFS(G, s) form a spanning tree Ts of
Gs

Property 3

For each vertex v in Li

L0

L1

The path of Ts from s to v has i edges


Every path from s to v in Gs has at
least i edges

C
F

L2

C
E

D
F

54

Analysis

Graphs

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 Sv deg(v) = 2m

27

Graphs

55
Graphs

Applications
Using the template method pattern, we can
specialize the BFS traversal of a graph G to
solve the following problems in O(n + m) time

Compute the connected components of G


Compute a spanning forest of G
Find a simple cycle in G, or report that G is a forest
Given two vertices of G, find a path in G between
them with the minimum number of edges, or
report that no such path exists

56
Graphs

DFS vs. BFS


Applications

DFS

BFS

Spanning forest, connected


components, paths, cycles

Shortest paths

Biconnected components

L0

A
B

C
E

D
F

DFS

L1

L2

C
E

D
F

BFS

28

Graphs

57

DFS vs. BFS (cont.)

Graphs

Cross edge (v,w)

Back edge (v,w)

w is in the same level as v


or in the next level in the
tree of discovery edges

w is an ancestor of v in the
tree of discovery edges

L0

A
B

C
E

D
F

DFS

L1

L2

C
E

D
F

BFS

29

Potrebbero piacerti anche