Download presentation
Presentation is loading. Please wait.
Published byThomas Jansen Modified over 6 years ago
1
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. v1 v2 v3 v6 v5 v4 b d e g a c f Figure 1 2019/1/2 chapter25
2
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. 2019/1/2 chapter25
3
2-path: A 2-path is a directed subgraph consisting of two consecutive edges. For example: 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. u v w 2019/1/2 chapter25
4
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. 2019/1/2 chapter25
5
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 e a c g f Figure 2 2019/1/2 chapter25
6
2-optimal Euler path problem becomes a TSP problem in line graph.
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 in-degree 2 and out-degree 2 can be solved in polynomial time. (TSP in general is NP-complete) 2019/1/2 chapter25
7
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. 2019/1/2 chapter25
8
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(e1)+w(e2)-w(e3)-w(e4) where e1 and e2 forms a bad pair for v,and e3 and e4 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 Step3. 2019/1/2 chapter25
9
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. 2019/1/2 chapter25
10
j f i k e l a d m p g b h c o t n s r q Figure 3 2019/1/2 chapter25
11
Step1:we get 5 circles shown below:
f l k j i a c d b p o m n t q r s 2019/1/2 chapter25
12
Step2: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 2019/1/2 chapter25
13
Step3:the minimum spanning tree for the above tree is as follows:
4 2 1 3 2019/1/2 chapter25
14
Step4:The 2-optimal Euler path is shown below:
k j i h g e f a c d b p o m n t q r s 2019/1/2 chapter25
15
Theorem: The above algorithm is correct. Proof:
The total cost of the 2-paths obtained in Step1 is not greater than that of the optimal solution. To obtain an Euler circuit, we have to merge the k circles obtained in Step1.(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 Step1 + the cost of the (minimum) spanning tree. Since we use a minimum spanning tree, the cost of our solution is the smallest. 2019/1/2 chapter25
16
2019/1/2 chapter25
17
Chapter 25 Single-Source Shortest Paths
Problem Definition Shortest paths and Relaxation Bellman-Ford algorithm Dijkstra’s algorithm 2019/1/2 chapter25
18
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. 2019/1/2 chapter25
19
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. 2019/1/2 chapter25
20
Shortest path: The weight of path p=<v0,v1,…,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)= 2019/1/2 chapter25
21
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 2019/1/2 chapter25
22
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 = 2019/1/2 chapter25
23
a b -4 h i 3 -1 2 4 3 c d 6 8 5 -8 3 5 11 g s -3 e 3 f 2 7 j -6 Figure1 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. 2019/1/2 chapter25
24
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) 2019/1/2 chapter25
25
Optimal substructure of a shortest path:
Lemma (basic) Subpaths of shortest paths are shortest paths Let p= <v1,v2,…,vk> be a shortest path from vertex v1 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. v1 … vi … vj … vk 2019/1/2 chapter25
26
Corollary25.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 2019/1/2 chapter25
27
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] 2019/1/2 chapter25
28
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 2019/1/2 chapter25
29
u v u v 2 2 5 9 5 6 RELAX(u,v) RELAX(u,v) u v u v 2 2 5 7 5 6 (a) (b)
Figure2 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. 2019/1/2 chapter25
30
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 vV, d[v](s, v). this invariant is maintained for ever. once d[v] achieves its lower bound (s, v),it never changes. 2019/1/2 chapter25
31
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. 2019/1/2 chapter25
32
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 uV-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. 2019/1/2 chapter25
33
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) 2019/1/2 chapter25
34
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. 2019/1/2 chapter25
35
u v 10 5 2 1 3 4 6 9 7 8 s Single Source Shortest Path Problem x y (a) 2019/1/2 chapter25
36
u v 1 10 8 10 9 s 2 3 4 6 7 5 5 8 2 x y (b) (s,x) is the shortest path using one edge. It is also the shortest path from s to x. 2019/1/2 chapter25
37
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. 2019/1/2 chapter25
38
u v 1 8 14 10 9 s 2 3 4 6 7 5 5 7 2 x y (c) 2019/1/2 chapter25
39
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. 2019/1/2 chapter25
40
u v 1 8 13 10 9 s 2 3 4 6 7 5 5 7 2 x y (d) 2019/1/2 chapter25
41
7 9 5 8 10 2 1 3 4 6 s u v x y (e) 2019/1/2 chapter25
42
u v 1 8 9 10 9 s 2 3 4 6 7 5 5 7 2 x y (f) 2019/1/2 chapter25
43
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. 2019/1/2 chapter25
44
The End: Have a nice day. 2019/1/2 chapter25
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.