User:Zelhar/Algorithms1 maman15



1.1
I assume that G=(V,E) is given in the form of adjacency lists, and that at O(V) time). Moreover, I assume V={1,...,n}, and that E is also given as a list of ordered pairs (building such list takes O(E)).   Find-Triangles(G)        for (i=1 to n):            for (j=1 to n):                a[i,j]=0        for (u&isin;V):            for (v&isin;Adj[v]):                if (u<v): a[u,v]=1       //each edge {u,v} is given a direction: (u,v) :  u<v          //now a[] represent G without self edges and with         t = 0         //t is the triangles counter        for (e&isin;E):            u = e[0]            v = e[1]            for (x&isin;V):                if ( (u<x<v) &and; (a[u,x]*a[x,v]&ne;0) ):                    t = t+1    return t Clearly this algorithms counts all triplets (i,j,k) such that i<j<k and (i,j),(j,k),(i,k) &isin; E, Which corresponds to number of non degenerated triangles in G. The run time is clearly O(VE+V2)=O(VE) (assuming |E|=&Omega;(V)). It wouldn't be difficult to also search for degenerated triangles of the form (i,i,k) without affecting the asymptotic run time.
 * E|=%Omega;(V) (if E=o(V) than we can search and then delete isolated vertices

1.2
(Again I assumed there are no self edges) Find-T(G): »Construct A - G's adjacency matrix as if it was digraph with edges as in 1.1 »B=AA //using Fast matrix multiplication such as Strassen. »C=B*A where * stands for scalar multiplication t=0 for (i=1 to n): for (j=i+1 to n): t=t+c[i,j] return t/3

We know that in B, B[i,j] = |{paths of length 2 from i to j}| - this fact has been proved in a slide published at website. It follows that A[i,j]B[i,j]=k > 0 iff (i,j)&isin;E &and; there are B[i,j]=k (since A[i,j]=1) triangles with base (i,j). Each triangle is counted this way 3 times, so t must be divided by 3.

The running time: Constructing A takes O(n2), B take O(nlog27) to calculate, C takes O(n2), the nested loop take O(n2), hence the algorithm takes O(n2).

2.1
This is easy using the fact that Ak[i,j] = |{paths of length k from i to j}|: if there are no cycles- let k be the length of the longest path in G (k<=n-1 by the pigeon hole principle), then &forall;(i,j)Ak+1[i,j]=0. On the other hand, if (i,j) belongs to a cycle of length l, then any k revolutions of this cycle are a path of length lk and &forall;kAlk[i,j] &ne; 0, hence A is not nilpotent.

2.2
Since A is 0-1 matrix, it is the adjacency matrix of a unique digraph G on n vertices. If A is nilpotent then G has no cycles, but then the every path in G is of length &le; n-1, and so An[i,j] = 0 as explained in 2.1

2.3
Find-nil-deg(A) k=1 B[1]=A while (B&ne;0): B[2k]=B[k]B[k] //use strassen method k=2k C = B[k/2] //C &ne;0 l = k/2 j=lg(k)-2 while (j>1): X = CB[j] if (X!=0): C = X               l = l + 2^j j = j-1 return l+1 The first loop finds the first power of 2 that nullifies A. The second Loop then finds the exact list power that nullifies A, by halving the unknown gap in each iteration. There are O(lgn) iterations in the loops (We know that A^n=0 since the characteristic polynomial of A is x^n). In each iteration it performs matrix multiplication using strassens method, so in total the running time is: O((nlg(7))lg(n)).

P3
$$ X = \begin{pmatrix} I & 0 \\ B & A \end{pmatrix} A^3 = \begin{pmatrix} I & 0 \\ B+AB+A^{2}B & A^3 \end{pmatrix} Y= \begin{pmatrix} I & 0 \\ B & -A \end{pmatrix} Y^3= \begin{pmatrix} I & 0 \\ B-AB+A^2B & -A^3 \end{pmatrix} $$ but: $$ 2AB = B+AB+A^{2}B - B-AB+A^2B $$ This method requires O(S(6n)), but in order to calculate AB, we actually only need to know the multiplications of the 8 sub-matrices: a,b,c,d,e,f,g,h which are (n/2), so we can use the method above on to multiply pairs of sub-matrices and combine the result in O(n^2)+O(S(n)) = O(S(n)), since clearly any method that calculates A^3 must take at list O(n^2).

P4
We use the cancellation lemma. (w2nk)2 = w2n2k = wnk. Let q(x)=p(x2). Then for k=0...n-1: Then yk = yk+n = q(w2nk) = p(wnk) = vk DFT2n(q(x)) = (yj)undefined.

P5
w = (1/(1^2), 1/2^2, ..., 1/(n-1)^2) u = (q1,..., qn-1) v = (1/((n-1)-1)^2, 1/(n-2)^2,..., 1/1^2,0) Let * stand for convolution operation. For 0