Dynamic Programming Min
Dynamic Programming Min
HUFFMAN( )
1 n |C|
2 Q C
3 for i 1 to n 1
4 do allocate a new node z
5 left[z] x EXTRACT-MIN(Q)
6 right[z] y EXTRACT-MIN(Q)
7 f[z] f[x] + f[y]
8 INSERT(Q,Z)
9 return EXTRACT-MIN(Q)
Complexity: O( log )
Dynamic Programming
is typically applied to
optimization problems. In such problem there
can be . Each solution has a
value, and we wish to find with the
optimal value.
The development of a dynamic programming
algorithm can be broken into a sequence of four
steps:
1. Characterize the structure of an optimal solution.
2. Recursively define the value of an optimal solution.
3. Compute the value of an optimal solution in a bottom-up
fashion.
4. Construct an optimal solution from computed information.
11
Assembly-Line Scheduling
An automobile chassis enters each assembly line, has parts
added to it at a number of stations, and a finished auto
exits at the end of the line.
Each assembly line has n stations, numbered j = 1, 2,...,n.
We denote the jth station on line i ( where i is 1 or 2) by Si,j.
The jth station on line 1 (S1,j) performs the same function as
the jth station on line 2 (S2,j).
The stations were built at different times and with different
technologies, however, so that the time required at each
station varies, even between stations at the same position
on the two different lines. We denote the assembly time
required at station Si,j by ai,j.
As the coming figure shows, a chassis enters station 1 of
one of the assembly lines, and it progresses from each
station to the next. There is also an entry time ei for the
chassis to enter assembly line i and an exit time xi for the
completed auto to exit assembly line i.
A Manufacturing Problem:
Find Fast Way Through A Factory
12
A Manufacturing Problem:
Find Fast Way Through A Factory
Normally, once a chassis enters an assembly line, it
passes through that line only. The time to go from
one station to the next within the same assembly
line is negligible.
Occasionally, a special rush order comes in, and
the customer wants the automobile to be
manufactured as quickly as possible.
For the rush orders, the chassis still passes through
the n stations in order, but the factory manager
may switch the partially-completed auto from one
assembly line to the other after any station.
A Manufacturing Problem:
Find Fast Way Through A Factory
The time to transfer a chassis away from assembly
line i after having gone through station Sij is ti,j,
where i = 1, 2 and j = 1, 2, ..., n-1 (since after the
nth station, assembly is complete).
The problem is to determine which stations to
choose from line 1 and which to choose from line 2
in order to minimize the total time through the
factory for one auto.
13
A Manufacturing Problem:
Find Fast Way Through A Factory
14
Step 2: A recursive solution
The fast time is denoted by
f* = min (f1[n]+x1, f2[n]+x2)
where n is the station
e1 a1,1 if• j• 1,
f1[ j ]
min( f1[ j 1] a1, j , f 2 [ j 1] t 2, j 1 a1, j ) if• j• 2
e2 a2,1 if• j• 1,
f2[ j]
min( f 2 [ j 1] a2, j , f1[ j 1] t1, j 1 a2, j ) if• j• 2
15
FASTEST-WAY procedure
FASTEST-WAY(a, t, e, x, n)
1 f1[1] e1 + a1,1 ;
2 f2[1] e2 + a2,1 ;
3 for j 2 to n
4 do if f1[j-1] + a1,j f2[j-1] + t2,j-1 +a1,j
5 then f1[j] f1[j-1] + a1,j ; l1[j] 1 ;
6 else f1[j] f2[j-1] + t2,j-1 +a1,j ; l1[j] 2;
10 if f1[n] + x1 f2[n] + x2
11 then f* = f1[n] + x1 ; l* = 1 ; (n)
12 else f* = f2[n] + x2 ; l* = 2 ;
16