Everything Goes Better With Bacon

Whenever someone considers a large movie database like Internet Movie Database, or IMDB, inevitably the classic six degrees of Kevin Bacon problem comes up. It is a famous problem posed like this, “…any individual involved in the Hollywood, California film industry can be linked through his or her film roles to actor Kevin Bacon within six steps” [http://en.wikipedia.org/wiki/Six_Degrees_of_Kevin_Bacon]. This problem even helped Kevin Bacon begin his own social charity organization called SixDegrees.org linking people with charities that they might be interested in. Below is an example of how InfiniteGraph can be used to store and navigate through large sets of connected data like the IMDB.

In the example, I will show how to both find the links between various actors and Kevin Bacon, but also how to output the navigation results in various formats including JSON and GraphML. Note: Custom navigator plugins and custom formatter plugins (including the default JSON/GraphML formatters) can be created and used in any InfiniteGraph (2.1) graph database instance. See the InfiniteGraph developer wiki for more details and examples of how to write and use custom plugins (http://wiki.infinitegraph.com).

Here is a visualization of the actors connected to Kevin Bacon within just two degrees of separation (up to 1500 connections).

Radial view using the IG Visualizer.

Spring view using the IG Visualizer.
As you can see it can be rather tricky to find all of the connections, but if you go up to even 4 degrees, you may be overwhelmed with the results which is why the navigational features that InfiniteGraph offers are so powerful! To create a navigator plugin that finds all paths between two actors. I first wrote a path and result qualifier which qualifies the paths based on certain given criteria. By using the navigator plugin, I can reuse this code and dynamically set field values (using the Parameter annotation) at runtime. Below is an example of the path qualifier, set using the PathQualifier annotation, which filters out all paths that are longer than the degree value.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

packagecom.ig.samples.bacon;

importcom.infinitegraph.navigation.Path;

importcom.infinitegraph.navigation.Qualifier;

importcom.infinitegraph.plugins.Parameter;

importcom.infinitegraph.plugins.PathQualifier;

@PathQualifier

publicclassBaconPQ implementsQualifier

{

@Parameter(required=true)

publicintdegree = 3;

publicbooleanqualify(Path path)

{

if(path.size() <= degree)

returntrue;

else

returnfalse;

}

}

Below is an example of the result qualifier which uses the ResultQualifier annotation and filters out all paths that don’t have target vertex id as a final destination of the navigation. Notice that unless the targetId field value is set, the result qualifier will never qualify any results.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

packagecom.ig.samples.bacon;

importcom.infinitegraph.Vertex;

importcom.infinitegraph.navigation.Path;

importcom.infinitegraph.navigation.Qualifier;

importcom.infinitegraph.plugins.ElementId;

importcom.infinitegraph.plugins.ResultQualifier;

@ResultQualifier

publicclassBaconRQ implementsQualifier

{

@ElementId(required=true)

publiclongtargetId;

publicbooleanqualify(Path path)

{

Vertex currentV = path.getFinalHop().getVertex();

if(currentV.getId() == targetId)

returntrue;

else

returnfalse;

}

}

If I jar up these two classes and import them into the InfiniteGraph application, I can use them to navigate through my data. I call my navigator plugin jar, “BaconNavigator.jar”. Here is how I begin to write the application to access and use my navigator plugin. I connect to my IG instance of the IMDB. I execute my navigator given a few different targets: Tom Cruise, Tom Hanks, and Emma Stone.

Continuing the example, here is the code which pulls the vertex references for Kevin Bacon and all of my target actors from the manual index called “Person_names_index”. Also, it imports the navigator plugin, retrieves an instance of the navigator bundle from the plugin manager and sets the field value “targetId” with the value for a single target vertex id. Finally, I can output the results of each navigation from Kevin Bacon to a given target vertex in JSON and GraphML format.

Finally, I am able to write the method which takes my navigator bundle, executes the navigator plugin on a start vertex (Kevin Bacon) and outputs the results of the navigation to file. Because of the default formatter plugins packaged with the IG core, I can output the navigation results via standard JSON or GraphML. Note: An output stream instance must be set in the format handler for the formatter to write. The output stream does not have to be to a file, but if not defined, an exception may be thrown. The FormatterBridge class allows you to wrap a FormatHandler instance as a NavigationResultHandler.

Bacon, Kevin (I)Going to Pieces: The Rise and Fall of the Slasher Film2006ActorHanks, TomActorBoffo! Tinseltown's Bombs and Blockbusters2006ActorActorBeyond All Boundaries2009ActorProducerActorApollo 131995ActorActor