0% found this document useful (0 votes)
24 views

Tut 7

Uploaded by

pedanticwiles
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Tut 7

Uploaded by

pedanticwiles
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

COL351: Analysis and Design of Algorithms Fall 2024

Tutorial Sheet 7
Announced on: Sept 07 (Sat)

Problems marked with (⋆) will not be asked in the tutorial quiz.

1. This problem describes the cut property of minimum spanning trees (MSTs).
Let G = (V, E) be a connected and weighted graph where the edge costs may not be distinct.
For any subset S ⊆ V of the vertices, let S̄ := V \ S denote the vertices not in S. Let E(S, S̄)
denote the set of all edges with one endpoint each in S and S̄. Show that if there exists a
unique edge e ∈ E(S, S̄) with the smallest weight among the edges in E(S, S̄), then e must
belong to every MST of graph G.

Proof by Contradiction: Assume that there exists a subset S ∈ V such that there exists
a unique edge e ∈ E(S, S̄) with the smallest weight among the edges in E(S, S̄) and e is
not part of an MST T of G.
Now, if we add e to the MST, a unique cycle C gets created. By double crossing lemma,
there is another edge f ∈ C such that f ∈ E(S, S̄). Since e was the unique edge of
minimum weight crossing the cut (S, S̄), we know that weight of f is more than weight
of e. So, we can remove f and keep e which gives a lower weight spanning tree. But
this is a contradiction because we supposedly started with a MST, and now we have a
collection of edges which is a spanning tree that weighs less. Thus, the original MST
was not actually minimal.

2. Consider the following alleged proof of the cut property from Problem 1.
Let T be a minimum spanning tree that does not contain e. Since T is a spanning tree, it must contain
an edge f with one end in S and the other end in S. Since e is the cheapest edge with this property, we
have ce < c f , and hence T \ { f } ∪ {e} is a spanning tree that is cheaper than T.
Is this proof correct? If not, then explain the error.

The proof is incorrect.

Intuitive Idea: On a high level, as mentioned in the Proof of Ques. 1, while adding
the edge e, we removed the edge which was a crossing edge of the cut (S, S̄) as well as
part of the cycle induced in T ∪ {e}. Whereas in the proof given in Ques. 2, we remove
any edge crossing the cut (S, S̄), not necessarily part of a cycle. Because of this, the
resulting graph might not be a spanning tree.

1
Tutorial Sheet 7:

Figure 1: Counter example graph, the solid edges denote the edges of the spanning
tree T, and dashed edges denote the edges of the graph not present in T.

As a counterexample, consider the (weighted) graph shown. consider the S = { a, b, d}


and S̄ = V \ S = {c}. The edge weights are such that the edge e is the unique
minimum weight edge across the cut (S, S̄) Now, f is an edge with one end in S and
other edge S̄. But, it can be seen that T \ { f } ∪ {e} is not a spanning tree and hence,
the proof is flawed.

• Written ”I do not know how to approach this problem” - 0.6 points

• – Claiming that the proof is incorrect - 1 point


– Mentioning the error that f also needs to be part of the cycle formed in
T ∪ {e} (just giving a counterexample for the proof also works ) - 2 points

3. Suppose you are given a connected weighted graph G = (V, E) with a distinguished vertex s
where all edge costs are positive and distinct. Is it possible for a tree of shortest paths from
s and a minimum spanning tree in G to not share any edges? If so, give an example. If not,
give a reason.

Consider the shortest path tree TD generated by taking this distinguished vertex s as
the source node. Let e be the minimum weight outgoing edge in TD from s to some
neighboring vertex, say t.

Claim 1. The edge e is part of every MST of G.

Proof. When e is removed from TD , it gives rise to two connected components. Let
S and S̄ denote the mutually exclusive non-empty vertex sets of these connected
components. Thus, e becomes the minimum cost crossing edge between S and S̄. As
discussed in Question 1, edge e should be part of every MST of G.

So, we have shown that at least one edge is common between TD and any MST of G.

2
Tutorial Sheet 7:

4. Consider a connected undirected graph G with distinct edge costs. Design a linear-time
algorithm to find a minimum bottleneck spanning tree, which is a spanning tree T that minimizes
the maximum edge cost maxe∈T ce . You can assume access to a subroutine FindMedian that,
given as input an unsorted list of n numbers, computes the median in O(n) time.
For the tutorial quiz, you can provide a plain English description of the algorithm as your
solution. You don’t need to write a complete pseudocode, but you are welcome to include
it in addition to or instead of the English description. Additionally, please provide a brief
justification (one or two sentences) for the correctness and running time of the algorithm.

Note: For simplicity, we assume that all edge costs are distinct. The same arguments
will apply even when edge costs are not distinct.

Informal Idea: We use a divide and conquer style algorithm. We partition the edges
into two equal halves, such that every edge of right half has larger weight than every
edge of left half. Now, we check if we can make a spanning tree from edges of just the
left half. If so, we recurse on the edges of the left half, If not, the MBST will contain at
least one edge from right half and hence, including any edge from left half does not
increase the cost of MBST and hence, all edges of left half can be joined together to
form a supernode and we find an MBST in the new graph.
ALGORITHM 1: Algorithm for calculating the Minimum Bottleneck Spanning Tree (MBST)
Input: Undirected graph G
1 Function MBST(G):
2 E ← set of edges of G
3 if | E| = 1 then
4 return E
5 else
6 em ← Edge of E with median cost
7 E> ← Set of edges with cost greater than cem
8 E⩽ ← E − E> ▷ Edges with cost at most cem
9 if G ( E⩽ ) is connected then
10 return MBST ( G ( E⩽ ))
11 else
12 F ← Set of edges such that each connected component of G ( E⩽ ) is a tree
13 return F ∪ MBST ( G ( E> )η ) ▷ G ( E> )η is the graph with edge set E>
and each connected component of G ( E⩽ ) is replaced by a super
vertex in G ( E> )η

Time Complexity:
• Finding the median and dividing the edges into two sets: O(| E|)
• Finding the edge set F: O(| E|)
• At every iteration, the number of edges are halved (assuming distinct edge costs):
T (| E|) = T ( |E2 | ) + O(| E|). By Master Theorem, the overall time complexity is
O(| E|).

3
Tutorial Sheet 7:

Proof of Correctness: We will prove by strong induction on the number of edges in G:


Inductive Hypothesis P(n): For a connected graph G with ⩽ n edges, the above
algorithm outputs the edges of minimum bottleneck spanning tree.
Base Case P(1): It can be seen that the base case is correct, since there is only one edge.
Inductive Case P(n) =⇒ P(n + 1):
To show this, we make the following claims:
Claim 2. If G ( E⩽ ) is connected then MBST of G ( E⩽ ) is an MBST of G.

Proof. The proof is simple, as there exists a spanning tree of G using just the edges of
E⩽ and hence, MBST of G does not use any edge from E> .

Claim 3. If G ( E⩽ ) is not connected then union of F and MBST of ( G ( E> )η ) is an MBST of


G.

Proof. This case is also simple, as any spanning tree of G requires at least one edge
from E> and hence, MBST of G can contain any edge from E⩽ without increasing the
cost of MBST of G.

From the above two claims, the inductive case is easy to show (Try it yourself!).
Try it yourself: Prove that every Minimum Spanning Tree is an MBST.

• Written ”I do not know how to approach this problem” - 0.6 points

• Note: No marks will be deducted for assuming have distinct edge costs
– Algorithm Description - 2 points
* Finding the edge with median cost - 0.5 points
* Handling the case if G ( E⩽ ) is connected - 0.5 points
* Handling the else part - 1 point
– Justification for correctness - 0.5 points
– Mentioning the recursive relation to justify O( E) running time - 0.5 points

5. Suppose we are given the minimum spanning tree T of a given graph G = (V, E) with
distinct edge costs, and a new edge e = (u, v) of cost c that we will add to G. Design an
O(|V |) algorithm to find the minimum spanning tree of the graph G + e. Clearly write the
pseudocode and briefly justify (in one or two sentences) why the algorithm is correct and has
the desired running time.

4
Tutorial Sheet 7:

ALGORITHM 2: Update Minimum Spanning Tree with New Edge


Data: T: The current MST of graph G = (V, E)
e = (u, v): The new edge with cost c
Result: Updated MST
1 Function UpdateMST(T, e):
2 T ← T ∪ {e}
3 Use DFS/BFS to find the unique cycle C in T

4 e ← Heaviest edge in C

5 T ← T \ {e }
6 return T

We are given the Minimum Spanning Tree (MST) T of a graph G = (V, E) with distinct
edge costs, and a new edge e = (u, v) with cost c is added. The goal is to efficiently
compute the new MST for G + e by detecting the cycle formed and removing the
heaviest edge if necessary.

We prove this short lemma as a helper:

Lemma 1 (Cycle Property). For any cycle C in the graph, if the weight of an edge e of C is
larger than the weights of all other edges of C, then this edge cannot belong to any Minimum
Spanning Tree (MST).

Proof. Assume the contrary, i.e., that e belongs to an MST T1 . Deleting e will break
T1 into two subtrees, with the endpoints of e in different subtrees. The remainder of
C reconnects the subtrees, hence there is an edge f in C with endpoints in different
subtrees. This edge f reconnects the subtrees into a new tree T2 with a weight less than
that of T1 because the weight of f is less than the weight of e.
This contradicts the assumption that T1 is an MST, thus proving that the edge e cannot
belong to an MST.

The above lemma suggests an alternate algorithm to find an MST of G:

• Initialise T = G

• While there exists a cycle in T: Remove the largest edge of the cycle from T.

• return T

Proof Idea: To compute MST of G + e, suppose we use the above algorithm and first
keep removing largest edges from the cycles not containing the edge e. Then, it can be
seen that we will be left with the subgraph T + e. Now, to obtain the MST of G + e, we
remove the heaviest edge from the only cycle of T + e.

Time Complexity: The algorithm runs in O(|V |) (since number of edges is also |V |-1)
time since the cycle detection and finding the heaviest edge can both be done using
DFS or BFS, which are linear-time operations for a tree.

5
Tutorial Sheet 7:

• Written ”I do not know how to approach this problem” - 0.6 points

• Note: No points will be deducted for assuming that cost of e is distinct from
existing edge costs.
– Adding e to T and removing the heaviest edge of C - 1 point
– Claiming that the heaviest edge of a cycle does not belong to any MST - 0.5
points
– Justifying why can there not exist another spanning tree of G + e which does
not have lesser cost than the spanning tree returned by our algorithm - 1
point
– Justification for O(V ) running time - 0.5 points

6. You wish to drive from point A to point B along a highway minimizing the time that you are
stopped for gas. You are told beforehand the capacity C of your gas tank in liters, your rate F
of fuel consumption in liters/kilometer, the rate r in liters/minute at which you can fill your
tank at a gas station, and the locations A = x1 , x2 , . . . , xn−1 , xn = B of the gas stations along
the highway. So, if you stop to fill your tank from 2 liters to 8 liters, you would have to stop
for 6/r minutes. Consider the following two algorithms:
a) Stop at every gas station, and fill the tank with just enough gas to make it to the next gas
station.
b) Stop if and only if you don’t have enough gas to make it to the next gas station and if
you stop fill the tank up all the way.
For each algorithm, either prove or disprove that the algorithm correctly solves the problem.
Your proof of correctness must use an exchange argument.

a) Let G represent the greedy algorithm defined in the problem, and let OPT denote
an optimal algorithm. Assume, for the sake of contradiction, that the greedy
algorithm G is not optimal, which implies tG > tOPT , where tG and tOPT are the
total times taken by G and OPT, respectively.
In our greedy solution G, the car stops at all gas stations x1 , x2 , . . . , xn , refueling
v j liters at each station x j . In contrast, the optimal solution OPT stops at fewer
gas stations: xi1 , xi2 , . . . , xim , where m < n, and refuels u j liters at each xi j .
The case m = n is not possible, as it would imply that OPT is identical to G (Hint:
compare ui and vi for any i).
Now, consider the case where m < n, meaning OPT stops at fewer stations than
G. Let k be the first index where ( xik , uk ) ̸= ( xk , vk ). At this index, xik = xk
(Hint: Use the fact that ( xik−1 , uk−1 ) = ( xk−1 , vk−1 )). Additionally, we must have
uk > vk , because if uk ⩽ vk , the car would not have enough fuel to reach xk+1 , as

6
Tutorial Sheet 7:

vk represents the minimum fuel required to make it to xk+1 .


We can now modify the optimal solution OPT by replacing the stop ( xik , uk ) in
OPT with the pair of stops ( xk , vk ) and ( xk+1 , uk − vk ). This adjustment does not
increase the total time of the solution. If xik+1 = xk+1 in OPT, we can further com-
bine the stops ( xk+1 , uk − vk ) and ( xik+1 , uk+1 ) into a single stop at xk+1 , refueling
uk − vk + uk+1 liters.
By making this transformation, we reduce the number of differences between
OPT and G by one, without increasing the time required by OPT. Repeating
this process iteratively for all remaining differences will eventually transform
OPT into G, implying tOPT = tG , which contradicts our initial assumption that
tG > tOPT .
Therefore, the greedy algorithm G is indeed optimal.

b) Consider the following counterexample involving three gas stations, x1 , x2 , x3 ,


located along a highway:

C = 150 liters,
F = 1 liter/kilometer,
r = 1 liter/minute,
x1 = 0 km,
x2 = 100 km,
x3 = 200 km.

If we stop at x2 and fill the tank completely with 100 liters (to refill from the
current level of 50 liters), the time required to refuel will be:

100
t= = 100 minutes.
r
However, if we instead refill only the amount needed to reach x3 (i.e., 50 liters),
the refueling time would be:

50
t′ = = 50 minutes.
r
Since t′ = 50 minutes < t = 100 minutes, this demonstrates that the algorithm
which always fills the tank completely is not optimal, as refueling only what is
necessary can reduce the total time spent.

7. (⋆) In tutorial sheet 6, you saw an example where the “top down” divide-and-conquer strategy
for the optimal prefix-free code problem is suboptimal. You have also seen in class that the
Huffman coding algorithm is optimal.

7
Tutorial Sheet 7:

However, you love divide-and-conquer algorithms and can’t stop thinking about them. So
instead, you devise the following proposal: Define a median symbol whose frequency, along
with the frequencies of all other symbols with higher (respectively, lower) frequency, adds
up to at least 50% of the total. That is, if the frequencies in ascending order are p1 , p2 , . . . , pn ,
then pℓ is the frequency of the median symbol if


1 n n
1 n
∑ pi ⩾ 2 i∑
pi and ∑ pi ⩾ 2 i∑
pi .
i =1 =1 i =ℓ =1

At each step, your algorithm divides the current set of symbols into two parts, namely, those
that are more (respectively, less) frequent than the median, and assigns the median to the
“underdog” part, i.e., the part with the smaller total (breaking ties arbitrarily).
Prove or disprove: The above algorithm returns an optimal prefix-free code.

Figure 2: Σ-tree for above algorithm

8
Tutorial Sheet 7:

Figure 3: Σ-tree for Huffman

Consider Σ = { A, B, C, D, E} with frequencies { 177 3 3 3 1


, 17 , 17 , 17 , 17 }. The average leaf
depth of the constructed Σ tree using the above algorithm is:

1 3 3 3 7 38
×3+ ×3+ ×2+ ×2+ ×2 =
17 17 17 17 17 17
The average leaf depth of the constructed Σ tree using the huffman algorithm is:

1 3 3 3 7 35
×3+ ×3+ ×3+ ×3+ ×1 = .
17 17 17 17 17 17
Hence, the above algorithm is not optimal.

8. (⋆) Suppose you are given a connected graph G = (V, E) with a cost ce on each edge e. In
class, we saw that when all edge costs are distinct, G has a unique minimum spanning tree.
However, G may have many minimum spanning trees when the edge costs are not all distinct.
Here we formulate the question: Can Kruskal’s Algorithm be made to find all the minimum
spanning trees of G?
Recall that Kruskal’s Algorithm sorted the edges in order of increasing cost, then greedily
processed the edges one by one, adding an edge e as long as it did not form a cycle. When
some edges have the same cost, the phrase “in order of increasing cost” has to be specified
a little more carefully: We’ll say that an ordering of the edges is valid if the corresponding
sequence of edge costs is nondecreasing. We’ll say that a valid execution of Kruskal’s Algorithm
is one that begins with a valid ordering of the edges of G.

9
Tutorial Sheet 7:

For any graph G, and any minimum spanning tree T of G, is there a valid execution of
Kruskal’s Algorithm on G that produces T as output? Give a proof or a counterexample.

Intuition: To obtain an MST T from Kruskal’s Algorithm, we construct an ordering in


which we prefer edges present in T. Then we can prove that this ordering would give
us the tree T by using induction.

Formally, for an MST T if we want the following condition to hold: For every two
edges e1 and e2 , if they have the same costs and e1 is in T while e2 is not in T, then
e1 should appear before e2 in the ordering. If we run Kruskal’s Algorithm on a
valid ordering which satisfies the above criteria, we claim that we would get the MST T.

Notation:
• We denote a valid ordering O = {e1 , e2 , . . . , em }, with O[i ] = ei as the indexing
operation.
• Let OT be a valid ordering which satisfies the condition stated above for an MST
T.
• Let index till(c, O) denote the index i in a valid ordering O such that ∀ j<i . cO[ j] < c
and ∀ j⩾i . cO[ j] ⩾ c
Induction Hypothesis: During the run of Kruskal on an ordering OT , when we are at
the index index till(c, OT ), then we would have added exactly the edges to the tree
which have cost less than c and appear in T.

Base case: For c = mini∈m (cei ), index till(c, OT ) = 0, there are no edges with cost less
than c and we wouldn’t have added any edges to the tree either (because we are at the
start of the algorithm), hence the hypothesis is satisfied.

Inductive Case: Suppose the hypothesis holds for all values of c less than c∗ . We claim
that after processing all the edges in OT with weight c∗ we would end up with exactly
the edges which have weight ⩽ c∗ and are also in T.

Lets consider the case where there exists at least one vertex in E with weight c∗ , as the
other case is not interesting. Now let i = index till(c∗ , OT ), which means cOT [i] = c∗ .
a) Case 1: If there doesn’t exist an edge in T with weight c∗ then we prove that
adding any edge with the weight c∗ to the current tree will create a cycle. This is
true because of our induction hypothesis saying that the MST created so far is
the same as T, so if we can add an edge with weight c∗ to it then we can replace
an edge in T with weight larger than c∗ with this edge, and thus contradicting
the fact that T is an MST.
b) Case 2: Let j be the largest index such that OT [ j] ∈ T and cOT [ j] = c∗ . According
to the constraints on OT , we know that all edges OT [i ], OT [i + 1], . . . , OT [ j] are

10
Tutorial Sheet 7:

in T. We claim that all of these edges can be added to the current tree without
causing a cycle, as if the cycle exits in this tree then the same cycle will exist in
T. Also all edges in OT after j with weight c∗ will not be added to the tree by
Kruskal’s algorithm. This is because after index j if we can add another edge
with weight c∗ without causing a cycle then we can replace an edge in T with
larger weight than c∗ , and thus contradict it being an MST.

Hence we prove that the induction hypothesis holds, which means that at the end of
the algorithm we would obtain exactly the same tree T. This proves that for every MST
T there exists an ordering which would make Kruskal’s algorithm output T.

11

You might also like