How to decide if two people are connected

Here is the problem:

provided that two people are registered on the social network website, how to decide whether they are connected or not?

my analysis (after reading more): in fact, the question is looking for - the shortest path from A to B in the graph. I think that BFS and Dijkstra Algorithms work here, and the time complexity is the same (O (V + E)), because it is an unweighted schedule, so we cannot use the priority queue. Thus, a simple task can solve the problem. But, both of them do not solve the problem, which: find a path between them.

Bidrectrol should be the best solution for now.

+5
source share
5 answers

, . A, A .. , B, A B, .

, , , A B .

, . .

+3

- " ", union(from,to), find(A) is find(B) - True, A B . , , A B.

+2

, : N A B , K, B . K = 3 N 5, .. 5 .

+1

dfs , , .

, .. . . .

A * search. : "A * , node node ( )."

: A *, " , A * , ". , , . ( ;).

+1

: .

. , , O (1): , .

, , / .

.

- / . . , .

:

  • . n node, node n .

  • (i,j) ( i j):

    • , node i j . k l. k, l. i j, l.
  • node, .

. , , , , .

, node , , , .

0
source

All Articles