tirgul 12 algorithm for single-source-shortest-paths (s-s-s-p) problem application of s-s-s-p for...

23
Tirgul 12 Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem Application of s-s-s-p for Solving a System of Difference Constraints

Post on 19-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Tirgul 12 Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem Application of s-s-s-p for Solving a System of Difference Constraints

Tirgul 12

Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem

Application of s-s-s-p for Solving a System of Difference Constraints

Page 2: Tirgul 12 Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem Application of s-s-s-p for Solving a System of Difference Constraints

Dijkstra – Review The idea:

Maintain a set of vertices whose final shortest path weights from s have already been determined

Each time, the vertex whose estimate for the shortest path is minimal is added to the set and all edges leaving it are ‘Relaxed’

Assumption: all edge weights are non-negative Proof of correctness depends on it

Exercise – where does the proof fail if there are negative weights ?

Page 3: Tirgul 12 Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem Application of s-s-s-p for Solving a System of Difference Constraints

Dijkstra Run Time In case the priority queue is implemented as a

binary heap.

Build heap takes O(V). Operation over the heap O(logV). Altogether O(VlogV).

Going over the adjacent list O(E). Relaxation is an operation over the heap

O(logV). Altogether O(ElogV).

Every thing together O(ElogV+VlogV)=O(ElogV)

Page 4: Tirgul 12 Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem Application of s-s-s-p for Solving a System of Difference Constraints

Bellman-Ford’s Algorithm Can handle negative weights, as long as there

are no negative cycles reachable from s

If there is a negative cycle – the solution to the problem is not defined.

Page 5: Tirgul 12 Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem Application of s-s-s-p for Solving a System of Difference Constraints

Bellman-Ford’s Algorithm The idea:

The shortest path from s to any other vertex does not contain a positive cycle (can be eliminated to produce a lighter path)

The longest path (in number of edges) without cycles between any pair of vertices is |V|-1 edges long

it is enough to check paths of up to |V|-1 edges

Page 6: Tirgul 12 Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem Application of s-s-s-p for Solving a System of Difference Constraints

Bellman-Ford’s Algorithm

truereturn

false return,if

, edgeeach for

,,Relax do

, edgeeach for

1 to 1 for

),(Initialize

vuwudvd

GEvu

wvu

GEvu

Vi

sG

Page 7: Tirgul 12 Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem Application of s-s-s-p for Solving a System of Difference Constraints

Bellman-Ford’s Algorithm The first pass over the edges – only neighbors

of s are affected (1 edge paths)

The second pass – shortest paths of 2 edges are found

After |V|-1 passes, all possible paths are checked. If we need to update any vertex in the last pass – there is a negative cycle reachable from s in G

Page 8: Tirgul 12 Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem Application of s-s-s-p for Solving a System of Difference Constraints

Bellman-Ford Run Time The algorithm run time is:

Goes over v-1 vertexes, O(V) For each vertex relaxation over E, O(E) Altogether O(VE)

Page 9: Tirgul 12 Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem Application of s-s-s-p for Solving a System of Difference Constraints

Application of Bellman-Ford The Bellman-Ford algorithm can be used to

solve a set of difference constraints.

The set of difference constraints is formalized to the following set of linear inequalities :

331

451

10

45

35

34

14

13

52

51

21

xxxxxxxxxxxxxxxx

Page 10: Tirgul 12 Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem Application of s-s-s-p for Solving a System of Difference Constraints

Application of Bellman-Ford There are many uses for a set of difference

constraints, for instance: The variables can represent the times of

different events The inequalities are the constraints over

there synchronization.

The set of linear inequalities can also be expressed in matrix notation:

A · x b

Page 11: Tirgul 12 Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem Application of s-s-s-p for Solving a System of Difference Constraints

Application of Bellman-Ford A ·x b

331

4511

0

11000101000110001001001011001010001

00011

5

4

3

2

1

xxxxx

Page 12: Tirgul 12 Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem Application of s-s-s-p for Solving a System of Difference Constraints

Application of Bellman-Ford What is the connection between the Bellman-Ford

algorithm and a set of linear inequalities?

We can interpreted the problem as a directed graph.

The graph is called the constraint graph of the problem

After constructing the graph, we could use the Bellman-Ford algorithm.

The result of the Bellman-Ford algorithm is the vector x that solves the set of inequalities.

Page 13: Tirgul 12 Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem Application of s-s-s-p for Solving a System of Difference Constraints

Application of Bellman-Ford Building the constraint graph out of matrix A:

Each column represents a vertex (node)

Each row represents an edge

If edge i goes out of vertex j than A[i,j]= -1

If edge i goes into vertex j than A[i,j]= 1

Otherwise A[i,j]= 0 Each row contains a single ‘1’, a single ‘-1’

and zeros.

Page 14: Tirgul 12 Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem Application of s-s-s-p for Solving a System of Difference Constraints

Application of Bellman-Ford The problem is represented by the graph:

v1

v5

v4

v3

v2

Page 15: Tirgul 12 Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem Application of s-s-s-p for Solving a System of Difference Constraints

Application of Bellman-Ford Extending the constraint graph to a single-

source-shortest-paths problem will be done by :

Adding vertex v0 that directs at all the other vertices.

Weight all edges from v0 as zero.

The weight of the other edges is determined by the inequality constraints.

Page 16: Tirgul 12 Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem Application of s-s-s-p for Solving a System of Difference Constraints

Application of Bellman-Ford Formally: Each node vi corresponds to a variable xi in the

original problem, and an extra node - v0 (will be s).

Each edge is a constraint, except for edges (v0 ,vi ) that were added.

Assign weights:

n})|i,v{(v}bx)|x,v{(vE i ijji ..10 constraint a is

},v,,vv{V n10

0 for

if is a constraint

( , ) 0 1

( , ) i

i j k i j k

w v v i n

w v v b x x b

Page 17: Tirgul 12 Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem Application of s-s-s-p for Solving a System of Difference Constraints

Application of Bellman-Ford The extended constraint graph:

v0

v1

v5

v4

v3

v2

0

0

0

0

0

-1

0

15

4

-3-3

-1

Page 18: Tirgul 12 Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem Application of s-s-s-p for Solving a System of Difference Constraints

Application of Bellman-Ford The solution:

v1

v5

v3

v2

0

0

0

0

0

-1

0

15

4

-3-3

-1

-4

v0

v4

-5

-3

-1

0

Page 19: Tirgul 12 Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem Application of s-s-s-p for Solving a System of Difference Constraints

Application of Bellman-Ford The Bellman-Ford solution for the extended constraint graph is a set of

values which meets the constraints.

Formally:

Why is this correct?

0 , iv v

ii vvx ,: 0

Page 20: Tirgul 12 Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem Application of s-s-s-p for Solving a System of Difference Constraints

Application of Bellman-Ford Because ,j i j i

0 0, , ,j i i jv v v v w v v

0 0, , ,j i i jv v v v w v v

for some j i kx x b k

Page 21: Tirgul 12 Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem Application of s-s-s-p for Solving a System of Difference Constraints

Application of Bellman-Ford If there is a negative cycle reachable from v0 –

there are no feasible solutions: Suppose the cycle is where vk=v1 v0 cannot be on it ( it has no incoming edges)

This cycle corresponds to :

kvvv ,,, 21

2,1

3,2

1, 1

2 1 1 2

3 2 2 3

1 1 1 1

,

,

,k

k

k

k k k

x x b w v v

x x b w v v

x x b w v v

Page 22: Tirgul 12 Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem Application of s-s-s-p for Solving a System of Difference Constraints

Application of Bellman-Ford The left side sums to 0.

The right side sums to the cycle’s weight w(c)

We get

But we assumed the cycle was negative…

cw0

0)( 1

1

11

xxxx k

k

iii

Page 23: Tirgul 12 Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem Application of s-s-s-p for Solving a System of Difference Constraints

Question Can any shortest path from to any vertex in

the extended constrains graph be positive? v0