Home

Awesome

Cairo-graphs: A graph library in Cairo

Introduction

Cairo-Graphs is a graph library written in Cairo that allows you to create and interact with graphs, entirely in Cairo, without state persistence.

Graph implementation

The graph is implemented as an array of Vertex. A vertex is composed of three elements :

Since the memory is immutable in cairo, I had to make a choice when building the graph. Since I store in each vertex an array containing the neighboring vertices, I had to track how many neighbors each vertex had (adjacent_vertices_count[i]). But since the memory is immutable in cairo, to avoid having complex operations requiring rebuilding the whole graph on each change I introduced an external array that tracks how many neighbors each vertex has. That way, when I add a neighbor to a vertex, I can just append it to the adjacent_vertices_count array of the current vertex, and I can update the number of neighbors it has by rebuilding the adjacent_vertices_count array.

This implementation is probably not the most efficient, and one should expect modifications - I have to study the benefits of using another data structure, for example, a dict, to track how many neighbors each vertex has.

Here is how the Graph struct is built :

How to use it

Create a graph

To create a graph, import the GraphMethods namespace from cairo_graphs.graph.graph that exposes several methods :

The easiest way is to have an array of Edge, Edge being a struct with the following fields :

You can for example simply call build_directed_graph_from_edges(edges_len,edges) to create a directed graph from an array of edges.

Graph algorithms

For now, two algorithms are implemented :

Testing

To run the tests, install protostar and run :

protostar test