12 Writing searches of your own
This chapter shows how to write your own depth-first and breadth-first searches on Graphity's traversers without recursion, and how to loop over the arrays of a graph.
The algorithms of this guide cover the common questions. For the others, you write a search of your own. It can, for example, collect what you need on the way, follow only some edges, or stop when you have the answer. Graphity’s traversers keep the stack or the queue and the visited nodes, and they call your code at the right moments. They run without recursion, so that a deep graph does not overflow the call stack.
Problems in this chapter
Section titled “Problems in this chapter”- 12.1 Write a depth-first search without recursionYou write a depth-first search of your own, with hooks that are called when a node is entered and when it is left.
- 12.2 React to every edge of a depth-first searchYou run a depth-first search that reports every edge with the state of its target and lets you follow the edge, skip it or stop.
- 12.3 Write a breadth-first searchYou write a search of your own that works off a queue, level by level - breadth-first, or following only the nodes you choose.
- 12.4 Loop over the arrays of a graph directlyYou read the out-nodes and in-nodes of a graph from the arrays of its CSR layout (compressed sparse row), for the fastest loops over all edges.
The examples of this chapter use a family tree and the calls between the methods of a small program.
Depth-first and breadth-first
Section titled “Depth-first and breadth-first”A depth-first search follows the outgoing edges from a node as deep as it can before it turns back. A breadth-first search visits the nodes in the order of their distance from the start node: first the start node, then those one edge away, then those two edges away, and so on. Both visit each node at most once. Here, both start at node 0:
- Level
- Start node
- Edge not followed
| node | 0 | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|---|
| depth-first: entered | 1st | 2nd | 5th | 3rd | 4th | 6th |
| depth-first: left | 6th | 3rd | 5th | 1st | 2nd | 4th |
| breadth-first: visited | 1st | 2nd | 3rd | 4th | 5th | 6th |
The depth-first search enters 3 and 4 from 1 and leaves both before it enters 2. It leaves 0 last. Between entering and leaving, a node is on the current path. That path is the stack of the search.
The breadth-first search visits the nodes level by level. A level holds the
nodes at the same distance from the start node, and the frames show the levels. Neither search
follows the dashed edge 2 -> 4: when they meet it, node 4 is visited already.
A traverser calls your code at certain moments of its search. These pieces of code are called hooks. You start a traverser at the nodes you choose. Graphity has these traversers:
DfsTraverserDfsTraverserDfsTraverserDfsTraversergoes depth-first. It calls one hook when it enters a node and another when it leaves the node, after everything the search reached from there.DfsEdgeTraverserDfsEdgeTraverserDfsEdgeTraverserDfsEdgeTraversergoes depth-first as well. It also reports every edge with the state of its target: not visited yet, on the current path, or finished. Your hook decides whether to follow the edge.FrontierTraverserFrontierTraverserFrontierTraverserFrontierTraverserworks off a first-in-first-out queue, level by level. It is the base of a breadth-first search, or of any search that chooses which nodes enter the queue.
You write the hooks in a subclass of the traverser, as methods that override its own. The traverser’s protected methods tell the state of the search, such as whether a node is visited. They also let you stop the search.
You write the hooks in a type of your own and pass it to the run. The type implements the trait of
the traverser: DfsHooks, DfsEdgeHooks or FrontierHooks. Each hook also receives a DfsControl
or a FrontierControl. Its methods tell the state of the search, such as whether a node is visited.
They also let you stop the search.
You write the hooks in a subclass of the traverser, as methods that override its own. The traverser’s protected methods tell the state of the search, such as whether a node is visited. They also let you stop the search.
You write the hooks in a subclass of the traverser, as methods that override its own. The traverser’s protected methods tell the state of the search, such as whether a node is visited. They also let you stop the search.
The visited nodes add up over the runs of one traverser: a node is visited at most once in its lifetime. A loop over all edges that needs no search at all reads the arrays of the graph directly.