algorithm - Interview stumper: friends of friends of friends -
suppose have social network billion users. on each user's page, want display number of users friends, friends of friends, , on, out 5 degrees. friendships reciprocal. counts don't need update right away, should precise.
i read on graphs, didn't find suggested scalable approach problem. think of take way time, way space, or both. driving me nuts!
one interesting approach translate friend graph adjacency matrix, , raise matrix 5th power. gives adjacency matrix containing counts of number of paths-of-length-5 between each node.
note you'll want matrix multiplication algorithm can take advantage of sparse matrices, since friends adjacency matrix sparse first couple levels. lucky you, people have done lot of work on how multiply huge matrices (especially sparse ones) efficiently.
here's video twitter's oscar boykin mentions approach computing followers of followers @ twitter.
Comments
Post a Comment