Skip to content

Instantly share code, notes, and snippets.

@Radcliffe
Created February 16, 2015 00:47
Show Gist options
  • Save Radcliffe/0f3bc84cb56ca5413e5c to your computer and use it in GitHub Desktop.
Save Radcliffe/0f3bc84cb56ca5413e5c to your computer and use it in GitHub Desktop.
A "square-plus-two" digraph
# This Python script generates a directed graph with
# vertex set V = {0, 1, 2, ..., m-1} and edge set
# E = {(n, f(n)) : n in V} where f(n) = n*n + c (mod m).
#
# Requires the python-igraph package (http://igraph.org/python/)
#
# Author: David Radcliffe (14 February 2014)
# Based on an idea of James Tanton
# https://twitter.com/jamestanton/status/549558473868849152
#
# Output: https://pbs.twimg.com/media/B91mPp_CEAAYscj.png
m = 1000
c = 2
from igraph import *
g = Graph(directed=True)
g.add_vertices(m)
for n in xrange(m):
g.add_edge(n, (n*n+c) % m)
plot(g, vertex_size=4, edge_arrow_width=0.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment