Data Stream Algorithms Lecture Notes

Total Page:16

File Type:pdf, Size:1020Kb

Data Stream Algorithms Lecture Notes Data Stream Algorithms Lecture Notes Amit Chakrabarti Dartmouth College Latest Update: July 2, 2020 DRAFT Preface This book grew out of lecture notes for offerings of a course on data stream algorithms at Dartmouth, beginning with a first offering in Fall 2009. It’s primary goal is to be a resource for students and other teachers of this material. The choice of topics reflects this: the focus is on foundational algorithms for space-efficient processing of massive data streams. I have emphasized algorithms that are simple enough to permit a clean and fairly complete presentation in a classroom, assuming not much more background than an advanced undergraduate student would have. Where appropriate, I have provided pointers to more advanced research results that achieve improved bounds using heavier technical machinery. I would like to thank the many Dartmouth students who took various editions of my course, as well as researchers around the world who told me that they had found my course notes useful. Their feedback inspired me to make a book out of this material. I thank Suman Kalyan Bera, Sagar Kale, and Jelani Nelson for numerous comments and contributions. Of special note are the 2009 students whose scribing efforts got this book started: Radhika Bhasin, Andrew Cherne, Robin Chhetri, Joe Cooley, Jon Denning, Alina Djamankulova, Ryan Kingston, Ranganath Kondapally, Adrian Kostrubiak, Konstantin Kutzkow, Aarathi Prasad, Priya Natarajan, and Zhenghui Wang. Amit Chakrabarti April 2020 DRAFT Contents 0 Preliminaries: The Data Stream Model6 0.1 The Basic Setup..............................................6 0.2 The Quality of an Algorithm’s Answer..................................6 0.3 Variations of the Basic Setup.......................................7 1 Finding Frequent Items Deterministically8 1.1 The Problem................................................8 1.2 Frequency Estimation: The Misra–Gries Algorithm...........................8 1.3 Analysis of the Algorithm........................................9 1.4 Finding the Frequent Items........................................ 10 Exercises................................................. 10 2 Estimating the Number of Distinct Elements 11 2.1 The Problem................................................ 11 2.2 The Tidemark Algorithm......................................... 11 2.3 The Quality of the Algorithm’s Estimate................................. 12 2.4 The Median Trick............................................. 13 Exercises................................................. 13 3 A Better Estimate for Distinct Elements 15 3.1 The Problem................................................ 15 3.2 The BJKST Algorithm.......................................... 15 3.3 Analysis: Space Complexity....................................... 16 3.4 Analysis: The Quality of the Estimate.................................. 16 3.5 Optimality................................................. 17 Exercises.................................................DRAFT 17 4 Approximate Counting 19 4.1 The Problem................................................ 19 4.2 The Algorithm.............................................. 19 2 Dartmouth: CS 35/135 CONTENTS Data Stream Algorithms 4.3 The Quality of the Estimate........................................ 20 4.4 The Median-of-Means Improvement................................... 21 Exercises................................................. 22 5 Finding Frequent Items via (Linear) Sketching 23 5.1 The Problem................................................ 23 5.2 Sketches and Linear Sketches....................................... 23 5.3 CountSketch................................................ 24 5.3.1 The Quality of the Basic Sketch’s Estimate........................... 24 5.3.2 The Final Sketch......................................... 25 5.4 The Count-Min Sketch.......................................... 26 5.4.1 The Quality of the Algorithm’s Estimate............................. 26 5.5 Comparison of Frequency Estimation Methods............................. 27 Exercises................................................. 28 6 Estimating Frequency Moments 29 6.1 Background and Motivation....................................... 29 6.2 The (Basic) AMS Estimator for Fk .................................... 29 6.3 Analysis of the Basic Estimator...................................... 30 6.4 The Final Estimator and Space Bound.................................. 32 6.4.1 The Soft-O Notation....................................... 32 7 The Tug-of-War Sketch 33 7.1 The Basic Sketch............................................. 33 7.1.1 The Quality of the Estimate................................... 34 7.2 The Final Sketch............................................. 34 7.2.1 A Geometric Interpretation.................................... 34 Exercises................................................. 35 8 Estimating Norms Using Stable Distributions 36 8.1 A Different `2 Algorithm......................................... 36 8.2 Stable Distributions............................................ 37 8.3 The Median of a Distribution and its Estimation............................. 38 8.4 The Accuracy of the Estimate....................................... 38 8.5 Annoying Technical Details........................................ 39 9 Sparse Recovery 40 9.1 The Problem................................................DRAFT 40 9.2 Special case: 1-sparse recovery...................................... 40 9.3 Analysis: Correctness, Space, and Time................................. 41 9.4 General Case: s-sparse Recovery..................................... 42 Exercises................................................. 44 3 Dartmouth: CS 35/135 CONTENTS Data Stream Algorithms 10 Weight-Based Sampling 45 10.1 The Problem................................................ 45 10.2 The `0-Sampling Problem......................................... 46 10.2.1 An Idealized Algorithm..................................... 46 10.2.2 The Quality of the Output.................................... 47 10.3 `2 Sampling................................................ 48 10.3.1 An `2-sampling Algorithm.................................... 48 10.3.2 Analysis:............................................. 48 11 Finding the Median 51 11.1 The Problem................................................ 51 11.2 Preliminaries for an Algorithm...................................... 51 11.3 The Munro–Paterson Algorithm..................................... 53 11.3.1 Computing a Core........................................ 53 11.3.2 Utilizing a Core.......................................... 53 11.3.3 Analysis: Pass/Space Tradeoff.................................. 53 Exercises................................................. 55 12 Geometric Streams and Coresets 56 12.1 Extent Measures and Minimum Enclosing Ball............................. 56 12.2 Coresets and Their Properties....................................... 56 12.3 A Coreset for MEB............................................ 57 12.4 Data Stream Algorithm for Coreset Construction............................ 58 Exercises................................................. 59 13 Metric Streams and Clustering 60 13.1 Metric Spaces............................................... 60 13.2 The Cost of a Clustering: Summarization Costs............................. 61 13.3 The Doubling Algorithm......................................... 61 13.4 Metric Costs and Threshold Algorithms................................. 63 13.5 Guha’s Cascading Algorithm....................................... 63 13.5.1 Space Bounds........................................... 64 13.5.2 The Quality of the Summary................................... 64 Exercises................................................. 65 14 Graph Streams: Basic Algorithms 66 14.1 Streams that Describe Graphs....................................... 66 14.1.1 Semi-Streaming SpaceDRAFT Bounds.................................. 67 14.2 The Connectedness Problem....................................... 67 14.3 The Bipartiteness Problem........................................ 67 14.4 Shortest Paths and Distance Estimation via Spanners.......................... 68 14.4.1 The Quality of the Estimate................................... 69 4 Dartmouth: CS 35/135 CONTENTS Data Stream Algorithms 14.4.2 Space Complexity: High-Girth Graphs and the Size of a Spanner................ 69 Exercises................................................. 70 15 Finding Maximum Matchings 71 15.1 Preliminaries............................................... 71 15.2 Maximum Cardinality Matching..................................... 71 15.3 Maximum Weight Matching....................................... 72 Exercises................................................. 74 16 Graph Sketching 75 16.1 The Value of Boundary Edges...................................... 75 16.1.1 Testing Connectivity Using Boundary Edges.......................... 75 16.1.2 Testing Bipartiteness....................................... 76 16.2 The AGM Sketch: Producing a Boundary Edge............................. 77 17 Counting Triangles 79 17.1 A Sampling-Based Algorithm...................................... 79 17.2 A Sketch-Based Algorithm........................................ 80 Exercises................................................. 80 18 Communication Complexity and a First Look at Lower Bounds 81 18.1 Communication Games, Protocols, Complexity............................. 81 18.2 Specific Two-Player Communication
Recommended publications
  • Data Streams and Applications in Computer Science
    Data Streams and Applications in Computer Science David P. Woodruff IBM Research Almaden [email protected] Abstract This is a short survey of my work in data streams and related appli- cations, such as communication complexity, numerical linear algebra, and sparse recovery. The goal is give a non-technical overview of results in data streams, and highlight connections between these different areas. It is based on my Presburger lecture award given at ICALP, 2014. 1 The Data Stream Model Informally speaking, a data stream is a sequence of data that is too large to be stored in available memory. The data may be a sequence of numbers, points, edges in a graph, and so on. There are many examples of data streams, such as internet search logs, network traffic, sensor networks, and scientific data streams (such as in astronomics, genomics, physical simulations, etc.). The abundance of data streams has led to new algorithmic paradigms for processing them, which often impose very stringent requirements on the algorithm’s resources. Formally, in the streaming model, there is a sequence of elements a1;:::; am presented to an algorithm, where each element is drawn from a universe [n] = f1;:::; ng. The algorithm is allowed a single or a small number of passes over the stream. In network applications, the algorithm is typically only given a single pass, since if data on a network is not physically stored somewhere, it may be impossible to make a second pass over it. In other applications, such as when data resides on external memory, it may be streamed through main memory a small number of times, each time constituting a pass of the algorithm.
    [Show full text]
  • CS 61A Streams Summer 2019 1 Streams
    CS 61A Streams Summer 2019 Discussion 10: August 6, 2019 1 Streams In Python, we can use iterators to represent infinite sequences (for example, the generator for all natural numbers). However, Scheme does not support iterators. Let's see what happens when we try to use a Scheme list to represent an infinite sequence of natural numbers: scm> (define (naturals n) (cons n (naturals (+ n 1)))) naturals scm> (naturals 0) Error: maximum recursion depth exceeded Because cons is a regular procedure and both its operands must be evaluted before the pair is constructed, we cannot create an infinite sequence of integers using a Scheme list. Instead, our Scheme interpreter supports streams, which are lazy Scheme lists. The first element is represented explicitly, but the rest of the stream's elements are computed only when needed. Computing a value only when it's needed is also known as lazy evaluation. scm> (define (naturals n) (cons-stream n (naturals (+ n 1)))) naturals scm> (define nat (naturals 0)) nat scm> (car nat) 0 scm> (cdr nat) #[promise (not forced)] scm> (car (cdr-stream nat)) 1 scm> (car (cdr-stream (cdr-stream nat))) 2 We use the special form cons-stream to create a stream: (cons-stream <operand1> <operand2>) cons-stream is a special form because the second operand is not evaluated when evaluating the expression. To evaluate this expression, Scheme does the following: 1. Evaluate the first operand. 2. Construct a promise containing the second operand. 3. Return a pair containing the value of the first operand and the promise. 2 Streams To actually get the rest of the stream, we must call cdr-stream on it to force the promise to be evaluated.
    [Show full text]
  • Chapter 2 Basics of Scanning And
    Chapter 2 Basics of Scanning and Conventional Programming in Java In this chapter, we will introduce you to an initial set of Java features, the equivalent of which you should have seen in your CS-1 class; the separation of problem, representation, algorithm and program – four concepts you have probably seen in your CS-1 class; style rules with which you are probably familiar, and scanning - a general class of problems we see in both computer science and other fields. Each chapter is associated with an animating recorded PowerPoint presentation and a YouTube video created from the presentation. It is meant to be a transcript of the associated presentation that contains little graphics and thus can be read even on a small device. You should refer to the associated material if you feel the need for a different instruction medium. Also associated with each chapter is hyperlinked code examples presented here. References to previously presented code modules are links that can be traversed to remind you of the details. The resources for this chapter are: PowerPoint Presentation YouTube Video Code Examples Algorithms and Representation Four concepts we explicitly or implicitly encounter while programming are problems, representations, algorithms and programs. Programs, of course, are instructions executed by the computer. Problems are what we try to solve when we write programs. Usually we do not go directly from problems to programs. Two intermediate steps are creating algorithms and identifying representations. Algorithms are sequences of steps to solve problems. So are programs. Thus, all programs are algorithms but the reverse is not true.
    [Show full text]
  • File I/O Stream Byte Stream
    File I/O In Java, we can read data from files and also write data in files. We do this using streams. Java has many input and output streams that are used to read and write data. Same as a continuous flow of water is called water stream, in the same way input and output flow of data is called stream. Stream Java provides many input and output stream classes which are used to read and write. Streams are of two types. Byte Stream Character Stream Let's look at the two streams one by one. Byte Stream It is used in the input and output of byte. We do this with the help of different Byte stream classes. Two most commonly used Byte stream classes are FileInputStream and FileOutputStream. Some of the Byte stream classes are listed below. Byte Stream Description BufferedInputStream handles buffered input stream BufferedOutputStrea handles buffered output stream m FileInputStream used to read from a file FileOutputStream used to write to a file InputStream Abstract class that describe input stream OutputStream Abstract class that describe output stream Byte Stream Classes are in divided in two groups - InputStream Classes - These classes are subclasses of an abstract class, InputStream and they are used to read bytes from a source(file, memory or console). OutputStream Classes - These classes are subclasses of an abstract class, OutputStream and they are used to write bytes to a destination(file, memory or console). InputStream InputStream class is a base class of all the classes that are used to read bytes from a file, memory or console.
    [Show full text]
  • Efficient Semi-Streaming Algorithms for Local Triangle Counting In
    Efficient Semi-streaming Algorithms for Local Triangle Counting in Massive Graphs ∗ Luca Becchetti Paolo Boldi Carlos Castillo “Sapienza” Università di Roma Università degli Studi di Milano Yahoo! Research Rome, Italy Milan, Italy Barcelona, Spain [email protected] [email protected] [email protected] Aristides Gionis Yahoo! Research Barcelona, Spain [email protected] ABSTRACT Categories and Subject Descriptors In this paper we study the problem of local triangle count- H.3.3 [Information Systems]: Information Search and Re- ing in large graphs. Namely, given a large graph G = (V; E) trieval we want to estimate as accurately as possible the number of triangles incident to every node v 2 V in the graph. The problem of computing the global number of triangles in a General Terms graph has been considered before, but to our knowledge this Algorithms, Measurements is the first paper that addresses the problem of local tri- angle counting with a focus on the efficiency issues arising Keywords in massive graphs. The distribution of the local number of triangles and the related local clustering coefficient can Graph Mining, Semi-Streaming, Probabilistic Algorithms be used in many interesting applications. For example, we show that the measures we compute can help to detect the 1. INTRODUCTION presence of spamming activity in large-scale Web graphs, as Graphs are a ubiquitous data representation that is used well as to provide useful features to assess content quality to model complex relations in a wide variety of applica- in social networks. tions, including biochemistry, neurobiology, ecology, social For computing the local number of triangles we propose sciences, and information systems.
    [Show full text]
  • Streaming Quantiles Algorithms with Small Space and Update Time
    Streaming Quantiles Algorithms with Small Space and Update Time Nikita Ivkin Edo Liberty Kevin Lang [email protected] [email protected] [email protected] Amazon Amazon Yahoo Research Zohar Karnin Vladimir Braverman [email protected] [email protected] Amazon Johns Hopkins University ABSTRACT An approximate version of the problem relaxes this requirement. Approximating quantiles and distributions over streaming data has It is allowed to output an approximate rank which is off by at most been studied for roughly two decades now. Recently, Karnin, Lang, εn from the exact rank. In a randomized setting, the algorithm and Liberty proposed the first asymptotically optimal algorithm is allowed to fail with probability at most δ. Note that, for the for doing so. This manuscript complements their theoretical result randomized version to provide a correct answer to all possible by providing a practical variants of their algorithm with improved queries, it suffices to amplify the success probability by running constants. For a given sketch size, our techniques provably reduce the algorithm with a failure probability of δε, and applying the 1 the upper bound on the sketch error by a factor of two. These union bound over O¹ ε º quantiles. Uniform random sampling of ¹ 1 1 º improvements are verified experimentally. Our modified quantile O ε 2 log δ ε solves this problem. sketch improves the latency as well by reducing the worst-case In network monitoring [16] and other applications it is critical 1 1 to maintain statistics while making only a single pass over the update time from O¹ ε º down to O¹log ε º.
    [Show full text]
  • Turnstile Streaming Algorithms Might As Well Be Linear Sketches
    Turnstile Streaming Algorithms Might as Well Be Linear Sketches Yi Li Huy L. Nguyên˜ David P. Woodruff Max-Planck Institute for Princeton University IBM Research, Almaden Informatics [email protected] [email protected] [email protected] ABSTRACT Keywords In the turnstile model of data streams, an underlying vector x 2 streaming algorithms, linear sketches, lower bounds, communica- {−m; −m+1; : : : ; m−1; mgn is presented as a long sequence of tion complexity positive and negative integer updates to its coordinates. A random- ized algorithm seeks to approximate a function f(x) with constant probability while only making a single pass over this sequence of 1. INTRODUCTION updates and using a small amount of space. All known algorithms In the turnstile model of data streams [28, 34], there is an under- in this model are linear sketches: they sample a matrix A from lying n-dimensional vector x which is initialized to ~0 and evolves a distribution on integer matrices in the preprocessing phase, and through an arbitrary finite sequence of additive updates to its coor- maintain the linear sketch A · x while processing the stream. At the dinates. These updates are fed into a streaming algorithm, and have end of the stream, they output an arbitrary function of A · x. One the form x x + ei or x x − ei, where ei is the i-th standard cannot help but ask: are linear sketches universal? unit vector. This changes the i-th coordinate of x by an additive In this work we answer this question by showing that any 1- 1 or −1.
    [Show full text]
  • C++ Input/Output: Streams 4
    C++ Input/Output: Streams 4. Input/Output 1 The basic data type for I/O in C++ is the stream. C++ incorporates a complex hierarchy of stream types. The most basic stream types are the standard input/output streams: istream cin built-in input stream variable; by default hooked to keyboard ostream cout built-in output stream variable; by default hooked to console header file: <iostream> C++ also supports all the input/output mechanisms that the C language included. However, C++ streams provide all the input/output capabilities of C, with substantial improvements. We will exclusively use streams for input and output of data. Computer Science Dept Va Tech August, 2001 Intro Programming in C++ ©1995-2001 Barnette ND & McQuain WD C++ Streams are Objects 4. Input/Output 2 The input and output streams, cin and cout are actually C++ objects. Briefly: class: a C++ construct that allows a collection of variables, constants, and functions to be grouped together logically under a single name object: a variable of a type that is a class (also often called an instance of the class) For example, istream is actually a type name for a class. cin is the name of a variable of type istream. So, we would say that cin is an instance or an object of the class istream. An instance of a class will usually have a number of associated functions (called member functions) that you can use to perform operations on that object or to obtain information about it. The following slides will present a few of the basic stream member functions, and show how to go about using member functions.
    [Show full text]
  • Subtyping, Declaratively an Exercise in Mixed Induction and Coinduction
    Subtyping, Declaratively An Exercise in Mixed Induction and Coinduction Nils Anders Danielsson and Thorsten Altenkirch University of Nottingham Abstract. It is natural to present subtyping for recursive types coin- ductively. However, Gapeyev, Levin and Pierce have noted that there is a problem with coinductive definitions of non-trivial transitive inference systems: they cannot be \declarative"|as opposed to \algorithmic" or syntax-directed|because coinductive inference systems with an explicit rule of transitivity are trivial. We propose a solution to this problem. By using mixed induction and coinduction we define an inference system for subtyping which combines the advantages of coinduction with the convenience of an explicit rule of transitivity. The definition uses coinduction for the structural rules, and induction for the rule of transitivity. We also discuss under what condi- tions this technique can be used when defining other inference systems. The developments presented in the paper have been mechanised using Agda, a dependently typed programming language and proof assistant. 1 Introduction Coinduction and corecursion are useful techniques for defining and reasoning about things which are potentially infinite, including streams and other (poten- tially) infinite data types (Coquand 1994; Gim´enez1996; Turner 2004), process congruences (Milner 1990), congruences for functional programs (Gordon 1999), closures (Milner and Tofte 1991), semantics for divergence of programs (Cousot and Cousot 1992; Hughes and Moran 1995; Leroy and Grall 2009; Nakata and Uustalu 2009), and subtyping relations for recursive types (Brandt and Henglein 1998; Gapeyev et al. 2002). However, the use of coinduction can lead to values which are \too infinite”. For instance, a non-trivial binary relation defined as a coinductive inference sys- tem cannot include the rule of transitivity, because a coinductive reading of transitivity would imply that every element is related to every other (to see this, build an infinite derivation consisting solely of uses of transitivity).
    [Show full text]
  • Finding Graph Matchings in Data Streams
    Finding Graph Matchings in Data Streams Andrew McGregor⋆ Department of Computer and Information Science, University of Pennsylvania, Philadelphia, PA 19104, USA [email protected] Abstract. We present algorithms for finding large graph matchings in the streaming model. In this model, applicable when dealing with mas- sive graphs, edges are streamed-in in some arbitrary order rather than 1 residing in randomly accessible memory. For ǫ > 0, we achieve a 1+ǫ 1 approximation for maximum cardinality matching and a 2+ǫ approxi- mation to maximum weighted matching. Both algorithms use a constant number of passes and O˜( V ) space. | | 1 Introduction Given a graph G = (V, E), the Maximum Cardinality Matching (MCM) problem is to find the largest set of edges such that no two adjacent edges are selected. More generally, for an edge-weighted graph, the Maximum Weighted Matching (MWM) problem is to find the set of edges whose total weight is maximized subject to the condition that no two adjacent edges are selected. Both problems are well studied and exact polynomial solutions are known [1–4]. The fastest of these algorithms solves MWM with running time O(nm + n2 log n) where n = V and m = E . | | | | However, for massive graphs in real world applications, the above algorithms can still be prohibitively computationally expensive. Examples include the vir- tual screening of protein databases. (See [5] for other examples.) Consequently there has been much interest in faster algorithms, typically of O(m) complexity, that find good approximate solutions to the above problems. For MCM, a linear time approximation scheme was given by Kalantari and Shokoufandeh [6].
    [Show full text]
  • Lecture 1 1 Graph Streaming
    CS 671: Graph Streaming Algorithms and Lower Bounds Rutgers: Fall 2020 Lecture 1 September 1, 2020 Instructor: Sepehr Assadi Scribe: Sepehr Assadi Disclaimer: These notes have not been subjected to the usual scrutiny reserved for formal publications. They may be distributed outside this class only with the permission of the Instructor. 1 Graph Streaming Motivation Massive graphs appear in most application domains nowadays: web-pages and hyperlinks, neurons and synapses, papers and citations, or social networks and friendship links are just a few examples. Many of the computing tasks in processing these graphs, at their core, correspond to classical problems such as reachability, shortest path, or maximum matching|problems that have been studied extensively for almost a century now. However, the traditional algorithmic approaches to these problems do not accurately capture the challenges of processing massive graphs. For instance, we can no longer assume a random access to the entire input on these graphs, as their sheer size prevents us from loading them into the main memory. To handle these challenges, there is a rapidly growing interest in developing algorithms that explicitly account for the restrictions of processing massive graphs. Graph streaming algorithms have been particularly successful in this regard; these are algorithms that process input graphs by making one or few sequential passes over their edges while using a small memory. Not only streaming algorithms can be directly used to address various challenges in processing massive graphs such as I/O-efficiency or monitoring evolving graphs in realtime, they are also closely connected to other models of computation for massive graphs and often lead to efficient algorithms in those models as well.
    [Show full text]
  • Streaming Algorithms
    3 Streaming Algorithms Great Ideas in Theoretical Computer Science Saarland University, Summer 2014 Some Admin: • Deadline of Problem Set 1 is 23:59, May 14 (today)! • Students are divided into two groups for tutorials. Visit our course web- site and use Doodle to to choose your free slots. • If you find some typos or like to improve the lecture notes (e.g. add more details, intuitions), send me an email to get .tex file :-) We live in an era of “Big Data”: science, engineering, and technology are producing in- creasingly large data streams every second. Looking at social networks, electronic commerce, astronomical and biological data, the big data phenomenon appears everywhere nowadays, and presents opportunities and challenges for computer scientists and mathematicians. Com- paring with traditional algorithms, several issues need to be considered: A massive data set is too big to be stored; even an O(n2)-time algorithm is too slow; data may change over time, and algorithms need to cope with dynamic changes of the data. Hence streaming, dynamic and distributed algorithms are needed for analyzing big data. To illuminate the study of streaming algorithms, let us look at a few examples. Our first example comes from network analysis. Any big website nowadays receives millions of visits every minute, and very frequent visit per second from the same IP address are probably gen- erated automatically by computers and due to hackers. In this scenario, a webmaster needs to efficiently detect and block those IPs which generate very frequent visits per second. Here, instead of storing all IP record history which are expensive, we need an efficient algorithm that detect most such IPs.
    [Show full text]