Академический Документы
Профессиональный Документы
Культура Документы
Graphs
Readings
Optional
[Weiss] ch20
[CLR] ch5.4
Exercise
20.5
Graph
edge
vertex
11
nus.soc.cs1102b.week10
Weighted Graph
1
0
-2
5
nus.soc.cs1102b.week10
24 October 2002
12
Undirected Graph
13
nus.soc.cs1102b.week10
Complete Graph
Path
a
b
c
d
e
15
nus.soc.cs1102b.week10
Cycle
a
b
g
c
d
f
e
16
nus.soc.cs1102b.week10
24 October 2002
Disconnected Graph
17
nus.soc.cs1102b.week10
Formally
A graph G = (V, E, w), where
18
nus.soc.cs1102b.week10
Example
V = { a, b, c }
E = { (a,b), (c,b), (a,c) }
w = { ((a,b), 4), ((c, b), 1), ((a,c),-3) }
a
-3
c
4
1
19
nus.soc.cs1102b.week10
Adjacent Vertices
adj(v)
adj(a)
adj(b)
adj(c)
v |adj(v)| = |E|
4
1
adj(v): Neighbours of v
20
nus.soc.cs1102b.week10
24 October 2002
Applications
Travel Planning
direct
flight
city
5
cost
Internet
network
link
computer
25
nus.soc.cs1102b.week10
The Web
web link
web page
28
nus.soc.cs1102b.week10
24 October 2002
Module Selection
Topological Sort
prerequisite
module
30
nus.soc.cs1102b.week10
SDU Matchmaking
participant
32
nus.soc.cs1102b.week10
Terrorist
http://www.orgnet.com/hijackers.html
knows
suspects
34
nus.soc.cs1102b.week10
Other Applications
Biology
VLSI Layout
Vehicle Routing
Job Scheduling
Facility Location
:
:
37
nus.soc.cs1102b.week10
24 October 2002
Implementation
Adjacency Matrix
double vertex[][];
1
-3
-3
4
2
39
nus.soc.cs1102b.week10
Adjacency List
EdgeList vertex[];
1
-3
1
-3
2
3
neighbour
4
2
cost
40
nus.soc.cs1102b.week10
Vertex Map
Clementi
1
hash
table
-3
2
3
41
nus.soc.cs1102b.week10
24 October 2002
Breadth-First
Search
Breadth-First Search
A
43
nus.soc.cs1102b.week10
Breadth-First Search
A
50
nus.soc.cs1102b.week10
Breadth-First Search
0
51
nus.soc.cs1102b.week10
24 October 2002
BFS(v)
Q = new Queue
Q.enq (v)
while Q is not empty
B
curr = Q.deq()
if curr is not visited
F
print curr
mark curr as visited
foreach w in adj(curr)
if w is not visited
Q.enq(w)
A
C
E
53
nus.soc.cs1102b.week10
v.parent = null
B
while Q is not empty
curr = Q.deq()
F
if curr is not visited
mark curr as visited
foreach w in adj(curr)
if w is not visited
w.parent = curr
Q.enq(w)
C
E
54
nus.soc.cs1102b.week10
Calculating Level
Q = new Queue
Q.enq (v)
v.level = 0
B
while Q is not empty
curr = Q.deq()
F
E
if curr is not visited
mark curr as visited
foreach w in adj(curr)
if w is not visited
w.level = curr.level + 1
Q.enq(w)
C
D
55
nus.soc.cs1102b.week10
56
nus.soc.cs1102b.week10
24 October 2002
Running Time
Q = new Queue
Q.enq (v)
while Q is not empty
curr = Q.deq()
if curr is not visited
print curr
mark curr as visited
foreach w in adj(curr)
if w is not visited
Q.enq(w)
Main Loop
( adj ( w)) = ( E )
wV
Initialization
(V )
Total Running Time
(V + E )
57
nus.soc.cs1102b.week10
Depth-First
Search
Depth-First Search
A
59
nus.soc.cs1102b.week10
Depth-First Search
A
65
nus.soc.cs1102b.week10
24 October 2002
Depth-First Search
A
66
nus.soc.cs1102b.week10
DFS(v)
S = new Stack
S.push (v)
while S is not empty
curr = S.pop()
if curr is not visited
print curr
mark curr as visited
foreach w in adj(v)
if w is not visited
S.push(w)
A
B
F
C
E
67
nus.soc.cs1102b.week10
A
B
F
C
E
68
nus.soc.cs1102b.week10
69
nus.soc.cs1102b.week10
24 October 2002
71
nus.soc.cs1102b.week10
Single-Source
Shortest Path
Definition
A path on a graph G is a sequence of vertices v0,
v1, v2, .. vn where (vi,vi+1)E
The cost of a path is the sum of the cost of all
edges in the path.
A
B
F
C
E
D
73
nus.soc.cs1102b.week10
74
nus.soc.cs1102b.week10
24 October 2002
ShortestPath(s)
Run
BFS(s)
w.level:
w.parent:
75
nus.soc.cs1102b.week10
Positive Weighted
Shortest Path
A
1
3
B
3
F
1
4
C
1
5
D
76
nus.soc.cs1102b.week10
77
nus.soc.cs1102b.week10
Idea 1
s
w
10
8
2
6
v
78
nus.soc.cs1102b.week10
24 October 2002
Definition
distance(v) : shortest distance so far from
s to v
parent(v) : previous node on the shortest
path so far from s to v
cost(u, v) : the cost of edge from u to v
80
nus.soc.cs1102b.week10
Example
s
w
10
8
2
6
distance(w) = 8
cost(v,w) = 2
parent(w) = v
81
nus.soc.cs1102b.week10
Relax(v,w)
d = distance(v) + cost(v,w)
if distance(w) > d then
distance(w) = d
parent(w) = v
s
w
10
6
w
10
8
Idea 2
w
6
6
v
6
6
79
nus.soc.cs1102b.week10
24 October 2002
Dijkstras Algorithm
A
1
3
B
3
F
1
4
C
1
83
nus.soc.cs1102b.week10
Dijkstras Algorithm
0
5
1
10
93
nus.soc.cs1102b.week10
Dijkstras Algorithm
color all vertices yellow
foreach vertex w
distance(w) = INFINITY
distance(s) = 0
94
nus.soc.cs1102b.week10
Dijkstras Algorithm
while there are yellow vertices
v = yellow vertex with min distance(v)
color v red
foreach neighbour w of v
relax(v,w)
95
nus.soc.cs1102b.week10
24 October 2002
Running Time
O(V2 + E)
Initialization O(V)
foreach vertex w
distance(w) = INFINITY
distance(s) = 0
pq = new PriorityQueue(V)
98
nus.soc.cs1102b.week10
Main Loop
while pq is not empty
v = pq.deleteMin()
foreach neighbour w of v
relax(v,w)
99
nus.soc.cs1102b.week10
24 October 2002
Main Loop
O((V+E) log V)
cost(u,v) < 0?
A
-5
1
3
B
3
F
C
1
-4
101
nus.soc.cs1102b.week10
Problem 1
w
6
10
v
-5
6
6
102
nus.soc.cs1102b.week10
Problem 2
A
-5
1
3
B
3
F
1
-4
C
1
5
2
103
nus.soc.cs1102b.week10
24 October 2002
Basic Idea
foreach edge (u,v)
relax(u , v)
105
nus.soc.cs1102b.week10
Bellman-Ford Algorithm
0
3
-2
-4
8
3
-3
106
nus.soc.cs1102b.week10
Bellman-Ford Algorithm
0
1
-3
2
3
-1
1
-4
1
3
3
-2
110
nus.soc.cs1102b.week10
I claim that the running time for BellmanFord algorithm is O(VE). Verify this claim.
Bellman-Ford Algorithm
do |V|-1 times
foreach edge (u,v)
relax(u,v)
// check for negative weight cycle
foreach edge (u,v)
if distance(u) < distance(v) + cost(v,u)
ERROR: has negative cycle
111
nus.soc.cs1102b.week10
24 October 2002