in reply to best way to keep track of who's friends with whom

Are you asking about how to work with the data when it is in memory or how to store it long term for each user?

Long term storage is normally handled by a database and you would likely use a cross reference database table whose primary key includes the ids of both friends. To make sure you don't double add friend pairs you would need a rule for deciding which of any two friends goes in the first key field and which goes in the second key field.

If you already know how you are storing your friends pairs, but just want to know a good data structure for manipulating the information in memory, then you will need to provide more information. The best data structure will depend on how you are planning to use the data.

Best, beth

  • Comment on Re: best way to keep track of who's friends with whom

Replies are listed 'Best First'.
Re^2: best way to keep track of who's friends with whom
by dsheroh (Monsignor) on Oct 05, 2009 at 10:26 UTC
    To make sure you don't double add friend pairs you would need a rule for deciding which of any two friends goes in the first key field and which goes in the second key field.
    ...assuming that friendship is commutative. (i.e., "If Alice likes Bob, then Bob likes Alice.") If it's directional, as on LiveJournal or Twitter, then this would not apply, since "Alice likes Bob" and "Bob likes Alice" are independent of each other.
      Totally agreed.

      In case you do want to treat friendship as commutative, here's a simple rule: require that the value of the id A is numerically smaller than the value of id B. If it's the other way around, swap them.

Re^2: best way to keep track of who's friends with whom
by leocharre (Priest) on Oct 05, 2009 at 13:38 UTC

    beth, that's a interesting way you mention. (Sounds like a question of storage, not execution time handling.)

    I think what Beth is suggesting deals with data normalization techniques- where you break down everything into the smallest most simple tables possible(?).

    It makes it so the data is not duplicated, it makes your sql queries a little more complicated (a lot), but the whole thing works better.

    Basically your friend people table:

    person_id name
    1   joe
    2   leo
    3   jimmy
    

    And your friend table:

    person_a_id person_b_id
    1           2
    1           3
    

    This shows that joe is friends with leo and jimmy- but leo and jimmy are not friends.