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
随机推荐
- dnn 添加图片
public string fileUpload() { if (fuPhoto.PostedFile != null && fuPhoto.P ...
- (二)Python 学习第二天--爬5068动漫图库小案例
(注:代码和网站仅仅是学习用途,非营利行为,源代码参考网上大神代码,仅仅用来学习
- SpringMVC(四)@RequestParam
使用@RequestParam可以将URL中的请求参数,绑定到方法的入参上,并通过@RequestParam的3个参数进行配置 Modifier and Type Optional Element D ...
- js生成web安全色
256色里有40种颜色在Macintosh和Windows里显示的效果不一样,所以能安全使用的只有216色. <!DOCTYPE HTML> <html> <head&g ...
- [数据结构】【c语言】链表的创建和遍历
第一次写代码的博客,一个刚刚接触的新手,来这里主要是为了记录自己,方便自己以后浏览,也欢迎大家指正.先来个简单的,动态链表的创建和遍历. #include<stdio.h> #includ ...
- Problem 56
Problem 56 https://projecteuler.net/problem=56 Powerful digit sum A googol (10100) is a massive numb ...
- Spider-Python实战之通过Python爬虫爬取图片制作Win7跑车主题
1. 前期准备 1.1 开发工具 Python 3.6 Pycharm Pro 2017.3.2 Text文本 1.2 Python库 requests re urllib 如果没有这些Python库 ...
- 关于read和fread
1.fread与read的区别---open和fopen的区别--fread函数和fwrite函数:http://blog.csdn.net/dreamtdp/article/details/7560 ...
- BZOJ 1641 USACO 2007 Nov. Cow Hurdles 奶牛跨栏
[题解] 弗洛伊德.更新距离的时候把$f[i][j]=min(f[i][j],f[i][k]+f[k][j])$改为$f[i][j]=min(f[i][j],max(f[i][k],f[k][j])) ...
- SQLServer中的Cross Apply、Outer Apply
https://www.2cto.com/database/201304/206330.html