Skip to content

9.2 Number the nodes in a different order

You renumber a graph in depth-first order or in an order of your own, such as a topological one.

You want the nodes of a graph numbered in an order that suits your next step. The node IDs of a graph often follow from how it was built, such as the order in which a parser met the methods or in which the rows of a database came. Another numbering helps in these cases:

  • Many searches run over a large graph. In the order of a depth-first search, nodes that the search visits one after another get adjacent node IDs. Later searches then read the arrays of the graph more sequentially, which can make these searches faster.
  • A loop runs over the nodes by node ID, such as the steps of a build plan. In a topological order, the loop meets the source of every edge before its target.
  • A report lists the nodes by node ID. If you number them by name, every list of node IDs comes out sorted by name.

The first example graph calls models the calls between the ten methods of a small program. It has an edge from each method to each method it calls. A depth-first search from main (node 0) visits the methods in an order of its own. What node ID does each method get in that order?

The second example graph steps models the steps that build and publish the program. It has an edge a -> b if step b may start only after step a. The steps get the node IDs of a topological order.

The picture after shows the calls with their new node IDs:

The calls between the methods
The calls, numbered in depth-first order
0: main1: parseArgs2: run3: load4: save5: show6: parse7: format8: read9: write
0: main1: parseArgs2: run3: load6: save9: show4: parse7: format5: read8: write
Output
  • Method with its new node ID
  • From a method to a method it calls
// In the order of a depth-first search: nodes visited one after another get adjacent
// IDs.
NodeMapping searchOrder = NodeMapping.reindex(calls);
Graph inSearchOrder = Graphs.remapped(calls, searchOrder);
// searchOrder, new to old: [0, 1, 2, 3, 6, 8, 4, 7, 9, 5] - parse and read follow
// load
// In an order of your own, here a topological one: every edge then goes to a higher
// ID.
NodeMapping runOrder = NodeMapping.forPermutation(TopOrder.of(steps));
Graph inRunOrder = Graphs.remapped(steps, runOrder);
// runOrder, new to old: [2, 0, 1, 4, 3, 5, 6, 7, 8] - lint, generate, compile, docs,
// ...
Full example
// In the order of a depth-first search: nodes visited one after another get adjacent
// IDs.
let search_order = NodeMapping::reindex(&calls);
let in_search_order = graphs::remapped(&calls, &search_order);
// search_order, new to old: [0, 1, 2, 3, 6, 8, 4, 7, 9, 5] - parse and read follow
// load
// In an order of your own, here a topological one: every edge then goes to a higher
// ID.
let run_order = NodeMapping::for_permutation(&top_order::of(&steps)?)?;
let in_run_order = graphs::remapped(&steps, &run_order);
// run_order, new to old: [2, 0, 1, 4, 3, 5, 6, 7, 8] - lint, generate, compile, docs,
// ...
Full example
// In the order of a depth-first search: nodes visited one after another get adjacent
// IDs.
const searchOrder = NodeMapping.reindex(calls);
const inSearchOrder = Graphs.remapped(calls, searchOrder);
// searchOrder, new to old: [0, 1, 2, 3, 6, 8, 4, 7, 9, 5] - parse and read follow
// load
// In an order of your own, here a topological one: every edge then goes to a higher
// ID.
const runOrder = NodeMapping.forPermutation(TopOrder.of(steps));
const inRunOrder = Graphs.remapped(steps, runOrder);
// runOrder, new to old: [2, 0, 1, 4, 3, 5, 6, 7, 8] - lint, generate, compile, docs,
// ...
Full example
// In the order of a depth-first search: nodes visited one after another get
// adjacent IDs.
NodeMapping searchOrder = NodeMapping.Reindex(calls);
Graph inSearchOrder = Graphs.Remapped(calls, searchOrder);
// searchOrder, new to old: [0, 1, 2, 3, 6, 8, 4, 7, 9, 5] - parse and read follow
// load
// In an order of your own, here a topological one: every edge then goes to a
// higher ID.
NodeMapping runOrder = NodeMapping.ForPermutation(TopOrder.Of(steps).AsSpan());
Graph inRunOrder = Graphs.Remapped(steps, runOrder);
// runOrder, new to old: [2, 0, 1, 4, 3, 5, 6, 7, 8] - lint, generate, compile,
// docs, ...
Full example

Both mappings go from new to old node IDs. In depth-first order, load keeps node ID 3 and is followed by the methods it calls: parse and read. Only then come save and the methods it calls, and show comes last. In the topological order, the steps start with lint and generate, which wait for no other step. Every edge of inRunOrderin_run_orderinRunOrderinRunOrder leads to a higher node ID.