Counting Triangles in Data Streams∗
∗ † Luciana S. Buriol Gereon Frahling Stefano Leonardi Departamento de Eletrˆonicae Heinz Nixdorf Institut Dipartimento di Informatica e Computac¸˜ao University of Paderborn, Sistemistica Universidade Federal de Germany Universit´adi Roma “La Santa Maria, Brazil frahling@uni- Sapienza", Italy [email protected] paderborn.de [email protected]
Alberto Christian Sohler Marchetti-Spaccamela Heinz Nixdorf Institut Dipartimento di Informatica e University of Paderborn, Sistemistica Germany Universit´adi Roma “La Sapienza", Italy [email protected] [email protected]
ABSTRACT tion of clustering and transitivity coefficient, and discovery We present two space bounded random sampling algorithms of frequent patterns in large graphs. that compute an approximation of the number of triangles We have implemented both algorithms and evaluated their in an undirected graph given as a stream of edges. Our first performance on networks from different application domains. , algorithm does not make any assumptions on the order of The sizes of the considered graphs varied from about 8 000 , edges in the stream. It uses space that is inversely related to nodes and 40 000 edges to 135 million nodes and more than the ratio between the number of triangles and the number of 1 billion edges. For both algorithms we run experiments s , , , , , triples with at least one edge in the induced subgraph, and with parameter =1000, 10 000, 100 000, 1 000 000 to constant expected update time per edge. Our second algo- evaluate running time and approximation guarantee. Both rithm is designed for incidence streams (all edges incident to algorithms appear to be time efficient for these sample sizes. the same vertex appear consecutively). It uses space that is The approximation quality of the first algorithm was vary- s , , inversely related to the ratio between the number of triangles ing significantly and even for =1000 000 we had more and length 2 paths in the graph and expected update time than 10% deviation for more than half of the instances. O(log |V |·(1+s·|V |/|E|)), where s is the space requirement The second algorithm performed much better and even for s , of the algorithm. These results significantly improve over =10000 we had an average deviation of less than 6% previous work [20, 8]. Since the space complexity depends (taken over all but the largest instance for which we could only on the structure of the input graph and not on the num- not compute the number of triangles exactly). ber of nodes, our algorithms scale very well with increasing Categories and Subject Descriptors: H.3 [Information graph size and so they provide a basic tool to analyze the Systems]: Information Storage and Retrieval structure of large graphs. They have many applications, for General Terms: Algorithms, Theory, Performance. example, in the discovery of Web communities, the computa- Keywords: Streaming algorithms, graph algorithms, net- work analysis. ∗This work was partially supported by the EU within the 6th Framework Programme under contract 001907 “Dynam- ically Evolving, Large Scale Information Systems” (DELIS) 1. INTRODUCTION ∗ Part of this work was done while the author was post-doc Graphs are fundamental structures for modeling complex at Universit´a degli Studi di Roma “La Sapienza” relationships between data in Web documents, chemical com- † Part of this work was done while the author was visiting the pounds, XML, social networks etc. A basic tool to uncover School of Computer Science at Carnegie Mellon University their structural design principles and to extract relevant in- formation is to mine the most frequent interconnection pat- terns occurring in the graph. The computation of network indices based on counting the Permission to make digital or hard copies of all or part of this work for number of certain small subgraphs is a basic tool in the anal- personal or classroom use is granted without fee provided that copies are ysis of the structure of large networks. The clustering coeffi- not made or distributed for profit or commercial advantage and that copies cient [18] is defined as the normalized sum of the fraction of bear this notice and the full citation on the first page. To copy otherwise, to neighbor pairs of a vertex of the graph that are connected. republish, to post on servers or to redistribute to lists, requires prior specific The related transitivity coefficient of a graph [6], is defined permission and/or a fee. PODS’06, June 26Ð28, 2006, Chicago, Illinois, USA. as the ratio between three times the number of triangles and Copyright 2006 ACM 1-59593-318-2/06/0006 ...$5.00. the number of length two paths in the graph. More recently,
253
much attention has been devoted to the analysis of complex The algorithms are obtained through a so called ”list” effi- networks arising in information systems, software systems, cient reduction to the problem of computing frequency mo- overlay networks etc. Mining the most frequent subgraphs is ments [1]. Subsequently, more algorithms have also been here aimed to identify the building blocks of universal classes developed for the adjacency stream model [8]. These solu- of complex networks [14]. As an example, the occurrence of tions are still far from being practical for most real world a very large number of certain dense subgraphs has been networks. observed in the Webgraph, the graph formed by Web pages and hyperlinked connections [10], in the attempt of trac- 1.1 Our contribution ing the emergence of hidden cyber-communities. A stochas- In this work we specifically present unbiased estimators tic model of the growth of the Webgraph [9], the ”copying for the number of triangles in the graph and the number of model”, has these dense subgraphs as building blocks of the cliques of any size. Our data stream algorithms compute a process of network formation. (1 + )-approximation with probability 1 − δ.Acomplete Finding frequent graph patterns also finds application to overview of the application of our method to other classes graph databases where the classical graph query problem of subgraphs will be given in the full version of our work. consists of finding all the graphs of the database containing When estimating the number of triangles, if the graph is a given query pattern as a subgraph. A direct indexing of given as a stream of edges in arbitrary order, the data struc- 1 1 |T1|+|T2| the most frequent subgraphs, up to a maximum size, that O 2 · · Ti ture uses ( log( δ ) (1+ |T3| )) memory cells, where occur in the graph [19] has for instance been proposed as an denotes the set of node-triples having i edges in the induced alternative to path indexing [16]. subgraph. |T3| is therefore the measure we like to estimate. Counting the number of certain subgraphs in a large graph This is always better than the naive sampling algorithm that is a challenging computational task. The current state of 1 1 |T0|+|T1|+|T2| requires O( 2 log( )(1+ )) memory cells, while the art provides methods that are either computational in- δ |T3| it dramatically improves by a cubic factor the solution pro- feasible on large data sets or do not provide any guarantee vided in [20]. Comparing our results in this model with the on the accuracy of the estimation. The best known meth- previous work in [8], we obtain a one-pass algorithm that ods for the solution of the simplest non trivial version of achieves the same space bound and better update time as this problem, i.e. counting the number of triangles in a the three pass algorithm from [8]. The two other algorithms subgraph, reduces to matrix multiplication [3]. This is not in [8] either require bounded maximum degree or are incom- computational feasible even on graphs of medium size, be- parable to our result because the space complexity depends cause of time complexity and the space required to store on different parameters (e.g., thenumberofcyclesoflength the whole graph and the related data structures in main 4 and 6 in the graph). The number of memory cells used memory. Schank and Wagner [15] give an extensive experi- by our algorithm still depends on the cardinality of T ,that mental study of the performance of algorithms for counting 1 can be as large as O(|E|·|V |). Our method in the case of and listing triangles in graphs. graphs in arbitrary order is therefore of practical interest for A natural way to address the problem of computing with networks with a large enough number of triangles. massive data sets is to resort to the data stream model [7, Of greater practical relevance is our method when the 12]. In this model data arrives in a stream, one item at graph is given as an incidence stream. The number of mem- a time, and the algorithms are required to use very little 1 1 ory cells used by our data structure is O( 2 log( )log(|V |)(1+ space and per-item processing time. Secondary and slower δ |T2| memory storage devices naturally produce data streams for |T3| )). To give a flavor of the quality of our result, observe which multiple passes of computation are usually prohibitive |T3| / that |T2| is exactly equal to 1 3oftheinverseofthetransi- due to the volumes of stored data. In several network con- tivity coefficient of the graph, a universal measure closely re- texts, the application receive data without pace from remote lated to the clustering coefficient, whose value for networks sources. Data stream computation allows also to compute of practical interest is hardly bigger than 105. Therefore, on-line relevant quantities without incurring a large cost for the number of memory cells of our data structure depends, organizing and storing data. We think for example of a for all practical purposes, only logarithmically on the size of distributed crawler collecting Web pages and their links, the network. Our algorithmic results improve by a quadratic and performing structural analysis of the Webgraph prior factor the result of [20] and always improves over the naive to transfer data to a storage device. sampling method. Data stream algorithms have been proposed for problems Our method is suitable to be adapted to several other like computation of frequency moments [1], histograms [5], classes of subgraphs. As an example, we provide, in the in- Wavelet transforms [4], and others. This large body of work cidence stream model, an algorithm to estimate the number contrasts with a lack of efficient solutions of natural graph 1 1 |Sα| of cliques of size α that uses O( 2 log( )log(|V |)(1 + )) problems in the streaming model of computation [7]. Bar- δ |Kα| memory cells, where Sα is the number of stars of cardinality Yosseff, Kumar and Sivakumar [20] give a first solution for α and Kα is the number of cliques of size α in the graph counting triangles in the data stream model. They consider (see [2] for details). both the “adjacency stream” model where the graph is pre- In the second part of this work, we provide an optimized sented as a sequence of edges in arbitrary order and there implementation of our data stream algorithms and test it is no bound on the degree of a vertex, and the “incidence on networks of various sizes collected in different application stream” model where they consider only bounded-degree domains. Our dataset includes large webgraphs, graphs of graphs and all edges incident to a vertex are presented suc- the largest online encyclopedia Wikipidia [11], graphs of col- cessively. Their algorithms provide an approximation with laborations between actors and authors. For all networks we probability 1 − δ using a number of memory cells in some consider, a sample of size 104 already suffices to provide an cases smaller than a naive sampling technique algorithm. excellent approximation of the number of triangles in the in-
254
cident stream model. The running times of our algorithms, Furthermore |T3| = E[β] ·|E|·(|V |−2)/3. measured in terms of operations performed in main mem- Proof. We look at all triples of nodes in V .Eachtriple ory, is always lower than the running times measured on the belongs to one of the sets T , T , T ,orT . The algorithm same computing platform for the best known implementa- 0 1 2 3 chooses such a triple by choosing an edge e =(a, b) together tion of several heuristics provided in [15]. For the model with one node v ∈ V \{a, b}. Therefore, no triple from T with stream of edges in arbitrary order, our optimized im- 0 is chosen. plementation shows that a sample of size 106 is sufficient Let us denote by t = {v, w, u} a fixed triple from T . for a reasonable approximation of the number of triangles of 1 Wlog. let (v,w) ∈ E and so (v, u), (w, u) ∈/ E. The algo- certain networks, whereas it is still too small for some net- rithm chooses t, iff it samples edge (v, w)andvertexu. works for which the algorithm in the incident stream model Now assume t ∈ T .Thent is chosen by SampleTri- provides an excellent approximation with a sample of size 2 angle, iff one of the two edges in the triple is sampled 104. and v equals to the remaining node of the triple. For the T 1.2 Structure of the paper same reason, a triple in 3 is counted whenever one of its three edges and the remaining vertex is chosen. Since there We present in Section 2 the algorithm for counting the |E|· |V |− |T | ·|T | ·|T | number of triangles in the adjacent stream model, in sec- are ( 2) = 1 +2 2 +3 3 choices for the algorithm to sample an edge and a node, it follows tion 3 the model for counting the number of triangles in |T | β ·|E| |V |− / the incidence stream model. Section 4 presents the opti- 3 = E[ ] ( 2) 3. mized implementations of the algorithms and Section 5 the A streaming algorithm which outputs a good estimate of description of our dataset and the experimental results. T3 easily follows: we start 3 |T | +2·|T | +3·|T | 2 2. STREAMS OF EDGES IN ARBITRARY s ≥ · 1 2 3 · ln( ) 2 |T | δ ORDER 3 We consider undirected graphs without self-loops. Each parallel instances of SampleTriangleP.Eachofthesein- 1 s βi βi edge is an unordered pair of nodes (v, w) such that (v, w)= stances outputs a value .Weuses i=1 as an estima- w, v V { ,...,n} n tor for ( ). We assume that = 1 and is known in ·|T | E β 3 3 advance. We have access to a stream consisting of all edges [ ]= |T1|+2·|T2|+3·|T3| . We then return in the graph. The edges appear in arbitrary order and no ! Xs ‹ edge is repeated in the stream. There is no bound on the f 1 T3 := βi ·|E|(|V |−2) 3 degree of the nodes. s i=1
2.1 3 Pass Algorithm as an estimation for the cardinalty of T3. We will first present an algorithm which passes three times Lemma − δ over the stream. A different algorithm with the same space 2.2. With probability 1 the following state- complexity has been presented in [8]. However, our algo- ment holds: f rithm has a significantly improved update time and as we (1 − ) ·|T3| < T3 < (1 + ) ·|T3| later show, we can combine the three passes to a one-pass algorithm. Proof. We use Chernoff’s Bounds: SampleTriangle Xs We introduce a streaming algorithm , ˆ 1 ˜ { , } |T |/ |T | Pr βi ≥ (1 + )E[β] which outputs a 0 1 variable with expected value 3 3 ( 1 + s i=1 2·|T2 |+3·|T3 |). Using the property |T1|+2·|T2 |+3·|T3 | = 2 s |E|·(|V |−2) (Lemma 2.1) we can estimate |T | using 1/ 2 ˆ X ˜ 3