0

Suppose there is a new social page like facebook but every time someone publishes something, the publication automatically is re-published by all it's friends. This function causes a chain reaction, causing every friends of the first person's friends to also re-publish it and so on.

The publication can be done only once from every user.

Is there any graph algorithm that I could use to count the total number of the publications/republications and how?

1 Answers1

0

Easiest algorithm seems to be to store 3 lists:

  • one with all people who did not publish the post (at start everyone except the root person)
  • one with people who did publish the post but did not chain-react their friends (initial only root person)
  • one with people who did publish and chain-react to their friends (empty at first).

The algorithm would look something like

as long as there are people in the 2nd list:  
  choose one person P from 2nd list  
  find all friends of P within the first group and move them to the second group 
  move P to the third group 
when you are finished return the size of the 3rd group.
DuDa
  • 3,718
  • 4
  • 16
  • 36