ebook img

Algebraic Graph Algorithms Lecture Notes (Stanford CS367) PDF

34 Pages·2017·1.578 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Algebraic Graph Algorithms Lecture Notes (Stanford CS367)

Lectures 1 and 2 Matrix multiplication and matrix inversion Scribe: Jessica Su Date: September 25, 2015 Editor: Kathy Cooper This class is about matrix multiplication and how it can be applied to graph algorithms. 1 Prior work on matrix multiplication Definition 1.1. (Matrix multiplication) Let A and B be n-by-n matrices (where entries have O(logn) bits). Then the product C, where AB =C is an n-by-n matrix defined by ([i,j])=(cid:80)n A(i,k)B(k,j). k=1 We will assume that operations (addition and multiplication) on O(logn) integers takes O(1) time, i.e. we’ll be working in a word-RAM model of computation with word size O(logn). There has been much effort to improve the runtime of matrix multiplication. The trivial algorithm mul- tipliesn×nmatricesinO(n3)time. Strassen(’69)surprisedeveryonebygivinganO(n2.81)timealgorithm. This began a long line of improvements until in 1986, Coppersmith and Winograd achieved O(n2.376). After 24 years of no progress, in 2010 Andrew Stothers, a graduate student in Edinburgh, improved the run- ning time to O(n2.374). In 2011, Virginia Williams got O(n2.3729), which was the best bound until Le Gall gotO(n2.37287)in2014. Manybelievethattheultimateboundwillben2+O(1),butthishasyettobeproven. Todaywe’lldiscusstherelationshipbetweentheproblemsofmatrixinversionandmatrixmultiplication. 2 Matrix multiplication is equivalent to matrix inversion Matrix inversion is important because it is used to solve linear systems of equations. Multiplication is equivalent to inversion, in the sense that any multiplication algorithm can be used to obtain an inversion algorithm with similar runtime, and vice versa. 2.1 Multiplication can be reduced to inversion Theorem 2.1. If one can invert n-by-n matrices in T(n) time, then one can multiply n-by-n matrices in O(T(3n)) time. Proof. Let A and B be matrices. Let   I A 0 D =0 I B 0 0 I where I is the n-by-n identity matrix. One can verify by direct calculation that   I −A AB D−1 =0 I −B 0 0 I Inverting D takes O(T(3n)) time and we can find AB by inverting C. Note that C is always invertible since its determinant is 1. (cid:3) 1 2.2 Inversion can be reduced to multiplication Theorem 2.2. Let T(n) be such that T(2n) ≥ (2+ε)T(n) for some ε > 0 and all n. If one can multiply n-by-n matrices in T(n) time, then one can invert n-by-n matrices in O(T(n)) time. Proof idea: First, we give an algorithm to invert symmetric positive definite matrices. Then we use this to invert arbitrary invertible matrices. The rest of Section 2 is dedicated to this proof. 2.2.1 Symmetric positive definite matrices Definition 2.1. A matrix A is symmetric positive definite if 1. A is symmetric, i.e. A=At, so A(i,j)=A(j,i) for all i,j 2. A is positive definite, i.e. for all x(cid:54)=0, xtAx>0. 2.2.2 Properties of symmetric positive definite matrices Claim 1. All symmetric positive definite matrices are invertible. Proof. Suppose that A is not invertible. Then there exists a nonzero vector x such that Ax = 0. But then xtAx = 0 and A is not symmetric positive definite. So we conclude that all symmetric positive definite matrices are invertible. (cid:3) Claim 2. Any principal submatrix of a symmetric positive definite matrix is symmetric positive definite. (An m-by-m matrix M is a principal submatrix of an n-by-n matrix A if M is obtained from A by removing its last n−m rows and columns.) Proof. Let x be a vector with m entries. We need to show that xtMx > 0. Consider y, which is x padded with n−m trailingzeros. Since A is symmetricpositive definite, ytAy >0. But ytAy =xtMx, since allbut the first m entries are zero. (cid:3) Claim 3. For any invertible matrix A, AtA is symmetric positive definite. Proof. Letxbeanonzerovector. Considerxt(AtA)x=(Ax)t(Ax)=||Ax||2 ≥0. Wenowshow||Ax||2 >0. For any x (cid:54)= 0, Ax is nonzero, since A is invertible. Thus, ||Ax||2 > 0 for any x (cid:54)= 0. So AtA is positive definite. Furthermore, it’s symmetric since (AtA)t =AtA. (cid:3) Claim 4. Let n be even and let A be an n×n symmetric positive definite matrix. Divide A into four square blocks (each one n/2 by n/2): (cid:20)M Bt(cid:21) A= . B C Then the Schur complement, S =C−BM−1Bt, is symmetric positive definite. The proof of the above claim will be in the homework. 2.2.3 Reduction for symmetric positive definite matrices Let A be symmetric positive definite, and divide it into the blocks M, Bt, B, and C. Again, let S = C−BM−1Bt. By direct computation, we can verify that (cid:20)M−1+M−1BtS−1BM−1 −M−1BtS−1(cid:21) A−1 = −S−1BM−1 S−1 2 Therefore, we can compute A−1 recursively, as follows: (let the runtime be t(n)) Algorithm 1: Inverting a symmetric positive definite matrix Compute M−1 recursively (this takes t(n/2) time) Compute S =C−BM−1Bt using matrix multiplication (this takes O(T(n)) time) Compute S−1 recursively (this takes t(n/2) time) Compute all entries of A−1 (this takes O(T(n)) time) The total runtime of the procedure is (cid:88) t(n)≤2t(n/2)+O(T(n))≤O( 2jT(n/2j)) j (cid:88) ≤O( (2/(2+ε))jT(n))≤O(T(n)). j 2.2.4 Reduction for any matrix Supposethatinvertingasymmetricpositivedefinitematrixreducestomatrixmultiplication. Thenconsider the problem of inverting an arbitrary invertible matrix A. By Claim 3, we know that AtA is symmetric positive definite, so we can easily find C = (AtA)−1. Then CAt = A−1A−tAt = A−1, so we can compute A−1 by multiplying C with At. 3 Boolean Matrix Multiplication (Introduction) Scribe: Robbie Ostrow Editor: Kathy Cooper Given two n × n matrices A,B over {0,1}, we define Boolean Matrix Multiplication (BMM) as the following: (cid:95) (AB)[i,j]= (A(i,k)∧B(k,j)) k Note that BMM can be computed using an algorithm for integer matrix multiplication, and so we have BMM for n×n matrices is in O(nω+O(1)) time, where ω < 2.373 (the current bound for integer matrix multiplication). Most theoretically fast matrix multiplication algorithms are impractical. Therefore, so called “combi- natorial algorithms” are desirable. “Combinatorial algorithm” is loosely defined, but one has the following properties: • Doesn’t use subtraction • All operations are relatively practical (like a lookup tables) Remark 1. No O(n3−(cid:15)) time combinatorial algorithms for matrix multiplication are known for ε>0, even for BMM! Such an algorithm would be known as “truly subcubic.” 4 Four Russians In 1970, Arlazarov, Dinic, Kronrod, and Faradzev (who seem not to have all been Russian) developed a combinatorial algorithm for BMM in O( n3 ), called the Four-Russians algorithm. With a small change logn to the algorithm, its runtime can be made O( n3 ). In 2009, Bansal and Williams obtained an improved log2n 3 algorithm running in O( n3 ) time. In 2014, Chan obtained an algorithm running in O( n3 ) and then, log2.25n log3n mostrecently,in2015Yu,aStanfordgraduatestudent,achievedanalgorithmthatrunsinO( n3 ). Today log4n we’ll present the Four-Russians algorithm. 4.1 Four-Russians Algorithm We start with an assumption: • We can store a polynomial number of lookup tables T of size nc where c≤2+(cid:15), such that given the index of a table T, and any O(logn) bit vector x, we can look up T(x) in constant (O(1)) time. Theorem 4.1. BMM for n×n matrices is in O( n3 ) time (on a word RAM with word size O(logn)). log2n Proof. We give the Four Russians’ algorithm. (More of a description of the algorithm than a full proof of correctness.) Let A and B be n×n boolean matrices. Choosing an arbitrary (cid:15), we can split A into blocks of size (cid:15)logn×(cid:15)logn. That is, A is partitioned into blocks A for i,j ∈[ n ]. Below we give a simple example i,j (cid:15)logn of A: i Ai,j (cid:15)logn { j Foreachchoiceofi,j wecreatealookuptableT correspondingtoA withthefollowingspecification: i,j i,j For every bit vector v with length (cid:15)logn: T [v]=A ·v. i,j i,j That is, T takes keys that are (cid:15)logn-bit sequences and stores (cid:15)logn-bit sequences. Also since there i,j are n(cid:15) bit vectors of (cid:15)logn bits, and A ·v is (cid:15)logn bits, we have |T |=n(cid:15)(cid:15)logn. i,j i,j The entire computation time of these tables is asymptotically n ( )2n(cid:15)log2n=n2+(cid:15), logn since there are ( n )2 choices for i,j, nε vectors v, and for each A and each v, computing A v take logn i,j i,j O(log2n) time for constant ε. Given the tables that we created in subcubic time, we can now look up any A ·v in constant time. ij We now consider the matrix B. Split each column of B into n parts of εlogn consecutive entries. (cid:15)logn Let Bk be the jth piece of the kth column of B. Each A Bk can be computed in constant time, because it j ij j can be accessed from T [Bk] in the tables created from preprocessing. ij j To calculate the product Q=AB, we can do the following. From j = 1 to n : Q = Q ∨(A ∧Bk), by the definition. With our tables T, we can calculate (cid:15)logn ik ik ij j the bitwise “and” (or *) in constant time, but the “or” (or sum) still takes O(logn) time. This gives us an algorithm running in time O(n· n 2·logn)=O( n3 ) time, the original result of the four Russians. logn logn 4 How can we get rid of the extra logn term created by the sum? We can precompute all possible pairwise sums! Create a table S such that S(u,v) = u ∨ v where u,v ∈{0,1}(cid:15)logn. This takes us time O(n2(cid:15)(cid:15)logn), since there are n2(cid:15) pairs u,v and each component takes only O(logn) time. This precomputation allows us constant time lookup of any possible pairwise sum of (cid:15)logn bit vectors. Hence,eachQ =Q ∨(A ∧Bk)operationtakesO(1)time,andthefinalalgorithmasymptoticruntime ik ik ij j is n·(n/εlogn)2 =n3/log2n, where the first n counts the number of columns k of B and the remaining term is the number of pairs i,j. Thus, we have a combinatorial algorithm for BMM running in O( n3 ) time. log2n (cid:3) Note: can we save more than a log factor? This is a major open problem. 5 Transitive Closure Definition 5.1. The Transitive Closure (TC) of a directed graph G=(V,E) on n nodes is an n×n matrix (cid:40) 1 if v is reachable from u such that ∀u,v ∈E (T(u,v)= 0 otherwise Transitive Closure on an undirected graph is trivial in linear time– just compute the connected compo- nents. In contrast, we will show that for directed graphs the problem is equivalent to BMM. Theorem 5.1. Transitive closure is equivalent to BMM Proof. We prove equivalence in both directions. Claim 5. If TC is in T(n) time then BMM on n×n matrices is in O(T(3n)) time. Proof. Consider a graph like the one below, where there are three rows of n vertices. Given two boolean matrices A and B, we can create such a graph by adding an edge between the ith vertex of the first row and the jth of the second row iff A ==1. Construct edges between the second and third rows in a similar ij fashion for B. Thus, the product AB can be computed by taking the transitive closure of this graph. It is equivalenttoBMMbysimplytakingthe∧ofij →jk. Sincethegraphhas3nnodes,givenaT(n)algorithm for TC, we have an O(T(3n)) algorithm for BMM. (Of course, it takes n2 time to create the graph, but this is subsumed by T(n) as T(n)≥n2 as one must at least print the output of AB.) (cid:3) Claim 6. If BMM is in T(n) time, such that T(n/2)≤T(n)/(2+(cid:15)) , then TC is in O(T(n)) time. We note that the condition in the claim is quite natural. For instance it is true about T(n)=nc for any c>1. Proof. Let A be the adjacency matrix of some graph G. Then (A+I)n is the transitive closure of G. Since we have a T(n) algorithm for BMM, we can compute (A+I)n using logn successive squarings of A+I in O(T(n)logn) time. We need to get rid of the log term to show equivalence. We do so using the following algorithm: 1. Compute the strongly connected components of G and collapse them to get G(cid:48), which is a D.A.G. We can do this in linear time. 5 2. Compute the topological order of G(cid:48) and reorder vertices according to it (linear time) 3. Let A be the adjacency matrix of G(cid:48). (A+I) is upper triangular. Compute C =(A+I)∗, i.e. the TC of G(cid:48). 4. Uncollapse SCCs. (linear time) All parts except (3) take linear time. We examine part (3). Consider the matrix (A+I) split into four sub-matrices M,C,B, and 0 each of size n/2×n/2. (cid:20) (cid:21) M C (A+I)= 0 B (cid:20)M∗ M∗CB∗(cid:21) We claim that (A+I)∗ = 0 B∗ The reasoning behind this is as follows. Let U be the first n/2 nodes in the topological order, and let V be the rest of the nodes. Then M is the adjacency matrix of the subgraph induced by U and B is the adjacency matrix induced by V. The only edges between U and V go from U to V. Thus, M∗ and U∗ represents the transitive closure restricted to U ×U and V ×V. For the TC entries for u ∈ U and v ∈ V, we note that the only way to get from u to v is to go from u to possibly another u(cid:48) ∈U using a path within U, then take an edge (u(cid:48),v(cid:48)) to a node v(cid:48) ∈V and then to take a path from v(cid:48) to v within V. I.e. the U×V entries of the TC matrix are exactly M∗CB∗. SupposethattheruntimeofouralgorithmonnnodegraphsisTC(n). Tocalculatethetransitiveclosure matrix, we recursively compute M∗ and B∗. Since each of these matrices have dimension n/2, this takes 2TC(n/2) time. We then compute M∗CB∗, which takes O(T(n)) time, where T(n) was the time to compute the boolean product of n×n matrices. Finally, we have TC(n) ≤ 2TC(n) + O(T(n)). If we assume that there is some ε > 0 such that 2 T(n/2)≤T(n)/(2+(cid:15)), then the recurrence solves to TC(n)=O(T(n)). (cid:3) It follows from claim 1 and claim 2 that BMM of n×n matrices is equivalent in asymptotic runtime to TC of a graph on n nodes. (cid:3) 6 Other Notes BMM can also solve problems that look much simpler. For example: does a directed graph have a triangle? We can easily solve with an algorithm for BMM by taking the adjacency matrix, cubing it, and checking if the resulting matrix contains a 1 in the diagonal. Somewhat surprisingly, it is also known that for any (cid:15) > 0, an O(n3−(cid:15)) time combinatorial algorithm for triangle finding also implies an O(n3−(cid:15)/3) time combinatorial algorithm for BMM. Hence in terms of combinatorial algorithms BMM and triangle finding are “subcubic”-equivalent. 6 CS 367 Lecture 2 (part 1) All-Pairs Shortest Paths Scribe: Michael P. Kim Date: October 8, 2015 1 Introduction In this lecture, we will talk about the problem of computing all-pairs shortest paths (APSP) using matrix multiplication. Formally, given a graph G=(V,E), the goal is to compute the distance d(u,v) for all pairs of nodes u,v ∈ V. Given that G has n nodes and m edges, it is easy to come up with algorithms that run in O˜(mn) time 1 – for example, on graphs with nonnegative edge weights, we can run Dijkstra’s Algorithm from each node. When G is dense, this time is on the order of n3; the natural question is can we do better? (cid:16) (cid:17) For weighted graphs, the best known algorithm for APSP runs in O √n3 time, by Ryan Williams 2Ω( logn) (2014). A major open problem is whether there exist “truly subcubic” algorithms for this version of APSP, namely, algorithms running in time O(n3−ε) for some constant ε>0. Forunweightedgraphs, weknowofalgorithmsthatachievethissubcubicperformance. Inparticular, for undirectedgraphsthereisanalgorithmrunninginO(nωlogn)timebySeidel(1992),andfordirectedgraphs there is an algorithm running in O(n2.575) time by Zwick (2002). This difference in runtime persists even with improvements in the matrix multiplication exponent, ω. For instance, if ω =2, then Seidel’s algorithm would run in O˜(n2) time, whereas Zwick’s algorithm would run in O˜(n2.5) time. In this lecture, we will talk about the algorithms for unweighted graphs. In particular, we discuss a baseline algorithm using a hitting set which will work for directed or undirected graphs, and then describe Seidel’s algorithm for undirected graphs. 2 Hitting Set Algorithm Given G, and particularly the adjacency matrix A which represents G, we would like to compute distances betweenallpairsofnodes. Anaturalfirstalgorithmwouldbetocomputethedistancesbysuccessiveboolean matrix multiplication of A with itself. The (i,j)th entry in Ak is 1 if and only if i has a path of length k to j. Thus, if the graph has finite diameter, then for all i,j, d(i,j)=min{k | Ak[i,j]=1}. Fact 2.1. If G has diameter D we can compute APSP in O(Dnω) time. If the diameter of G is small, then we have found a fast algorithm for computing APSP, but D can be O(n) in which case we have no improvement from O(n3) run time. Nevertheless, we can compute all short distanceslessthansomek inO(knω)time, andthenemployanothertechniquetocomputelongerdistances. The key idea is to use a “hitting set”. Lemma 2.1. (Hitting Set) Let S be a collection of n2 sets of size ≥k over V =[n]. With high probability, a random subset T ⊆V of size O(nlogn) hits all the sets in S. k Withthishittingsetlemmainmind, wecanuseAlgorithm1tocomputedistancesthataregreaterthan or equal to k. Withhighprobability,thisalgorithmwillcomputedistances≥kcorrectly(asthesepathsinvolveatleast k nodes, so with high probability T hits the path). The algorithm requires running Dijkstra’s algorithm from O(nlogn) nodes so takes O˜(nn2) time. If we use this algorithm to compute long distances and the k k iterativematrixmultiplicationtocomputeshortdistances,wehaveanalgorithmforall-pairsshortestpaths. Theorem 2.1. Let G be a directed or undirected graph on n nodes, with unit weights. APSP of G can be computed in O˜(knω+ nn2) time. k 1O˜(T(n))=O(T(n)·polylogn). Inotherwords,thepoly-logarithmictermshavebeendropped. 1 Algorithm 1: LongDist(V,E) Pick T ⊆V randomly s.t. |T|=c· nlogn for large enough constant c k foreach t∈T do Compute Dijkstra(t) foreach u,v ∈V do Compute d(u,v)=min d(u,t)+d(t,v) t∈T (cid:16) (cid:17) When we optimize for a choice of k and set it to n(3−ω)/2, the runtime comes out to be O˜ n3+2ω which is roughly O˜(n2.69). 3 Seidel’s Algorithm While this first algorithm gives us a fast algorithm for computing APSP, the question remains, can we do better? In particular, can we avoid computing short and long distances separately? Can we leverage matrix multiplication to compute all the shortest paths? In fact, we can improve on the hitting set algorithm for undirected graphs as follows. Given a graph G with adjacency matrix A, consider its boolean square A2 = A·A, where · represents boolean matrix multiplication. Consider a graph G(cid:48) with adjacency matrix A(cid:48) =A2∨A. (cid:108) (cid:109) Fact 3.1. d (s,t)= d(s,t) G(cid:48) 2 To see this fact, note that edges in A2 represent paths of length 2 in the original graph G, and G(cid:48) also contains the edges of G. Thus, any path of length 2k in G induces a path of length k in G(cid:48) using only edges of A2, and also any path of length 2k+1 induces a path of length k (from A2) followed by a single original edge, thus forming a path of length k+1. Now suppose that we have a way of determining the parity of the distance between all pairs of nodes. Then we can use the following recursive strategy to compute APSP. Algorithm 2: APSP Idea Given an adjacency matrix A Compute A2∨A Recursively compute d(cid:48) ← APSP(A2∨A) foreach u,v ∈V do if d(cid:48)(u,v) is even then d(u,v)=2d(cid:48)(u,v) else d(u,v)=2d(cid:48)(u,v)−1 Note that in each recursive call, the diameter of the graph decreases by 2, and that after logn iterations, A will be the all 1s matrix with 0s along the diagonal, which we can detect. Thus, if we can find a way to determine the parity of a u,v-path efficiently, we should obtain an efficient recursive algorithm for APSP. Consideranypairofnodesi,j ∈V andanothernodewhichisaneighborofj, k ∈N(j). Bythetriangle inequality (which holds in unweighted, undirected graphs), we know d(i,j)−1≤d(i,k)≤d(i,j)+1. Claim 1. If d(i,j)≡d(i,k) mod 2, then d(i,j)=d(i,k). Proof. By the triangle inequality, d(i,j) and d(i,k) differ by at most 1. Thus, if their parity is the same, they must also be equal. (cid:3) 2 Claim 2. Let d (i,j) be the distance between i and j in G2 defined by A2∨A. Then, (a) if d(i,j) is even G2 and d(i,k) is odd then d (i,k)≥d (i,j). (b) If d(i,j) is odd and d(i,k) is even, d (i,k)≤d (i,j) and G2 G2 G2 G2 there exists a k(cid:48) ∈N(j) such that d (i,k(cid:48))<d (i,j). G2 G2 Proof of (a). d(i,j) d (i,j)= G2 2 d(i,k)+1 d(i,j) d (i,k)= ≥ G2 2 2 so d (i,k)≥d (i,j). (cid:3) G2 G2 Proof of (b). d(i,j)+1 d(i,k) d (i,j)= ≥ =d (i,k) G2 2 2 G2 so in general, d (i,k) ≤ d (i,j), and for the neighbor of j along the shortest path from i to j, which we G2 G2 call k(cid:48), we know d(i,k(cid:48))<d(i,j). (cid:3) Claim 3. If d(i,j) is even, then (cid:88) d (i,k)≥deg(j)d (i,j) G2 G2 k∈N(j) and if d(i,j) is odd, then (cid:88) d (i,k)<deg(j)d (i,j) G2 G2 k∈N(j) This third claim follows directly from the first two. Additionally, if we can compute the sums here in O(nω) time, then the overall runtime will be O(nωlogn) as desired. The right expression can be computed in O(n2) time, which will be subsumed by the O(nω) term. Consider D, an n×n matrix where D(i,j) = d (i,j). We want to decide for each i,j pair whether G2 (cid:80) d (i,k)<deg(j)d (i,j). Consider the integer matrix product DA. Note that k∈N(j) G2 G2 (cid:88) (D·A)[i,j]= d (i,k) G2 k∈N(j) so this matrix product allows us to compute the left expression. Now we are ready to state Seidel’s Algorithm in full. Claim 4. Seidel’s Algorithm runs in O(nωlogd) time where d refers to the diameter of the graph. Proof. The run time can be expressed as the following recurrence relation. d T(n,d)≤T(n, )+O(nω) 2 =⇒ T(n,d)≤O(nωlogd) Because d≤n, this run time is upper bounded by O(nωlogn). (cid:3) Note that Seidel’s Algorithm relies on fast integer matrix multiplicaion, which runs in O(nω), but for whichnoknownfastcombinatorialalgorithmsexist. Somequestionsremainopenwhoseanswerscouldspeed up the computation of APSP in theory and in practice: Is the integer matrix multiplication step avoidable? Are there fast combinatorial matrix multiplication algorithms over the integers? 3 Algorithm 3: Seidel(A) if A is all 1s except the diagonal then return A else Compute boolean product A2 D ← Seidel(A2∨A) Compute integer product D·A R←0n×n foreach i,j ∈V do if DA(i,j)<deg(j)D(i,j) then R(i,j)←2D(i,j)−1 else R(i,j)←2D(i,j) return R References [1] Raimund Seidel, On the All-Pairs-Shortest-Path Problem in Unweighted Undirected Graphs, Journal of Computer and System Sciences 51, pp. 400-403 (1995). 4

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.