Skip to content

1.1 Build a graph from a list of edges

You build a graph from pairs of node IDs, given in any order and with duplicates.

You have the edges of a graph as pairs of numbers and want a graph that the algorithms of Graphity can work on. The pairs come in no particular order, and some of them may come more than once. This is the usual case when a graph comes from outside the program:

  • a file of edges, such as an export from another tool;
  • the result of a database query;
  • the output of another program or library.

The example graph models the dependencies between the eight packages of a small program. It has a node for each package and an edge p -> q for each package q that p uses: app uses cli, ui and service; ui uses service, model and util; and so on. The packages are numbered from 0 to 7, and each dependency is a pair of these numbers. The pair for ui -> model happens to be listed twice. How do these pairs become this graph?

0: app1: cli2: ui3: service4: model5: storage6: json7: util

A GraphBuilderGraphBuilderGraphBuilderGraphBuilder takes the nodes and the edges, and GraphBuilder.buildGraphBuilder::buildGraphBuilder.buildGraphBuilder.Build turns them into a graph:

// The packages of a program, numbered from 0 to 7, and the dependencies between them
// as pairs of package numbers: an edge p -> q means that package p uses package q.
String[] names = {"app", "cli", "ui", "service", "model", "storage", "json", "util"};
int[][] uses = {
{0, 1}, {0, 2}, {0, 3}, {1, 7}, {2, 3}, {2, 4}, {2, 7}, {3, 4}, {3, 5}, {4, 5},
{5, 4}, {5, 6}, {5, 7}, {6, 7}, {2, 4}};
// One node per package, numbered as in names, and one edge per pair.
var builder = new GraphBuilder();
builder.addNodes(names.length);
for (int[] pair : uses) {
builder.addEdge(pair[0], pair[1]);
}
Graph dependencies = builder.build();
int nodeCount = dependencies.nodeCount(); // 8
int edgeCount = dependencies.edgeCount(); // 14 - the pair {2, 4} was given twice
Full example
// The packages of a program, numbered from 0 to 7, and the dependencies between them
// as pairs of package numbers: an edge p -> q means that package p uses package q.
let names = [
"app", "cli", "ui", "service", "model", "storage", "json", "util",
];
#[rustfmt::skip]
let uses = [
(0, 1), (0, 2), (0, 3), (1, 7), (2, 3), (2, 4), (2, 7), (3, 4), (3, 5), (4, 5),
(5, 4), (5, 6), (5, 7), (6, 7), (2, 4),
];
// One node per package, numbered as in names, and one edge per pair.
let mut builder = GraphBuilder::new();
builder.add_nodes(names.len() as u32);
for (source, target) in uses {
builder.add_edge(source, target);
}
let dependencies = builder.build();
let node_count = dependencies.node_count(); // 8
let edge_count = dependencies.edge_count(); // 14 - the pair (2, 4) was given twice
Full example
// The packages of a program, numbered from 0 to 7, and the dependencies between them
// as pairs of package numbers: an edge p -> q means that package p uses package q.
const names = ["app", "cli", "ui", "service", "model", "storage", "json", "util"];
const uses = [
[0, 1], [0, 2], [0, 3], [1, 7], [2, 3], [2, 4], [2, 7], [3, 4], [3, 5], [4, 5],
[5, 4], [5, 6], [5, 7], [6, 7], [2, 4]];
// One node per package, numbered as in names, and one edge per pair.
const builder = new GraphBuilder();
builder.addNodes(names.length);
for (const [p, q] of uses) {
builder.addEdge(p, q);
}
const dependencies = builder.build();
const nodeCount = dependencies.nodeCount; // 8
const edgeCount = dependencies.edgeCount; // 14 - the pair [2, 4] was given twice
Full example
// The packages of a program, numbered from 0 to 7, and the dependencies between
// them as pairs of package numbers: an edge p -> q means that package p uses
// package q.
string[] names =
["app", "cli", "ui", "service", "model", "storage", "json", "util"];
int[][] uses =
[
[0, 1], [0, 2], [0, 3], [1, 7], [2, 3], [2, 4], [2, 7], [3, 4], [3, 5],
[4, 5], [5, 4], [5, 6], [5, 7], [6, 7], [2, 4],
];
// One node per package, numbered as in names, and one edge per pair.
var builder = new GraphBuilder();
builder.AddNodes(names.Length);
foreach (int[] pair in uses)
{
builder.AddEdge(pair[0], pair[1]);
}
Graph dependencies = builder.Build();
int nodeCount = dependencies.NodeCount; // 8
int edgeCount = dependencies.EdgeCount; // 14 - the pair [2, 4] was given twice
Full example

The builder numbers the nodes from 0 in the order they are added, so the node IDs match the positions in names. The edges may come in any order. GraphBuilder.buildGraphBuilder::buildGraphBuilder.buildGraphBuilder.Build sorts them by their source and then their target, and this order gives each edge its edge index. GraphBuilder.buildGraphBuilder::buildGraphBuilder.buildGraphBuilder.Build also merges the duplicate. The result is the graph of the picture, with 8 nodes and 14 edges.