Solution 8 (Dynamic Programming) Exercise 1 ... - ETH Zürich
Solution 8 (Dynamic Programming) Exercise 1 ... - ETH Zürich
Solution 8 (Dynamic Programming) Exercise 1 ... - ETH Zürich
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
<strong>Exercise</strong>s<br />
Informatik II for D-MAVT<br />
Sommersemester 2007<br />
<strong>Solution</strong> 8<br />
(<strong>Dynamic</strong> <strong>Programming</strong>)<br />
Institute for Computational Science<br />
Dept. of Computer Science, <strong>ETH</strong> Zürich<br />
Prof. Dr. Joachim M. Buhmann,<br />
Prof. Dr. Ivo Sbalzarini, Dr. Bernd Fischer<br />
Tel. +41-1-632 6527<br />
E-Mail {jbuhmann,ivos,bernd.fischer}@inf.ethz.ch<br />
Web http://people.inf.ethz.ch/befische/info2<br />
<strong>Exercise</strong> 1 (Dijkstra’s Shortest Path Algorithm)<br />
The solution drawn in graphs:<br />
b<br />
b<br />
26<br />
96<br />
26<br />
96<br />
a<br />
0<br />
26<br />
26<br />
26<br />
33<br />
e<br />
65<br />
5<br />
21<br />
d<br />
a<br />
0<br />
26<br />
26<br />
26<br />
33<br />
e 52<br />
65<br />
5<br />
21<br />
91<br />
d<br />
50<br />
50<br />
17<br />
26<br />
25<br />
33<br />
25<br />
f<br />
50<br />
50<br />
17<br />
26<br />
25<br />
33<br />
25<br />
f<br />
50<br />
c<br />
b<br />
33<br />
50<br />
c<br />
b<br />
33<br />
26<br />
96<br />
26<br />
96<br />
a<br />
0<br />
26<br />
26<br />
26<br />
33<br />
e 52<br />
65<br />
5<br />
21<br />
91<br />
d<br />
a<br />
0<br />
26<br />
26<br />
26<br />
33<br />
e 52<br />
65<br />
5<br />
21<br />
73<br />
d<br />
50<br />
50<br />
17<br />
26<br />
33<br />
25<br />
25<br />
83<br />
f<br />
50<br />
50<br />
17<br />
26<br />
33<br />
25<br />
25<br />
77<br />
f<br />
50<br />
c<br />
33<br />
50<br />
c<br />
33
b<br />
26<br />
96<br />
26<br />
96<br />
a<br />
0<br />
26<br />
26<br />
26<br />
33<br />
e 52<br />
65<br />
5<br />
21<br />
73<br />
d<br />
a<br />
0<br />
26<br />
26<br />
26<br />
33<br />
e 52<br />
65<br />
5<br />
21<br />
73<br />
d<br />
50<br />
50<br />
17<br />
26<br />
33<br />
25<br />
25<br />
77<br />
f<br />
50<br />
50<br />
17<br />
26<br />
33<br />
25<br />
25<br />
77<br />
f<br />
50<br />
c<br />
33<br />
50<br />
c<br />
33<br />
The solution in a table:<br />
node a b c d e f<br />
S X<br />
costs 0 26 50<br />
bp - a a<br />
S X X<br />
costs 0 26 50 91 52<br />
bp - a a b b<br />
S X X X<br />
costs 0 26 50 91 52 83<br />
bp - a a b b c<br />
S X X X X<br />
costs 0 26 50 73 52 77<br />
bp - a a e b e<br />
S X X X X X<br />
costs 0 26 50 73 52 77<br />
bp - a a e b e<br />
S X X X X X X<br />
costs 0 26 50 73 52 77<br />
bp - a a e b e<br />
The following drawing shows the trace back path for the shortest path of node f.<br />
node<br />
a<br />
b c d e<br />
f<br />
bp<br />
−<br />
a a e b<br />
e<br />
The shortest path is thus<br />
a → b → e → f<br />
<strong>Exercise</strong> 2 (<strong>Dynamic</strong> Programing and Recursive Functions)<br />
1. A recursive definition of the shortest path problem would be as follows: Given a graph G with vertices<br />
V = 1,...,n and edges E ⊂ V × V with weights w ij , the shortest path S(s,t) from s to t is defined<br />
by the recursive function R(s,t,A) as<br />
S(s,t) = R (s,t,V \ {t}) (1)<br />
R(s,t,A) = min<br />
i∈S {R(s,i,A \ {t}) + w it} (2)<br />
2
2. To compute the runtime, we define a helper function c(n), that is the cost for the computation of the<br />
shortest path in a graph with n nodes. c(n) is defined recursively as well.<br />
c(n) = O ((n − 1) · c(n − 1)) (3)<br />
The set S has a size of at most n − 1. For every element in S we have to solve one shortest path<br />
problem on a graph with n − 1 nodes.<br />
Thus the runtime of the algorithm is O((n − 1)!).<br />
<strong>Exercise</strong> 3 (List Scheduling)<br />
0001 function a=listscheduling(t,m)<br />
0002<br />
0003 % The number of jobs is n<br />
0004 n = size(t,2);<br />
0005<br />
0006 % The vector machineload stores the load of the machines. In the<br />
0007 % beginning all machines are idle.<br />
0008 machineload(1:m) = 0.0;<br />
0009<br />
0010 % Assign the jobs to the machines in the order they are written in the<br />
0011 % input vector<br />
0012 for i=1:n<br />
0013<br />
0014 % Find the machine that is minimal loaded up to now<br />
0015 [M,I] = min(machineload);<br />
0016<br />
0017 % I(1) now contains the index of the machine that is minimal loaded<br />
0018 a(i) = I(1);<br />
0019<br />
0020 % Update the machine load.<br />
0021 machineload(I(1)) = machineload(I(1)) + t(i);<br />
0022 end;<br />
0023<br />
3