Homework6_Solutions
pdf
keyboard_arrow_up
School
University of Texas *
*We aren’t endorsed by this school
Course
360C
Subject
Computer Science
Date
Dec 6, 2023
Type
Pages
8
Uploaded by ConstableGiraffeMaster829
ECE360C: Algorithms
Homework Assignment #6
University of Texas at Austin
Due: October 13, 2023 (11:59pm)
SOLUTIONS
STUDENT CHOICE:
Homework Assignment #6
– Solutions
Solutions are for individual use only by students registered for ECE 360C in Spring
2023 semester. They should not be otherwise shared or posted.
Problem 1: Remember [20 points]
Answer the following true/false questions.
True
False
□
⊠
In the interval scheduling problem, the greedy choice that considers the
intervals in decreasing order by end time (rather than increasing order by
end time)
does
result in an optimal algorithm.
⊠
□
In Dijkstra’s algorithm, once you remove a node from the queue (because
it has the current minimum distance estimate), you are guaranteed to know
the shortest path from the source to that node.
□
⊠
Dijkstra’s algorithm computes the shortest cost path from every vertex in a
graph to every other vertex in a graph.
□
⊠
Dijkstra’s algorithm will work, without modification, on a graph that has
negative weight edges, as long as there are no negative weight cycles.
□
⊠
For a dense graph (when
m >> n
), the running time of Prim’s algo-
rithm (which is
O
(
m
log
n
)) is asymptotically faster than Kruskal’s algorithm
(which is
O
(
m
log
m
)).
⊠
□
In a graph where each edge has a unique weight, there is only a single
minimum spanning tree.
□
⊠
In the Interval Scheduling Problem we discussed in class (where our goal
is to schedule the maximum number of compatible jobs), the job with the
earliest finishing time will be present in
every
optimal solution.
⊠
□
If all edges in a graph have equal weight, then the shortest path between two
nodes is the path with the least number of edges between those two nodes.
□
⊠
Let
T
be the MST of a graph
G
, then the path from
u
to
v
in
T
is the
shortest path from
u
to
v
in
G
.
□
⊠
The first step of every greedy algorithm is to sort the input in some way
using some metric.
Homework Assignment #6: October 13, 2023 (11:59pm)
2
Problem 2: Understand
Consider the problem of matching a set of available skis to a set of skiers. The input consists of
n
skiers with heights
p
1
, . . . p
n
and
n
sets of skis with lengths
s
1
, . . . s
n
. The problem is to assign
each skier a ski to minimize the average difference between the height of a skier and the length of
his or her assigned set of skis. That is, if the
i
th
skier is given the
α
(
i
)
th
pair of skis, then you want
to minimize:
1
n
n
X
i
=1
|
p
i
−
s
α
(
i
)
|
Consider the following greedy algorithm:
Find the difference between the height of each skier and the length of each set of skis.
Choose the smallest
difference
first, and assign those skis to that skier.
Continue to
make the next greediest choice until all skiers have been assigned skis
Prove (or disprove) that this greedy choice is optimal:
Solution
This choice is not optimal.
Consider the following example: skiers:
{
1
,
6
}
, skis:
{
5
,
10
}
.
This choice would pair (6
,
5) and (1
,
10) for an average difference of 6.5 vs.
the optimal
average difference of 4.
Homework Assignment #6: October 13, 2023 (11:59pm)
3
Problem 3: Apply [20 points]
Increasingly, in remote rural areas of the world, it is becoming beneficial to create “mini-grids” to
provide local electrical access rather than relying on expensive connections to a national grid
1
. In
this problem, we’ll explore the use of graph algorithms to optimize the deployment of a minigrid
in a village.
Consider a village that has
n
homes that need access to the minigrid.
Locations have already
been determined for the installation of
k
solar generation stations, spread around the village. It
is possible to connect each home to one or more generation stations, but not every home can be
connected to every station (e.g., because of distance or other physical obstructions). For simplicity,
assume that the cost of connecting a home to a generation station, if it is possible, is exactly
proportional to the distance between the two. We will create a graph in which both homes and
generation stations are vertices (i.e., the total number of vertices is
n
+
k
). An edge exists between
a home and a generation station if they can be connected; such an edge has a weight equivalent to
the distance between the two. (Note: the graph is bipartite – there are no edges between generation
stations and there are no edges between houses.)
(a)
Give an efficient algorithm to find the minimum cost to install a minigrid in the village such
that every home is connected to exactly one generation station.
Your algorithm must be
efficient, but you do not have to spend time searching for an algorithm that is asymptotically
optimal. However the solution (i.e., the total cost of all the wire you need to run to connect
all of the homes to stations) must be optimal.
Connect(G)
1
sum = 0
2
for
each home:
3
do
home.minconnection = infinity
4
for
each station:
5
do if
edge exists and length
<
minconnection
6
then
minconnection = length
7
sum += home.minconnection
8
if
sum = infinity
9
then
10
return false
11
else
12
return sum
Alternate:
As an alternative, compute the MST of the graph (e.g., using Prim’s algorithm). This MST
is connected, but this means that it is likely that some homes are connected to more than
one generation station.
This is not necessary.
So for each home, revisit it’s links in the
MST. remove all but the lowest cost one. This has a larger runtime
1
https://microgridnews.com/improving-energy-access-in-rural-africa-depends-on-renewable-energy-microgrids/
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
Homework Assignment #6: October 13, 2023 (11:59pm)
4
(b)
Give the runtime of your algorithm and briefly justify it.
O
(
nk
) or
O
(
m
), because iterating over every station for every home, in other words
iterating over all edges
Alternate:
The runtime of Prim’s algorithm is
O
(
m
log(
n
+
k
)).
Then we have to go through each
home (there are
n
of them), doing
O
(
m
) work,
in aggregate
. So the total running time is
O
(
m
log(
n
+
k
)) +
O
(
m
+
n
), which is upperbounded by the runtime of Prim’s.
Working solution:
(c)
Suppose there is ultimately only the ability to install
k
−
1 stations in the village.
These
stations must be installed at the
k
predetermined locations considered above, but, in the
end, one location will be without a station. Assuming the same home-to-station connectivity
constraints as above, give an efficient algorithm that determines whether
k
−
1 stations are
sufficient to connect all of the homes to electricity and, if it is possible, outputs the optimal
connectivity graph (i.e., the graph that connects all homes to stations with the least amount
of wire). Again, your algorithm does not have to be asymptotically optimal, but it must be
efficient and it must result in the optimal connectivity graph.
Run algorithm from part a)
k
times.
Each time, remove a different station.
Return the
lowest cost station removal, or return false if it cannot be done.
(d)
Give the runtime of your algorithm and briefly justify it.
O
(
nk
2
) or
O
(
mk
). We execute algorithm from part a), which is
O
(
m
), and we do it
k
times.
Alternate:
O
(
km
log(
n
+
k
))
.
Homework Assignment #6: October 13, 2023 (11:59pm)
5
Problem 4: Analyze
Consider a situation where you have to find available classrooms for
n
different lectures. Of course,
you must avoid scheduling two or more overlapping lecture in the same room. Each lecture
i
begins
at
s
i
and ends at
t
i
.
(a)
Find an algorithm that assigns the smallest number of rooms possible.
Solution
The greedy choice we will make to solve this problem is to schedule the earliest compatible
classes first.
That is to say, we will take the earliest class that has not been scheduled,
assign it to a new classroom. Then we will take the earliest class that doesn’t overlap with
this class, and assign it to the same classroom. We will repeat this until no more classes
can be assigned to this room, then we will create a new room, and repeat the process.
We will sort the list of classes,
c
by starting time before running the following algorithm on
it.
GreedyRooms
(
c
)
1
d
←
0 (number of allocated classrooms)
2
for
j = 1 to n
3
do if
class j is compatible with a classroom, k
4
then
schedule j in classroom k
5
update the finish time of classroom k
6
else
allocate a new classroom d + 1
7
schedule class j in classroom d + 1
8
d
←
d + 1
Each classroom k maintains the finish time of the last job added.
Classrooms are kept in a priority queue.
The running time of this algorithm is
O
(
n
log(
n
))
(b)
Show that your algorithm is optimal.
Solution
In our algorithm, we start a new classroom
d
because we need to schedule a job
j
that is
incompatible with all other classrooms 1
...d
−
1. Because we chose to schedule greedily by
start time, the incompatibilities in classrooms 1
...d
−
1 are caused by lectures with start
times
≤
s
j
. Thus, there are
d
lectures that overlap, and we would need
≥
d
classrooms to
schedule all of them.
Homework Assignment #6: October 13, 2023 (11:59pm)
6
Problem 5: Evaluate [20 points]
Consider the problem of making change for
n
cents using the fewest number of coins. Assume that
each coin’s value is an integer.
(a)
Describe a greedy algorithm to make change consisting of quarters, dimes, nickels, and pennies.
Prove that your algorithm yields an optimal solution.
Solution
Select the largest coin denomination that is less than
n
. Let this denomination be
c
k
. Use
one of these coins. Repeat for
n
−
c
k
cents.
Proof that the greedy algorithm is optimal
: At a step of the algorithm, we say a
particular choice of coin is safe if it preserves the invariant that the coins chosen so far form
a subset of an optimal solution. We need to show that every choice the greedy algorithm
makes is safe, which will then guarantee that the final set of coins will be an optimal solution.
(Similar proof strategy as in the MST algorithms.)
We will argue that an optimal solution for
n
≥
25 must include at least 1 quarter. Thus
the greedy choice in this case is safe.
Similarly, we’ll argue that an optimal solution for
10
≤
n <
25 must include at least one dime, and that an optimal solution for 5
≤
n <
10
must include at least one nickel, implying that the greedy choice in every case is safe.
Note the following properties of optimal solutions: No optimal solution can include 5 pen-
nies. Similarly, no optimal solution can include 2 nickels, or 1 nickel and 2 dimes. (These
can be replaced with 1 nickel, 1 dime, and 1 quarter, respectively.) Finally, we note that
no optimal solution can include 3 dimes because this can be replaced with 1 quarter and 1
nickel. This implies that the largest optimal solution not including quarters is for
n
= 24 (2
dimes + 4 pennies). Thus an optimal solution for
n
≥
25 must include at least 1 quarter.
Similarly, the largest optimal solution not including quarters or dimes is for
n
= 9 (1 nickel
+ 4 pennies). Thus an optimal solution for 10
≤
n <
25 must include at least one dime.
Finally, the largest optimal solution not including quarters, dimes, or nickels is for
n
= 4
(4 pennies). Thus an optimal solution for 5
≤
n <
10 must include at least one nickel.
(b)
Give a set of coin denominations for which your greedy algorithm does not yield an optimal
solution. Your set should include a penny to ensure that you can always successfully make
change.
Solution
One example is a scheme with a 1 cent coin, a 6 cent coin, and an 8 cent coin. To make 12
cents, the greedy algorithm would use 5 coins, while the optimal algorithm would use 2.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
Homework Assignment #6: October 13, 2023 (11:59pm)
7
Problem 6: Create [20 points]
A thief is given the choice of
n
objects to steal, but only has one knapsack with a capacity of taking
M
weight. Each object
i
has weight
w
i
, and profit
p
i
.
(a) First, suppose that the objects are divisible (e.g., the thief is in a cheese shop and the items
are rolls of cheese that can be cut). For each object
i
, if a fraction
x
i
, 0
≤
x
i
≤
1 (taking
x
i
= 1 would be taking the entire object) is placed in the knapsack, then the profit earned
is
p
i
x
i
. Come up with an efficient greedy algorithm for maximizing the profit of the thief.
Prove the correctness and running time.
Solution
Efficient Greedy Algorithm.
If all objects fit in the knapsack, take them all. Otherwise:
∀
i
, set
x
i
= 0. Index the objects as 1
, ..., n
s.t. for any two objects with indices
i
and
j
respectively,
i < j
⇐⇒
p
i
/w
i
≥
p
j
/w
j
, i.e.
the items are sorted in decreasing order of
the ratio
p
i
/w
i
.
Let
k
be the maximum non-negative integer s.t.
∑
k
i
=1
w
i
≤
M
.
Then,
∀
i
∈ {
1
,
2
, .., k
}
, set
x
i
= 1.
Finally, if there is still space in the knapsack, i.e.
if
M
−
∑
k
i
=1
w
i
=
α >
0, set
x
k
+1
=
a/w
k
+1
(i.e. fill the remaining space with the largest possible
quantity of object
k
+ 1).
Correctness.
The key argument for the algorithm’s correctness is that an optimal solution
would have to take as much quantity as possible of object 1 (i.e. the object
i
with the highest
p
i
/w
i
ratio). We can prove this by contradiction: Assume that
∃
O
=
{
x
1
=
o
1
, ..., x
n
=
o
n
}
s.t.
O
is an optimal solution with value
P
(
O
) and
O
doesn’t take the greatest amount of
object 1 possible, i.e.
o
1
< min
{
1
, M/w
i
}
.
There are two possible cases for solution
O
.
Either the knapsack is full or it isn’t. If it isn’t, we can increase the amount of item
i
that
we take, and thus have a solution of value greater than that of
O
. Since we assumed that
O
is optimal, this is a contradiction. If the knapsack is full,
∃
j
̸
= 1 s.t.
o
j
>
0. By removing
ϵ
weight of item
j
, s.t.
ϵ
∈
(0
, min
{
w
1
−
o
1
, w
j
∗
o
j
}
], and adding
ϵ
weight of item 1 in its place,
we get a solution that has value
P
(
O
)
−
ϵ
∗
p
j
/w
j
+
ϵ
∗
p
2
/w
2
=
P
(
O
)+
ϵ
∗
(
p
2
/w
2
−
p
j
/w
j
)
>
P
(
O
). Since we assumed that
O
is optimal, this again is a contradiction.
Applying this argument recursively, we get that our greedy algorithm outputs an optimal
solution.
Running Time.
Sorting takes
O
(
n
log
n
) time.
Iterating through our list of objects
to figure out how many of them can be added to the solution takes
O
(
n
) time.
Total
O
(
n
log
n
).
Homework Assignment #6: October 13, 2023 (11:59pm)
8
(b) Now, suppose the items cannot be taken fractionally.
In other words, the thief can either
take an entire item or leave it behind. Suppose we also know the following: the order of these
items when sorted by increasing weight is the same as their order when sorted by decreasing
value. Give a greedy algorithm to find an optimal solution to this variant of the knapsack
problem. Prove the correctness and running time.
Solution
Very similar to part (a).
Efficient Greedy Algorithm.
If all objects fit in the knapsack, take them all. Otherwise:
∀
i
, set
x
i
= 0.
Index the objects as 1
, ..., n
s.t.
for any two objects with indices
i
and
j
respectively,
i < j
⇐⇒
p
i
≥
p
j
.
Let
k
be the maximum non-negative integer s.t.
∑
k
i
=1
w
i
≤
M
. Then,
∀
i
∈ {
1
,
2
, .., k
}
, set
x
i
= 1.
Correctness.
Proof by contradiction. Assume there is an optimal solution
O
that does
not include the first
k
objects. Let
i
∈
1
, ..., k
be an object that is not included in
O
. There
either is an item
j > i
in
O
, or then isn’t. If there isn’t, we can add
i
to the knapsack and
get a higher value than that of
O
. This is a contradiction. If there is such an item
j
, we can
remove
j
and add
i
in its place, since
w
j
≥
w
i
, and get a solution with a higher value than
that of
O
, since
v
j
≤
v
i
. Again, this is a contradiction to our initial assumption. Hence,
the optimal solution must include the first
k
objects, so our algorithm is correct.
Running Time.
Sorting takes
O
(
n
log
n
) time.
Iterating through our list of objects
to figure out how many of them can be added to the solution takes
O
(
n
) time.
Total
O
(
n
log
n
).
Recommended textbooks for you

Operations Research : Applications and Algorithms
Computer Science
ISBN:9780534380588
Author:Wayne L. Winston
Publisher:Brooks Cole

C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning

C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Np Ms Office 365/Excel 2016 I Ntermed
Computer Science
ISBN:9781337508841
Author:Carey
Publisher:Cengage
COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
Computer Science
ISBN:9780357392676
Author:FREUND, Steven
Publisher:CENGAGE L
Recommended textbooks for you
- Operations Research : Applications and AlgorithmsComputer ScienceISBN:9780534380588Author:Wayne L. WinstonPublisher:Brooks ColeC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr
- Programming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengageNp Ms Office 365/Excel 2016 I NtermedComputer ScienceISBN:9781337508841Author:CareyPublisher:CengageCOMPREHENSIVE MICROSOFT OFFICE 365 EXCEComputer ScienceISBN:9780357392676Author:FREUND, StevenPublisher:CENGAGE L

Operations Research : Applications and Algorithms
Computer Science
ISBN:9780534380588
Author:Wayne L. Winston
Publisher:Brooks Cole

C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning

C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Np Ms Office 365/Excel 2016 I Ntermed
Computer Science
ISBN:9781337508841
Author:Carey
Publisher:Cengage
COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
Computer Science
ISBN:9780357392676
Author:FREUND, Steven
Publisher:CENGAGE L