## #MonthOfJulia Day 24: Graphs

If you’re not too familiar Graph Theory, then it might be an idea to take a moment to get the basics. Graphs are an extremely versatile data structure for storing data consisting of linked entities. I’m going to look at two packages for managing graphs in Julia: LightGraphs and Graphs.

## LightGraphs

As usual, the first step is to load the package.

LightGraphs has methods which generate a selection of standard graphs like `StarGraph()`

, `WheelGraph()`

and `FruchtGraph()`

. There are also functions for random graphs, for example, `erdos_renyi()`

and `watts_strogatz()`

. We’ll start off by creating two small graphs. One will have 10 nodes connected by 20 random edges. The other will be a directed star graph consisting of four nodes, the central node being connected to every other node.

It’s simple to find the degree and neighbours of a given node.

There’s a straightforward means to add and remove edges from the graph.

The package has functionality for performing high level tests on the graph (checking, for instance, whether it is cyclic or connected). There’s also support for path based algorithms, but we’ll dig into those when we look at the Graphs package.

## Graphs

Before we get started with the Graphs package you might want to restart your Julia session to purge all of that LightGraphs goodness. Take a moment to browse the Graphs.jl documentation, which is very comprehensive.

As with LightGraphs, there are numerous options for generating standard graphs.

Graphs uses the GraphViz library to generate plots.

Of course, a graph can also be constructed manually.

Individual vertices (a vertex is the same as a node) can be interrogated. Since we are considering a directed graph we look separately at the edges exiting and entering a node.

Vertices can be created with labels and attributes.

Those vertices can then be used to define edges, which in turn can have labels and attributes.

Finally the collection of vertices and edges can be gathered into a graph.

It’s possible to systematically visit all connected vertices in a graph, applying an operation at every vertex. `traverse_graph()`

performs the graph traversal using either a depth first or breadth first algorithm. In the sample code below the operation applied at each vertex is `LogGraphVisitor()`

, which is a simple logger.

We can use Dijkstra’s Algorithm to calculate the distance from a given vertex to all other vertices in the graph. We see, for instance, that the distance from vertex 1 to vertex 4 is three steps. Since vertex 1 and vertex 20 are not connected, the distance between them is infinite. There are a couple of other algorithms available for calculating shortest paths.

As with the most of the packages that I have looked at already, the functionality summarised above is just a small subset of what’s available. Have a look at the home pages for these packages and check out the full code for today (which looks at a number of other features) on github. Some time in the future I plan on looking at the EvolvingGraphs which caters for graphs where the structure changes with time.