Wednesday, March 4, 2026

A collatz weekend

TLDR; found an opensource python software for printing a graph. My program got referenced by the developer over at the repo discussions page. Understood where ascii and where graphical printing makes sense. I also share a few thoughts on opensource. 

My youtube algorithm suggested a Veritasium video on the collatz conjecture. In it there was a graph - for a long time I wanted to print it. Here is an example of the graph which was shown in the Veritasium video. And what follows are two images I generated using other opensource software.

Fig1. Graph plot from the Veritasium video

Fig 2. pygraphviz plot


Fig 3. Ascii plot using binarytree python library

These graphs are generally called collatz graph or collatz chains. It is built on a simple mathematical rule which we won't discuss here. The specific graph I have printed is called a reverse-collatz graph. But note that the 3rd image is an ascii print of the same graph. 

Oh wait! Do you know what is a "graph"? Or do you know what "ascii" is? I think I must devote some time to introduce some fancy computer science terms. However, I am not going to delve deep into them. The impatient readers can skip the next few sections and directly read from the section called "My opensource library hunt"

Graphs

YOU don't really need to know what a graph is...It has no significance in your day-to-day life! That picture above is an example of a graph. It is a node which is connected to other nodes via lines called edges. In practice, the nodes/edges represent something. I am sure most visitors here have used Google Maps; it uses "graphs". Its neatly abstracted to you, the consumer. 

Binary Tree

The collatz graph which I tried to print is a kind of "binary tree". I have hidden most of the "binariness" to make it look plesant. Another example of a binary tree is the famous pascal's triangle. This is another mathematical term that is of no consequence to people's lives.

Fig 4. Somewhere in Ladakh

Yet it is out there in world. Staring back at you. Oblivious; in plain sight.

ASCII

It is an encoding standard for storing and transmitting information. Simply put if I save some text file (with a few sentences) onto a disk, and, I sent that disk to you...you use your computer to access that disk and read that text file and it contents...if the computer displays the correct sentences - how do you think that is possible?

This is a remarkable invention by human beings. It is the backbone of the internet. It is because of ascii many subtle things happen in life - good or bad:

1. You are able to read this blog post.

2. An expectant father is able to view the first pictures of his new born child while he is located on a different continent compared to his spouse's labour ward.

3. People nefariously indulge in relatively evil practices like pornography, blackmail, riots, etc.

4. Etc.

Shakespear once said, the web of life is of mingled yarn - the good and bad together. Maybe I failed in social science - and today its via ascii that I am coming to realize a lot more about our own species. (And I am a quadrigenarian)

Why print in ascii?

1. I can easily print the graph in a text file

2. I don't have to worry about scale. Most plotting libraries, will take into account the drawing area and auto-scale the plots.

3. I don't need special software to view my outputs. A simple text editor (with word-wrap-off) would do.

My opensource library hunt

In python, we usually have matplotlib for plotting stuff. But instead I thought that I would want to see the graphs better and not be worried about "scale" or drawing-area-sizes. Therefore ascii; there was no further reason than that initially. 

Finally came to discover a small library phart - Python Heirarchial Ascii Rendering Tool. Of course if you are someone who quickly alluded to some nasty things humans and often other mammals indulge in; you are excused.

The version I had got via pypi (i.e. pip install). It had a dependency of another pythonic graph software called "networkx". It was fairly quick for me to setup and get to printing outputs. But later I came to understand that the plots were incorrect. And I posted in on the github repo discussions. I waited for the response. Meanwhile I discovered another graph plotting package. This wasn't a python library. It was a C program called graphiviz. And there were python ports of graphviz which I then explored. And it internally used matplotlib to plot. But then the phart developer of the repo got back with fixes to my issue. And then I got to printing.

What kind of plot to choose and when?

This section won't be useful for anyone unless you are into research involving graph theory or you need to do some graph based computations.

(A) Choose a image-based graph plotting solutions when you need the following:

1. Auto-scaling.

2. High detail in nodes/edges

3. Aesthetics

(B) Choose ascii graph plotting when you need the following:

1. Searchability

2. Plotting to scale is not needed

3. Simple text file based sharing is important.

Most people will choose approach A. However, the final say is really a matter of requirement. I was doing it as a weekend fun project. If I were a PG student writing a research paper on the Collatz conjecture, I'd probably do both ascii printing and graphical renderings. But I'd use the latter when I write my research paper.

Opensource Affairs

Here is a small appreciation to phart developer and opensource

When I first tinkered with phart it was buggy. My output wasn't coming right. I thought it may be my incorrect use of the APIs. I posted onto the repo discussions page, and, in fact shared by whole program there and asked the developer if I was doing it wrong. He admitted his code had a bug. And he fixed it in a span of few days. It didn't matter if he took a month, or, even a year. He got back to me. These are qualities I adore.

Fig 5. ascii plot using phart

I have posted issues and interacted in developer forums and slack groups of other opensource software. The larger they are it is likely that the issue you post (or raise) is not given enough attention. There are many reasons why this can happen, but, I leave it to you the reader. And there are a few issues I have raised in some repos that belong to a few infamous organizations that need attention.