Therefore, if there is a majority element, the algorithm outputs the majority element.
2
(d) (3 pts.)
Optional bonus problem: Check my proof
(This is an
optional
bonus challenge problem. Only solve it if you can’t get enough of the algorithms
goodness. We reserve the right not to grade bonus problems if necessary.)
If
G
= (
V
,
E
)
is an undirected graph and
S
✓
V
is a set of vertices, call the edge
(
u
,
v
)
good
if one of
u
,
v
is in
S
and the other is not in
S
; and call the set
S nice
if it makes at least half of the edges of the graph
CS 170, Fall 2014, Sol 1
4
good. We want an algorithm that, given an undirected graph
G
= (
V
,
E
)
, outputs a nice set
S
. Consider
the following algorithm and proof of correctness:
Algorithm M
AKE
N
ICE
(
G
)
:
1. Set
S
:
=
/0.
2. Loop until
S
is nice:
3.
Let
w
be a vertex whose degree is maximal (resolve ties arbitrarily).
4.
Set
S
:
=
S
[
{
w
}
.
5.
Delete all edges that have
w
as an endpoint.
6.
If
S
is nice (for the original graph), return
S
.
Proof
: Imagine keeping track of the number of good edges (from the original graph), as the algorithm
executes. We will show that the number of good edges increases in each iteration of the loop.
In particular, when we add
w
to
S
in step 4, every edge currently in the graph that has
w
as an endpoint
will shift from nongood to good.
(Why? Well, the algorithm immediately deletes all edges whenever one of their endpoints enters
S
, so
whenever execution reaches step 3, all remaining edges connect two vertices in
V
\
S
. In particular,
all edges that haven’t been deleted yet are nongood.
But the ones that have
w
as an endpoint will
immediately become good when
w
is added to
S
.)
Since we strictly increase the number of good edges each time we execute an iteration of the loop,
eventually the number of good edges must exceed

E

/
2.
2
Is this proof of correctness valid? Write “Valid” or “Invalid.” If you write “Valid”, analyze the running
time of the algorithm. If you write “Invalid”, explain what is wrong (list the first step in the reasoning
that doesn’t follow).
Solution:
The proof is Invalid. The proof states correctly that:
. . . when we add
w
to
S
in step 4, every edge currently in the graph that has
w
as an endpoint
will shift from nongood to good.
It then uses this to claim that:
Since we
strictly increase
the number of good edges each time we execute an iteration of the
loop, eventually the number of good edges must exceed

E

/
2.
However, this claim does not follow.
The problem is that there is no proof that the number of good edges is strictly increasing, only that in
each iteration some edges become good. It ignores the fact that some edges may become nongood
again. Indeed, an edge first become good when one of its vertices is added to
S
and goes back to being
nongood when the other vertex is added to
S
.