You are on page 1of 46

Graphs

L S Vinh
Computer science department
Email: vinhls@vnu.edu.vn

Graphs

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
3
4
ORD
18
2
14
SFO

2555

337

HNL

LAX

43
7
1
1233

802

DFW

7
8
3
1
1120

LGA

10
9

MIA
2

Edge Types

Directed edge
ordered pair of vertices (u,v)
first vertex u is the origin
second vertex v is the
destination
e.g., a flight

Undirected edge
unordered pair of vertices (u,v)
e.g., a flight route

ORD

ORD

flight
AA 1206
849
miles

Directed graph
all the edges are directed
e.g., route network

Undirected graph
all the edges are undirected
e.g., flight network
3

PVD

PVD

(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(nm )
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 4one

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(nm )
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
5
trees

Topological Sorting
Number vertices, so that (u,v) in E implies
u < v wake up 1
A typical student day
2
study computer sci.

7
play

nap

eat
4

8
write c.s. program

9
for professors

10
sleep

5
more c.s.

6
work out

11
6

Algorithm for Topological

Sorting
Method TopologicalSort(G)
H G // Temporary copy of G
n G.numVertices()
while H is not empty do
Let v be a vertex with no outgoing edges
Label v n
nn-1
Remove v from H

Running time: O(n + m). How?

7

Topological Sorting
Algorithm using DFS

Simulate the algorithm by

using depth-first search

Algorithm topologicalDFS(G)
Input dag G
Output topological ordering of
G
n G.numVertices()
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
v)
O(n+m)topologicalDFS(G,
time.

Algorithm topologicalDFS(G, v)
Input graph G and a start vertex v of
G
Output labeling of the vertices of G
in the connected component of
v
setLabel(v, VISITED)
for all e G.incidentEdges(v)
if getLabel(e) UNEXPLORED
w opposite(v,e)
if getLabel(w)
UNEXPLORED
setLabel(e,
DISCOVERY)
topologicalDFS(G, w)
Label v with topological number n
nn-1
8

Topological Sorting
Example

Topological Sorting
Example

9
10

Topological Sorting
Example

8
9
11

Topological Sorting
Example

7
8
9
12

Topological Sorting
Example

7
8
9
13

Topological Sorting
Example

7
8
9
14

Topological Sorting
Example

4
6

7
8
9
15

Topological Sorting
Example
3
4
6

7
8
9
16

Topological Sorting
Example
2

3
4
6

7
8
9
17

Topological Sorting
Example
2

3
4
6

7
8
9
18

Shortest Paths
A

8
B
2

2
7
5
E

C
3

4
1

D
8

Weighted Graphs

In a weighted graph, each edge has an associated

numerical value, called the weight of the edge
Edge weights may represent, distances, costs, etc.
Example:
In a flight route graph, the weight of an edge represents
the distance in miles between the endpoint airports

LAX

1233

DFW

849

14
7
138
1120

PVD

1205

337

HNL

2555

3
4
17

ORD

802

SFO

1843

LGA

10
9

MIA
20

Shortest Paths

Given a weighted graph and two vertices u and v, we

want to find a path of minimum total weight between u
and v.
Length of a path is the sum of the weights of its edges.

Example:
Shortest path between Providence and Honolulu

Applications
Internet packet routing
Flight reservations
Driving directions
1843
SFO

LAX

1233

DFW

Phm Bo Sn - DSA

849

14
7
8
3
1
1120

PVD

1205

Dec 2008

337

HNL

2555

43
7
1

ORD

802

LGA

10
9

MIA
21

Dijkstra Algorithm
Given G=(V, E), where weight (u,v) > 0
is the weight of edge (u, v) E. Find
the shortest path from s to e.
General idea
- d[v]: the shortest distance from s to v.
- pre[v] = u: the previous vertex of v
on
the shortest path from s to
v.
Relaxation (s, u, v):
If d [u] + weight (u, v) < d [v]
d [v] = d [u] + weight (u, v)
pre[v] = u

suv

d(u) 50
s

d(u) 50
s

10 d(z) 75

10 d(z) 60

Dijkstra algorithm
Known = {the set of vertices which the shortest paths are known}
Unknown = {the set of vertices which the shortest paths are unknown}
Repeat
Find u unknown with the smallest d[u]
if u = e then
done
else {
known = known {u}
unknown = unknown {u}
for each edge (u, v):
relaxation (s, u, v)
}

Dijkstra Algorithm {
Known = {s};
Unknown = {E} {s}
for v unknown {
d[v] = weight [s, v];
pre[v] = s;
}
u = s; // at the first step
while (u != e ){
Select u such that d[u] = min {d[x] | x unknown}
Known = Known {u}
Unknown = Unknown {u}
for v Unknown
if d[u] + weight [u, v] < d[v] {
//relax (s, u, v)
d[v] = d[u] + weight[u,v];
pre[v] = u;
}
}
path = {e}
while (e != s) {
e = pre[e]
path = {e} path
}
}

Example
A

8
B

2
7

C
3

9
F

2
7
5
E

C
3

B
2

D
8

2
7

C
3

4
1

D
8

F
25

5
E

8
D

11

4
1

8
D

5
E

8
B

Example (cont.)
A

8
B
2

C
3

5
E

4
1

D
8

5
A

8
B
2

C
3

4
1

8
F

26

D
5

8
B
2

C
0

5
E

-8

D
9

d(C)=5!
27

Bellman-Ford
Algorithm

Works even with

Algorithm BellmanFord(G, s)
negative-weight edges
for all v G.vertices()
if v s
Must assume directed
setDistance(v, 0)
edges (for otherwise we
else
would have negativesetDistance(v, )
weight cycles)
for i 1 to n-1 do
Iteration i finds all
for each e G.edges()
shortest paths that use i
{ relax edge e }
edges.
u G.origin(e)
Running time: O(nm).
z G.opposite(u,e)
r getDistance(u) weight(e)
Can be extended to
if r getDistance(z)
detect a negativesetDistance(z,r)
weight cycle if it exists
How?

28

Bellman-Ford Example
Nodes are labeled with their d(v) values
0

-2
7

-2

8
8

-2
7

3
5

-2

7
1

-2

-2
1

-2
3

-2

-2
5 8

-1

7
3

-2

-2
9

1
29

4
9

-1
5

Minimum Spanning Trees

Spanning subgraph
Subgraph of a graph G
containing all the vertices
of G

Spanning tree
Spanning subgraph that is
itself a (free) tree

Minimum spanning tree (MST)

Spanning tree of a
weighted graph with
minimum total edge
weight

Applications

ORD

10

PIT

DEN

9
3

DCA

STL

8
DFW

2
ATL

Communications networks
Transportation networks
31

Cycle Property
Cycle Property:
Let T be a minimum
spanning tree of a
weighted graph G
Let e be an edge of G
that is not in T and let
C be the cycle formed
by e with T
For every edge f of C,
weight(f) weight(e)
Proof:
If weight(f) weight(e) we
can get a spanning
tree of smaller weight
by replacing e with f

C
2

7
Replacing f with e yields
a better spanning tree
f

8
4

C
2

3
8
7
32

Partition Property
U

Partition Property:
Consider a partition of the vertices of
G into subsets U and V
Let e be an edge of minimum weight
across the partition
There is a minimum spanning tree of
G containing edge e
Proof:
Let T be an MST of G
If T does not contain e, consider the
cycle C formed by e with T and let f
be an edge of C across the partition
By the cycle property,
weight(f) weight(e)
Thus, weight(f) weight(e)
We obtain another MST by replacing
f with e

4
9

8
8

e
7

Replacing f with e yields

another MST
U

4
9

8
8

e
7
33

Kruskals Algorithm
A priority queue
stores the edges
outside the cloud

Key: weight
Element: edge

algorithm

We are left with one

cloud that
encompasses the
MST
A tree T which is our
MST

Algorithm KruskalMST(G)
for each vertex V in G do
define a Cloud(v) of {v}
let Q be a priority queue.
Insert all edges into Q using their
weights as the key
T
while T has fewer than n-1 edges do
edge e = T.removeMin()
Let u, v be the endpoints of e
if Cloud(v) Cloud(u) then
Merge Cloud(v) and Cloud(u)
return T

34

Example
2704

BOS

867

849
ORD
1846

LAX

1391

1464
1235

187
144

JFK

1258

184

802

SFO
337

740
621

PVD

BWI
1090

DFW

946
1121
MIA

2342
Dec 2008

Phm Bo Sn - DSA

35

Example
2704

BOS

867

849
ORD
1846

LAX

1391

1464
1235

187
144

JFK

1258

184

802

SFO
337

740
621

PVD

BWI
1090

DFW

946
1121
MIA

2342
Dec 2008

Phm Bo Sn - DSA

36

Example
2704

BOS

867

849
ORD
1846

LAX

1391

1464
1235

187
144

JFK

1258

184

802

SFO
337

740
621

PVD

BWI
1090

DFW

946
1121
MIA

2342
Dec 2008

Phm Bo Sn - DSA

37

Example
2704

BOS

867

849
ORD
1846

LAX

1391

1464
1235

187
144

JFK

1258

184

802

SFO
337

740
621

PVD

BWI
1090

DFW

946
1121
MIA

2342
Dec 2008

Phm Bo Sn - DSA

38

Example
2704

BOS

867

849
ORD
1846

LAX

1391

1464
1235

187
144

JFK

1258

184

802

SFO
337

740
621

PVD

BWI
1090

DFW

946
1121
MIA

2342
Dec 2008

Phm Bo Sn - DSA

39

Example
2704

BOS

867

849
ORD
1846

LAX

1391

1464
1235

187
144

JFK

1258

184

802

SFO
337

740
621

PVD

BWI
1090

DFW

946
1121
MIA

2342
Dec 2008

Phm Bo Sn - DSA

40

Example
2704

BOS

867

849
ORD
1846

LAX

1391

1464
1235

187
144

JFK

1258

184

802

SFO
337

740
621

PVD

BWI
1090

DFW

946
1121
MIA

2342
Dec 2008

Phm Bo Sn - DSA

41

Example
2704

BOS

867

849
ORD
1846

LAX

1391

1464
1235

187
144

JFK

1258

184

802

SFO
337

740
621

PVD

BWI
1090

DFW

946
1121
MIA

2342
Dec 2008

Phm Bo Sn - DSA

42

Example
2704

BOS

867

849
ORD
1846

LAX

1391

1464
1235

187
144

JFK

1258

184

802

SFO
337

740
621

PVD

BWI
1090

DFW

946
1121
MIA

2342
Dec 2008

Phm Bo Sn - DSA

43

Example
2704

BOS

867

849
ORD
1846

LAX

1391

1464
1235

187
144

JFK

1258

184

802

SFO
337

740
621

PVD

BWI
1090

DFW

946
1121
MIA

2342
Dec 2008

Phm Bo Sn - DSA

44

Example
2704

BOS

867

849
ORD
1846

LAX

1391

1464
1235

187
144

JFK

1258

184

802

SFO
337

740
621

PVD

BWI
1090

DFW

946
1121
MIA

2342
Dec 2008

Phm Bo Sn - DSA

45

Example
2704

BOS

867

849
ORD
1846

LAX

1391

1464
1235

187
144

JFK

1258

184

802

SFO
337

740
621

PVD

BWI
1090

DFW

946
1121
MIA

2342
Dec 2008

Phm Bo Sn - DSA

46