In today’s lecture we will see two applications of negative binomially distributed random variables.

**1. Example: Quicksort **

Quicksort is one of the most famous algorithms for sorting an array of comparable elements. (We assume for simplicity that the array has no equal elements.) The quicksort algorithm is recursive. In each recursive call it picks a pivot, then partitions the current array into two parts: the elements that are strictly smaller than the pivot, and the elements that are at least the pivot. This partitioning process takes time that is linear in the size of the current array. It then recursively sorts the two parts, stopping the recursion when the current array consists of a single element.

It is well-known that quicksort probably takes time to sort an array of length if each partitioning step chooses the pivot element uniformly at random from the current array. There are many ways to prove this fact. We now give a short proof using Chernoff bounds applied to random variables with the negative binomial distribution.

Let be the size of the original input array. Notice that the total amount of work done by all subroutines at level of the recursion is , since each element of the input array appears in at most one subproblem at level . So we obtain a runtime bound of if we can show that the maximum level of any recursive subproblem is .

Every leaf of the recursion corresponds to a distinct element of the input array, so there are exactly leaves. We will show that every leaf has level . To do so, fix a particular element of the input array and consider all partitioning steps involving . Intuitively, we would like each recursive call to partition the current array into two halves of nearly equal size. To formalize this, we say that a partitioning step is “good” if it partitions the current array into two parts, both of which have size *at least one third* of that array. So the probability of being good is .

Each good partitioning step shrinks the size of the current array by a factor of or better, so after good partitioning steps the current array has size at most . So can be involved in at most good partitioning steps, irrespective of the random decisions. Our only worry is that could be involved in many bad partitioning steps. We can upper bound the number of partitioning steps involving using a negative binomially distributed random variable: the number of trials needed to see successes when the probability of success is .

Recall that in the previous lecture we used the Chernoff bound to prove the following tail bound for negative binomially distributed random variables.

Claim 1Let have the negative binomial distribution with parameters and . Pick and set . Then .

We apply this claim with , so . Then the probability that more than trials are needed to see successes is at most . Applying a union bound over all elements of the array, the probability that any leaf of the recursion has level greater than is at most . Therefore the running time is with probability at least .

**2. Peer-to-peer Systems **

Around 10 years ago, there was a lot of interest in “peer-to-peer systems”, both academically, and in the real world. A peer-to-peer system is a decentralized approach for organizing computers and data in a distributed system.

On the academic side, there was interesting work giving novel ways to organize data and nodes in a distributed system (Consistent Hashing, Chord, Pastry, Kademlia, etc.). In the real world, many large peer-to-peer systems were developed, primarily for file sharing (Gnutella, Kazaa, BitTorrent, etc.). Several large technology companies were formed based on these technologies (e.g., Akamai and Skype).

The rough design goals of these peer-to-peer systems are as follows.

- Every node should maintain connections to a small number of other nodes. (In other words, the graph of connections between nodes should have small degree.)
- No node knows who all the other nodes in the system are, or how they are connected, or even the number of nodes.
- Any node should be able to send a message to any other node by traversing few intermediate nodes. (In other words, the graph of connections between nodes should have small diameter.) Moreover, the node should be able to efficiently
*find*a short path from itself to . - We assume that nodes are mostly cooperative: if node asks node to take some action, e.g., form a connection, or forward a message onwards, then node will oblige.

** 2.1. Example: SkipNet **

We will discuss a system SkipNet which meets these design goals. Specifically, in a system with nodes, every node will maintain connections to other nodes, and any node can send a message to any other node while traversing only intermediate nodes. The system’s design is based on ideas from theoretical computer science, in particular the dictionary data structure known as Skip Lists. We discuss this system because its analysis illustrates the usefulness of random variables with the negative binomial distribution.

Suppose there are nodes in the peer-to-peer system. Every node has a string identifier (e.g., “www.cs.ubc.ca”) and a random bitstring , for some parameter whose value we will choose later.

The nodes are organized into several doubly-linked lists. Each node is a member of several of these lists, but not all of them. Each of these lists is sorted using the nodes’ string identifiers. Every pair of nodes that are adjacent in these lists forms a bidirectional network connection.

Formally, for every bitstring of length at most , there is a list . We denote the length of bitstring by . The list contains all nodes for which is a prefix of their random bitstring. We say that the list has “level” equal to . For example, for the empty bitstring , there is a list which contains *all* nodes, sorted by their identifiers. This list has level . At level there are two lists, and , each of which contain a subset of the nodes: the list contains all nodes whose random bitstrings start with a , and contains all nodes whose random bitstrings start with a . So, for each level, every node belongs to exactly one list at that level.

Claim 2Let be a bitstring of length . Then the expected size of the list is .

*Proof:* Consider any node and let its random bitstring be . The probability that is a prefix of is exactly .

This suggests that for most bitstrings with the list is empty. The following claim makes this more precise.

Claim 3Let . With probability at least , every list with contains at most one node.

*Proof:* Note that if two nodes belong to different lists at level then they also belong to different lists at every level . So it suffices to prove the claim for the case .

Consider two nodes with random bitstrings and . These nodes belong to the same list at level if and only if and have equal prefixes of length , i.e., the first bits of equals the first bits of . So the probability that these two nodes belong to the same list at level is . By a union bound,

Recall that we only create network connections between pairs of nodes that are adjacent in any list. So the previous claim has two implications. First of all, we should choose . There is no point in creating lists at any level higher than because they will almost certainly contain at most 1 node, and therefore will not be used to create any network connections between nodes. Second of all, since every node belongs to lists, it has only neighbors in total, and therefore participates in only network connections. This satisfies our first goal in the design of the peer-to-peer system.

**Sending Messages.** It remains to discuss how a node can send a message to any another node. Recall that this will happen by a sequence of intermediate nodes forwarding the message towards the final destination. The simplest way to do this would be to use the list at level 0. Since every node belongs to this list, a node can just send the message to his neighbor until the message arrives at the destination. Note that this process does not involve “flooding”: in our example above, if wants to send a message to then the message would traverse and would not be sent to nodes , or .

However, that process of forwarding messages along the list is not very efficient. If there are nodes in the system, then the message might need to be forwarded times before arriving at its destination. (The Skype network has tens of millions of active users, so forwarding each message millions of times would not be very desirable!)

To send messages more efficiently, we will make use of the other lists. The main idea is: a list at a high level has few nodes, and those nodes are essentially uniformly distributed amongst all nodes, so the connections formed by high-level lists allow one to “skip” over many nodes in the list. To be a bit more precise, consider any bitstring . Let and be the bitstrings respectively obtained by appending and to . Every node in randomly chooses (by choosing its random bitstring) whether to join or , with roughly half of the nodes going to each. If the nodes in who join perfectly interleave the nodes who join then every connection between adjacent nodes in corresponds to a *path of two connections* in . But, due to the randomness, the connections in may not correspond to a path of exactly two connections, it might correspond to just one connection in , or a path of three connections, etc.

This discussion suggests that sending a message using the connections in instead of allows one to make roughly twice as much progress towards the destination. So we’d prefer to send the message using only the high-level lists. Of course, that is not always possible: the source and the destination might belong to different high-level lists, in which case one must use the low-level lists too. So we devise the following rule for routing messages from a node .

- Send the message through node ‘s level list as far as possible
*towards*the destination without going*beyond*the destination, arriving at node . - Send the message from through node ‘s level list as far as possible
*towards*the destination without going*beyond*the destination, arriving at node . - Send the message from through node ‘s level list as far as possible
*towards*the destination without going*beyond*the destination, arriving at node . - …
- Send the message from through the level 0 list to the destination (which we can also call ).

Consider our previous example. Suppose that node wants to send a message to node . Node ‘s level 3 list contains only node , so it cannot be used to make progress towards the destination. So . The same is true for its level 2 list. So . Node ‘s level 1 list has a connection to node , which is not beyond the destination, so we send the message to . Node can continue sending the message through node ‘s level 1 list to node . So . Finally, node can send the message through the level 0 list to , which is the destination.

The correctness of this routing rule is guaranteed by the last step: sending the message through the level 0 list always reaches the destination because that list contains all nodes. It remains to analyze how efficient the rule is.