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
随机推荐
- [Windows Server 2003] ASP.net安装方法
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:安装ASP.n ...
- 【YOLO】只检测人
一.修改源代码 cfg/coco.data classes= #修改成1 train = /home/pjreddie/data/coco/trainvalno5k.txt valid = coco_ ...
- java_io学习_编码
package io; public class encodingDemo{ public static void main(String[] args) throws Exception{ // T ...
- C# 字符串到字节数组,字节数组转整型
; , ); byte[] bytes = BitConverter.GetBytes(num);//将int32转换为字节数组 num = BitConverter.ToInt32(bytes, ) ...
- SpringMVC知识点总结一(非注解方式的处理器与映射器配置方法)
一.SpringMVC处理请求原理图(参见以前博客) 1. 用户发送请求至前端控制器DispatcherServlet 2. DispatcherServlet收到请求调用HandlerMappi ...
- UGUI世界坐标转换为UI本地坐标(游戏Hud的实现)
实现世界坐标的原理是: 世界坐标和UGUI的坐标分属两个坐标系,他们之间是无法进行转换的,需要通过屏幕坐标系来进行转换(因为屏幕坐标是固定的),即先将游戏场景中的世界坐标通过游戏场景Camera转化为 ...
- 洛谷——P3205 [HNOI2010]合唱队
P3205 [HNOI2010]合唱队 题目描述 为了在即将到来的晚会上有更好的演出效果,作为AAA合唱队负责人的小A需要将合唱队的人根据他们的身高排出一个队形.假定合唱队一共N个人,第i个人的身高为 ...
- codevs 1160 蛇形矩阵
1160 蛇形矩阵 传送门 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题解 题目描述 Description 小明玩一个数字游戏,取个n行 ...
- JVM内存监控(五)
频繁fullgc排查 jvm配置 -Xms200m -Xmx200m -Xmn50m -XX:PermSize=30m -XX:+HeapDumpBeforeFullGC -XX:+HeapDumpA ...
- MySql 内存表使用
MySql 内存表使用 内存表使用哈希散列索引把数据保存在内存中,因此具有极快的速度,适合缓存中小型数据库,但是使用上受到一些限制,以下是蓝草使用的一些感受. 1.heap对所有用户的连接是可见的,这 ...