Вы находитесь на странице: 1из 6

# Constructing a Huffman code

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

## Step1: The structure of the fastest

way through the factory
the fast way through station S1,j is either
the fastest way through Station S1,j-1 and then directly through
station S1,j, or
the fastest way through station S2,j-1, a transfer from line 2 to
line 1, and then through station S1,j.
Using symmetric reasoning, the fastest way through
station S2,j is either
the fastest way through station S2,j-1 and then directly through
Station S2,j, or
the fastest way through station S1,j-1, a transfer from line 1 to
line 2, and then through Station S2,j.

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

## Step 3: Computing the fastest times

Simple method of recursive algorithm for above
equations that running time is exponential in n:
Let ri(j) be the number of references made to fi[j] in a recursive algorithm.
r1(n) = r2(n) = 1
r1(j) = r2(j) = r1(j+1) + r2(j+1) for j=1, 2, , n-1
Since ri(j)= 2n-j, thus f1[1]=2n-1
The total number of references to all fi[j] values is (2n).
We can do much better if we compute the fi[j] values in
different order from the recursive way.
Observe that for j 2, each value of fi[j] depends only on the values of
f1[j-1] and f2[j-1].

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;

## 7 if f2[j-1] + a2, j f1[j-1] + t1,j-1 +a2,j

8 then f2[j] f2[j 1] + a2,j ; l2[j] 2
9 else f2[j] f1[j 1] + t1,j-1 + a2,j ; l2[j] 1;

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 ;

## Step 4: Constructing the fastest way

through the factory
output
PRINT-STATIONS(l, n)
1 i l* line 1, station 6
2 print line i ,station n line 2, station 5
3 for j n downto 2 line 2, station 4
4 do i li[j] line 1, station 3
5 print line i ,station j 1 line 2, station 2
line 1, station 1
l* = 1, use station S1,6
l1[6] = 2, use station S2,5
l2[5] = 2, use station S2,4
l2[4] = 1, use station S1,3
l1[3] = 2, use station S2,2
l2[2] = 1, use station S1,1

16

## Нижнее меню

### Получите наши бесплатные приложения

Авторское право © 2021 Scribd Inc.