You are on page 1of 9

Assignment #5, CS4/531

Due Date: Sun. Nov. 29, 2020


Solution
Total points: 46

1. (0 points) Find the strongly connected components of the graph G = (V, E) in Fig. 1, by
using the DFS based algorithm in section 22.5. Your solution should provide the following:
• Run DFS on G. Indicate the DFS trees constructed by DFS algorithm; indicate the d[u]
and f [u] values for each vertex u ∈ V .

• Draw the graph GT . Label the vertices in the reverse order of f [u] values.

• Run DFS algorithm on GT . Indicate the DFS trees constructed by the DFS algorithm.

• Indicate the strongly connected components of G.


Answer:

• Run DFS on G, as shown in Fig.1. (The DFS tree was indicated in red color.)

a [1,18] [7,8]
[2,11] h
b
[13,14]
e [12,17]
g
c [3,10]

[15,16] f d [4,5] [6,9] i

Figure 1: Graph for Strongly Connected Components Problem.

[1,8] [12,15]
1 a b h
8
5 [9,18]

[3,4]
[2,7]
4 2 g
e 6
c [11,16]

3 9
7
[5,6] f d [10,17] [13,14]
i

Figure 2: Run DFS on GT and find Strongly Connected Components.

• The graph GT was drawn as in Fig.2. And the vertices were labeled in the decreasing
order of f [u] values in red color.

1
• Run DFS on GT , as shown in Fig.2, and the DFS tree was indicated in red color.
• The strongly connected components of G are shown in Fig.2, as circled in blue color.

2. (8 points) Consider the graph in Figure 3. Suppose that G is given by adjacency list
representation where, for each vertex v, the neighbors of v in Adj(v) are in alphabetical order.
Run the DFS-based biconnectivity algorithm on G with a as the starting vertex. You should:

1. Run DFS algorithm on G; indicate the DFS tree constructed;


2. Rename the vertices of G by integers in the order they are visited by the DFS algorithm;
3. Compute low(v) for each vertex v;
4. List the cut vertices found by the algorithm.

Answer:
• Run DFS algorithm on G, as shown in Fig.3, the DFS tree is indicated in red color. And
the vertices are renamed by integers in the order they are visited by the DFS algorithm;

4 d
9 [13,14] g 8 [4,5] [3,6]
h [12,15] c 3
[2,17] 2
7 i b [8,9]
[11,16] f 6
[1,18]
1 a [7,10] e 5

Figure 3: Run DFS for Biconnectivity Algorithm

• The low(v) values for each vertex v are computed as Table 1:


• The cut vertices found by the algorithm are 2(b) and 7(i).

v 1 2 3 4 5 6 7 8 9
low(v) 1 1 2 2 2 2 1 7 7

Table 1: Low values for BCC algorithm

3. (0 points) Run Dijkstra’s algorithm on the directed graph in Figure 4, using the vertex a as
the source. In the style of Figure 24.6 (page 659), show the d[∗] and the π[∗] values and the
vertices in the set S after each iteration of the while loop.

4. (8 points) Bipartite Graph Testing problem.


Definition: An undirected graph G = (V, E) is a bipartite graph if its vertex set V can be
partitioned into X and Y (namely V = X ∪ Y and X ∩ Y = ∅) such that: (1) no two vertices
in X are adjacent in G; and (2) no two vertices in Y are adjacent in G.

2
a 5 a 5
0 oo c 0 oo c
9 9
4 2 4 2
4 e 3 e
2 b 3 2 b 3
3 oo 3 oo
1 2 1 2
2 4 2 4
2 oo oo 2 4 oo
d d
g h 4 g h 4
5 6 5 6
f oo f 7

a 5 a 5
0 5 c 0 5 c
9 9
4 2 4 2
3 e 3 e
2 b 3 2 b 3
3 oo 3 oo
1 2 1 2
2 4 2 4
2 4 oo 2 4 8
d d
g h 4 g h 4
5 6 5 6
f 7 f 7

a 5 a 5
0 5 c 0 5 c
9 9
4 2 4 2
3 e 3 e
2 b 3 2 b 3
3 14 3 14
1 2 1 2
2 4 2 4
2 4 8 2 4 8
d d
g h 4 g h 4
5 6 5 6
f 7 f 7

a 5 a 5
0 5 c 0 5 c
9 9
4 2 4 2
3 e 3 e
2 b 3 2 b 3
3 10 3 10
1 2 1 2
2 4 2 4
2 4 8 2 4 8
d d
g h 4 g h 4
5 6 5 6
f 7 f 7

Figure 4: The execution of Dijkstra’s Algorithm

Bipartite Graph Testing problem: Given a graph G = (V, E), determine if G is a


bipartite graph or not.
Design an efficient algorithm for solving this problem.
Note: Bipartite graphs form an important graph class with many applications. Many NP-
complete problems for general graphs can be solved in polynomial time for bipartite graphs.
To solve this problem, you may or may not need the following:
Theorem: G is a bipartite graph if and only if G contains no odd cycles (an odd cycle is a
cycle in G with an odd number of edges).

Answer:
The problem can be solved by using any spanning tree of G. For example, we can use a BFS
tree T generated by the BFS algorithm. All operations described below can be easily done while

3
performing BFS. But for clarity, we describe each step separately.
1. Construct a BFS T of G.
2. Label the vertices of G by X and Y as follows: the root r of G is labeled X. The children
of r (namely, the vertices of level 1) are labeled by Y . In general, the vertices on an even level
are labeled by X; the vertices on an odd level are labeled by Y .
3. Scan all non-tree edges e = (u, v) one-by-one. If an edge e = (u, v) connects two vertices
both labeled by X, or both labeled by Y , output “G is not bipartite”. If all non-tree edges
e = (u, v) connects vertices of different labels, output “G is bipartite”.
Clearly the algorithm takes O(n + m) time.

5. (2+8 = 10 points) Consider the flow network G shown in Fig. 5.


1. What is the capacity of the cut ({s, a, b}, {c, d, e, t})?

2. Show the execution of Edmond-Karp algorithm on this network. Compute the max-flow
of G. Show the progress of the algorithm after each iteration, (as shown in Figure 26.6 on
Page 726, or Class note 09, page 36/89).

9
a b
4
10 4 3
2 5 2
s c t
4
3
5
d 10
e
9

Figure 5: Max-Flow Problem

Answer:
(1) The edges that cross the cut ({s, a, b}, {c, d, e, t}) are; {(s → d), (a → d), b → d), (b →
c), (b → t)} as shown in Figure 6. The capacity of the cut is 5 + 2 + 4 + 3 + 4 = 18.

{s,a,b} {c,d,e,t} cut


9 b
a 4
10 4 3
2 5 c 2 t
s
4
3
5
d e 10
9

Figure 6: The cut of Max-Flow problem

(2) See Figure 7

6. (0 points) An undirected graph G = (V, E) represents a communication network. Each vertex


of G represents a computing site, and each edge represents a bi-directional communication link.

4
9 b 4/9 b
a 4 a 4/4
10 4 3 4/10 4 3
2 5 c 2 t 2 5 c 2 t
s s
4 4
3 3
5 5
d e 10 d e 10
9 9
5 b 4/9 b
a 4 a 4/4
4 4
4 3 4/10 4 3
6 2 5 c 2 t 2 5 c 2 t
s s
4 4
3 3
5 5/5
d e 10 d e 5/10
9 5/9
5 b 6/9 b
a 4 a 4/4
4 4
4 3 6/10 4 2/3
6 2 5 c 2 t 2 5 c 2/2 t
s s
4 4
3 5 3
5/5
5 d 5 5 d 5/10
e e
4 5/9
3 6/9 b
a b a 4/4
6 6 4
1 2 8/10 4 2/3
4
s
4 2 5 c 2 t s 2/2 5 c 2/2 t
4 4
3 5 3
5/5
5 d 5 5 d 7/10
e e
4 7/9
3 b 8/9
a 6 a b
8 4 4/4
4 1 2 10/10 2/3
2 2 2/4
7 c t
s
4 s 2/2 5 c 2/2 t
3 7 4
3
5 7 5/5
d e 3 d 9/10
2 e
9/9
1 b
a 8
10 4
2 1 2
7 2 c 2 t
s
4
3 9
5 d 9 1
e

Figure 7: The execution of Edmond-Karp algorithm

5
The network administrator must set up k communication requests: For each i (1 ≤ i ≤ k), a
connection must be established between the site ai and the site bi . (A connection is a path
Pi in G between ai and bi ). To ensure the quality and safety of the connection, the paths Pi
(1 ≤ i ≤ k) must be edge-disjoint. (In other words, each edge of G can be in at most one path
Pi .)
Problem: Can this be done? If so, how to determine the paths Pi (1 ≤ i ≤ k)?
Try to convert this problem (called it P ) to a basic Max-Flow problem (called it Q), using
the methods described in Classnote 9, pages 12/78 to 16/78. Once you have a max-flow function
f (∗) for Q, try to guess how to solve the original problem P from f .
Unfortunately, this approach will not work. Explain what is wrong with this approach.

Answer:
The conventional (described in Classnote 09) way to convert this problem to the basic max-
flow problem is as follows: For each undirected edge e = (u, v), replace e by a pair of directed
edges u → v and v → u. Then, add a new source s and connected s to each ai by a dummy
edge. Next, add a new sink t and connect each bi to t by a dummy edge. All edges have capacity
1. (Figure 8 (1) shows an input graph G. Figure 8 (2) shows flow the network constructed from
G by using this procedure).

b b
3 3
a1 b a1 b
2 2

c s c t
d d
b1 b1
a2 a2
a3 a3
(1) (2)

Figure 8: An Example of Problem 7.

Now the problem is converted to a basic Max-Flow problem Q. We can call the max-flow
algorithm to find a max-flow function f from s to t. It is tempting to claim: The original
problem has a solution iff |f | = k.
But this claim is false. Since all edges of Q have capacity 1, the flow value f (e) = 0 or 1 for
every edge e. Thus |f | = k iff there are k edge disjoint paths Pi (1 ≤ i ≤ k) from s to t, and
every edge e on Pi has flow value f (e) = 1. Because there are exactly k edges leaving s and
exactly k edges entering t, the path Pi = s → aj =⇒ bl → t (for some 1 ≤ j ≤ k and 1 ≤ l ≤ k).
Here → indicates a single edge, and Ri = aj =⇒ bl indicates a path from aj to bl . So far, these
arguments are correct. However, we cannot ensure the path Ri is a path from aj to bj . Without
this endurance, the solution of the max-flow problem does not lead to a solution of the original
problem.
In Figure 8 (2), the dashed lines are the dummy edges added into G. The thick lines indicate
the edges with flow value 1. The thin lines indicate the edges with flow value 0. In this example,
we have a flow function f with |f | = 3. From f , we can find 3 edge disjoint paths from s to t,
which leads to a path a1 =⇒ b3 , a path a2 =⇒ b2 , and a path a3 =⇒ b1 . Actually, it can be
seen that there are NO edge disjoint paths from ai to bi (1 ≤ i ≤ 3).
This problem is actually a special case of the multi-commodity flow problem, defined in
Class note 9, page 19/76: the demand di = 1 for all 1 ≤ i ≤ k, and all edges have capacity

6
1. Once we solve the multi-commodity flow problem, we will have a solution for this problem.
Unfortunately, as stated in the class note, the multi-commodity problem cannot be converted
to a basic max-flow problem, and is much harder to solve.

7. (10 points) Doctor Scheduling Problem.:


A large hospital needs to make a vacation schedule that satisfies the following requirements.
• There are k vacation periods. Each period spans several contiguous days. (Example: the
week of Christmas, Thanksgiving weekend, July 4th weekend etc). Let Dj be the set of
days in the jth vacation period. The set of vacation days is the union of all Dj ’s.
• There are n doctors at the hospital. The doctor i has a set Si of vacation days when
he is available to work. (This may include certain days from a vacation period, but not
others. For example, a doctor may be able to work on Friday, Saturday and Sunday during
Thanksgiving weekend, but not on the Thursday).
• We have a parameter c. Each doctor should be assigned to work at most c vacation days
total and only days when he or she is available.
• For each vacation period Dj , each doctor should be assigned to work at most one of the
days in Dj . (For example, a doctor can be assigned to work 1 day during Thanksgiving
weekend and 1 day during July 4th weekend. But he cannot be assigned to work 2 days
during the Thanksgiving weekend).
• The hospital needs to assign one doctor to work on each vacation day.
Describe a polynomial time algorithm that determine whether all above requirements can
be satisfied. (Hint: Convert it to a max-flow problem.)

Answer: Without the requirement that each doctor can work at most one day in each
vacation period (*), the problem can be solved easily by constructing a network as follows. We
have a node ui representing each doctor attached to a node vl representing each day when he or
she can work; this edge has capacity 1. We attach a source s to each doctor node ui by an edge
of capacity c, and attach each day node vl to a sink t by an edge with upper and lower bounds
of 1. The network would be as Figure 9.
HOLIDAYS

DOCTORS

s s t

Figure 9: The network for Problem 2 without constraint(*)

Now to handle the requirement (*), we take each pair (i, j) consists of a doctor i and vacation
period j, we add a ”vacation gadget” as follows. We include a new node wij with an incoming

7
edge of capacity 1 from doctor node ui , and with outgoing edges of capacity 1 to each day in
vacation period j when doctor i is available to work. This gadget makes sure that the flow from
ui into the days associated with vacation period j is at most 1. The network would be as Figure
10.
HOLIDAYS

GADGETS

DOCTORS

s t

Figure 10: The network for Problem 2

Now, we will show that there is a way to assign doctors to vacation days in a way that
respects all constraints if and only if the max flow has value d = j |Dj | where d is the total
P

vacation days.
First, if there is a way to assign doctors to vacation days in a way that respect all constraints,
then we can construct the following flow. If the doctor i works on day l of vacation period j, then
we send one unit of the flow along the path s, ui , wij , vl , t; we do this for such (i, l) pairs. Since
the assignment of doctors satisfied all the constraints, the resulting flow respects all capacities,
and it sends d units of flow out of s and into t, so it meets the demands.
Conversely, suppose there is a flow with value d. For this direction of the proof, we will show
how to use this flow to construct a schedule for all the doctors. Since there is a feasible flow
in which all flow value are integers, we can construct the following schedule. If the edge wij , vl
carries a unit of flow, then we have doctor i work on day l. Because of the capacities, the
resulting schedule has each doctor work at most c days, at most one in each vacation period,i
and each day is covered by one doctor.

8. (10 pts) Consider the problem faced by a hospital that is trying to evaluate whether its blood
supply is sufficient.
There are four types of blood: A, B, AB, and O. Patients with blood type A can receive
only blood types A or O in a transfusion. Patients with blood type B can receive only B or O.
Patients with blood type O can receive only O. Patients with blood type AB can receive any of
the four types.

(a) Let sO , sA , sB , sAB denote the supply in whole units of different blood types on hand.
Assume that the hospital knows the projected demand for each blood type dO , dA , dB , dAB
for the coming week. Describe how to convert this problem to a max-flow problem on a
network-flow G; and how to tell, from the value of the max-flow function f (∗), if the blood
on hand suffice for the projected need.

(b) Consider the following example. The typical distribution of blood types in US patients is
roughly 45% type O, 42% type A, 10% type B and 3% type AB. The hospital wants to

8
know if the blood supply it has on hand would be enough if 100 patients arrive with the
expected type distribution. There is a total of 105 units of blood an hand. The table
below gives these demand, and the supply on hand.

Blood Type Supply Demand


O 50 45
A 36 42
B 11 10
AB 8 3

Is the 105 units of blood on hand enough to satisfy the 100 units of demand? Find an
allocation that satisfies the maximum possible number of patients. Use an argument based
on a minimum-capacity cut to show why not all patients can receive blood. Also, provide
an explanation for this fact that would be understandable to the hospital administrators,
who have not taken a course on algorithms. (So, for example, this explanation should not
involve the words flow, cut or graph in the sense we use them in our course).

Answer:
(a) Construct a flow-network G = (V, E) as shown in Fig 11 (a). All edges in the middle have
infinity capacity, (or if you want to be precise, just set it to the sum of supplies and demands).
Run the max-flow algorithm on this G to calculate a max-flow function f (∗). The supply
suffices for the projected demand if and only if the max-flow value |f | = dA + dB + dO + dAB .
8

36/ 8
s dA
6/
8

A 36/36 42/42
dO 44/
8

s
O 50/50 44/45
s dB
B 10/11 10/10
10/
8

s
AB 3/8
d 3/3
8

3/
8

AB

(a) (b)

Figure 11: Blood Supply Problem.

(b) In this particular case, the capacities and the max-flow values are shown in Fig 11 (b).
The cut shown has capacity 36+50+10+3=99, which is less than the total demand of 100.
Hence, the supply cannot satisfy the demand.
The cut also gives a layman explanation: the total type A and O supply is only 86, which
cannot satisfy the total type A and O demand of 87, since type B and AB cannot be used to
supply type A and O demands.

You might also like