Professional Documents
Culture Documents
MIT6 0002F16 Lec1
MIT6 0002F16 Lec1
D2>>>@1 ?
;3888:(('+ ) *)
,$&"+&("#(8#&"($&#&!'"-(#"
J && -"-(#"A2C
! &(-+(#"$('##!$)((#" #!$ ,(-
! &(-+('#!'!$ #&(!'
D2>>>? ')"(
D2>>>@? @
%## $ )*(* , $)
&# !'('
J &#&!!"$&# !''"(#
J
!$&#*-#)&$&#&!!"' '
J $-#) &"(#"$() !(&
"&,&''
J &-'! $&#&!!"$&# !'
'"(# $-#) &"'"
$&#&!!"#"$(
"''"!"('"(,(##
J "#(&(#""!#&( '
#)(!(& #*&- ()&'
"$&# !'('
,!1'#"#*
D2>>>@? A
%-%)
*%#&(*%;388891
&#&!!"''"!"('('&
J #)'!#&#"($&# !(#'# *("#"
$&#&!!"
()&#"("(!#&'(&(
()&'+ ('(&$
''#)( &""(#$&#&!0!#&#)($$"
-#)&(#"(#('"
D2>>>@? B
%$ $%+((%(## $! "")
+(#" ('#-(#"
#(+&""&"
'"$'
#+#-#)((#&" /
D2>>>@? C
%#&+** %$"%")
'"#!$)((#"(# $)"&'("(+#& "
++ *
Experimental devices that help us to understand
something that has happened or to predict the future
Images © sources unknown. All rights reserved. This content is excluded from our Creative
D2>>>@? E
%#&+** %$"%")
'"#!$)((#"(# $)"&'("(+#& "
++ *
Experimental devices that help us to understand
something that has happened or to predict the future
Images © sources unknown. All rights reserved. This content is excluded from our Creative
Commons license. For more information, see https://ocw.mit.edu/help/faq-fair-use.
Images © sources unknown. All rights reserved. This content is excluded from our Creative
Commons license. For more information, see https://ocw.mit.edu/help/faq-fair-use.
D2>>>@? G
$&)!(%"#)
Images © sources unknown. All rights reserved. This content is excluded from our Creative
Commons license. For more information, see https://ocw.mit.edu/help/faq-fair-use.
D2>>>@? ?>
$&)!(%"#
#)* !('(&"(0'#(&'!,!)!
+("$'((-#)"&&-
#)+#) (#(!#&'()("-#)"&&-
#+#-#)##'+'()(#("+(#
*"/
+#*&"('
J >7?"$'$&# !
J #"(")#)'#&&(#" "$'$&# !
Images © sources unknown. All rights reserved. This content is excluded from our Creative
Commons license. For more information, see https://ocw.mit.edu/help/faq-fair-use.
D2>>>@? ??
/
)*6,%( *$&)!(%"#
Images © sources unknown. All rights reserved. This content is excluded from our Creative
Commons license. For more information, see https://ocw.mit.edu/help/faq-fair-use.
D2>>>@? ?@
859$&)!(%"#2%(#" 0
(!'&$&'"(-$&0
"$'"#!!#((!'+((#(
+(#"#!#&("
*(#&00# "(0&$&'"('('(#
* (!'2 !"(#(*(#&'"
(!
*(#&00# "(0')'(#"(+(&
#&"#((!'&("2
0(! '("2
0(! '"#(("
D2>>>@? ?A
859$&)!(%"#2%(#" 0
D2>>>@? ?B
(+*%("%( *#
?2")!&( $#'' #!"(#"'#(!'2(
'(#'-0"&( ')'('#('(#(!'2''
($#+&'(2
@2!#* #(#!"(#"'+#'(#( )"('
,'( #++(2
A2&#!(&!""#!"(#"'##'"-#"
+#'* )'( &'(2
D2>>>@? ?C
*$%*(* "
#+'$#+&'(/
J *(#&00# "(0')'(#"(+(&#&"#(
(!'&("2
0(! '("2
0(!
'"#(("
#+!"-$#'' &"(* )'" */
J '!"-&"("&-")!&''"&$&'"(
" ('
#&,!$ 0(&&?>>(!'(###'&#!0(
$#+&'('#'./
J ?0@DE0DC>0D>>0@@F0@@G0B>?0BGD0E>A0@>C0AED
D2>>>@? ?D
(+)* $*+& 1
'0"#
>7?"$'$&# !'"&"( -,$#""(
)(#"4('$&
D2>>>@? ?E
(/"%( *#(* ""*($* ,
while knapsack not full
put “best” available item in knapsack
)(+(#''(!"/
J #'(* )
J '(,$"'*
J '(* )7)"('
D2>>>@? ?F
$.#&"
#)&#)((#'(#+"(#
!
#)"#+#+!)-#)
* )&"(##'0220
-#) #")('!#&("
$$ '
)(-#)* #&
)(0220-#)#"4(+"(
(##"')!!#&("EC>
#&'
##'"+((#('
"$'$&# !
D2>>>@? ?G
$+
(4' ##($&#&!((+")'(#+(
(##&&
def getValue(self):
return self.value
def getCost(self):
return self.calories
def density(self):
return self.getValue()/self.getCost()
def __str__(self):
return self.name + ': <' + str(self.value)\
+ ', ' + str(self.calories) + '>'
D2>>>@? @?
+ "$+%%%)
def buildMenu(names, values, calories):
"""names, values, calories lists of same length.
name a list of strings
values and calories lists of numbers
returns list of Foods"""
menu = []
for i in range(len(values)):
menu.append(Food(names[i], values[i],
calories[i]))
return menu
D2>>>@? @@
#&"#$** %$%". "(/
def greedy(items, maxCost, keyFunction):
"""Assumes items a list, maxCost >= 0,
keyFunction maps elements of items to numbers"""
itemsCopy = sorted(items, key = keyFunction,
reverse = True)
result = []
totalValue, totalCost = 0.0, 0.0
for i in range(len(itemsCopy)):
if (totalCost+itemsCopy[i].getCost()) <= maxCost:
result.append(itemsCopy[i])
totalCost += itemsCopy[i].getCost()
totalValue += itemsCopy[i].getValue()
D2>>>@? @A
"%( *# $/
def greedy(items, maxCost, keyFunction):
itemsCopy = sorted(items, key = keyFunction,
reverse = True)
result = []
totalValue, totalCost = 0.0, 0.0
for i in range(len(itemsCopy)):
if (totalCost+itemsCopy[i].getCost()) <= maxCost:
result.append(itemsCopy[i])
totalCost += itemsCopy[i].getCost()
totalValue += itemsCopy[i].getValue()
D2>>>@? @B
) $(/
def testGreedy(items, constraint, keyFunction):
taken, val = greedy(items, constraint, keyFunction)
print('Total value of items taken =', val)
for item in taken:
print(' ', item)
D2>>>@? @C
) $(/
def testGreedys(maxUnits):
print('Use greedy by value to allocate', maxUnits,
'calories')
testGreedy(foods, maxUnits, Food.getValue)
print('\nUse greedy by cost to allocate', maxUnits,
'calories')
testGreedy(foods, maxUnits,
lambda x: 1/Food.getCost(x))
print('\nUse greedy by density to allocate', maxUnits,
'calories')
testGreedy(foods, maxUnits, Food.density)
testGreedys(800) /
D2>>>@? @D
"#
!)'(#&("#"-!#)')"(#"'
J lambda H?0@03"I1H,$&''#"I
J ()&"')"(#"#"&)!"('
"*&-"-0'&
#'' (#+&(!."#!$ ( !,$&''#"'
#"4(9)'def "'(
D2>>>@? @E
) $(/
def testGreedys(foods, maxUnits):
print('Use greedy by value to allocate', maxUnits,
'calories')
testGreedy(foods, maxUnits, Food.getValue)
print('\nUse greedy by cost to allocate', maxUnits,
'calories')
testGreedy(foods, maxUnits,
lambda x: 1/Food.getCost(x))
print('\nUse greedy by density to allocate', maxUnits,
'calories')
testGreedy(foods, maxUnits, Food.density)
'&--"'(- +-'+""&/
J &-('(&-':##'0?>>>;
D2>>>@? @G
(%)$%$)%(/
'-(#!$ !"(
#!$)((#" -"(
For information about citing these materials or our Terms of Use, visit: https://ocw.mit.edu/terms.