5

I can store any undirected simple graph N vertices using $b = (N-1)N/2$ bits, by creating a mask of the edges on the upper diagonal of the adjacency matrix. For example the adjacency matrix of $K_3$ is

$$ A = [[0,1,1],[1,0,1],[1,1,0]] $$

which can be stored as the bit-mask $011101110$ or as an integer in base-10 as $238$. In general, this number isn't unique (due to graph isomorphisms) but it doesn't matter for my purposes. From a practical standpoint, this means I can store graphs up to $N=11$ in a database using a 64-bit integer.

My question now involves trees, which are considerably more edge-sparse. Is there a mapping scheme that can allow me to store (and quickly reconstruct) trees $N>11$ using a single 64-bit integer?

Hooked
  • 6,785
  • 1
    This question may be more suited for one of the CS SE's, if you agree let me know and I can close and repost. – Hooked Jun 02 '15 at 20:54
  • Can we assume the tree is a spanning tree? – Asinomás Jun 02 '15 at 20:55
  • @Gamamal there was no restriction that the graph had to be connected, thus a spanning tree might not exist. – Hooked Jun 02 '15 at 20:58
  • @Hooked Then it would be called "forrest". – dtldarek Jun 02 '15 at 20:59
  • @Gamamal even for connected graphs, isn't every tree a spanning tree to some graph? For example, shouldn't every tree be a spanning tree to the complete graph? – Hooked Jun 02 '15 at 21:02
  • I think you can store an undirected tree or even a forrest in $n\log_2 n$ bits: each vertex points to some other vertex if there is edge between them (the parent for an arbitrary root). Note that $16\cdot 4 = 4\cdot 2^4=64$. For a directed version you need some $2n$ more bits. – dtldarek Jun 02 '15 at 21:19

1 Answers1

5

The number of spanning trees in a graph with $n$ vertices is $n^{n-2}$, A common way to adress a tree is through its Prüfer code.

If we want to talk about trees that may not be spanning trees we could adress this issue by first assigning the vertex set of the tree and then giving its Prüfer code.

Anyways it is not possible to store every single tree on $n$ vertices using a $64$-bit intger because the number of such trees is more than $n^{n-2}$ and $n^{n-2}$ is stricty larger than $2^{64}$ for $n\geq 18$

Asinomás
  • 107,565
  • Perfect! I was looking for a labeling like this! I knew there was some $N$ at which you couldn't store a tree using a single 64-bit integer -- obviously there are an infinite such trees. – Hooked Jun 02 '15 at 21:04
  • 2
    I had no idea there exists such a neat bijection. – dtldarek Jun 02 '15 at 21:12