Skip to main content

Traversal: The Skeleton Key of Algorithmics

  • Chapter
  • First Online:
Book cover Python Algorithms
  • 14k Accesses

Abstract

■■■

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD 79.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD 99.99
Price excludes VAT (USA)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Notes

  1. 1.

    I’ve “stolen” the subtitle for this chapter from Dudley Ernest Littlewood’s The Skeleton Key of Mathematics.

  2. 2.

    If you’re not a gamer, feel free to imagine this as your office building, dream home, or whatever strikes your fancy.

  3. 3.

    I’ll be using dicts with adjacency sets as the default representation in the following, although many of the algorithms will work nicely with other representations from Chapter 2 as well. Usually, rewriting an algorithm to use a different representation isn’t too hard either.

  4. 4.

    This is the running time of all the traversal algorithms in this chapter, except (sometimes) IDDFS.

  5. 5.

    Hey, even the story of Newton and the apple is apocryphal.

  6. 6.

    Tracing your tour from a, you should end up with the node sequence a, b, c, d, e, f, g, h, d, c, i, j, i, k, i, c, b, l, b, a.

  7. 7.

    This recursive version would be harder to use if you were actually faced with a real-life maze, of course.

  8. 8.

    And just like that, a spelunker can turn troglodyte.

  9. 9.

    People seem to end up walking in circles when wandering in the wild as well. And research by the U.S. Army suggests that people prefer going south, for some reason (as long as they have their bearings). Neither strategy is particularly helpful if you’re aiming for a complete traversal, of course.

  10. 10.

    My translation.

  11. 11.

    You can perform the same procedure even if your boots aren’t muddy. Just make sure to clearly mark entries and exits (say, with a piece of chalk). In this case, it’s important to make two marks when you come to an old intersection and immediately start backtracking.

  12. 12.

    In fact, in some contexts, the term backtracking is used as a synonym for recursive traversal, or depth-first search.

  13. 13.

    The dfs_topsort function can also be used to sort the nodes of a general graph by decreasing finish times, as needed when looking for strongly connected components, discussed later in this chapter.

  14. 14.

    In other words, let’s think inductively.

  15. 15.

    IDDFS isn’t completely equivalent to Ore’s method because it doesn’t mark edges as closed in the same way. Adding that kind of marking is certainly possible and would be a form of pruning, discussed later in this chapter.

  16. 16.

    On the other hand, we’ll be jumping from node to node in a manner that could not possibly be implemented in a real-life maze.

  17. 17.

    To have any memory savings, you’d have to remove the S set. Because you’d be traversing a tree, that wouldn’t cause any trouble (that is, traversal cycles).

  18. 18.

    This might seem like cheating because I’m using topological sorting on a non-DAG. The idea is just to get the nodes sorted by decreasing finish time, though, and that’s exactly what dfs_topsort does—in linear time.

  19. 19.

    Actually, walk will return a traversal tree for each strong component.

Author information

Authors and Affiliations

Authors

Rights and permissions

Reprints and permissions

Copyright information

© 2014 Magnus Lie Hetland

About this chapter

Cite this chapter

Hetland, M.L. (2014). Traversal: The Skeleton Key of Algorithmics. In: Python Algorithms. Apress, Berkeley, CA. https://doi.org/10.1007/978-1-4842-0055-1_5

Download citation

Publish with us

Policies and ethics