Skip to main content

Bacon Numbers

Now for a more practical example. Bacon Numbers are the degrees of separation from a given actor to Kevin Bacon. For example, if Elvis Presley was in "Change of Habit" with Edward Asner who was in "JFK" with Kevin Bacon, then Elvis Presley has a Bacon Number of 2. Let's see how we would use datalog to solve this problem:

Live Editor
Result
Loading...

The gist is that we use this query:

const BaconNumberQuery = datalog.query(({ BaconNumber, Actor, NextActor, CurrentBaconNumber, MovieName }) => {
InMovie({ Actor, MovieName })
InMovie({ MovieName, Actor: NextActor })
BaconNumbers({ Actor, number: BaconNumber })
BaconNumbers({ Actor: NextActor, number: CurrentBaconNumber })
})

to find a link between an Actor and a NextActor. We also pull in their Bacon Numbers so we can compare if using the link to Actor is better than using the NextActor's CurrentBaconNumber. The comparison is done in mapEffect. If the link through Actor is a better Bacon Number then we change NextActor's Bacon Number to be that.

As an exercise for the reader, try changing this so that it shows the trail of movies too.