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
随机推荐
- strut2 拦截器 使用
拦截器是strut2里一个很振奋人心的应用.通过配置拦截器可以在action执行之前进行一些初始化或者是其他的操作,但是在action执行之后,返回结果就已经确定,结果是很难改变了(目前我还不知道怎么 ...
- python基础--字符串操作、列表、元组、文件操作
一.变量及条件判断 1.字符串.布尔类型.float.int类型,None都是不可变变量 2.字符串是不可变变量,不可变变量就是指定义之后不能修改它的值 3.count +=1和count=count ...
- vue编辑回显问题
真是疯了,vue怪毛病真多 就下面这玩意儿,多选组合框,新增的时候好用的不行不行的,到了编辑的时候,要回显数据,怪毛病一堆一堆的 首先,回显的时候要传一个数组,但是这个数组里的元素得是字符串类型的,如 ...
- Java切换JDK版本的方法及技巧
由于项目的不同安排,之前项目开发时,使用的jdk版本为1.8,现临时接手一以前项目,需要更换jdk版本. 安装 不再赘述,去Oracle网站(https://www.oracle.com/techne ...
- Swift 3到5.1新特性整理
本文转载自:https://hicc.me/whats-new-in-swift-3-to-5-1/,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有. Hipo 2.0 重写从 Swif ...
- ubuntu14.04禁用USB外存储设备
ubuntu 14.04中禁用usb外存储设备: 在网上找了很多方法,大概都是下面的命令,而实际测试的时候没有什么作用. gsettings set org.gnome.desktop.media-h ...
- AtCoder Beginner Contest 089完整题解
A - Grouping 2 Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement There a ...
- 4.几个「不难但却很有用」的 Git 技能点
阅读 Git 原理详解及实用指南 记录 tag:不可移动的 branch tag 是一个和 branch 非常相似的概念,它和 branch 最大的区别是:tag 不能移动.所以在很多团队中,tag ...
- (蓝桥)2017C/C++A组第一题迷宫
#include<iostream> #include<memory.h> using namespace std; char mi[10][10] ; int visited ...
- 暑假集训D17总结
考试 玄学的一次考试= = T1乱搞 只会乱搞出前二十分 然后真的拿了二十分 T2模拟 自己造数据 没有一个是在十分钟内跳出来的 然后竟然A了 T3暴力 觉得如果老爷机心情不好就会被卡到20 然后 ...