v22.0310-003: Homework 4 Solution


  1. Problem 7.1 (page 219) in Text.
    This is a proof by induction of the maximum number of edges in a graph.
    To get full credit, the proof should be logically perfect and every sentence should be an Englist statement. So the whole proof should read like an essay. You should probably rewrite your proof several times until you get it right.

    Solution: :
    To prove by Induction: A graph with n vertices has at most n(n-1)/2 edges.

    Base Case: n = 0.
    In this case, the number of vertices is 0, and therfore the number of edges is also 0, and the result is satisfied.
    Induction step:
    Let G = (V,E) be the graph with n vertices; i.e. |V| = n. We have to show that

    	|E| <= n(n-1)/2
    	
    Let v be any vertex in G; i.e. v is an element of V. Let
    	V1 = V - {v}.
    	
    Also, let the edge set, E of G be decomposed as
    	E1 = { e in E | e is incident on v}
    	E2 = E - E1,
    	
    where - denotes set difference.

    Then, (V1, E2) is a graph with n-1 vertices. Therefore, by the induction hypothesis,

    	|E2|  <=  (n-1)(n-2)/2
    	
    Also,
    	|E1| <= n-1,
    	
    because there are at most n-1 vertices different from v.

    Thus,

    	|E| = |E1| + |E2| <= (n-1) + ( (n-1)(n-2)/2 ) = n(n-1)/2
    	
    This proves the induction step.

  2. Problem 7.4 (page 220) in Text.
    [Example of Depth First Search]

    Solution:
    One possible tree is:

    
                                     (1)
                                      |
    				  |
    				 (2)
    				  |
    				  |
    				 (3)
    				  |
    				  |
    				 (5)
    				  |
    				  |
    				 (4)
    				  |
    				  |
    				 (6)
    
    
    
    	
    This does not show the back edges.

  3. Problem 7.5 (page 221) in Text.
    [Example of Breadth First Search]

    Solution:
    The following is a BFS tree:

    
                                          (1)
    				       |
                 /--------------------------------------------------------\
                 |                         |                              |
                 |                         |                              |
                (2)                       (4)                            (6)
                 |                         |
                 |                         |
                (3)                       (5)
    
    	
    The other alternative is:
    
                                          (1)
    				       |
                 /--------------------------------------------------------\
                 |                         |                              |
                 |                         |                              |
                (2)                       (4)                            (6)
                 |                                                        |
                 |                                                        |
                (3)                                                      (5)
    
    	

  4. Programming Question.
    Solution
    :
    import java.math.*;
    import java.util.*;
    
    
    public final  class mult {
        private static int numBits = 20;
        private static int maxTrials = 10;
    
        private static BigInteger mult1(BigInteger n1, BigInteger n2)
        {
    	int length1 = n1.bitLength();
    	int length2 = n2.bitLength();
    	int length = (length1 > length2) ? length1: length2;
    	int shift = length / 2;
    	int shift2 = shift + shift;
    
    	BigInteger a1, b1, a2, b2;
    	BigInteger t1, t2, t3;
    
    	if(length <= 1) return n1.multiply(n2);
    
    
    	a1 = n1.shiftRight (shift);
    	a2 = n2.shiftRight (shift);
    	b1 = n1.subtract (a1.shiftLeft(shift));
    	b2 = n2.subtract (a2.shiftLeft(shift));
    
    	t1 = mult1(a1, a2);
    	t2 = mult1(a1.add(b1), a2.add(b2));
    	t3 = mult1(b1, b2);
    
    	return t1.shiftLeft(shift2).add(t2.subtract(t1).subtract(t3).shiftLeft(shift)).add(t3);
    
    
        }
    
        public static BigInteger multiply(BigInteger n1, BigInteger n2)
        {
    
    	int s1, s2;
    
    	s1 = n1.signum();
    	s2 = n2.signum();
    
    	if ((s1 == 0) || (s2 == 0))
    	    return BigInteger.ZERO;
    
    	if(s1 < 0)
    	    {
    		if(s2 < 0)
    		    return mult1(n1.negate(), n2.negate());
    		else
    		    return mult1(n1.negate(), n2).negate();
    	    }
    	else
    	    {
    		if(s2 < 0)
    		    return mult1(n1, n2.negate()).negate();
    		else
    		    return mult1(n1, n2);
    	    }
    
    
        }
    
        public static void main (String [] args){
    
    	Random rnd = new Random ();       /* Use the starting time as seed for the **
    					  ** Random number generation              */
    	BigInteger b1, b2, r1, r2;
    	int iteration;
    
    
    
    
    	if (args.length == 0) {
    	    System.out.println ("Number of bits is " + numBits);
    	}
    	else if (args.length > 1) {
    
    	    System.err.println("Usage: java mult [numBits]");
    	    return;
    	}
    	else{
    	    try{
    		numBits = Integer.parseInt(args[0]);
    	    }
    	    catch(Exception e){
    		System.err.println("Usage: java mult [numBits]");
    		return;
    	    }
    	    if(numBits <= 0) {
    		System.err.println("Usage: java mult [numBits: positive integer]");
    		return;
    	    }
    
    	}
    
    	for(iteration = 0; iteration < maxTrials; iteration++){
    	    b1 = new BigInteger (numBits, rnd);
    	    b2 = new BigInteger (numBits, rnd);
    	    r1 = b1.multiply(b2);
    	    r2 = multiply(b1, b2);
    	    System.out.println("            b1 = " + b1.toString());
    	    System.out.println("            b2 = " + b2.toString());
    	    System.out.println("normal product = " + r1.toString());
    	    System.out.println("    my product = " + r2.toString());
    
    	    System.out.println("-----------------------------------------------");
    
    
    	}
        }
    
    
    
    }
    
    



  5. RECOMMENDED EXTRA PROBLEMS: Do not hand in, as they will not be graded. But we will give solutions and discuss this. Please try them yourself first!