There is a theory that says six degrees of seperations is the highest degree for people to be connected through a chain of acquaintances. (You know the Baker - Degree of seperation
1, the Baker knows someone you don't know - Degree of separation2)We have a list of People
P, listAof corresponding acquaintances among these people, and a personxWe are trying to implement an algorithm to check if person
xrespects the six degrees of separations. It returnstrueif the distance fromxto all other people inPis at most six, false otherwise.We are tying to accomplish
O(|P| + |A|)in the worst-case.
To implement this algorithm, I thought about implementing an adjacency list over an adjacency matrix to represent the Graph G with vertices P and edges A, because an Adjacency Matrix would take O(n^2) to traverse.
Now I thought about using either BFS or DFS, but I can't seem to find a reason as to why the other is more optimal for this case.
I want to use BFS or DFS to store the distances from x in an array d, and then loop over the array d to look if any Degree is larger than 6.
DFS and BFS have the same Time Complexity, but Depth is better(faster?) in most cases at finding the first Degree larger than 6, whereas Breadth is better at excluding all Degrees > 6 simultaneously.
After DFS or BFS I would then loop over the array containing the distances from person x, and return true if there was no entry >6 and false when one is found.
With BFS, the degrees of separations would always be at the end of the Array, which would maybe lead to a higher time complexity?
With DFS, the degrees of separations would be randomly scattered in the Array, but the chance to have a degree of separation higher than 6 early in the search is higher.
I don't know if it makes any difference to the Time Complexity if using DFS or BFS here.