Solution for Part E -- Big-oh (part deux)



  1. Describe briefly what kind of nodes are in each of the following sets at any one time: in set S; in set C; in both set S and set C; in neither set S nor set C.

    Answer: set S: unprocessed vertices. set C: vertices in the current component that have received a colour, but whose neighbours we haven't yet checked. set S and set C: no vertex is ever in both sets at the same time. not in set S or in set C: vertices in components that have been fully processed. That is, they are coloured and their neighbours have been checked.

  2. In terms of which edges are present in a graph, what is the worst (most time-consuming) situation for the algorithm? Explain.

    Answer: The worst case is a graph where the nodes can be divided into two sets where each node in one set is connected to every node in the other. (Note: This is called a ``complete bipartite'' graph.)

    Proof (your explanation didn't have to be this formal):

    When a vertex is placed in C, it is removed from S. This happens exactly once for each vertex v.

    Therefore each iteration of the middle loop reduces the number of iterations of the outer loop by 1. Thus, the number of times the guards of the middle and outer loops are tested, combined, is O(n).

    So the way to maximize the total number of steps is to maximize the number of iterations of the inner loop. This happens when all edges are present in a bipartite graph, so that inner loop always checks n/2 neighbours, which gives a worst-case running time of O( tex2html_wrap_inline110 ) for the whole algorithm. (If more edges are present, then the graph is not bipartite, and a conflict will be found before all vertices are processed.)

    Note: we don't need to prove that all vertices are visited, since this isn't a correctness proof.

  3. In this worst case, how often is the outermost loop executed?

    Answer: The outermost loop executes only once, since the middle loop processes all the vertices.

  4. How many times can a particular vertex be removed from S, and why?

    Answer: once, since vertices are never placed back in S.

  5. How many times can a particular vertex be inserted into C, and why?

    Answer: Once. The insertions happen in the first part of the if-statement, and the guard for that is true only when the current vertex w does not yet have a colour; in that first part, w is given a colour.

  6. Based on these answers and the assumptions made above, analyze the worst-case running time of the 2-colour algorithm. Use big-oh notation, and explain all of your steps.

    Answer: The inner loop is O(n). The middle loop executes n times, once for each vertex placed in C. The outer loop executes exactly once (see question 3 above). So the algorithm is O( tex2html_wrap_inline110 ).

  7. If removing a vertex from a SetOfInts is O(n), rather than O(1) as we assumed earlier, how is the worst-case running time of 2-colour affected?

    Answer: Since the innermost loop has a removal from S, and this takes O(n), the running time is increased by an order of magnitude to O( tex2html_wrap_inline134 ).



About this document ...

This document was generated using the LaTeX2HTML translator Version 96.1 (Feb 5, 1996) Copyright © 1993, 1994, 1995, 1996, Nikos Drakos, Computer Based Learning Unit, University of Leeds.

The command line arguments were:
latex2html -split 0 -no_navigation Esoln.

The translation was initiated by Diane Horton on Mon Apr 27 11:30:36 EDT 1998


Diane Horton
Mon Apr 27 11:30:36 EDT 1998