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.
Problem
Section titled “Problem”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.
Example
Section titled “Example”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?
Solution
Section titled “Solution”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(); // 8int edgeCount = dependencies.edgeCount(); // 14 - the pair {2, 4} was given twice// 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(); // 8let edge_count = dependencies.edge_count(); // 14 - the pair (2, 4) was given twice// 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; // 8const edgeCount = dependencies.edgeCount; // 14 - the pair [2, 4] was given twice// 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; // 8int edgeCount = dependencies.EdgeCount; // 14 - the pair [2, 4] was given twiceThe 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.
Good to know
Section titled “Good to know”- One node at a time.
GraphBuilder.addNodeGraphBuilder::add_nodeGraphBuilder.addNodeGraphBuilder.AddNodeadds a single node and returns its node ID, for when the nodes come one by one. - An edge to a node that does not exist is rejected at the call that adds it, not later in
GraphBuilder.buildGraphBuilder::buildGraphBuilder.buildGraphBuilder.Build. See Build a graph from objects of your own for what to do if your node IDs come from elsewhere and you do not know their count. - Many edges.
GraphBuilder.ensureEdgeCapacityGraphBuilder::ensure_edge_capacityGraphBuilder.ensureEdgeCapacityGraphBuilder.EnsureEdgeCapacityreserves the room for the edges once, instead of growing step by step. Use it if you know roughly how many edges the builder will hold in all. That number includes duplicates: here it is the 15 pairs, not the 14 edges of the graph. - Counts.
Graph.nodeCountGraph::node_countGraph.nodeCountGraph.NodeCountandGraph.edgeCountGraph::edge_countGraph.edgeCountGraph.EdgeCountreturn the size of a graph, andGraph.isEmptyGraph::is_emptyGraph.isEmptyGraph.IsEmptytells whether it has no nodes at all. - The builder stays usable. After
GraphBuilder.buildGraphBuilder::buildGraphBuilder.buildGraphBuilder.Build, you can add more and build again; the graph you got before does not change. - Edges in batches. See Build trees and paths in a few calls for how to add several edges at once, such as all edges of one node or a path through several nodes.
API used
Section titled “API used”GraphBuilder()GraphBuilder::newnew GraphBuildernew GraphBuilderGraphBuilder.addNodes(int)GraphBuilder::add_nodesGraphBuilder.addNodesGraphBuilder.AddNodesGraphBuilder.addNode()GraphBuilder::add_nodeGraphBuilder.addNodeGraphBuilder.AddNodeGraphBuilder.addEdge(int, int)GraphBuilder::add_edgeGraphBuilder.addEdgeGraphBuilder.AddEdgeGraphBuilder.build()GraphBuilder::buildGraphBuilder.buildGraphBuilder.BuildGraphBuilder.ensureEdgeCapacity(int)GraphBuilder::ensure_edge_capacityGraphBuilder.ensureEdgeCapacityGraphBuilder.EnsureEdgeCapacityGraph.nodeCount()Graph::node_countGraph.nodeCountGraph.NodeCountGraph.edgeCount()Graph::edge_countGraph.edgeCountGraph.EdgeCountGraph.isEmpty()Graph::is_emptyGraph.isEmptyGraph.IsEmpty