4

I'm writing some software that takes a group of users and compares each user with every other user in the group. I need to display the amount of comparisons needed for a countdown type feature.

For example, this group [1,2,3,4,5] would be analysed like this:

1-2, 1-3, 1-4, 1-5
2-3, 2-4, 2-5
3-4, 3-5
4-5

By creating little diagrams like this I've figured out the pattern which is as follows:

Users - Comparisons
2     -   1
3     -   3 (+2)
4     -   6 (+3)
5     -   10 (+4)
6     -   15 (+5)
7     -   21 (+6)
8     -   28 (+7)
9     -   36 (+8)

I need to be able to take any number of users, and calculate how many comparisons it will take to compare every user with every other user.

Can someone please tell me what the formula for this is?

MJD
  • 65,394
  • 39
  • 298
  • 580
Owen
  • 141
  • MJD is right, of course, but... What kind of comparison are you doing? Maybe a sort would do? If there are many users, it can be faster, depending on your needs. – Jean-Claude Arbaut May 02 '14 at 15:25
  • 2
    You're looking for the cardinal of the complete graph $K_n$, which is actually $\frac{n(n-1)}{2}$ – yago May 02 '14 at 15:25
  • Wow, so simple, thanks MJD! And Jean, it's nothing to do with sorting, that would be about as simple as myList.Sort(myComparer). – Owen May 02 '14 at 15:28
  • To be convined, draw $n$ points, and link each point to all of its $n-1$ neighbours. The links represent the comparaisons. Now here is a way to count the number of such edges : ask, for each point, to count the number of edges he is connected to. Each point has $n-1$ edges, and there are $n$ points, so you'll get $n(n-1)$. But actually, you counted each edges two times, one time for each of its two vertices, so you have to divide by two, getting the result. – yago May 02 '14 at 15:30
  • 2
    This is the handshake problem. http://mathworld.wolfram.com/HandshakeProblem.html – jamesSampica May 02 '14 at 16:07
  • If you're lazy, Excel can solve such problems for you. Put the "Users" formula in the first column, the first "Comparisons" value in the first row of the next column, and use the incremental formula you discovered to define the next cells in the second column. Now ask Excel to graph the two columns as an XY graph, and ask for a polynomial trendline. Add the formula to the graph, check the result yourself. – MSalters May 02 '14 at 16:56
  • In computer science terms, this is a $O(n^2)$ operation - Doubling the number of users will take four times as long to do your comparison. See this blog for more on "Big O notation". – Bobson May 02 '14 at 17:03
  • 1
    @Bobson I'm not sure BigO has much to do with this question. There seems to be no mention of asymptotic behaviour at all here. He simply wants a closed form for this recurrence relation (which I'm surprised nobody has pointed out, are the triangular numbers) – Cruncher May 02 '14 at 17:25
  • @Cruncher - It's in the first sentence - he's writing software to do this comparison, and was just asking about estimating the time. – Bobson May 02 '14 at 17:31
  • @Bobson Ah, you're right(I seem to have immediately reduced the problem to "find the closed form of this"). BigO is worth looking at in that case. However he may be asked for a precise analysis, if he wants to know exactly how many comparisons happen. – Cruncher May 02 '14 at 17:41
  • This questions is shown in the list of related questions on the right: http://math.stackexchange.com/questions/52194/formula-for-the-number-of-connections-needed-to-connect-every-node-in-a-set See also http://math.stackexchange.com/questions/2260/proof-for-formula-for-sum-of-sequence-123-ldotsn and http://math.stackexchange.com/questions/60578/what-is-the-term-for-a-factorial-type-operation-but-with-summation-instead-of-p – Martin Sleziak May 03 '14 at 10:37

7 Answers7

16

You want to know how many ways there are to choose $2$ users from a set of $n$ users.

Generally, the number of ways to choose $k$ elements from a set of order $n$ (that is, all elements in the set are distinct) is denoted by $$ \binom{n}{k} $$

and is equivalent to $$ \frac{n!}{(n-k)!k!} $$

In the case of $k=2$ the latter equals to $$ \frac{n!}{(n-2)!2!}=\frac{n(n-1)}{2} $$

which is also the sum of $1+2+...+n-1$.

For more information see Binomial coefficient and Arithmetic progression

Belgi
  • 23,150
13

The sum of $0+\cdots + n-1$ is $$\frac12(n-1)n.$$

Here $n$ is the number of users; there are 0 comparisons needed for the first user alone, 1 for the second user (to compare them to the first), 2 for the third user, and so on, up to the $n$th user who must be compared with the $n-1$ previous users.

For example, for $9$ people you are adding up $0+1+2+3+4+5+6+7+8$, which is equal to $$\frac12\cdot 8\cdot 9= \frac{72}{2} = 36$$ and for $10$ people you may compute $$\frac12\cdot9\cdot10 = \frac{90}2 = 45.$$

MJD
  • 65,394
  • 39
  • 298
  • 580
5

The following way to getting the solution is beautiful and said to have been found by young Gauss in school. The idea is that the order of adding $1+2+\cdots+n=S_n$ does not change the value of the sum. Therefore:

$$1 + 2 + \ldots + (n-1) + n=S_n$$ $$n + (n-1) + \ldots + 2 + 1=S_n$$

Adding the two equations term by term gives

$$(n+1)+(n+1)+\ldots+(n+1)=2S_n$$

so $n(n+1)=2S_n$. For $n$ persons, there are $S_{n-1}$ possibilities, as others answers have shown already nicely.

0

The discrete sum up to a finite value $N$ is given by,

$$\sum_{n=1}^{N} n = \frac{1}{2}N(N+1)$$

Proof:

The proof by induction roughly boils down to:

$$S_N = 1+ 2 +\dots+N$$

$$S_{N+1}= 1+ 2 + \dots + N + (N+1) = \underbrace{\frac{1}{2}N(N+1)}_{S_N} + (N+1)$$

assuming that the induction hypothesis is true. The right hand side:

$$\frac{N(N+1)}{2}+(N+1)=\frac{(N+1)(N+2)}{2}$$

which is precisely the induction hypothesis applied to $S_{N+1}$.


Just for your own curiosity, the case $N=\infty$ is of course divergent. However, with the use of the zeta function, it may be regularized to yield,

$$\sum_{n=1}^{\infty}n = \zeta(-1)=-\frac{1}{12}$$

JPhy
  • 1,766
0

$$N=2:\ 1 + 2 = (1 + 2) = 1\times3$$

$$N=4:\ 1 + 2 + 3 + 4 = (1 + 4) + (2 + 3) = 2\times5$$

$$N=6:\ 1 + 2 + 3 + 4 + 5 + 6 = (1 + 6) + (2 + 5) + (3 + 4) = 3\times7$$

$$N=8:\ 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8= (1 + 8) + (2 + 7) + (3 + 6)+ (4 + 5) = 4\times9$$

More generally, $N/2\times(N + 1)$.

For odd $N$, sum the $N-1$ first terms (using the even formula) together with $N$, giving $(N-1)/2\times N+N=N\times(N+1)/2$.

  • Except that you should never count yourself, so you should be starting at 0 instead of 1. You'll have to take your result and subtract N from it. So for N = 2 you take your 1 x 3 = 3 - N = 3 - 2 = 1, which matches the table listed by OP. – corsiKa May 02 '14 at 17:31
  • @corsiKa: the formula explicitly shows the sum from 1 to $N$ inclusive (triangular numbers) and is perfectly correct. It is NOT the formula for the number of comparisons between $N$ users. –  May 02 '14 at 21:38
  • I think you're missing my point. I'm not saying your math is wrong at all. I'm saying it doesn't match what the OP is looking for. If you are not giving he formula for the number of comparisons between N users, then you aren't answering the OPs question, which makes this an incorrect answer for this question. – corsiKa May 02 '14 at 21:44
  • The sum must be taken from $1$ to $Users-1$ inclusive. –  May 02 '14 at 21:51
  • Yes. And the sum 1 + 2 + ... n-1 is n*(n-1)/2 while your answer says n*(n+1)/2. – corsiKa May 02 '14 at 22:28
  • No, my answer is about the sum 1 + 2 + ... N, which is N.(N + 1)/2. And obviously N = Users-1 answers the question. The OP didn't use N. –  May 02 '14 at 22:48
  • If the OP has a set, which he does, I would expect (lacking something that clarifies otherwise) the size of the set to be N, not N+1. It rather violates the principle of least surprise. If the question has a table in which f(2) = 1 and you have a table where f(2) = 3 I would say you did not answer the OP's question. If you're going to define N, you should make that definition explicit. I like your answer, but in its current form it does not satisfactorily answer the question. – corsiKa May 02 '14 at 23:09
0

Here is another way to find the sum of the first $n$ squares that generalizes to sums of higher powers.

$(k+1)^2 - k^2 = 2k+1$

$\sum_{k=1}^n ( (k+1)^2 - k^2 ) = \sum_{k=1}^n (2k+1)$

$(n+1)^2 - 1^2 = 2 \sum_{k=1}^n k + n$

$\sum_{k=1}^n k = \frac{ (n+1)^2 - 1 - n }{2} = \frac{n^2+n}{2}$

user21820
  • 57,693
  • 9
  • 98
  • 256
-1

This is kind of a pseudo code:

Say you have n number of people, and you labeled them.

for i in (1,2,3,...,n), person i need to compare with all the people who has a number larger (strictly), so person i need to compare (n-i) times.

so adding up would be (n-1) + (n-2) + ... + 3 + 2 + 1...

which would be the sum from 1 to (n-1)

TYZ
  • 377