Grokking Algorithms



Yüklə 348,95 Kb.
Pdf görüntüsü
səhifə115/122
tarix05.12.2023
ölçüsü348,95 Kb.
#173611
1   ...   111   112   113   114   115   116   117   118   ...   122
grokking-algorithms-illustrated-programmers-curious

answers to exercises


229
6.3
Here’s a small graph of my morning routine.
For these three lists, mark whether each one is valid or invalid.
Answers:
A—Invalid; B—Valid; C—Invalid.
6.4
Here’s a larger graph. Make a valid list for this graph.
 Answer: 
1—Wake up; 2—Exercise; 3—Shower; 4—Brush teeth; 
5—Get dressed; 6—Pack lunch; 7—Eat breakfast.
answers to exercises


230
6.5
Which of the following graphs are also trees?
 Answers:
A—Tree; B—Not a tree; C—Tree. The last example is 
just a sideways tree. Trees are a subset of graphs. So a tree is always 
a graph, but a graph may or may not be a tree.
CHAPTER 7
7.1
In each of these graphs, what is the weight of the shortest path 
from start to finish?
 Answers:
A: A—8; B—60; C—Trick question. No shortest path is 
possible (negative-weight cycle).
answers to exercises


231
CHAPTER 8
8.1
You work for a furniture company, and you have to ship furniture 
all over the country. You need to pack your truck with boxes. All 
the boxes are of different sizes, and you’re trying to maximize 
the space you use in each truck. How would you pick boxes to 
maximize space? Come up with a greedy strategy. Will that give 
you the optimal solution? 
Answer:
A greedy strategy would be to pick the largest box that 
will fit in the remaining space, and repeat until you can’t pack any 
more boxes. No, this won’t give you the optimal solution.
8.2
You’re going to Europe, and you have seven days to see everything 
you can. You assign a point value to each item (how much you 
want to see it) and estimate how long it takes. How can you 
maximize the point total (seeing all the things you really want to 
see) during your stay? Come up with a greedy strategy. Will that 
give you the optimal solution? 
Answer: 
Keep picking the activity with the highest point value that 
you can still do in the time you have left. Stop when you can’t do 
anything else. No, this won’t give you the optimal solution.
For each of these algorithms, say whether it’s a greedy algorithm or not.

Yüklə 348,95 Kb.

Dostları ilə paylaş:
1   ...   111   112   113   114   115   116   117   118   ...   122




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin