Current location: Hot Scripts Forums » Other Discussions » Database » [SOLVED] Friend to Friend Shortest Path


[SOLVED] Friend to Friend Shortest Path

Reply
  #1 (permalink)  
Old 06-05-08, 03:40 AM
sandeep.kumar's Avatar
sandeep.kumar sandeep.kumar is offline
Wannabe Coder
 
Join Date: Jun 2008
Location: India
Posts: 112
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] Friend to Friend Shortest Path

Hi All,

I need to implement "Friend of a Friend" problem with minimal costs using either MySQL5 or PHP/MySQL. I wonder if there is an implementation that would just allow to solve this problem with minimal costs
The description is as below:-

I want to build a "social network" extension and try to find the best way to implement a Friend of a Friend connection path. (Facebook has something like that I think)

e.g. We both have the same friend, but don't know each other. The algorithm should give me a path from me, over our same friend to you. (Me - Friend - You)

But it should also find connections, that have a bigger distance, e.g. (Me - Friend1 - Friend2 - Friend3 - You) but not exceeding this situation - (Me - Friend1 - Friend2 - Friend3 - Friend4 - You)

I stored user relations in database as I (user_id|friend_id = me|friend and friend|me)

Any help would be really grateful
Reply With Quote
  #2 (permalink)  
Old 06-05-08, 04:36 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
I'm moving this to the database section.
Reply With Quote
  #3 (permalink)  
Old 06-05-08, 04:42 AM
sandeep.kumar's Avatar
sandeep.kumar sandeep.kumar is offline
Wannabe Coder
 
Join Date: Jun 2008
Location: India
Posts: 112
Thanks: 0
Thanked 0 Times in 0 Posts
Why r u moving it to Database section it may be stay here.
Reply With Quote
  #4 (permalink)  
Old 06-05-08, 04:49 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
You posted this in the "New Members & Introductions" section.

You might be a new member, but your question seems more database related...
Reply With Quote
  #5 (permalink)  
Old 06-05-08, 04:51 AM
sandeep.kumar's Avatar
sandeep.kumar sandeep.kumar is offline
Wannabe Coder
 
Join Date: Jun 2008
Location: India
Posts: 112
Thanks: 0
Thanked 0 Times in 0 Posts
ok and thanks for your help.
Reply With Quote
  #6 (permalink)  
Old 06-06-08, 06:12 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
If you have a table with two fields (user,friends), you could use two queries.
PHP Code:

$query1="SELECT friends FROM table WHERE user='you'";
$results=mysql_query($query1);
while(
$row=musql_fetch_assoc($results))
{
 
$array1[]=$row["friends"];
 }
$query2="SELECT friends FROM table WHERE user='me'";
$results=mysql_query($query1);
while(
$row=musql_fetch_assoc($results))
{
 
$array2[]=$row["friends"];
 } 
Then use the array_search() function to see if value from array1 exists in array2 and display the results.
__________________
Jerry Broughton
Reply With Quote
  #7 (permalink)  
Old 06-06-08, 06:35 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
I'm not sure, but maybe you can do it all in one query:
SQL Code:
  1. SELECT
  2.   friends
  3. FROM
  4.   mytable AS t1
  5. INNER JOIN
  6.   mytable AS t2
  7. ON
  8.   t1.friends = t2.friends
  9. WHERE
  10.   t1.user='me'
  11. AND
  12.   t2.user='you'
In theory it should work, but I haven't tested it
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
  #8 (permalink)  
Old 06-06-08, 08:30 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Quote:
Originally Posted by UnrealEd View Post
I'm not sure, but maybe you can do it all in one query:
SQL Code:
  1. SELECT
  2.   friends
  3. FROM
  4.   mytable AS t1
  5. INNER JOIN
  6.   mytable AS t2
  7. ON
  8.   t1.friends = t2.friends
  9. WHERE
  10.   t1.user='me'
  11. AND
  12.   t2.user='you'
In theory it should work, but I haven't tested it
Your query is very well thought out, and it will work this way:
PHP Code:

<?php
$sql
="SELECT * FROM mytable AS t1 INNER JOIN mytable AS t2 ON t1.friends = t2.friends WHERE t1.user='me' AND t2.user='you' LIMIT 4";
$results=mysql_query($sql)or die(mysql_error());
echo 
"Me - ";
while(
$row=mysql_fetch_assoc($results))
{
 echo 
$row["friends"]." - ";
 }
echo 
"You";
?>
And this way:
PHP Code:

$sql="SELECT * FROM mytable AS t1 JOIN mytable AS t2 ON t1.friends = t2.friends WHERE t1.user='me' AND t2.user='you' LIMIT 4";
$results=mysql_query($sql)or die(mysql_error());
echo 
"Me - ";
while(
$row=mysql_fetch_assoc($results))
{
 echo 
$row["friends"]." - ";
 }
echo 
"You"
__________________
Jerry Broughton
Reply With Quote
  #9 (permalink)  
Old 06-09-08, 01:53 AM
sandeep.kumar's Avatar
sandeep.kumar sandeep.kumar is offline
Wannabe Coder
 
Join Date: Jun 2008
Location: India
Posts: 112
Thanks: 0
Thanked 0 Times in 0 Posts
If i have to search three or four level friends than will these query work fine?

I have a large database of Friend's connections than these query will take a lot of time.

I need to save time also.
Reply With Quote
  #10 (permalink)  
Old 06-09-08, 02:59 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Quote:
Originally Posted by sandeep.kumar View Post
If i have to search three or four level friends than will these query work fine?

I have a large database of Friend's connections than these query will take a lot of time.

I need to save time also.
It all depends on how you have your database setup.
__________________
Jerry Broughton
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with absolute path and relative path samot PHP 4 11-13-08 10:12 PM
[SOLVED] How to find the full path of curent page? adrianTNT PHP 6 06-09-08 09:11 AM
adding a user as friend through an email link! KamalRahman ASP.NET 1 04-23-08 10:38 AM
getting the path to cgi-bin abtimoteo Perl 1 09-04-05 01:25 AM
Absolutely New to VB.Net and Need a Little Help nothingofvalue Windows .NET Programming 2 07-23-05 02:56 PM


All times are GMT -5. The time now is 05:03 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.