1 / 23

Tirgul 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. Dijkstra – Review. The idea: Maintain a set of vertices whose final shortest path weights from s have already been determined

rossa
Télécharger la présentation

Tirgul 12

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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

  2. 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 ?

  3. 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)

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

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

  6. Bellman-Ford’s Algorithm

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

  8. 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)

  9. 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 :

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

  11. Application of Bellman-Ford A ·x  b

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

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

  14. v1 v2 v5 v3 v4 Application of Bellman-Ford • The problem is represented by the graph:

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

  16. 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:

  17. 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:

  18. 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:

  19. 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?

  20. Application of Bellman-Ford • Because

  21. 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 :

  22. 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…

  23. Question v0 • Can any shortest path from to any vertex in the extended constrains graph be positive?

More Related