NEO4J – Shortest Paths in the Cineasts Database


As I pointed out in my last post, Counting Many Paths Between Nodes In NEO4J
https://rodgersnotes.wordpress.com/2013/08/16/counting-many-paths-between-nodes-in-neo4j/

the more complex and interconnected the graph, the number of paths between nodes goes up exponentially. However, those queries did not use the built in functionality, ShortestPath.

Last winter, I watched the video, Cypher for SQL Professionals, with Andres Taylor. In the video, there were a number of queries to the Cineasts database, that did  use the ShortestPath functionality.

Bacon Lucy:

One of the queries in the video was Bacon Lucy. How many nodes of actors and movies separated the performers, Kevin Bacon, and Lucy Liu?  

START
lucy = node:Person(name=”Lucy Liu”),
kevin = node:Person(name=”Kevin Bacon”)
MATCH P = shortestPath (
lucy – [:ACTS_IN*] – kevin )
RETURN
EXTRACT ( n in NODES(P) :
COALESCE (n.name?, n.title?) )

Returned 1 row.
Query took 69ms

EXTRACT ( n in NODES(P) : COALESCE (n.name?, n.title?) )
[“Lucy Liu”,”3 Needles”,”Olympia Dukakis”,”Picture Perfect”,”Kevin Bacon”]

I thought that was pretty cool, so I ran a few other queries of famous actors from almost 100 years ago, to the present. Using the built in function, AllShortestPaths.

———-

Fonda Family:

Nodes between actor Henry Fonda and his granddaughter, the actress Bridget Fonda.

START
p = node:Person(name= “Henry Fonda” ),
c = node:Person(name= “Bridget Fonda” )
MATCH P = allshortestPaths (
p – [:ACTS_IN*] – c )
RETURN
length ( NODES(P) ) as the_length
, EXTRACT ( n in NODES(P) :
COALESCE (n.name?, n.title?) ) as the_path

Returned 94 rows.
Query took 246ms

the_length the_path

7 [“Henry Fonda”,”12 Angry Men”,”Jack Warden”,”While You Were Sleeping”,”Bill Pullman”,”Lake Placid”,”Bridget Fonda”]
7 [“Henry Fonda”,”Once Upon a Time in the West”,”Lionel Stander”,”New York, New York”,”Robert De Niro”,”Jackie Brown”,”Bridget Fonda”]
7 [“Henry Fonda”,”How the West Was Won”,”George Peppard”,”The Victors”,”George Hamilton”,”Doc Hollywood”,”Bridget Fonda”]

7 [“Henry Fonda”,”Advise & Consent”,”Burgess Meredith”,”Rocky II”,”Talia Shire”,”The Godfather: Part III”,”Bridget Fonda”]
7 [“Henry Fonda”,”On Golden Pond”,”Jane Fonda”,”Georgia Rule”,”Dermot Mulroney”,”Point of No Return”,”Bridget Fonda”]

7 [“Henry Fonda”,”Battle of the Bulge”,”Charles Bronson”,”Kid Galahad”,”David Lewis”,”Lake Placid”,”Bridget Fonda”]
7 [“Henry Fonda”,”Once Upon a Time in the West”,”Charles Bronson”,”Kid Galahad”,”David Lewis”,”Lake Placid”,”Bridget Fonda”]

——–

Barrymore Family:

How many actors and movies link Lionel Barrymore, to his grand-daughter, Drew Barrymore?

START
l = node:Person(name=”Lionel Barrymore”),
d = node:Person(name=”Drew Barrymore”)
MATCH P = allshortestPaths (
l – [:ACTS_IN*] – d )
RETURN
EXTRACT ( n in NODES(P) :
COALESCE (n.name?, n.title?) ) as the_path

Returned 17 rows.
Query took 449ms

the_path

[“Lionel Barrymore”,”Calling Dr. Gillespie”,”Ava Gardner”,”The Bible: In the Beginning…”,”George C. Scott”,”Firestarter”,”Drew Barrymore”]

[“Lionel Barrymore”,”Captains Courageous”,”John Carradine”,”The Best of Sex and Violence”,”Kristine DeBell”,”Suddenly, Love”,”Drew Barrymore”]
[“Lionel Barrymore”,”Captains Courageous”,”John Carradine”,”The Howling”,”Dee Wallace”,”E.T.: The Extra-Terrestrial”,”Drew Barrymore”]

[“Lionel Barrymore”,”It’s a Wonderful Life”,”James Stewart”,”Anatomy of a Murder”,”George C. Scott”,”Firestarter”,”Drew Barrymore”]
[“Lionel Barrymore”,”It’s a Wonderful Life”,”James Stewart”,”The Big Sleep”,”Candy Clark”,”Cat’s Eye”,”Drew Barrymore”]

[“Lionel Barrymore”,”Key Largo”,”Edward G. Robinson”,”Scarlet Street”,”Joan Bennett”,”Suddenly, Love”,”Drew Barrymore”]

[“Lionel Barrymore”,”Key Largo”,”Lauren Bacall”,”Dogville”,”Nicole Kidman”,”Batman Forever”,”Drew Barrymore”]
[“Lionel Barrymore”,”Key Largo”,”Lauren Bacall”,”Prêt-à-porter”,”Julia Roberts”,”Confessions of a Dangerous Mind”,”Drew Barrymore”]

——

Chaplin To Cyrus:

Charlie Chaplin began starring in movies in 1914, in the silent era. He then continued making movies until 1967! Miley Cyrus starred in Hannah Montana: The Movie, in 2009, almost 100 years later.

START
l = node:Person(name=”Charlie Chaplin”),
m = node:Person(name=”Miley Cyrus”)
MATCH P = allshortestPaths (
l – [:ACTS_IN*] – m )
RETURN length ( nodes ( P) ) as the_length
, EXTRACT ( n in NODES(P) :
COALESCE (n.name?, n.title?) ) as the_path

Returned 59 rows.
Query took 144ms

the_length the_path

9 [“Charlie Chaplin”,”A Countess from Hong Kong”,”Geraldine Chaplin”,”BloodRayne”,”Michael Paré”,”Moon 44″,”Malcolm McDowell”,”Bolt”,”Miley Cyrus”]
9 [“Charlie Chaplin”,”A Countess from Hong Kong”,”Geraldine Chaplin”,”BloodRayne”,”Michael Paré”,”Moon 44″,”Malcolm McDowell”,”Super Rhino”,”Miley Cyrus”]

9 [“Charlie Chaplin”,”A Countess from Hong Kong”,”Marlon Brando”,”Apocalypse Now”,”Dennis Hopper”,”Firestarter 2: Rekindled”,”Malcolm McDowell”,”Bolt”,”Miley Cyrus”]
9 [“Charlie Chaplin”,”A Countess from Hong Kong”,”Marlon Brando”,”Apocalypse Now”,”Dennis Hopper”,”Firestarter 2: Rekindled”,”Malcolm McDowell”,”Super Rhino”,”Miley Cyrus”]

9 [“Charlie Chaplin”,”A Countess from Hong Kong”,”Marlon Brando”,”The Godfather”,”Robert Duvall”,”Phenomenon”,”John Travolta”,”Bolt”,”Miley Cyrus”]


9 [“Charlie Chaplin”,”A Countess from Hong Kong”,”Sophia Loren”,”The Millionairess”,”Miriam Karlin”,”A Clockwork Orange”,”Malcolm McDowell”,”Super Rhino”,”Miley Cyrus”]


9 [“Charlie Chaplin”,”A Countess from Hong Kong”,”Tippi Hedren”,”I Woke Up Early The Day I Died”,”Will Patton”,”The Punisher”,”John Travolta”,”Bolt”,”Miley Cyrus”]

9 [“Charlie Chaplin”,”The Kid”,”Jackie Coogan”,”Marlowe”,”William Daniels”,”Blades of Glory”,”Nick Swardson”,”Bolt”,”Miley Cyrus”]
9 [“Charlie Chaplin”,”The Kid”,”Kitty Bradbury”,”Our Hospitality”,”Monte Collins”,”Flying By”,”Billy Ray Cyrus”,”Hannah Montana Miley Cyrus: Best of Both Worlds Concert Tour”,”Miley Cyrus”]
9 [“Charlie Chaplin”,”The Kid”,”Kitty Bradbury”,”Our Hospitality”,”Monte Collins”,”Flying By”,”Billy Ray Cyrus”,”Hannah Montana: The Movie”,”Miley Cyrus”]

——–

What’s interesting is using AllShortestPaths.  There are so many.  Between Charlie Chaplin and Miley Cyrus, there are 59 paths, all 9 nodes in length.

Cool huh?

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: