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)/2Let 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)/2Also,
|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)/2This proves the induction step.
Solution:
One possible tree is:
(1) | | (2) | | (3) | | (5) | | (4) | | (6)This does not show the back edges.
Solution:
The following is a BFS tree:
(1) | /--------------------------------------------------------\ | | | | | | (2) (4) (6) | | | | (3) (5)The other alternative is:
(1) | /--------------------------------------------------------\ | | | | | | (2) (4) (6) | | | | (3) (5)
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("-----------------------------------------------"); } } }
For part (b), we will also do induction, but before we can do
that, we will need a combinatorial lemma:
Let di be the degree of the ith vertex
of G. Then,
d1 + d2 + ... + d|V| = 2 |E|The proof is simply noting that adding all the degrees, counts each edge twice.
So assuming all that has been proved. We proceed with the
induction.
We have to prove that if an undirected graph has |V|-1 edges
and no cycles, then the graph is connected.
The induction is on |V|.
Base case: |V| = 1. Thus the graph is just a single vertex,
and no edges. This is connected.
Induction Step: By the above lemma, there exists a vertex
v of degree 1. Let the only edge from v
connect it to the vertex v'.
Construct the graph G' by deleting v and the (single) edge
incident on it. This is a graph with |V| - 1 vertices and
|V|-2 edges and no cycles; thus it is connected by induction
hypothesis. Thus, in the original graph, G, if we pick any two
vertices, v1 and v2; either, both of them lie in G', in which
case there is a path connecting them in G', and hence in G; or
else, one of them, say v1, is actually v; and the other lies
in G'. In this case, by the connectedness of G', there is a
path from v' to v2, and appending the edge between v' and v to
this path we get a path from v (= v1) and v2.
Thus G is connected, and this completes the proof.