Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Challenge,how to implement an algorithm for six degree of separation?

Tags:

UserA-UserB-UserC-UserD-UserF

Users connected by '-' know each other.

And I need an algorithm for these 2 tasks:

  1. Calculate the path from UserX to UserY
  2. For UserX,calculate all users that is no more than 3 steps away.

Is there an efficient solution?

EDIT

My purpose is not to prove it right or wrong,but to calculate the result real time when necessary.

Plus,I think the most expressive way is code,even pseudo ones.

EDIT AGAIN

I've decided that this kind of job must be done inside database,so it must be a sql solution!

like image 312
user198729 Avatar asked Jan 16 '10 08:01

user198729


People also ask

How would you solve a six degrees of separation problem?

The fastest O(n) algorithm for six-degrees of separation would probably be finding the sets of all users 1-step away from UserX and UserY, and finding the intersection of those two sets. If none, then add the users 2-steps from UserX and intersect; then add users 2-steps from UserY and intersect; etc. up to 3.

How do you calculate degrees of separation?

In order to calculate the average degree of separation between the nodes in a graph, we consider one node in each of the steps, calculate the average and multiply it with the total number of nodes in that step.

What is the six degrees of separation concept?

Six degrees of separation is the theory that any person on the planet can be connected to any other person on the planet through a chain of acquaintances that has no more than five intermediaries.


1 Answers

Represent this list of users by a graph

  • Each user is a node
  • There is an edge between any two users who know each other
  1. Calculate the path from UserX to UserY
  2. For UserX,calculate all users that is no more than 3 steps away.

These questions are so closely related that the same algorithm actually solves both of them. You can call it "Dijkstra's algorithm with all edges having weight 1," or "breadth-first search."

Essentially, starting at the first node, visit all its relatives; then mark them all as visited, record the shortest path to each (the shortest path to them + the edge you just traversed), and repeat for each of them. Stop after you've reached your destination for Problem #1, stop after the shortest path is > 3 for Problem #2.

This will run in O(n) time. No, there is no faster way.

The fastest O(n) algorithm for six-degrees of separation would probably be finding the sets of all users 1-step away from UserX and UserY, and finding the intersection of those two sets. If none, then add the users 2-steps from UserX and intersect; then add users 2-steps from UserY and intersect; etc. up to 3.

If each person has an average of 100 friends, this could require looking at up to about 2,020,200 users, as opposed to the 1,010 billion for Dijkstra's algorithm. In practice, these numbers would be much smaller, since often two of your friends are also friends with each other.

This is the only method of solving six-degrees of separation (of those mentioned so far) that will work in practice.

like image 50
BlueRaja - Danny Pflughoeft Avatar answered Oct 07 '22 05:10

BlueRaja - Danny Pflughoeft