Announcement 2: on March 31 2015 (Tuesday) during the lecture time.

advertisement
Announcement 2:
• A 2 hour midterm (open book) will be given
on March 31 2015 (Tuesday) during the
lecture time.
2016/5/29
1
Single-Source Shortest Paths
• Problem Definition
• Shortest paths and Relaxation
• Dijkstra’s algorithm (can be viewed as a
greedy algorithm)
2016/5/29
2
B
A
Find a shortest path from A to B.
-need serious thinking to get a correct algorithm.
2016/5/29
3
Directed and Undirected Graph
2016/5/29
4
Adjacency-list representation for directed graph
1
4
2
5
(a)
2016/5/29
3
6
1
1,2
2
2,5 /
3
3,6
4
4,2
/
5
5,4
/
6
6,6
/
1,4
/
3,5
/
(b)
5
Adjacency-matrix representation for directed graph
It is NOT symmetric.
1
4
2
5
(a)
3
6
1
2
3
4
5
6
1
0
1
0
1
0
0
2
0
0
0
0
1
0
3
0
0
0
0
1
1
4
0
1
0
0
0
0
5
0
0
0
1
0
0
6
0
0
0
0
0
1
(c)
2016/5/29
6
Problem Definition:
• Real problem: A motorist wishes to find the shortest
possible route from Chicago to Boston.Given a road map
of the United States on which the distance between each
pair of adjacent intersections is marked, how can we
determine this shortest route?
• Formal definition: Given a graph G=(V, E, W), where each
edge has a weight, find a shortest path from s to v for some
interesting vertices s and v.
• s—source
• v—destination.
2016/5/29
7
Shortest path:
• The weight of path p=<v0,v1,…,vk > is the sum of
the weights of its constituent edges:
k
w( p )   w(vi  1, vi )
i 1
The cost of the shortest path from s to v
is denoted as (s, v).
2016/5/29
8
Negative-Weight edges:
• Edge weight may be negative.
• negative-weight cycles– the total weight in the
cycle (circuit) is negative.
• If no negative-weight cycles reachable from the
source s, then for all v V, the shortest-path
weight  (s, v) remains well defined,even if it has a
negative value.
• If there is a negative-weight cycle on some path
from s to v, we define  ( s, v) = -  .
2016/5/29
9
a
3
3
5
s 0
2
-4
b
c
6
5
-3 11
e
3

h 2 i


-1
4
d
f
8

7
g
-8
3

j

-6
Figure1 Negative edge weights in a directed graph.Shown within each vertex is its
shortest-path weight from source s.Because vertices e and f form a negative-weight
cycle reachable from s,they have shortest-path weights of -  . Because vertex g is
reachable from a vertex whose shortest path is -  ,it,too,has a shortest-path weight
of - .Vertices such as h, i ,and j are not reachable from s,and so their shortest-path
weights are  , even though they lie on a negative-weight cycle.
2016/5/29
10
Representing shortest paths:
• we maintain for each vertex vV , a predecessor
 [ v] that is the vertex in the shortest path
right before v.
• With the values of  , a backtracking process can
give the shortest path. (We will discuss that after
the algorithm is given)
2016/5/29
11
• Observation: (basic)
• Suppose that a shortest path p from a source s to a
vertex v can be decomposed into
p'
s 
u v
for some vertex u and path p’. Then, the
weight of a shortest path from s to v is
 ( s, v)   ( s, u )  w(u, v)
We do not know what is u for v, but we know u is in V and we
can try all nodes in V in O(n) time.
Also, if u does not exist, the edge (s, v) is the shortest.
Question: how to find (s, u), the first shortest from s to some node?
2016/5/29
12
Relaxation:
• The process of relaxing an edge (u,v) consists of testing
whether we can improve the shortest path to v found so far
by going through u and,if so,updating d[v] and  [v].
• RELAX(u,v,w)
• if d[v]>d[u]+w(u,v)
 d[u]+w(u,v) (based on observation)
•
then d[v] 
•
 [v]  u
2016/5/29
13
u
5
2
v
u
9
5
v
2
6
RELAX(u,v)
u
5
2
(a)
RELAX(u,v)
v
u
7
5
2
v
6
(b)
Figure2 Relaxation of an edge (u,v).The shortest-path estimate of each vertex
is shown within the vertex. (a)Because d[v]>d[u]+w(u,v) prior to relaxation,
the value of d[v] decreases. (b)Here, d[v]  d[u]+w(u,v) before the relaxation
step,so d[v] is unchanged by relaxation.
2016/5/29
14
Initialization:
• For each vertex v  V, d[v] denotes an upper bound on
the weight of a shortest path from source s to v.
• d[v]– will be (s, v) after the execution of the algorithm.
• initialize d[v] and [v] as follows: .
• INITIALIZE-SINGLE-SOURCE(G,s)
• for each vertex v  V[G]
 
•
do d[v] 
 [v] NIL
•
 0
• d[s] 
2016/5/29
15
Dijkstra’s Algorithm:
• Dijkstra’s algorithm assumes that w(e)0 for each e in the graph.
• maintain a set S of vertices such that
– Every vertex v S, d[v]=(s, v), i.e., the shortest-path from s to v
has been found. (Intial values: S=empty, d[s]=0 and d[v]=)
•
(a) select the vertex uV-S such that
d[u]=min {d[x]|x V-S}. Set S=S{u}
D[u]= (s, u) at this moment! Why?
• (b) for each node v adjacent to u do RELAX(u, v, w).
• Repeat step (a) and (b) until S=V.
2016/5/29
16
Continue:
•
•
•
•
•
•
•
•
•
DIJKSTRA(G,w,s):
INITIALIZE-SINGLE-SOURCE(G,s)
 
S
 V[G]
Q
while Q  
 EXTRACT -MIN(Q)
do u 
 S  {u}
S
for each vertex v  Adj[u]
do RELAX(u,v,w)
2016/5/29
17
Implementation:
• a adaptable priority queue Q stores vertices in V-S,
keyed by their d[] values.
• the graph G is represented by adjacency lists so
that it takes O(1) time to find an edge (u, v) in (b).
2016/5/29
18
u
v
1
8
8
10
s
0
9
3
2
4
6
7
5
8
8
2
x
y
(a)
2016/5/29
19
u
v
1
8
10
10
s
0
9
3
2
4
6
7
5
x
8
5
2
(b)
y
(s,x) is the shortest path using one edge. It is also the shortest path from s to x.
2016/5/29
20
Assume EXTRACT -MIN(Q)=x.
(s,x) is the shortest path using one edge.
– Why? Since (s, x) is the shortest among all
edges starting from s.
• It is also the shortest path from s to x.
Proof: (1) Suppose that path P: s->u…->x is the shortest
path. Then w (s,u) w(s, x).
(2) Since edges have non-negative weight, the total
weight of path P is at least w(s,u) w(s, x).
(3) So, the edge (s, x) is the shortest path from s to x.
2016/5/29
21
u
v
1
8
14
10
s
0
9
3
2
4
6
7
5
5
7
2
x
y
(c)
2016/5/29
22
u
v
1
8
13
10
s
0
9
3
2
4
6
7
5
5
7
2
x
y
(d)
2016/5/29
23
u
v
1
8
9
10
s
0
9
3
2
4
6
7
5
5
7
2
x
y
(e)
2016/5/29
24
u
v
1
8
9
10
s
0
9
3
2
4
6
7
5
5
7
2
x
y
(f)
2016/5/29
25
Time complexity of Dijkstra’s Algorithm:
• Time complexity depends on implementation of the
adaptable priority.
• Method 1: Use an array to story the Queue
• EXTRACT -MIN(Q) --takes O(|V|) time.
– Totally, there are |V| EXTRACT -MIN(Q)’s.
– time for |V| EXTRACT -MIN(Q)’s is O(|V|2).
• RELAX(u,v,w) --takes O(1) time.
– Totally |E| RELAX(u, v, w)’s are required.
– time for |E| RELAX(u,v,w)’s is O(|E|).
• Total time required is O(|V|2+|E|)=O(|V|2)
• Backtracking with [] gives the shortest path in inverse
order.
.
2016/5/29
26
Time complexity of Dijkstra’s Algorithm:
• Time complexity depends on implementation of
the adaptable priority.
• Method 2: The adaptable priority queue is
implemented as a heap. It takes O(log |V|) time to
do EXTRACT-MIN(Q) and O(log |V|) time for
each RELAX(u, v, w)’s. The total running time
is O((|V|+|E|)log |V|)=O(|E|log |V|) assuming the
graph is connected.
• When |E| is O(|V|) the second implementation is
better. If |E|=O(|V|2), then the first implementation
is better.
• Note that lots of maps are sparse graphs in which
|E|=O(|V|).
2016/5/29
27
• Method 3: The priority queue is
implemented as a Fibonacci heap. It takes
O(log |V|) time to do EXTRACT-MIN(Q)
and O(1) time to decrease the key value of
an entry. The total running time is O(|V|log
|V|+|E|). (not required)
2016/5/29
28
Adaptable Heap:
class ArrayNode {
double key;
String value; // value stored at this position
int id; // identificsation of the entry id=1,2, 3, …, n
add necessary methods here
}
public class MyAdaptableHeap{
protected ArrayNode T[]; // array of elements stored in the tree
protected int maxEntries; // maximum number of entries
protected int numEntries; //num of entries in the heap
protected int location[];
// an array with size=T.length. here location[id] stores the rank of entry id in T[].
Add necessary methods here. In particular, we need
Replace(id, k) (The key of entry id is updated to be key=k and we assume that the
entry is already in the heap.)
Binary Search Trees
29
Adaptable Heap:
Replace (id, k)
=replace (4, 25):
11/6
12/7
Array- 14/1
based rep
of the
complete
binary
tree T[]
go to location[4] to
find the rank=3 of
entry 4 and goto
T[rank]=T[3] and
update T[3]=25.
After change T[3]
from 13 to 25, the
binary tree is not a
13/4
15/3
16/2
17/5
heap any more.
0
location
0
11 12 13 14 15
16 17
Key value
1
2
3
5
6
7
Rank of the
node
4
6
5
7
1
2
1
2
3
4
3
4
5
Binary Search Trees
6
7
id-name of a node
30
Download