CSCI 256
Design and analysis of algorithms
Assignment 11<4/H1>

Due Monday, 4/30/2001


Only turn in problems from the second section.

Practice problems:

  1. Problem 27.2-1 on page 599 of the text.

    Solution: The flow from {s, v2, v} to {v1, v3, t} is 11 + 1 -4 +7 + 4 = 19. The capacity is 16 + 4 + 7 + 4 = 31.

Problems to be turned in:

  1. You head a major political party in the 2004 presidential election. Once again there are problems in Florida. You desperately need to get as many lawyers from New York to Tallahassee as you can by the end of the day. You have a schedule F of n flights available to you today. An entry Fi in this schedule is described by five values Fi = (si, ti, di, ai, ci):

    Give an efficient algorithm that will find a way to get the most lawyers as possible from new York to Tallahassee via airplane. You may assume that all flights run precisely on time (hah!), and that transfers between flights are instant (i.e., if ai ¾ di then it is possible for one lawyer to take flight i and then flight j). Be sure to argue correctness and analyze the running time of your algorithm.

    Hint: Transform the schedule into a max-flow instance, then solve the max-flow instance. Let the nodes be pairs of cities and times. Flights give some of the edges, but others arise from nodes representing the same cities, but different times. In your solution, be sure to describe the flow network carefully and illustrate it with an example. You may use any max-flow algorithm as a black box.

    Solution: For each plane flight segment, create a new node for the starting city and departing time, create another node for the ending city and time, and create an edge between the two with the capacity of the flight. For example if a plane leaves Chicago at 9am and arrives at Orlando at 1pm and has capacity 75, then create nodes < Chicago, 9am, d > and < Orlando, 1pm, a > (where the "d" and "a" are used to mark that Chicago is the departure city of the flight and Orlando is the arrival) and create an edge between them with capacity 75.

    We now add nodes representing the possibility of travellers catching connecting flights. If < C, ta, a > and < C, td, d > are nodes representing arrivals and departures from the same city, C, and ta <= td (i.e., it is possible to catch the departure after arriving), then add an edge from < C, ta, a > to < C, td, d > with infinite capacity.

    Finally add a new source node, s, and edges from the source node to all nodes representing departures from New York City, and give them all infinite capacity. Similarly add a new sink node, t, and add edges with infinite capacity to t from all nodes representing arrivals to Tallahassee.

    Capacities are unrestricted from the source to departure flights from NYC. Capacities are set to the number of seats available for edges corresponding to flights. Capacities are unrestricted in getting from arriving flights to connecting departure flights that are later in the day. Finally, capacities are unrestricted in edges corresponding to flights arriving in Tallahassee to the sink.

    As a result, any flow through this network will correspond to a feasible routing of lawyers from NYC to Tallahassee. Thus use Ford-Fulkerson to solve the maximum network flow problem and you are done.

  2. In the midterm you learned how to set up a round-robin tournament. Now we'd like to rank the players. Assume that a round-robin tournament is played among n players. That is, each player plays once against all n-1 other players. There are no draws, and the results of all games are given in a matrix. It is not possible in general to sort the players, since A may beat B, B may beat C, and C may beat A (in other words, the results are not necessarily transitive). We are interested in a "weak" sorting as follows. Design an algorithm to arrange the players in an order P1, P2, ..., Pn such that P1 beat P2, P2 beat P3, and so on (concluding with Pn-1 beating Pn) given the matrix of results. The worst-case running time of the algorithm should be O(n lg n). (You may assume that any entry in the matrix can be accessed in constant time.) Hint: Think divide and conquer. Also note that the solution to this problem does not depend on anything covered recently in class. (I assigned it as a warm-up for the next problem!)

    Solution: We proceed in a way similar to merge sort. If there are only two players, use the results of the game between them to rank them (one player is even easier!). If there are more than two players, divide them into two (nearly) equal groups (if the total is odd, then one group will have one extra player). Recursively rank the players in the two halves. Now we need only merge these two rankings into one single legal ranking.

    Let P1, P2, ..., Pn be the ranking of the players in the first group, while P'1, P'2, ..., P'n' be the ranking of players in the second group. We know that for all j, Pj beat Pj+1 and P'j beat P'j+1. Proceed as follows:

        Merge(P, n, P', n')
           i <- 1      // next available element from list P
           i' <- 1     // next available element from list P'
           j <- 1      // next empty slot in merged list, Q
           while i <= n & i' <= n' do
             // invariant:  if j>1 then Qj-1 beat both Pi and P'i'
             if Pi beat P'i' then
                Qj <- Pi
                i++
             else
                Qj <- P'i'
                i'++
             j++
           Add remaining elements from whichever list hasn't been used up to Q
        

    We need to show that this ordering is legal. I.e., we must show that for all k, Qk beats Qk+1. This follows from the while loop invariant, so we first show the invariant is true.

    The invariant is clearly true the first time it is encountered because j = 1. After the first time through, it is also true. Suppose that P1 beat P'1. Then Q1 = P1 and i and j are reset to be 2. Because P1 beat P2, clearly Q1 beat both P'1 and P2. I.e., Qj-1 beat both P'i and Pi. A similar argument works if P'1 beat P1.

    Suppose j > 1 and the invariant holds. We must show that if we go through the loop again the invariant will still hold. Again suppose that Pi beat P'i'. Then Qj <- Pi, and then j and i are both increased by 1. After the updates, Qj-1 = Pi-1 and Qj-1 beat P'i'. Of course it is also the case that Qj-1 = Pi-1 beat Pi. Thus the invariant holds. A similar argument goes through if P'i' beat Pi when the loop body is executed. Thus the invariant always holds.

    Let Qk and Qk+1 be successive elements of Q. Then when at the top of the while loop and j = k+1, it follows that Qk beats both Pi and P'i'. But inside the loop body, Qj = Qk+1 is set to either Pi or P'i'. Thus in either case, Qk beats Qk+1. Thus the generated ordering of Q is legal.

    The time for ranking is O(n lg n) because the merge portion of the algorithm takes time O(n) for n players, and the solution to T(n) = 2T(n/2) + O(n) is T(n) in O(n lg n).

  3. Consider the preceding problem in this assignment (ranking the players in a round-robin tournament).

    1. Show that any algorithm for the ranking players problem can be used to sort n numbers. Thus sorting reduces to the ranking players problem. Hint: Interpret the ordering of the numbers as giving the results of playing a "match" between the two numbers.

      Solution: We show that Sorting reduces to "Ranking players". Let x1, ... xn be a collection of numbers to be sorted. The reduction works as follows: We interpret xi < xj as "player xi" beating "player xj" (there is no need to record these in a matrix). Run the ranking players algorithm on the numbers to get a ranking Q of the players. This ranking results in putting the numbers in increasing order. (If two numbers are the same, then flip a coin to decide which beats the other in order to run the ranking algorithm.)

    2. Use the results of part (a) to show that Omega(n lg n) is a lower bound for the time of any algorithm to solve the ranking players problem on a set of n players. Hint: Suppose you could solve the ranking players problem in time less than n lg n, what would that tell you about a lower bound for sorting.

      Solution: If the ranking algorithm takes time T(n) for n players, then we use it to perform the sort of n numbers also in time T(n). (Interpreting the order as wins or losses takes no extra time and interpreting the ranking as an ordering of the numbers also takes no extra time.) Thus if T(n) is o(n lg n) we could also sort in time o(n lg n). Thus any ranking algorithm must take time at least Omega(n lg n).


    Back to:

  4. CS256 home page
  5. Kim Bruce's home page
  6. CS Department home page
  7. kim@cs.williams.edu