0% found this document useful (0 votes)
118 views6 pages

Network Flow Exercises and Algorithms

This document contains solutions to exercises from Chapter 3 of a textbook on network flows. Exercise 3.1 provides the pseudocode for a power-of-n procedure to calculate powers of a number n. Exercises 3.3, 3.5, 3.7, 3.9, 3.11, 3.13, 3.15, 3.17, 3.19, 3.21, and 3.23 provide solutions to problems analyzing the time complexity of algorithms using techniques like parameter balancing and differential calculus. Exercises 3.25, 3.27, and 3.29 involve representing problems as graphs and analyzing graph search algorithms.

Uploaded by

Manda Reema
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)
118 views6 pages

Network Flow Exercises and Algorithms

This document contains solutions to exercises from Chapter 3 of a textbook on network flows. Exercise 3.1 provides the pseudocode for a power-of-n procedure to calculate powers of a number n. Exercises 3.3, 3.5, 3.7, 3.9, 3.11, 3.13, 3.15, 3.17, 3.19, 3.21, and 3.23 provide solutions to problems analyzing the time complexity of algorithms using techniques like parameter balancing and differential calculus. Exercises 3.25, 3.27, and 3.29 involve representing problems as graphs and analyzing graph search algorithms.

Uploaded by

Manda Reema
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

CHAPTER 3

E XERCISE 3 . 1 .

procedure power-of-n ;
begin
i: = 1 ;
A[1]: = n;
while power + power ≤ n do
begin
power: = power + power;
A[i+1]: = A[i]*A[i];
i: = i+1;
end;
for k: = i downto 1 do
begin
if power + k ≤ n then A[i]: = A[i]*A[k];
power: = power + k;
end;
answer: = A[i];
end;

2
E XERCISE 3 . 3 . (a) 2log log n , n 2 , 1.5(log n) , 2 n , n!
(b) log log n, (log n) (log log n), 1000(log n)2 , 0.005n0.001 .

E XERCISE 3 . 5 . (a) f(n) is not O(g(n)) since f(n) > c g(n) for even n and for some constant c. However, f(n) =
Ω (g(n)), since for odd n, f(n) ≤ g(n).

(b) Since all even numbers (other than number 2) are not prime numbers, it follows that f(n) ≤ g(n) for all positive
n. Hence, f(n) = O(g(n)). In addition, f(n) ≥ g(n) for odd n. It follows that f(n) = Ω (g(n)) according to the definition
given in the exercise statement, and therefore f(n) = θ(g(n)).

(c) Notice that for all n ≥ 2, f(n) ≥ 3 and g(n) ≤ 6/6. Therefore, f(n) = Ω (g(n)). Also notice that for all n ≥ 2, f(n)
≤ 4 ≤ 4 g(n). Therefore, f(n) = θ(g(n)).

E XERCISE 3 . 7 . g(n, m) = m logdn ≤ m log 2 n ≤ m log 2 m = O(m 1+ε), because from Exercise 3.6(a), log m =

O(nε) for any ε > 0.

E XERCISE 3 . 9 . After the 1st pass, the element at the nth position will be the longest element. After the 2nd
pass, the element at the (n-1) th position will be the next longest element. In general, after k passes, the elements
from the (n-k+1)th position to the n th position will form a sorted list of the k longest elements. Hence after n
passes, the algorithm will terminate. Since each pass requires O(n) comparisons, the algorithm will run in O(n2 )
time. The algorithm may be proved to be Ω (n2 ) by simulating it on the following pathological problem instance :
{2, 3, 4, ..., n, 1}. In each pass, the element 1 will move to left by one position. After (n-1) passes, this element
moves to the first place, and a sorted list is obtained. The total number of comparisons in all passes are (n-1) + (n-
2) + .... + 1 = Ω (n2 ). Consequently, the bubble sort algorithm takes θ(n2 ) time.
Network Flows 3.2 Ahuja/Magnanti/Orlin

E XERCISE 3 . 1 1 . Define the potential function as φ = number of elements in queue. Initially φ = 0, and at the
termination of the set of operations, φ ≥ 0. Since the total number of insert operations is bounded above by n, there
is an increase of at most n in the potential function due to the insert operations. Hence, at most n elements can be
deleted from the queue due to all delete operations (decrease in φ = number of elements deleted). Consequently, this
sequence of insert and delete operations will take a total of O(n) time.

E XERCISE 3 . 1 3 . (a) Parameter balancing technique: By setting, n3 /k = knm, we obtain k = n/ m , which gives
n3 n
a time bound of + = 2n2 m = O(n2 m ).
n/ m m

d n3 n3
Differential Calculus : We set (knm + ) = 0, obtaining nm - 2 = 0 or k = n/ m ,
dk k k

which gives the same time bound as obtained by the parameter balancing technique.

m
(b) Parameter balancing technique : By setting nk = , we obtain k = m/n . This value of k yields a time
k
bound of O( nm ).

d m
Differential Calculus : Equating (nk + ) = 0 yields k = m/n , which gives the same time bound as obtained
dk k
by the parameter balancing technique.

m log n nk log n
(c) Parameter balancing technique : Setting = yields k = m/n, and the resulting time bound is
log k log k
m log n
O( ).
log (m/n)

d m log n nk log n K+1 n


Differential Calculus : Equating ( + ) = 0, yields = .
dk log k log k K log K m

A general method for solving this equation does not exist and hence this problem illustrates the inherent advantage of
the parameter balancing technique over the differential calculus method.

E XERCISE 3 . 1 5 . (a) Let vk be the flow value after the k th iteration. Then, (vk+1 - v k ) = (v* - vk )/m, where α =
1/m is the geometric improvement factor. The number of iterations is O((log H)/α ), where H = |(v* - v)|. Hence,
the number of iterations is at most m log (|v* - v | ).

(b) Using the same approach as above, we obtain α = 1/(2n2 ). As, H = |z - z * | , the number of iterations is
bounded by 2n2 log( |z - z * | ).

E XERCISE 3 . 1 7 . The dynamic programming algorithm in this case is similar to that in Section 3.3 except that
d(i, j) is defined differently. In this case,

d(i, j) = max{d(i-1, j), ui + d(i-1, j-w i ), 2u i + d(i-1, j-2w i ), ..., ku i + d(i-1, j-kw i )}, where k = h.

E XERCISE 3 . 1 9 . This problem has an error in it. The proposed dynamic program finds an odd directed walk, which
contains an odd directed cycle, as per Excercise 2.18. However, the odd cycle does not necessarily contain node i.
So, this problem is incorrect as stated.
Network Flows 3.3 Ahuja/Magnanti/Orlin

E XERCISE 3 . 2 1 .

S t e p 1 : Initialize d o (s) = 0, d o (i) = ∞ for all i ∈ N-{s}.


Step 2 : Define dk (j) = min { dk-1 (i) + c ij : (i, j)∈A} for all j = 1, 2, ..., n.

Thus the values of d k (.) for all 1 ≤ k ≤ n may be found by using the above dynamic programming technique.
This approach will also work even in the presence of negative cycles.

E XERCISE 3 . 2 3 . At any iteration, when [a, b] is the feasible interval, evaluate f(n) at the two points (3a+b)/4
and (a+3b)/4. If f((3a+b)/4) > f((a+3b)/4), then we exclude the interval [a, (3a+b)/4]; otherwise we exclude the
interval [(a+3b)/4, b]. In each iteration, the width of the interval decreases from (b-a) to 3(b-a)/4. We repeat this
process until the length of this interval is less than 2ε. The algorithm terminates in log4/3 (U/ε) = O(log(U/ε))
iterations.

E XERCISE 3 . 2 5 . Construct the graph G = (N, A) such that each unshaded square is represented by a node in N,
and nodes i and j are joined by an (undirected) arc if and only if the squares, that nodes i and j represent, are reachable
from each other by a single knight move. If a path P exists from node s to node t in the graph G, then the sequence
of arcs in P will define the sequence of moves required during a possible knight's tour.

E XERCISE 3 . 2 7 . Any partitioning is referred to as a feasible state of the jugs. There are 16 possible states, as
listed in the following table:

State number Jug a Jug b Jug c


(8 gallons) (5 gallons) (3 gallons)
1 8 0 0
2 3 5 0
3 3 2 3
4 5 0 3
5 7 1 0
6 7 0 1
7 6 2 0
8 6 0 2
9 5 3 0
10 4 1 3
11 4 4 0
12 2 5 1
13 2 3 3
14 1 5 2
15 1 4 3
16 0 5 3

Now we represent each state by a node and connect two nodes i and j by a (directed) arc (i, j) if and only if it is
possible to reach state j from state i by exactly one move. A path from node 1 (state 1) to node 11 (state 11) in the
resulting graph defines a possible sequence of moves.

E XERCISE 3 . 2 9 . Without any loss of generality we may assume that node k is visited before node l during the
course of the depth-first search algorithm. Whenever the algorithm adds any node j to LIST, the nodes which are in
the subtree rooted at node j are the nodes which are labeled between the events of node j being added to and deleted
from LIST. When node k is visited, the arc (k, l) is yet admissible, i.e., node l is yet unlabeled; the algorithm one
by one scans all the admissible arcs (k, j) incident to node k and builds the subtree rooted at each node j. At the time
Network Flows 3.4 Ahuja/Magnanti/Orlin

when the algorithm scans the arc (k, l), if the node l has already been labeled, it means that node l must be in one of
the subtrees of a node j which is an immediate descendant of node k. On the other hand, if the node l has not yet
been labeled at the time the algorithm scans the arc (k, l) node l becomes a direct descendant of node k, i.e., in either
case the node l (which is visited second) becomes a descendant of the node k (which is visited first).

The breadth first search tree need not necessarily satisfy this property. In the network given in Figure S3.29(a),
nodes 2 and 3 are connected by the arc (2, 3) but node 2 and node 3 are not connected by the ancestor-descendant
relationship in the breadth-first search tree illustrated in Figure S3.29(b).

1 1

2 3 2 3
(a) Figure S3.29 (b)

E x e r c i s e 3 . 3 1 . One can easily establish the following property of the depth-first search tree: if arc (i, j) is
present in A, then in the depth-first search tree either node i is an ancestor of node j or either node i is an ancestor of
node j or node j is an ancestor of node i. An immediate consequence of this property is that all nodes of a clique lie
on a path in the depth-first search tree. All nodes in a clique S need not appear consecutively on the path. In the
counterexample illustrated in Figure S3.31(a) with its depth-first search tree in S3.31(b). Observe that the nodes of
the clique {1, 4, 3} do not appear consecutively on one path.

1 2 1 2

4 3 4 3

(a) (b)

Figure S3.31

E XERCISE 3 . 3 3 . Suppose that the directed graph G is not strongly connected and the graph does not contain a
directed path from node k to node l. If we perform the search starting at node k and define S as the set of labeled
- -
nodes, then the cut [S, S ] will have no arc (i, j) with i ∈ S and j ∈ S .

E XERCISE 3 . 3 5 . Use the search algorithm O(n) times (using each node as the source node) in order to determine
the set of nodes reachable from each node. Since the search algorithm requires O(m) time, the transitive closure
algorithm would have time-complexity of O(nm).

E XERCISE 3 . 3 7 . Let the matrix τ denote the transitive closure of G. The following algorithm identifies all
strongly connected components of G in O(n2 ) time.

begin
unlable all nodes;
next: = 1;
repeat
select an unlabeled node, say node k;
label(k): = next;
for i: = 1 to n do
if τ ik = 1 and τ ki = 1 then label(i) = next;
next: = next+1;
until there are unlabeled nodes;
end;
Network Flows 3.5 Ahuja/Magnanti/Orlin

E XERCISE 3 . 3 9 . The example network illustrated in Figure S3.39 has the two distinct topological ordering, 1-2-
3-4 and 1-3-2-4. Observe that the topological ordering algorithm gives in the text will obtain a unique ordering if
and only if LIST contains exactly one node during every stage of the algorithm. Now, if the topological ordering is
unique then the nodes will enter in the LIST is some order i 1 -i2 -...-in , where ik will not be contained in LIST when
ik-1 is present in it (for all 2 ≤ k ≤ n). Hence the network must contain the arc (ik-1 , i k ), and thus a directed path
i1 -i2 -...-in must exist. Conversely, if the network contains the directed path i1 -i2 -...-in passing through all of its
nodes, then two distinct nodes ip and iq cannot be present in LIST at the same time (because LIST contains nodes
with zero indegrees , and only one node will have zero indegree at any point). Hence, we shall obtain a unique
topological order. This completes the proof of the result.

1 4

Figure S3.39

E XERCISE 3 . 4 1 . Let LIST be the set of unlabeled nodes, which we store as a doubly linked list. Initially, LIST
= N and next = 1. Repeat the following iterative step: Take out a node, say node k, from LIST, perform a search
starting at node k to identify all nodes in the component containing node k, and assign a label equal to next. (Note
that whenever a node is labeled, we delete it from LIST). Update next = next+1, and repeat the iterative step. It can
be easily seen that this method runs in O(n) time.

E XERCISE 3 . 4 3 . For each node j, let I(j) denote the set of all nodes i such that (i, j) is in A. Let α(s) = 1. Next
examine the nodes in the network in the topological order and for a node j being examined, compute α(j) using α(j)
=Σ i∈I(j) α(i). The justification of this method is straightforward.

E XERCISE 3 . 4 5 . In any Eulerian graph, the degree of every node is even because any Eulerian walk must enter
any node exactly as many times as it leaves it. Conversely, if the graph contains only even degree nodes, then the
graph must contain at least one cycle since the degree of each node in this connected graph is at least two. Removal
of this cycle yields a graph containing one or more components, each of which again contains only even degree
nodes. We repeat this process in order to decompose the graph into a collection of arc-disjoint cycles. We can
connect these cycles to form a closed walk as follows. We start at some nodes in one of the cycles, say W1 and visit
the nodes (and arcs) of W1 in order, until we either return to the starting node or encounter a node that also lies in a
directed cycle not yet visited, say W2 . In the former case, the walk is complete, and in the latter case, we visit cycle
W2 first before resuming the visit of the nodes in W1 . While visiting nodes in W2 , we follow the same policy, and
thus by using this method we can always find a closed directed walk in the network which traverses each arc exactly
once.

E XERCISE 3 . 4 7 . Let Pkl denote the unique tree path from node k to node l, and suppose that the longest path in
the tree, denoted by P, is different than this. There are two cases to consider.

Case 1. The path Pkl has no node in commom with the path P. Let P' denote the smallest path in the tree T that
connects a node in Pkl with a node in P. Define the subpaths a, b, c, d, and e, as given in the Figure S3.47(a). We
assume without any loss of generality that b ≥ a. Then clearly d + e ≥ d + c + b, since Pkl is the longest path from
node k to node l. This inequality implies that e ≥ c + b. Thus, e + c + b ≥ 2(c+b)≥ 2b≥ a+b=|P|. Consequently,
the length of the longest path from node k to node l is at least the length of P.
Network Flows 3.6 Ahuja/Magnanti/Orlin

Case 2. The path Pkl touches the path P. Let Pkl consist of the path segments e, b, and d, as shown in Figure
S3.47(b). Assume that c ≤ a+b. Then, e+b+d≥e+b+a implying that d≥a. Therefore, d+b+c≥a+b+c, establishing
that P kl is at least as large as the path P.

b d
a
c
a
b
c
e
d
e
(b)
(a) Figure S3.47

E XERCISE 3 . 4 9 # . Note that in Figure 3.15(b); the direction of arcs is not indicated. The direction of the arcs is
illustrated in Figure S3.49(a) and the residual network in Figure S3.49(b). The circulation can be decomposed into
flows along the following directed cycles: 2-3-4-5-2 (3 units); 2-3-1-4-5-2 (5 units); 1-4-5-1 (5 units); 1-4-5-2-1 (4
units). The flow is not optimal because it contains a negative cycle 1-2-5-4-1.

(c ,x ,u ) (c ,r )
ij ij ij ij ij
i j i j

(2,8,10) (2,2)
2 3 2 3
(-2,8)
(1,4,4) (5,5,5) (-1,4) (-5,5)

(6,3,4) (-3,12) (6,1) (-6,3)


(3,12,15) 1 (3,3) 1
(-2,5)
(0,1)
(2,5,8) (0,14)
(0,14,15) (2,3)

(4,3)
5 4 4 Figure S3.49
(4,17,20) 5
(-4,17)
(a) (b)

E XERCISE 3 . 5 1 . This result directly follows from the flow decomposition theorem discussed in Section 3.5.

E XERCISE 3 . 5 3 . Suppose that G is not strongly connected, i.e., there is a pair of nodes i and j such that there is
no directed path from node i to node j. Treating node i as the source node, apply a search algorithm to identify all
-
nodes reachable from node i. Let S be the set of reachable nodes. Clearly, j ∉ S. Hence, there is a cut [S, S ] so
-
that each arc in the cut [S, S ] is a backward arc (for, otherwise, we could add more nodes to S). It is also easy to see
-
that if there is cut [S, S ] with no forward arc, then G is not strongly connected. We have thus established that G is
-
not strongly connected if and only if there exists a cut [S, S ] with no forward arc. Now notice that if there is a cut
with no forward arc and each backward arc has a positive lower bound, then a feasible circulation cannot exist
(because mass balance constraints cannot be satisfied). This establishes that G is not strongly connected if and only
if a feasible circulation in G cannot exist. Alternatively, G is strongly connected if and only if a feasible circulation
in G exists.

You might also like