2 optimal Euler path problem Euler circuit in
2 -optimal Euler path problem Euler circuit in a directed graph: • An Euler circuit in a directed graph is a directed circuit that visits every edge in G exactly once. v 4 b d v 1 v 3 e g a c v 2 v 6 f v 5 Figure 1 12/30/2021 chapter 25 1
Theorem 2: • If all the vertices of a connected graph have equal in-degrees and out-degrees, then the graph has an Euler circuit. • in-degree-- number of edges pointing to the node • out-degree-- number of edges pointing out from the node. 12/30/2021 chapter 25 2
2 -path: • A 2 -path is a directed subgraph consisting of two consecutive edges. For example: u v w • v is called the midpoint. • Every 2 -path is given a cost (positive number) • A Euler circuit of m edges contains m 2 -paths, and the cost of the Euler circuit is the total weight of the m 2 -paths. 12/30/2021 chapter 25 3
2 -optimal Euler circuit problem: • Instance: A directed graph, each of its vertices has in-degree 2 and out-degree 2 and 2 -path has a cost. • Question: Find an Euler circuit with the smallest cost among all possible Euler circuits. 12/30/2021 chapter 25 4
Line graph: • If we view each edge in the above graph G(Figure 1) as a vertex, and each 2 -path in the above graph as an edge, we get another graph L(G), called line graph. d b a c g e f Figure 2 12/30/2021 chapter 25 5
• Observation: An Euler circuit in graph G corresponds to a Hamilton circuit in graph L(G). • 2 -optimal Euler path problem becomes a TSP problem in line graph. • Theorem : TSP in line graph with indegree 2 and out-degree 2 can be solved in polynomial time. (TSP in general is NP-complete) 12/30/2021 chapter 25 6
Facts: • For each node in G, there are four 2 -paths that form 2 pairs of 2 -paths. • In an Euler circuit, one of the pairs is used. • A pair of 2 -path for v is good if the total cost of this pair of 2 -paths is not less than that of the other pair. 12/30/2021 chapter 25 7
Algorithm: • For each node v in G, fixed the good pair of 2 -path for v. (This leads to a set of edge disjoint circles H. ) • Contract each circle Ci in H into a single node ni. There is an edge(undirected) between ni and nj if Ci and Cj have a common vertex, say, v. The weight on edge (ni, nj) is w(e 1)+w(e 2)-w(e 3)-w(e 4) where e 1 and e 2 forms a bad pair for v, and e 3 and e 4 forms a good pair for v. Call H’ be the resulting undirected graph. • Construct a minimum spanning tree H’ • Construct an Euler circuit for G by merging circles based on the minimum spanning tree obtained in Step 3. 12/30/2021 chapter 25 8
Example: • Suppose that the directed Euler graph G is Given in Figure 3(next page). There are 5 cycles. The 2 paths in circles have cost 1 and the costs on other 2 -paths are listed below: w(a, e)=2, w(h, b)=2; w(f, i)=3, w(l, g)=3; w(g, t)=2, w(s, h)=11; w(k, m)=1, w(p, l)=2; w(t, o)=2, w(n, q)=3. 12/30/2021 chapter 25 9
j d f e a i l b m p g c k h t o n s r q Figure 3 12/30/2021 chapter 25 10
Step 1: we get 5 circles shown below: e a d f j i c h t m p s o r 12/30/2021 l g b k q chapter 25 n 11
Step 2: the undirected graph constructed is: e, f, g, h 4 i, j, k, l 2 1 11 a, b, c, d 3 m, n, o, p q, r, s, t 12/30/2021 chapter 25 12
Step 3: the minimum spanning tree for the above tree is as follows: 4 2 1 3 12/30/2021 chapter 25 13
Step 4: The 2 -optimal Euler path is shown below: e a d f j i k l g c b m p h t o n s r 12/30/2021 chapter 25 q 14
Theorem: • The above algorithm is correct. • Proof: • The total cost of the 2 -paths obtained in Step 1 is not greater than that of the optimal solution. • To obtain an Euler circuit, we have to merge the k circles obtained in Step 1. (needs (k-1) merge operations) • These (k-1) merge operations correspond to (k-1) edges in the undirected graph. (In fact, a spanning tree) • The final cost is the cost of all the 2 -paths obtained in Step 1 + the cost of the (minimum) spanning tree. • Since we use a minimum spanning tree, the cost of our solution is the smallest. chapter 25 12/30/2021 15
12/30/2021 chapter 25 16
Chapter 25 Single-Source Shortest Paths • • Problem Definition Shortest paths and Relaxation Bellman-Ford algorithm Dijkstra’s algorithm 12/30/2021 chapter 25 17
Problem Definition: • Real problem: A motorist wishes to find the shortest possible route from Chicago to Boston. Given a road map of the United States on which the distance between each pair of adjacent intersections is marked, how can we determine this shortest route? • Formal definition: Given a graph G=(V, E, W), where each edge has a weight, find a shortest path from s to v for some interesting vertices s and v. • s—source • s—destination. 12/30/2021 chapter 25 18
• Simple but stupid solution: • we enumerate all the routes from Chicago to Boston, add up the distances on each route, and select the shortest. • It is easy to see, however, that even if we disallow routes that contain cycles, there are millions of possibilities, most of which are simply not worth considering. 12/30/2021 chapter 25 19
Shortest path: • The weight of path p=<v 0, v 1, …, vk > is the sum of the weights of its constituent edges: • We define the shortest-path weight from u to v by if there is a path from u to v otherwise • A shortest path from vertex u to vertex v is then defined as any path p with weight w(p)=. 12/30/2021 chapter 25 20
Variants: • Single-destination/source shortest-paths problem – If we change the direction of each edge in the given graph, single-destination becomes single -source. • Single-pair shortest-path problem • All-pairs shortest-paths problem 12/30/2021 chapter 25 21
Negative-Weight edges: • Edge weight may be negative. • negative-weight cycles– the total weight in the cycle (circuit) is negative. • If no negative-weight cycles reachable from the source s, then for all v V, the shortest-path weight remains well defined, even if it has a negative value. • If there is a negative-weight cycle on some path from s to v, we define =-. 12/30/2021 chapter 25 22
a 3 3 5 s 0 2 -4 b h -1 c d 6 5 -3 11 e 3 f 4 8 g 7 2 i -8 3 j -6 Figure 1 Negative edge weights in a directed graph. Shown within each vertex is its shortest-path weight from source s. Because vertices e and f form a negative-weight cycle reachable from s, they have shortest-path weights of. Because vertex g is reachable from a vertex whose shortest path is , it, too, has a shortest-path weight of. Vertices such as h, i , and j are not reachable from s, and so their shortest-path weights are , even though they lie on a negative-weight cycle. 12/30/2021 chapter 25 23
Representing shortest paths: • we maintain for each vertex v V , a predecessor [ v] that is the vertex in the shortest path right before v. • With the values of , a backtracking process can give the shortest path. (We will discuss that after the algorithm is given) 12/30/2021 chapter 25 24
Optimal substructure of a shortest path: • Lemma 25. 1 (basic) Subpaths of shortest paths are shortest paths Let p= <v 1, v 2, …, vk> be a shortest path from vertex v 1 to vertex vk and, for any i and j such that 1 i j k, let pij=<vi, vi+1, …, vj> be the subpath of p from vertex vi to vj. Then, pij is a shortest path from vi to vj. v 1 … vi … vj … vk 12/30/2021 chapter 25 25
• Corollary 25. 2 (basic) • Suppose that a shortest path p from a source s to a vertex v can be decomposed into s u v for some vertex u and path p’. Then, the weight of a shortest path from s to v is • for all edges (u, v) E, we have 12/30/2021 chapter 25 26
Initialization: • For each vertex v V, d[v] denotes an upper bound on the weight of a shortest path from source s to v. • d[v]– will be (s, v) after the execution of the algorithm. • initialize d[v] and [v] as follows: . • INITIALIZE-SINGLE-SOURCE(G, s) • for each vertex v V[G] • do d[v] • [v] NIL • d[s] 0 12/30/2021 chapter 25 27
Relaxation: • The process of relaxing an edge (u, v) consists of testing whether we can improve the shortest path to v found so far by going through u and, if so, updating d[v] and [v]. • RELAX(u, v, w) • if d[v]>d[u]+w(u, v) • then d[v] d[u]+w(u, v) (based on Lemma 25. 3) • [v] u 12/30/2021 chapter 25 28
u 5 2 v u 9 5 v 2 6 RELAX(u, v) u 5 2 RELAX(u, v) v u 7 5 (a) 2 v 6 (b) Figure 2 Relaxation of an edge (u, v). The shortest-path estimate of each vertex is shown within the vertex. (a)Because d[v]>d[u]+w(u, v) prior to relaxation, the value of d[v] decreases. (b)Here, d[v] d[u]+w(u, v) before the relaxation step, so d[v] is unchanged by relaxation. 12/30/2021 chapter 25 29
Properties of relaxation: (easy) • After relaxing edge (u, v) by executing RELAX(u, v, w), we have d[v] d[u]+w(u, v). • After initializing INITIALIZE-SINGLE-SOURCE(G, s), for all v V, d[v] (s, v). • this invariant is maintained for ever. • once d[v] achieves its lower bound (s, v), it never changes. 12/30/2021 chapter 25 30
Property: If no path connects a source vertex s V to a given vertex v V, then d[v]= . • Reasons: • After initializing by INITIALIZE-SINGLE-SOURCE(G, s), we have d[v]= (s, v)=. • This equality is maintained as invariant over any sequence of relaxation steps on the edges of G. 12/30/2021 chapter 25 31
Dijkstra’s Algorithm: • Dijkstra’s algorithm assumes that w(e) 0 for each e in the graph. • maintains a set S of vertices such that – Every vertex v S, d[v]= (s, v), i. e. , the shortest-path from s to v has been found. • repeatedly selects the vertex u V-S such that d[u]=min {d[x]|x S}. D[u]= (s, u) at this moment! Why? • a priority queue Q stores vertices in V-S, keyed by their d[] values. • the graph G is represented by adjacency lists. 12/30/2021 chapter 25 32
Continue: • • • DIJKSTRA(G, w, s): INITIALIZE-SINGLE-SOURCE(G, s) S Q V[G] while Q do u EXTRACT -MIN(Q) S S {u} for each vertex v Adj[u] do RELAX(u, v, w) 12/30/2021 chapter 25 33
Example: • The execution of Dijkstra’s algorithm. The source is the leftmost vertex. d[] values are shown within the vertices, and shaded edges indicate predecessor values: • if edge (u, v) is shaded, then [v]=u. • Red vertices are in the set S, and white vertices are in the priority queue Q=V-S. (a) The situation just before the first iteration of the while loop. The shaded vertex has the minimum d value and is chosen as vertex u. (b)-(f) The situation after each successive iteration of the while loop. The shaded vertex in each part is chosen as vertex u of the next iteration. The d and values shown in part (f) are the final values. 12/30/2021 chapter 25 34
v 1 u 8 6 8 4 3 2 0 9 10 s 7 5 8 8 2 y x (a) 12/30/2021 chapter 25 35
v 1 u 10 8 6 4 3 2 0 9 10 s 7 5 (b) 8 x 2 5 y (s, x) is the shortest path using one edge. It is also the shortest path from s to x. 12/30/2021 chapter 25 36
Assume EXTRACT -MIN(Q)=x. (s, x) is the shortest path using one edge. – Why? Since (s, x) is the shortest among all edges starting from s. • It is also the shortest path from s to x. Proof: (1) Suppose that path P: s->u…->x is the shortest path. Then w (s, u) w(s, x). (2) Since edges have non-negative weight, the total weight of path P is at least w(s, u) w(s, x). (3) So, the edge (s, x) is the shortest path from s to x. 12/30/2021 chapter 25 37
v 1 u 8 14 6 4 3 2 0 9 10 s 7 5 2 5 7 y x (c) 12/30/2021 chapter 25 38
Assume EXTRACT -MIN(Q)=y. Path: s->x->y is the shortest path using at most two edges. – Why? Since EXTRACT -MIN(Q)=y, we have (1) any path containing one edge: s->xx , xx is any node other than s, has cost at least w(s, x)+w(x, y), and (2) any path s->x->yy , where yy is any node other than s, is at least w(s, x)+w(x, y) long. From (1), any path s->xx->yy is at least w(s, x)+w(x, y) long. • It is also the shortest path from s to y. Proof: (1) Suppose that path P: s->xx-? yy…->y is the shortest path. Then w (s, xx)+s(xx, yy) w(s, x)+s(x, y). (2) Since edges have non-negative weight, the total weight of path P is at least w(s, x)+w(x, y). (3) So, the path s->x->y is the shortest path from s to y. 12/30/2021 chapter 25 39
v 1 u 8 13 6 4 3 2 0 9 10 s 7 5 2 5 7 y x (d) 12/30/2021 chapter 25 40
v 1 u 8 9 6 4 3 2 0 9 10 s 7 5 2 5 7 y x (e) 12/30/2021 chapter 25 41
v 1 u 8 9 6 4 3 2 0 9 10 s 7 5 2 5 7 y x (f) 12/30/2021 chapter 25 42
Time complexity of Dijkstra’s Algorithm: • Time complexity depends on implementation of the Queue. • Implementation 1: Use an array to story the Queue • EXTRACT -MIN(Q) --takes O(|V|) time. – Totally, there are |V| EXTRACT -MIN(Q)’s. – time for |V| EXTRACT -MIN(Q)’s is O(|V|2). • RELAX(u, v, w) --takes O(1) time. – Totally |E| RELAX(u, v, w)’s are required. – time for |E| RELAX(u, v, w)’s is O(|E|). • Total time required is O(|V|2+|E|)=O(|V|2) • Backtracking with [] gives the shortest path in inverse order. 12/30/2021 chapter 25 43
The End: • Have a nice day. 12/30/2021 chapter 25 44
- Slides: 44