Coursera - Algorithm (Princeton) - 习题与解答 - Week 1

Week 1

Social Network Connectivity

原题

Given a social network containing n n n members and a log file containing m m 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 m log ⁡ n m\log n mlogn or better and use extra space proportional to n n n.

分析

就是在整个网络中尝试寻找两个人之间是否存在关系

思路

使用并查集,使用加权的快速归并,就能达到时间要求

Union-find with Specific Canonical Element

原题

Add a method f i n d ( ) \mathtt{find()} find() to the union-find data type so that f i n d ( i ) \mathtt{find(i)} find(i) returns the largest element in the connected component containing i i i. The operations, u n i o n ( ) \mathtt{union()} union(), c o n n e c t e d ( ) \mathtt{connected()} connected(), and f i n d ( ) \mathtt{find()} find() should all take logarithmic time or better.

For example, if one of the connected components is { 1 , 2 , 6 , 9 } \{1, 2, 6, 9\} { 1,2,6,9}, then the f i n d ( ) \mathtt{find()} find()method should return 99 for each of the four elements in the connected components.

分析

寻找连通分量的最大值,可以使用路径压缩的相同思路,在更新连通分量情况时同时更新分量内的最大值

思路

使用一个额外的数组,用于储存对应节点所在通量最大值,其更新过程和路径压缩一致

Successor with Delete

原题

Given a set of n n n integers S = { 0 , 1 , . . . , n − 1 } S = \{ 0, 1, ... , n-1 \} S={ 0,1,.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值