Skip to content

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.

The examples of this chapter use a family tree and the calls between the methods of a small program.

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:

012345level 2level 1level 0
Context
  • 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:

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.