algorithm - LinkedIn type friends connection required in php -
I am creating a custom social network for one of my clients.
In this I have user friend of CSV as shown below in user table
UID user_name friends 1 John 2 2 jack 3,1 3 gary 2,4 4 Joey 3
In the above scenario, if the logged user is John and if he visits Joey's profile page, then there should be a connection between them
< P> John-> Jack-> Gary-> JOII am able to establish a connection at level 1
If J JOI's profile visits, so I am able to set the following:
Jack-Gary -> JOI
But for the second level I need the same routine that I I do not know that is the right solution + I can not even apply it.
So, can anyone help me with this?
Thanks in advance,
Sky
P: SI is not in a position to change the DB architecture tion: (
Here are some BFS codes that I wrote in Ruby; You should give a good enough idea that things work for PHP to translate it The other changes you make are the graphs with the DB query to get the friends of the current user [and Current] will be changed.
def bfs (graph, start, stop) queue = [start] visited = {} parent = {} current = zero while true where queue.empty ? Return zero end current = queue.shift if current == stop read_path (current, parent) end attack [current] = true graph [current] .each do | i | if not [i] and not queue. Endx I) Parent [i] = Current Q. Push (i) End end end end DEF watch_path (node, parent) a = [node] while parent [node] one . (Parent [node]) node = parent [node] end return. Averse end graph = {"a" => ["B", "c"], "b" = & gt; ["C", "d"], "c" = & gt; ["A", "e"], "d" => ["B", "c", "f"], "e" => ["C", "F"]} path = BFS (graph, "A", "F") p path
Comments
Post a Comment