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.
Problem
Section titled “Problem”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.
Example
Section titled “Example”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:
- Method with its new node ID
- From a method to a method it calls
Solution
Section titled “Solution”// 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,// ...// 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,// ...// 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,// ...// 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, ...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.
Good to know
Section titled “Good to know”- Where the search starts.
NodeMapping.reindexNodeMapping::reindexNodeMapping.reindexNodeMapping.Reindexstarts at node 0, takes the out-nodes of each node in ascending order, and starts again at the smallest node it has not visited, until every node has a new node ID. For another start node, collect the nodes in a listorderinenterNodeenter_nodeenterNodeEnterNode, in the order aDfsTraverserDfsTraverserDfsTraverserDfsTraverserenters them. Run it for the start node withrun(start)run_fromrun(startNode: number)Run(int), then for the rest withrun()runrun()Run(). See Write a depth-first search without recursion for such a traverser. Passorder&orderorderCollectionsMarshal.AsSpan(order), which holds every node once, toNodeMapping.forPermutationNodeMapping::for_permutationNodeMapping.forPermutationNodeMapping.ForPermutation. - A mapping of your own.
NodeMapping.ofNodeMapping::ofNodeMapping.ofNodeMapping.Oftakes an array from new to old node IDs, for any numbering you compute yourself. - A topological order needs a DAG (directed acyclic graph).
TopOrder.oftop_order::ofTopOrder.ofTopOrder.Ofrejects a graph with a cycle. See Put the nodes in dependency order. For a graph with cycles, see Put the nodes in dependency order although the graph has cycles. - The data follows the new numbering. Node data follows through the same mapping. See Carry node data over to a transformed graph. The edge indices change too. See Carry edge data over to a transformed graph.
- Renumbering for speed costs a depth-first search and a copy of the graph. On a large graph that many searches follow, it can make them faster; on a small graph, it makes no difference.
API used
Section titled “API used”NodeMapping.reindex(Graph)NodeMapping::reindexNodeMapping.reindexNodeMapping.ReindexNodeMapping.forPermutation(IntList)NodeMapping::for_permutationNodeMapping.forPermutationNodeMapping.ForPermutationTopOrder.of(Graph)top_order::ofTopOrder.ofTopOrder.OfNodeMapping.of(int[])NodeMapping::ofNodeMapping.ofNodeMapping.OfGraphs.remapped(Graph, NodeMapping)graphs::remappedGraphs.remappedGraphs.RemappedDfsTraverser.enterNode(int)DfsHooks::enter_nodeDfsTraverser.enterNodeDfsTraverser.EnterNodeDfsTraverser.run(int)DfsTraverser::run_fromDfsTraverser.run(startNode: number)DfsTraverser.Run(int)DfsTraverser.run()DfsTraverser::runDfsTraverser.run()DfsTraverser.Run()