How can I prove the “Six Degrees of Separation” concept programmatically?

前端 未结 4 1088
-上瘾入骨i
-上瘾入骨i 2021-01-31 23:22

I have a database of 20 million users and connections between those people. How can I prove the concept of \"Six degrees of separation\" concept in the most efficient w

相关标签:
4条回答
  • 2021-01-31 23:43

    I think the most efficient way (worst case) is almost N^3. Build an adjacency matrix, and then take that matrix ^2, ^3, ^4, ^5 and ^6. Look for any entries in the graph that are 0 for matrix through matrix^6.

    Heuristically you can try to single out subgraphs ( large clumps of people who are only connected to other clumps by a relatively small number of "bridge" nodes ) but there's absolutely no guarantee you'll have any.

    0 讨论(0)
  • 2021-01-31 23:45

    You can probably fit the graph in memory (in the representation that each vertex knows a list of its neighbors).

    Then, from each vertex n, you can run a breadth-first search (using a queue) to the depth of 6 and count number of vertices visited. If not all vertices are visited, you have disproved the theorem. In other case, continue with next vertex n.

    This is O(N*(N + #edges)) = N*(N + N*100) = 100N^2, if user has 100 connections on average, Which is not ideal for N=20 million. I wonder if the mentioned libraries can compute the diameter in better time complexity (general algorithm is O(N^3)).

    The computations for individual vertices are independent, so they could be done in parallel.

    A little heuristic: start with vertices that have the lowest degree (better chance to disprove the theorem).

    0 讨论(0)
  • 2021-01-31 23:47

    You just want to measure the diameter of the graph. This is exactly the metric to find out the seperation between the most-distantly-connected nodes in a graph.

    Lots of algorithms on Google, Boost graph too.

    0 讨论(0)
  • 2021-01-31 23:56

    Well a better answer has already been given, but off the top of my head I would have gone with the Floyd-Warshall all pairs shortest path algorithm, which is O(n^3). I'm unsure of the complexity of the graph diameter algorithm, but it "sounds" like this would also be O(n^3). I'd like clarification on this if anyone knows.

    On a side note, do you really have such a database? Scary.

    0 讨论(0)
提交回复
热议问题