Coursera Algorithms week1 查并集 练习测验: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.
分析:
题目的意思是有一个包含n个成员的社交网络,日志文件log按照时间戳顺序存储了两个成员之间成为朋友的时间,共有m条记录。让我们设计一个算法来根据这个log文件来计算m个成员全部通过朋友关系连通的时间。
这是个典型的并查集。思路是读取日志文件,遍历文件记录,逐条记录union。采用加权quick-union算法,就可以满足mlogn的复杂度要求。作业提交100
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner; import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.WeightedQuickUnionUF; public class SocialNetworkConnUF {
private FileInputStream ins;
private WeightedQuickUnionUF uf;
public SocialNetworkConnUF(int num, FileInputStream ins){
this.ins = ins;
uf = new WeightedQuickUnionUF(num);
} @SuppressWarnings("resource")
public String getEarliestConTime(){
Scanner scanner = new Scanner(ins,"utf-8");
String earliestConTime = null;
while(scanner.hasNextLine()){
String line = scanner.nextLine();
if(line != null && !line.trim().equals("")){
String[] lineArray = line.split(" ");
if(lineArray.length == 3){
String timestamp = lineArray[0];
int p = Integer.parseInt(lineArray[1]);
int q = Integer.parseInt(lineArray[2]);
if(uf.connected(p, q)) continue;
uf.union(p,q);
if(uf.count() == 1) {
earliestConTime = timestamp;
break;
}
}
} }
return earliestConTime;
}
public static void main(String[] args){
FileInputStream ins;
try {
ins = new FileInputStream("socialNetworkLog.txt");
SocialNetworkConnUF socialNet = new SocialNetworkConnUF(10, ins);
String earliestConnTime = socialNet.getEarliestConTime();
StdOut.println(" the earliest connected time is :" + earliestConnTime);
} catch (FileNotFoundException e) {
e.printStackTrace();
} }
/*
* socialNetworkLog.txt
* 20170714001 0 1
* 20170714002 4 5
* 20170714003 8 9
* 20170714004 2 4
* 20170714005 5 6
* 20170714006 7 8
* 20170714007 2 5
* 20170714008 6 7
* 20170714009 1 2
* 20170714010 0 3
* 20170714011 1 9
* 20170714012 3 7
*
*/
}
Coursera Algorithms week1 查并集 练习测验:1 Social network connectivity的更多相关文章
- Coursera Algorithms week1 查并集 练习测验:3 Successor with delete
题目原文: Given a set of n integers S = {0,1,…,N-1}and a sequence of requests of the following form: Rem ...
- Coursera Algorithms week1 查并集 练习测验: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 ...
- Coursera Algorithms week1 算法分析 练习测验: Egg drop 扔鸡蛋问题
题目原文: Suppose that you have an n-story building (with floors 1 through n) and plenty of eggs. An egg ...
- Coursera Algorithms week1 算法分析 练习测验: 3Sum in quadratic time
题目要求: Design an algorithm for the 3-SUM problem that takes time proportional to n2 in the worst case ...
- Coursera Algorithms week2 基础排序 练习测验: Dutch national flag 荷兰国旗问题算法
第二周课程的Elementray Sorts部分练习测验Interview Questions的第3题荷兰国旗问题很有意思.题目的原文描述如下: Dutch national flag. Given ...
- Coursera Algorithms week2 基础排序 练习测验: Permutation
题目原文: Given two integer arrays of size n , design a subquadratic algorithm to determine whether one ...
- Coursera Algorithms week2 基础排序 练习测验: Intersection of two sets
题目原文: Given two arrays a[] and b[], each containing n distinct 2D points in the plane, design a subq ...
- Coursera Algorithms week4 基础标签表 练习测验:Inorder traversal with constant extra space
题目原文: Design an algorithm to perform an inorder traversal of a binary search tree using only a const ...
- Coursera Algorithms week4 基础标签表 练习测验:Check if a binary tree is a BST
题目原文: Given a binary tree where each
随机推荐
- 【译】x86程序员手册08 -2.6中断和异常
2.6 Interrupts and Exceptions 中断和异常 The 80386 has two mechanisms for interrupting program execution: ...
- 拍拍贷投资工具|拍拍贷投标工具|PPD投标工具|PPD投资工具介绍
我们先来分析一下现在市场上在PPD投资的途径: 其他解决方案 1.在网站或者手机客户端手动投标 这种方法对于非常小额的资金是可以的,稍微多一点就会发现不可行,目前PPD手动刷新出来的标几乎都是你刚刷新 ...
- git怎么克隆远程仓库到本地仓库
参考: https://blog.csdn.net/zhangzeshan/article/details/81564990 不知道为什么输入git的克隆地址就会提示密码错误 ,使用http地址就直接 ...
- Render2
https://blog.csdn.net/wf19930209/article/details/81109388
- Linux之网络文件共享服务(NFS)
概念: NFS:Network File System 网络文件系统,基于内核的文件系统.Sun公司开发,通过使用NFS,用户和程序可以像访问本地文件一样访问远端系统上的文件,基于RPC(Remote ...
- node.js开发环境配置
node.js是什么 Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境.Node.js 使用了一个事件驱动.非阻塞式 I/O 的模型,使其轻量又高效.Node.j ...
- c# 用binary实现序列化和反序列化
直接用实例来说明序列化和反序列化: namespace DynamicTest{ class Program { static void Main(string[] args) { List<P ...
- CSS3 (1) - Beginner
/Library/Frameworks/Python.framework/Versions/3.5/bin git clone https://*corp.com/*-dev.git /usr/loc ...
- 允许MS SqlServer远程连接
实际问题: 服务器192.168.0.103上的SQL Express数据库实例,局域网内其余机器的Sql Server Management Studio都无法连接. 在本机上,可以用“.\SqlE ...
- noip模拟赛 楼
分析:题目可以转化为对于一个数,对它进行x次减法操作,n-x次加法操作,使他变成最小的非负整数.因为每减一次数就会减小,次数是一定的,所以可以二分x,就可以了. #include <cstdio ...