Graphs in Python for A Level Computer Science

Part 1 – Networkx Basics

networkx is a powerful python package that allows you to easily work with graphs in Python. Combining it with the matplotlib.pyplot package even makes it simple to draw graphs. This is very useful when learning or teaching about graphs for A Level Computer Science.

Setup

Nodes

Edges

OK so now let’s look at how we add edges to our graph.

The Fun Part – Drawing Your Graph

Now we get to draw our graph. They say a picture is worth a thousand words and I agree. Python makes this super easy for us with just a couple of lines of code.

I for one am pretty impressed by the networkx package. This article covers just the very basics, but already we have drawn a weighted graphs and learned how to add nodes and edges to an existing graph using networkx‘s powerful functionality.

Here’s the complete listing for this example, for your convenience:

Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Get the code for the classic Retro Alien Blitz Game using Python Turtle Graphics when you join our mailing list