Determine the output of the following Python 3 program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
This is a straightforward implementation to determine the Ramsey number R ( 5 , 5 ) ; that is, the smallest positive integer n such that no matter how the edges of the complete graph K n with n vertices is colored with two colors, there exists five vertices whose edge between any two of them is colored by the same color. (This latter portion is called a monochromatic clique of size 5 .)
To see how this implementation works, observe the following:
n
is the number of vertices in the graph; we will keep incrementing this value until we find the solution.f
is a Boolean variable indicating whether we have found the value ofn
. Initially, for n = 4 , clearly the answer is false; there are not even enough vertices.while not f
loop iterates until we find the value ofn
. At the beginning, we setf
to be true and increment the value ofn
; we will togglef
back to false when we find a counterexample, indicating that we need to continue to look for the value ofn
.e
is the list of edges in our graph. The linee = [(i,j) for i in range(n) for j in range(i+1,n)]
declares the edges.for k in range(len(e)+1): for b in itertools.combinations(e, k)
is a well-known method to loop over all possible subsetsb
ofe
.c
is a dictionary mapping each edge to its color ( 1 or 2 ).g
is a Boolean variable indicating whether we have found the clique in the graph. Initially this is false.; we toggle this true once we find the clique (third line from bottom).for i in e
loop simply assigns the colors;b
is the list of edges that are colored "blue" (color 2 ), and the rest is the list of edges that are colored "red" (color 1 ).for v in itertools.combinations(range(n), 5)
iterates over all 5 -element subset of the vertices. This is the vertices that we want to test, whether the edges between them are colored the same.a = c[(v[0], v[1])]
takes the color of the first edge. Sinceitertools.combinations
retains the order of elements in the iterable, in this caserange(n)
, andrange(n)
returns the vertices in increasing order, we are guaranteed that0 <= v[0] < v[1] < n
and thusc[(v[0], v[1])]
is defined.for i in v: for j in v
is a double loop that loops over all pairs of elements inv
, not necessarily different. However,if i < j:
afterwards forces the first element to be less than the second element.if c[(i, j)] != a: a = 0
changes the sought color to a non-existent color if the edge inspected is incorrect, indicating failure for the current set of vertices.if a: g = True
states that this graph is correct; we just found the clique.if not g: f = False
states that this value ofn
is incorrect; we just found a counterexample where there is no monochromatic clique.print((n-1)//7)
is the onlyprint
statement in the problem; upon getting the value ofn
, we put this to the weird-looking function(n-1)//7
and print its value.Now that we know the program computes the Ramsey number R ( 5 , 5 ) , we need to figure out its value... but unfortunately, this implementation will take a very long time to finish. We have to look for help.
The given Wikipedia article above states that 4 3 ≤ R ( 5 , 5 ) ≤ 4 9 , at the time of posting of this problem. Surprisingly, every value from 4 3 to 4 9 gives the same value when fed to the function n ↦ ⌊ 7 n − 1 ⌋ ; all of them returns 6 . This is our answer.
This problem is inspired from Halting Problem , a puzzle in MIT Mystery Hunt 2013.