Sei sulla pagina 1di 16

CS 97SI: INTRODUCTION TO PROGRAMMING CONTESTS

Jaehyun Park

Todays Lecture

Shortest Path Problem Floyd-Warshall Algorithm Dijkstras Algorithm Bellman-Ford Algorithm


System

of difference constraints

Maybe: Problem Discussion

Shortest Path Problem

Input: a weighted graph = ,


The

edges can be directed or not Sometimes, we allow negative edge weights

Output: the path between two given nodes and that minimizes the total weight (or cost, length)
Sometimes,

we want to compute all-pair shortest paths Sometimes, we want to compute shortest paths from to all other nodes

Floyd-Warshall Algorithm

Given a directed weighted graph Outputs a matrix where is the shortest distance from node to Can detect a negative-weight cycle Runs in 3 time Extremely easy to code
Coding

time less than a few minutes

Floyd-Warshall Pseudocode

Initialize to the given cost matrix For = 1 :


For

all and :
= min , +

If + < 0 for some and , then the graph has a negative weight cycle Done!
But

how does this work?

How does Floyd-Warshall work?

Define , , as the shortest distance from to , using 1 as intermediate nodes


(, , )

is the shortest distance from to , , 0 = cost(, )

The optimal path for , , may or may not have as an intermediate node
it does, , , = , , 1 + , , 1 Otherwise, , , = , , 1
If

Therefore, , , is the minimum of the two quantities above

How does Floyd-Warshall work?

We have the following recurrences and base cases


, , 0 = cost , , , = min { , , 1 + , , 1 , (, , 1)}

From the values of ,, 1 , we can calculate ,,


It

turns out that we dont need a separate matrix for each ; overwriting the existing values is fine

Thats how we get Floyd-Warshall algorithm

Dijkstras Algorithm

Given a directed weighted graph and a source


Important:

The edge weights have to be nonnegative!

Outputs a vector where is the shortest distance from to node Time complexity depends on the implementation:
Can

be 2 + , log , log

Very similar to Prims algorithm Intuition: Find the closest node to , and then the second closest one, then the third, etc.

Dijkstras Algorithm

Maintain a set of nodes , the shortest distances to which are decided Also maintain a vector , the shortest distance estimate from Initially, = , and = cost , Repeat until = :
with the smallest , and add it to For each edge of cost :
Find

= min , +

Bellman-Ford Algorithm

Given a directed weighted graph and a source Outputs a vector where is the shortest distance from to node Can detect a negative-weight cycle Runs in time Extremely easy to code
Coding

time less than a few minutes

Bellman-Ford Pseudocode

Initialize = 0 and = for all For = 1 1:


For

each edge of cost :


= min , +

For each edge of cost :


If

> + :
the graph contains a negative-weight cycle

Then

Why does Bellman-Ford work?

A shortest path can have at most 1 edges At the th iteration, all shortest paths of or less edges are computed After 1 iterations, all distances are final: for every edge of cost , + holds
Unless

there is a negative-weight cycle This is how the negative-weight cycle detection works

System of Difference Constraints

Given inequalities of the form


Want to find real numbers 1 , , that satisfy all the given inequalities Seemingly this has nothing to do with shortest paths
But

it can be solved using Bellman-Ford

Graph Construction

Create node for every variable Make an imaginary source node Create zero-weight edges from to all other nodes Rewrite the given inequalities as +
For

each of these constraint, make an edge from to with weight

Now we run Bellman-Ford using as the source

What happens?

For every edge with cost , the shortest distance vector will satisfy +
Setting

= gives a solution! that 1 2 1 is a negative-weight

What if there is a negative-weight cycle?


Assume

cycle From our construction, the given constraints contain 2 1 + 1 , 3 2 + 2 , etc. Adding all of them gives 0 (somethingnegative) i.e. the given constraints were impossible to satisfy

System of Difference Constraints

It turns out that our solution minimizes the span of the variables: max min
We

wont prove it This is a big hint on POJ 3169!

Potrebbero piacerti anche