Graphs: Hamiltonian Paths

    Graphs: Hamiltonian Paths

      Say you're being chased by ninjas. Why? It could be that

      • you made a pilgrimage to Jordan, breaking their sacred vow, "fights before Canaanites."
      • you broke curfew, breaking their other sacred vow, "fights before moonlights."
      • you've insulted a ninja, breaking their other other sacred vow, "niceties before fight-ceties."

      Whatever the reason for your ninja offense, you're being chased through a neighborhood, and you need to find a way to escape them.

      Quick.

      Luckily, the neighborhood's new for the ninjas, so you have a decent chance of making it through. There's just one problem: once the ninjas reach a new street, they station a ninja to stand sentry, meaning that once you run down a street, you can't do it again.

      Here's the map you have of the streets:

      A graph with eight nodes thirteen edges.

      If you reach an intersection of a road you've already run down, you can probably sneak past the sentry, but there's no way you can run down the same road twice. Because of the stationing, at every node you lose a couple of ninjas.

      You form a plan in your head, starting at Node F, to make a giant loop through the neighborhood without crossing

      • the same node twice
      • the same edge twice

      until you've reached your starting node (which is called a simple cycle—and sometimes a circuit, btw). One route you can think of looks like this:

      The graph, with a path: F > G > H > D > B > E > A > C > F

      Starting from F, you'll run full speed to G, H, D, B, E, A, C, and back again to F—hopefully losing all the ninjas in the meantime.

      Since you can also run the opposite direction, if it makes a difference, you could also travel the path in the opposite direction, starting from F and running full speed to C, A, E, B, D, H, G, and back to F.

      The circuit, but in the opposite direction.

      It's a toss-up whether either option's better for defending against the onslaught of ninjas, but they're both examples of Hamiltonian Circuits because they

      • visit every vertex once and only once (making them Hamilton Paths).
      • start and end from the same node (making them circuits).

      Smash the two names together, and you get the term Hamiltonian Circuit. (Funny how things work out like that…)

      Hamiltonian Paths and Circuits are named after after Sir William Rowan Hamilton, who invented a logic puzzle about using—you guessed it—Hamiltonian Paths to explore a dodecahedron (that's a 12-sided three dimensional shape).

      The best part? Unlike isomorphism and planarity, there's actually a concrete way to tell if a Hamiltonian Circuit or Path is possible in a graph.

      If the graph is complete—i.e. if every node links up to every other node—there's always a possible Hamiltonian Path/Circuit. In fact, in a complete graph with n vertices, there are a total number of (n - 1)! Hamiltonian Circuits possible. Because each vertex has a degree (whether in or out) of n – 1, you can always get from one node to any other node, no matter how many nodes you've already visited. (There's a little fudging of the numbers since we're counting both when the cycle or path goes one way and then does the same path in the opposite direction, but you get the idea.) By the time you get to a complete graph with 5 vertices, you'll find 24 possible Hamiltonian Circuits.

      That's a lot of avenues for ninjas to get lost.

      For every other graph…you'll have to play a game of guess and check (source).

      Just pray there aren't any more insulted ninjas chasing you.