Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 571135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:28:31+00:00 2026-05-13T13:28:31+00:00

UserA-UserB-UserC-UserD-UserF Users connected by ‘-‘ know each other. And I need an algorithm for

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:28:32+00:00Added an answer on May 13, 2026 at 1:28 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 311k
  • Answers 311k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Currently, you are trying to attach to sharepoint's processes which… May 13, 2026 at 10:17 pm
  • Editorial Team
    Editorial Team added an answer One way is to convert your application to an executable,… May 13, 2026 at 10:17 pm
  • Editorial Team
    Editorial Team added an answer The IP address is not wrong. However, the web server… May 13, 2026 at 10:17 pm

Related Questions

i am newbie in rails and try to perform left join in mysql. there
I have a model Group and model User They are connected both directions with
FYI This is homework. I have to build a Java Chat server. I have
I'm trying to implement row-based security checks for Django models. The idea is that
I have a pretty simple question which perhaps someone familiar with Server/Client design &

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.