Algorithms,part1 week1 - CSDN博客
Question 1
Social network connectivity. Given a social network containing N members and a log file containing M timestamps at which times pairs of members formed friendships, design an algorithm to determine the earliest time at which all members are connected (i.e., every member is a friend of a friend of a friend ... of a friend). Assume that the log file is sorted by timestamp and that friendship is an equivalence relation. The running time of your algorithm should be MlogN or better and use extra space proportional to N .
hint-union-find.
use another array to keep track of the number of each subtree for each node.
Then, use binary search (lgN) to search N for each time-stamp and union pair (M).
Question 2
Union-find with specific canonical element. Add a method find() to the union-find data type so that find(i) returns the largest element in the connected component containing i. The operations, union(), connected(), and find() should all take logarithmic time or better.
For example, if one of the connected components is
Read full article from Algorithms,part1 week1 - CSDN博客
No comments:
Post a Comment