# 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.

## Presentation on theme: "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."— Presentation transcript:

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 ?

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)

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.

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

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

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)

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 :

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

Application of Bellman-Ford A · x  b

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.

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.

Application of Bellman-Ford The problem is represented by the graph: v1v1 v5v5 v4v4 v3v3 v2v2

Application of Bellman-Ford Extending the constraint graph to a single-source- shortest-paths problem will be done by : Adding vertex v 0 that directs at all the other vertices. Weight all edges from v 0 as zero. The weight of the other edges is determined by the inequality constraints.

Application of Bellman-Ford Formally: Each node v i corresponds to a variable x i in the original problem, and an extra node - v 0 (will be s). Each edge is a constraint, except for edges (v 0,v i ) that were added. Assign weights:

Application of Bellman-Ford The extended constraint graph: v0v0 v1v1 v5v5 v4v4 v3v3 v2v2 0 0 0 0 0 0 1 5 4 -3

Application of Bellman-Ford The solution: v1v1 v5v5 v3v3 v2v2 0 0 0 0 0 0 1 5 4 -3 -4 v0v0 v4v4 -5 -3 0

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?

Application of Bellman-Ford Because

Application of Bellman-Ford If there is a negative cycle reachable from v 0 – there are no feasible solutions: Suppose the cycle is where v k =v 1 v 0 cannot be on it ( it has no incoming edges) This cycle corresponds to :

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 …

Question Can any shortest path from to any vertex in the extended constrains graph be positive? v0v0

Download ppt "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."

Similar presentations