Give two more shortest path trees for the fol

lowing graph

6 u v

3 1 2 4 s 2 7

5 3

x 6 y

Run through Dijkstras and see where there

are ties which can b e arbitrarily selected

There are twochoices forhowtoget to the third vertex

xboth of which cost

There are twochoices forhowto get to vertex v both of which cost

Lessons from the

contest

As predicted the sp eed dierence between the

fastest programs and average program dwarfed the

dierence b etween a sup ercomputer and a micro

computer have a bigger impact on

performance than hardware

Dierent algorithmsperform dierently on dier

ent data Thus even hard problemsmaybe tractable

on the kind of data you might b e interested in

None of the programs could eciently handle all

instances for n We will nd out why after

the midtermwhenwe discuss NPcompleteness

Many of the fastest programswere very short and

simple KISS My bet is that many of the en

hancements students built into them actually showed

them down This is where proling can comein

handy

The fast programswere often recursive

Winning Optimizations

Finding a good initial solution via randomization

orheuristic improvement help ed byestablishing a

go o d upp er b ound to constrict search

Using half the largest vertex degree as a lower

b ound similarly constricted search

Pruning a partial p ermutation the instant an edge

was the target made the dierence in going from

say to

Positioning the partial p ermutation vertices sepa

rated by b instead of meant signicantly earlier

cutos since any edge do es the job

Mirrorsymmetry can only save a factorof but

perhaps more could follow from partitioning the

vertices into equivalence classes by the same neigh

borho o d

Shortest Paths

Finding the shortest path b etween twonodes in a graph

arises in many dierent applications

Transp ortation problems nding the cheap est

waytotravel b etween twolocations

Motion planning what is the most natural way

foracarto on character to move ab out a simulated

environment

Communications problems howlookwillittake

foramessage to get b etween twoplaces Which

two lo cations are furthest apart ie what is the

diameter of the network

Shortest Paths and Sentence

Disambiguation

In our work on reconstructing text typ ed on an over

loaded telephone keypad we had to select which of

many p ossible interpretations was most likely

INPUT     

Blank Recognition

Token Token Token Token

  

Candidate Construction

Token Token Token Token

  

give a ping

of

me

ring

hive

sing

Sentence Disambiguating

a

give ping

of

P P

P P

Pq Pq

me ring

hive

sing

GIVE ME A RING OUTPUT

We constructed a graph where the vertices were the

p ossible wordsp ositions in the sentence with an edge

between p ossible neighb oring words Code C 1 Code C 2 Code C 3

P(W 1/C ) 2 P(W 3/C ) 1 1 P(W1 /C 2) 1 3

4 P(#/W 1) 1 P(W1 /#) 2 1 1 P(W /W ) 3 P(W2 /C 1) 2 1 P(W2 /C 3)

1 P(W2 /#) 2 P(W /C 2) # 1 2 # P(W3 /#)

1 3 P(W3 /C 1) P(W3 /C 3) 1 P(W4 /#)

1 3

P(W4 /C 1) P(W4 /C 3)

The weight of each edge is a function of the probability

that these two words will be next to each other in a

sentence hive me would be less than give me for

example

The nal system worked extremely well identifying

over of characters correctly based on grammatical

and statistical constraints

Dynamic programming the Viterbi algorithm can b e

used on the sentences to obtain the sameresults by

nding the shortest paths in the underlying DAG

Finding Shortest Paths

In an unweighted graph the cost of a path is just the

number of edges on the shortest path which can be

found in O n m time via breadthrst search

Inaweighted graph the weight of a path b etween two

vertices is the sum of the weights of the edges on a

path

BFS will not work on weighted graphs b ecause some

times visiting more edges can lead to shorter distance

ie

Note that there can b e an exp onential numb er of short

est paths b etween twonodessowe cannot rep ort all

shortest paths eciently

Note that negative cost cycles render the problem of

nding the shortest path meaningless since you can

always lo op around the negative cost cycle more to

reduce the cost of the path

Thus in our discussions we will assumethat all edge

weights are p ositive Other algorithmsdeal correctly

with negative cost edges

Minimum spanning trees are uneected by negative cost edges

Dijkstras Algorithm

We can use Dijkstras algorithm to nd the shortest

path b etween any twovertices s and t in G

The correctness of Dijkstras algorithm is based on this

If s xt is the shortest path from s to t

then s x had b etter b e the shortest path from

s to x orelsewecould makeitshorter

The distance from s to x will b e less than than of

that from s to tif all edges have p ositive weight

This suggests a dynamic programminglike strategy

where we store the distance from s to all nearbynodes

and use them to nd the shortest path to more distant

no des

The shortest path from s to s ds s If all edge

weights are positive the smallest edge incident to s

says x denes ds x

Wecanuse an arrayto store the length of the shortest

path to each no de Initialize each to to start

So on as weestablish the shortest path from s to a new

no de xwegothrough each of its incident edges to see

if there is a b etter wayfroms to other no des thru x

known fsg

for i to n do disti

foreachedge s v distv ds v

lasts

while l ast t

select v such that distv min disti

unk now n

foreachv x distxmindistx distv w v x

lastv

k now n known fv g

INCLUDE FIGURE OF EXAMPLES OF SHORTEST

PATH ALGORITHMS

Complexity O n if we use adjacency lists and a

Bo olean arraytomark what is known

This is essentially the sameasPrims algorithm

An O m lg n implementation of Dijkstras algorithm

would b e faster forsparse graphs and comes from us

ing a of the vertices ordered by distance and

up dating the distance to each vertex if necessary in

O lg ntimeforeachedgeout from freshly known ver

tices

Even b etter O n lg n mfollows from using Fib onacci

heaps since they p ermit one to do a decreasekey op

eration in O amortized time

AllPairs Shortest Path

Notice that nding the shortest path between a pair

of vertices s tinworst case requires rst nding the

shortest path from s to all other vertices in the graph

Many applications such as nding the center or di

ameter of a graph require nding the shortest path

between all pairs of vertices

We can run Dijkstras algorithm n times once from

each possible start vertex to solve allpairs shortest



path problem in O n Can wedobetter

Improving the complexityisanopenquestion but there

is a sup erslick dynamic programming algorithm which



also runs in O n

Dynamic Programming and

Shortest Paths

The fourstep approach to dynamic programming is

Characterize the structure of an optimal solution

Recursively dene the value of an optimal solution

Compute this recurrence in a b ottomup fashion

Extract the optimal solution from computed infor

mation

From the adjacency matrix wecan construct the fol

lowing matrix

D i j if i j and v v isnotinE

i j

D i j w i j if v v E

i j

D i j if i j

This tells us the shortest path going through no inter

mediate no des

There are several ways to characterize the shortest

path between two no des in a graph Note that the

shortest path from i to j i j using at most M

edges consists of the shortest path from i to k using

at most M edgesW k j forsome k

This suggests that we can compute allpair shortest

path with an induction based on the number of edges

in the optimalpath

m

Let di j be the length of the shortest path from i

to j using at most m edges



What is di j



di j if i j

if i j

m

What if weknow di j for all i j

m m m

di j mindi j mindi k w k j

m

mindi k w k j k i

since w k k

This gives us a recurrence which wecan evaluate in a

b ottom up fashion

for i to n

for j to n

m

di j

for k to n

 m m

di j Min di k di k dk j



This is an O n algorithm just likematrix multiplica

tion but it only go es from m to m edges

Since the shortest path between any two no des must

use at most n edges unless we have negative cost

cycles wemust rep eat that pro cedure n times m



to nforan O n algorithm



We can improve this to O n log n with the obser

vation that any path using at most m edges is the

function of paths using at most m edges each This is

n n n

just like computing a a x a So a logarithmic

number of multiplications suce for exp onentiation



Although this is slick observe that even O n log nis

slower than running Dijkstras algorithm starting from each vertex

The FloydWarshall Algorithm

An alternate recurrence yields a moreecient dynamic

programming formulation Number the vertices from

to n

k

Let di j be the shortest path length from i to j us

ing only vertices from k as p ossible intermediate

vertices



What is dj j With no intermediate vertices any



path consists of at most one edge so di j w i j

In general adding anew vertex k helps iapath

go es through it so

k

di j w i j ifk

k  k  k 

mindi j di k dk j ifk

Although this lo oks similarto the previous recurrence

it isnt The following algorithm implements it

o

d w

for k to n

for i to n

for j to n

k k  k  k 

di j mindi j di k dk j



This obviously runs in n time which asymptoti

cally is no b etter than acalls to Dijkstras algorithm

However the lo ops are so tight and it is so short and

simple that it runs b etter in practice by a constant

factor