Best Data Structure for supporting Social Network Links like linked-In does?

Last post 07-07-2008 1:26 AM by ramireddyindia. 5 replies.

Sort Posts:

  • Best Data Structure for supporting Social Network Links like linked-In does?

    07-04-2008, 7:57 PM
    • Loading...
    • pkellner
    • Joined on 11-12-2004, 10:42 AM
    • San Jose, California
    • Posts 3,379
    • Moderator
      TrustedFriends-MVPs

    I'm need a very scalable data structure that I can query a person list and see how closely related two people are.  I know things like:

     FriendA knows Friendb

    FriendB knows FriendC

    FriendC kind-of-knows FriendD

    How many links away are FriendA and FriendD and what is the relationship?

    Any referneces or suggestions would be great.

    thanks

    Peter Kellner
    http://73rdstreet.com and blogging at
    http://PeterKellner.net
    MVP, ASP.NET
  • Re: Best Data Structure for supporting Social Network Links like linked-In does?

    07-06-2008, 10:45 AM
    Answer
    • Loading...
    • SGWellens
    • Joined on 01-02-2007, 4:27 PM
    • MN, USA
    • Posts 3,106
    • Moderator
      TrustedFriends-MVPs

    Hey Peter,

    I think you are looking for "Dijkstra's Shortest Path Algorithm"

    http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=77262

    Steve Wellens
  • Re: Best Data Structure for supporting Social Network Links like linked-In does?

    07-06-2008, 7:46 PM
    • Loading...
    • pkellner
    • Joined on 11-12-2004, 10:42 AM
    • San Jose, California
    • Posts 3,379
    • Moderator
      TrustedFriends-MVPs

    That's pretty heavy stuff!  Thanks for the pointer. 

    I'm still scratching my head trying to understand it.  I get the string and the knots thing but having trouble turning it into the code shown.

    Thanks again!

    Peter Kellner
    http://73rdstreet.com and blogging at
    http://PeterKellner.net
    MVP, ASP.NET
  • Re: Best Data Structure for supporting Social Network Links like linked-In does?

    07-06-2008, 9:52 PM
    • Loading...
    • SGWellens
    • Joined on 01-02-2007, 4:27 PM
    • MN, USA
    • Posts 3,106
    • Moderator
      TrustedFriends-MVPs

    pkellner:
    I'm still scratching my head trying to understand it.

    Have you seen MY head?  I also find it complicated...I like to keep things as simple as possible.

     

    Steve Wellens
  • Re: Best Data Structure for supporting Social Network Links like linked-In does?

    07-06-2008, 10:44 PM
    • Loading...
    • pkellner
    • Joined on 11-12-2004, 10:42 AM
    • San Jose, California
    • Posts 3,379
    • Moderator
      TrustedFriends-MVPs

    Yeah, me too.

    I did find this example of that algorithm that helps me understand it (after I clicked like a monkey a lot of times)

    http://www.unf.edu/~wkloster/foundations/DijkstraApplet/DijkstraApplet.htm

    Thanks again for the pointer.

    Off to Italy Wednesday!

    Peter Kellner
    http://73rdstreet.com and blogging at
    http://PeterKellner.net
    MVP, ASP.NET
  • Re: Best Data Structure for supporting Social Network Links like linked-In does?

    07-07-2008, 1:26 AM

    May be this soluton seems to be funny to your experienced people. But my views on this are.

     this friends mechanism resembles me a graph datastructure.

    if we apply the breadth first search algorithm to  that procedure, and then after finding the required node, if we apply recursive traversal. i think we can get solution.

    actually the below solution is i tried it for some other post previously.

    I created some tables and tried.

    create table persons
    (
     PersonId int
     PersonName varchar(100)
    )
    create table Friends
    (
     FriendRelationId int identity(1,1),
     FSource int,
     FDestination int
     
    )

     

    create proc sc_test
    (
     @FSourceId int,
     @FDestinationId int,
     @MaxLevel int
    )


    as


    begin

     

    declare @temp table
    (
     TableId int identity(1,1),
     FSourceId int,
     myLevel int,
     ParentId int
    )

    declare @Result table
    (
     ResultId int identity(1,1),
     FSourceId int,
     FDestinationId int
    )

    declare @TableId int
    declare @CurId int
    declare @CurLevel int
    declare @ParentId int


    set @TableId = 1

    insert into @temp select FDestination,1,@FSourceId from Friends where FSource = @FSourceId

    while((select count(*) from @temp)>0)
    begin
     set @CurId = (select top 1 FSourceId from @temp where TableId = @TableId)
     if(@CurId = @FDestinationId)
     begin 
      goto l1
     end
     else
     begin
      set @CurLevel = (select top 1 myLevel from @temp where FSourceId = @CurId)  
      if(@CurLevel > @MaxLevel)
      begin
       goto l2
      end
      insert into @temp select FDestination,@CurLevel + 1,@CurId from Friends where FSource = @CurId and FDestination not in (select FSourceId from @temp)
     
     end
     set @TableId = @TableId + 1
     
    end

    l1:


     set @CurLevel = (select myLevel from @temp where TableId = @TableId)
     
     insert into @Result select ParentId,FSourceId from @temp where TableId = @TableId
     set @TableId = (select ParentId from @temp where TableId = @TableId)
     set @CurLevel = @CurLevel - 1
     while(@CurLevel > 0)
     begin
     
      insert into @Result (FSourceId,FDestinationID)  select ParentId,FSourceId from @temp where FSourceId = @TableId    
     

      set @TableId = (select ParentId from @temp where FSourceId = @TableId)
      set @CurLevel = @CurLevel - 1

     end
     select FSourceId,FDestinationId from @Result order by ResultId desc
    l2:


    end

Page 1 of 1 (6 items)
Microsoft Communities
Page view counter