240 likes | 470 Views
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
E N D
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 setand 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 outof 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.
v1 v2 v5 v3 v4 Application of Bellman-Ford • The problem is represented by the graph:
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.
Application of Bellman-Ford • Formally: • Each node vi corresponds to a variable xiin 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:
0 v1 0 v2 0 -1 v0 1 v5 5 0 4 v3 -3 -3 0 -1 0 v4 Application of Bellman-Ford • The extended constraint graph:
0 v1 0 -5 v2 0 -1 -3 v0 1 v5 5 0 4 -4 v3 -3 0 -3 0 -1 -1 0 v4 Application of Bellman-Ford • The solution:
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 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 :
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 v0 • Can any shortest path from to any vertex in the extended constrains graph be positive?