12

The two most common ways to traverse a graph are breadth-first search and depth-first search. Both of these search algorithms follow a common template:

  • Create a worklist W, seeded with the start node s.
  • While the worklist isn't empty:
    • Remove the first element of the worklist; call it v.
    • If v is not visited:
      • Mark v as visited.
      • For each node u directly connected to v, add u to W.

In a breadth-first search, the worklist W is implemented as a FIFO queue, while in depth-first search it's a LIFO stack. If W is a priority queue, you get uniform-cost search.

A while back I asked a question about a data structure for choosing random elements out of a bag. If you implement the above worklist W using this random bag, then you get a "random-first search" algorithm that randomly explores the nodes in the graph starting with the initial node.

My question is this: are there any known algorithms that use this type of search? That is, are there algorithms that work by generating a random spanning tree of the graph in this fashion?

Community
  • 1
  • 1
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • The wikipedia article on [Maze generation algorithm](http://en.wikipedia.org/wiki/Maze_generation_algorithm) mentions a randomized version of the DFS. – miku Jan 16 '12 at 20:45
  • I'm curious to know what lead you to come up with this scheme. – MAK Jan 16 '12 at 22:49

5 Answers5

6

Automatic puzzle generation is an application for which random-first search is a useful strategy.

Suppose you wish to generate an instance of a combinatorial puzzle like Sudoku. One approach is to generate a random, completely solved, instance, and then remove numbers as long as there's still a unique solution. How do you generate that random solved instance in the first place? You start with an empty grid and apply a random-first solving algorithm. In fact, it proves to be fairly easy to use the same code for both generation and solution, by switching between random-first and best-first strategies for picking the next number to try.

This is exactly what we did when writing the Nintendo DS game Zendoku, and I wrote a detailed article about our approach.

See also this answer discussing maze generation using randomized Prim's algorithm.

Community
  • 1
  • 1
Gareth Rees
  • 64,967
  • 9
  • 133
  • 163
2

This is exactly an implementation of the best-first search given a random heuristic.

moooeeeep
  • 31,622
  • 22
  • 98
  • 187
1

I don't know if there's a name for the specific algorithm you are describing. It sounds a bit like simulated annealing. In optimization theory, there's also the concept of a random search, but it does rely on an evaluation function, while what you describe doesn't seem to. There's also this Bachelor's Thesis by Brodeur and Childs that has a nice summary of random algorithms for graph search, including a discussion of when one might use them.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • While nice, these search algorithms are all somewhat informed, even the one in the thesis you linked. I'm primarily interested in searches that are random and entirely uninformed. But thanks! – templatetypedef Jan 16 '12 at 22:09
  • @templatetypedef - Perhaps you have discovered a new search algorithm. That gives you naming rights! Might I suggest the "Bumble Around Algorithm"? :) – Ted Hopp Jan 16 '12 at 22:35
0

Check out A*. You can adjust your heuristic function to whatever will fit your data best - sort of like moooeeeep's answer but with a little more detail and a wikipedia link. If you want a heuristic that has randomness it in, then you can write one.

Usually graphs have some sort of structure to them, and it makes sense to search them in a structured way (if you're looking for a path, then it makes sense to search a node that is connected to another node you already searched, not a random node which might be disconnected.) Most of the time I run those algoriths on directed acyclic graphs / DAG, or trees (connects DAGs.) If I really don't have any logical structure in my data, I typically don't try and make a graph out of it and then apply graph theory to it. I guess it depends on how random you want things to be.

Good luck!

Brian Stinar
  • 1,080
  • 1
  • 14
  • 32
0

It sounds like you are trying to strike a balance between BFS and DFS. This comes up in game programming where pruning is used to narrow the breadth so that more cycles can be spent on depth. Some examples are iterative deepening depth first search and best first search. A starting point can be found here: http://en.wikipedia.org/wiki/Alpha-beta_pruning#Other_algorithms

Paul Jackson
  • 2,077
  • 2
  • 19
  • 29