ex-.6-. Foundations of Operations Research Prof. E. Amaldi.6 Shortest paths with nonnegative costs Given the following graph, find a set of shortest paths from node to all the other nodes, using Dijkstra s algorithm. Can we solve the problem with Dynamic Programming? If yes, do so..7 Shortest paths with negative costs and ill-posedness Given the following graph, find the shortest paths between all pairs of nodes, or show that the problem is ill-posed by exhibiting a circuit of total negative cost. 7 8.8 An application of Dynamic Programming to machine renewal A company must buy a new machine and then decide a renewal plan for the next years, making sure that, at any point in time, the available machine works properly. At the beginning of each year of the planning horizon, the company must decide whether to keep the old machine or to substitute it. The maintenance costs and the expected revenue (when the machine is resold) are indicated in the following table. years maintenance (keuro) revenue when resold (keuro) - 7 6 9 Document prepared by L. Liberti, S. Bosio, S. Coniglio, and C. Iuliano. Translation to English by S. Coniglio
ex-.6-. Foundations of Operations Research Prof. E. Amaldi To avoid high maintenance costs of old machines, the machine can be resold at the beginning of the second, third, fourth, and fifth year, and an new one can be bought. For the sake of simplicity, we suppose that a new machine always costs k Euro. Show that the problem of determing a machine renewal plan of minimal total net cost (total cost for buying/rebuying the machine + maintenance costs - total revenue) can be solved via Dynamic Programming by finding a shortest path in an acyclic directed graph. Find an optimal renewal plan. Is it unique?.9 Project planning The preparation of the apple pie has long been a tradition at Rossi s family. First the weight of the ingredients has to be determined: flour, sugar, butter, eggs, apples, cream. The butter must then be melted down, and added to a mixture of flour, sugar, and eggs. Apples must be added to this new mixture, once they have been peeled and cut into thin slices. The mixture can then be cooked, in the already heated oven. It is advisable to whip the cream only after the apple slices have been added to the mixture. Once the cake is cooked, the cream is used to garnish it. The following table reports the time needed for each activity. Activity Time (minutes) A Weight the ingredients B Melt the butter C Mix flour, eggs and sugar D Peel the apples and cut them into slices E Heat the oven F Add butter to the mixture 8 G Add apples to the mixture H Cook the mixture in the oven I Whip the cream L Garnish Draw the graph (with activities associated to arcs) which represents the project precedences. Determine the earliest times and latest times associated to each node and determine the minimum total completion time of the project. Identify the critical activities and draw Gantt s diagram at earliest.. Shortest paths with negative costs Given the following graph, find the shortest paths between all pairs of nodes, or show that the problem is ill-posed, by exhibiting a circuit of total negative cost. Document prepared by L. Liberti, S. Bosio, S. Coniglio, and C. Iuliano. Translation to English by S. Coniglio
ex-.6-. Foundations of Operations Research Prof. E. Amaldi - - Document prepared by L. Liberti, S. Bosio, S. Coniglio, and C. Iuliano. Translation to English by S. Coniglio
ex-.6-. Foundations of Operations Research Prof. E. Amaldi Solution.6 Shortest paths with nonnegative costs Dijkstra s algorithm determines all the shortest paths from a given node s to all the other nodes in the graph. It can be applied to any (directed) graph with nonnegative edge costs. Data structures: S (subset of nodes with fixed labels), initialized to {s}; L(i) (cost of the shortest path from s to i), initialized to L(s) = ; p(i) (predecessor of node i in the shortest path from s to i), initialized to p(s) = s. Description: at each iteration, from the cut induced by S, we identify an arc (v, h), such that L(v) + c vh is minimum (c vh is the cost of the arc (v,h)). S S {h}, L(h) L(v)+c vh, p(h) v. The algorithm halts when S = V. Iterations: Initialization: S = {},L() =,p() = ; (v,h) = (,), L() =, p() =, S = {,}; (v,h) = (,), L() =, p() =, S = {,,}; (v,h) = (,), L() =, p() =, S = {,,,}; (v,h) = (,), L() =, p() =, S = {,,,,}; (v,h) = (,), L() =, p() =, S = {,,,,,}; (v,h) = (,7), L(7) =, p(7) =, S = {,,,,,,7}; (v,h) = (,6), L(6) =, p(6) =, S = G: STOP; Wecanplotthebehaviourofthealgorithmasfollows. Inthepictures, anodeishighlighted if it is in S, while an arc is represented with a dashed line if it can be selected in the current iteration, and in a solid line when it is chosen. The labels on the nodes in S indicate the cost of the shortest path from node. When a dashed arc is incident to two nodes in S, it is removed. This way, the dashed arcs always belongs to the cut induced by S. The arc (i,j) that is selected is always that with minimum cost L(i)+c ij. : L: : L: L: : L: L: L: : L: L: L: L: Document prepared by L. Liberti, S. Bosio, S. Coniglio, and C. Iuliano. Translation to English by S. Coniglio
ex-.6-. Foundations of Operations Research Prof. E. Amaldi L: L: L: L: L: L: L: : 7: L: L: L: L: L: L: L: L: L: 6: 8: 6 L: 7 L: L: L: L: L: L: L: L: L: A topological order can be introduced as follows. At iteration i, for i = {,...,n}, pick a node with no incident arcs, label it as node i, remove it from the graph, iterate until all nodes are removed, or there is no node with no incident arcs in this case the graph is not acyclic. The graph we are dealing with is acyclic, and its node indices already induce its topological order. The Dynamic Programming technique can therefore be applied. It has complexity O(m), smaller than that of the Dijkstra algorithm of O(n +m). L() =, p() = ; L() = L()+c =, p() = ; L() = min{l()+c,l()+c } = min{+,+} =, p() = ; L() = min{l()+c,l()+c } = min{+,+} =, p() = ; L() = min{l()+c,l()+c } = min{+,+} =, p() = ; L() = min{l()+c,l()+c } = min{+,+} =, p() = ; L(6) = min{l()+c 6,L()+c 6 } = min{+,+} =, p(6) = ; L(7) = min{l()+c 7,L()+c 7,L(6)+c 67 } = min{+,+,+} =,p(7) = ;.7 Shortest paths with negative costs and ill-posedness Since the graph contains negative arc costs, we use Floyd-Warshall s algorithm that finds a shortest path between each pair of nodes, or establishes that the problem is ill-posed by exhibiting a circuit of negative cost. The graph can contain cycles, but such cycles have to be of nonnegative cost. It has complexity O(n ), where n is the number of nodes in the graph. Data structures: let D be a matrix such that element d ij is, at the beginning of the algorithm, the cost of arc (i,j) and, at the end, the cost of a shortest path from i to j. Let P be a matrix of precedecessors (P = (p ij ), where p ij is the precedecessor of node j in the shortest path from i to j). Document prepared by L. Liberti, S. Bosio, S. Coniglio, and C. Iuliano. Translation to English by S. Coniglio
ex-.6-. Foundations of Operations Research Prof. E. Amaldi Description: at each iteration, we select a node with index h n and compute the length of the paths via h for all pairs of nodes (i,j) different from h. If the path from i to j, passing through h, is shorter than that the original one from i to j, matrices D and P are updated: d ij is set to the cost of the path through h, and p ij is updated to p hj. The algorithm halts when one of the following conditions holds () all nodes have been selected for the triangulations () there is a circuit of negative cost: the problem of finding a shortest path (not necessarily simple!) it not well defined. Indeed, it is possible to go through the negative cost circuit an arbitrary number of times, reducing the cost of the path containing such circuit at each iteration. The problem is, evidently, unbounded. (a) Initial configuration D 8 - -7 P In considering all pairs of nodes i,j, including those where i or j equals h, some useless iterations are performed. Indeed, they amount to check whether a path from i to h is shorter than that from i to h plus that from h to itself Document prepared by L. Liberti, S. Bosio, S. Coniglio, and C. Iuliano. Translation to English by S. Coniglio 6
ex-.6-. Foundations of Operations Research Prof. E. Amaldi (b) Iteration h = (triangulation on node ) d +d = 8 > d = d +d = > d = d +d = 8 < d = update d,p d +d = < d = update d,p d +d = 6 > d = d +d = < d = update d,p d +d ij = ( i,j) (c) Iteration h = (triangulation on node ) D 8 8 - -7 P d +d = 8 > d = d +d = 7 < d = 8 update d,p d +d = > d = d +d = 6 > d = d +d = > d = d +d = 9 > d = d +d = < d = update d,p d +d = < d = D 7 8 - - -7 - P update d,p d +d = > d = Document prepared by L. Liberti, S. Bosio, S. Coniglio, and C. Iuliano. Translation to English by S. Coniglio 7
ex-.6-. Foundations of Operations Research Prof. E. Amaldi (d) Iteration h = (triangulation on node ) d +d = > d = d +d = 8 > d = d +d = 8 > d = d +d = < d = update d,p d +d = > d = d +d = < d = 8 update d,p d +d = < d = update d,p d +d = > d = 7 d +d = < d = D 7 - - -7 - - P update d,p We obtain d = < and the algorithm halts: we found a circuit of total negative cost of - ( )..8 An application of Dynamic Programming to machine renewal Consider a directed graph with 6 nodes: nodes from to are associated to the beginning of each year, while node 6 indicates the end of the period of interest. For each pair i,j, i,j =,...,, i < j, arc (i,j) represents the choice of reselling, at the beginning of year j, a machine that was bought at the beginning of year i. The cost c ij is defined as j i c ij = a+ m k r j i, k=i where a is the buying cost of Euros, m k is the maintenance cost for year k, and r j is the price at which the machine is sold. We obtain the following directed acyclic graph 6 7 7 7 7 7 Any path from to 6 corresponds to a plan, with a cost equivalent to that of the path. Document prepared by L. Liberti, S. Bosio, S. Coniglio, and C. Iuliano. Translation to English by S. Coniglio 8
ex-.6-. Foundations of Operations Research Prof. E. Amaldi Therefore, we need to look for a shortest path from to 6. We apply the Dynamic Programming algorithm, obtaining (a) L() = ; (b) L() = 7, p() = ; (c) L() =, p() = ; (d) L() = 9, p() = ; (e) L() =, p() = ; (f) L(6) =, p(6) =. A shortest path (of cost ) is 6. It amounts to buy a new machine every years. Note that there is another optimal solution: 7..9 Project planning The following table indicates the duration of each activity and the related precedence relations: Activities Duration Predecessors A Weight the ingredients - B Meld the butter down A C Mix flour, eggs, and sugar A D Peel and cut the apples into slices A E Heaten the oven - F Add butter to the mixture 8 B,C G Add apples to the mixture D,F H Cook the mixture in the oven E,G I Whip the cream G L Garnish H,I We derive the precedence graph as follows. For each activity, we introduce an arc whose cost is equivalent to the duration of the activity (its two nodes represent the beginning and the end of the activity). For each precedence relation A i < A j, an fictitious arc (i,j) of duration is introduced (dashed lines) between the ending node of A i and the beginning node of j. We add a node s, connected to the beginning node of each activity, with cost, for all activities with no prerequisites. Similarly, we add a node t, and connect to it all the ending nodes for all the activities that are not a prerequisite for any other activities, with cost. B F A C I s D G L t E H Document prepared by L. Liberti, S. Bosio, S. Coniglio, and C. Iuliano. Translation to English by S. Coniglio 9
ex-.6-. Foundations of Operations Research Prof. E. Amaldi The graph can be reduced to a smaller one as shown in the lectures. The following graph is obtained. B F s A C G I E D H L t We use the Critical Path Method (CPM) to determine the ASAP and ALAP values, T min,t max, for each node of the graph. The algorithm is composed of two phases, were the Dynamic Programing technique is applied to find longest and shortest paths in the graph. The algorithm is outlined as follows. (a) T min () = ; (b) sort the nodes in topological order; (c) for each h =,...,n: let T min (h) = max{t min (i)+d ih (i,h) δ (h)}; (d) T max (n) = T min (n); (e) for each h = n,...,: let T max (h) = min{t max (i) d ih (i,h) δ + (h)}. We obtain the following solution [,] [,] [,] s A B F 8 [8,8] [,] C G I [,] D E H [,] [6,6] [67,67] L t The slacks for the activities are (a) σ(a) = T max () T min () d = = Document prepared by L. Liberti, S. Bosio, S. Coniglio, and C. Iuliano. Translation to English by S. Coniglio
ex-.6-. Foundations of Operations Research Prof. E. Amaldi (b) σ(b) = T max () T min () d = = (c) σ(c) = T max () T min () d = = (d) σ(d) = T max () T min () d = 8 = (e) σ(e) = T max (6) T min () d 6 = = (f) σ(f) = T max () T min () d = 8 8 = (g) σ(g) = T max (6) T min () d 6 = 8 = (h) σ(h) = T max (8) T min (7) d 78 = 6 = (i) σ(i) = T max (8) T min (6) d 68 = 6 = (j) σ(i) = T max (9) T min (8) d 89 = 67 6 7 = The critical activities are A,C,F,G,H,L. The ASAP Gantt diagram is I E D B A C F G H L 6 6 67 Document prepared by L. Liberti, S. Bosio, S. Coniglio, and C. Iuliano. Translation to English by S. Coniglio
ex-.6-. Foundations of Operations Research Prof. E. Amaldi. Shortest paths with negative costs. We apply Floyd-Warshall s algorithm. (a) Initial configuration (b) Iteration h = (triangulation on node ) d +d = + = 6 > d = d +d = + = = d = d +d = + = > d = d +d = + = > d = (c) Iteration h = (triangulation on node ) d +d = + = 6 > d = d +d = + = 9 > d = d +d = + = > d = d +d = + = = d = (d) Iteration h = (triangulation on node ) d +d = = > d = d +d = = < d = update d,p d +d = = > d = d +d = = > d = D - - D - - D - - D - - - P P P P Document prepared by L. Liberti, S. Bosio, S. Coniglio, and C. Iuliano. Translation to English by S. Coniglio