Week1 Job Interview Question

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.

 1 public class SocialNet {
 2     private int N;
 3     private String InputFileName;
 4     public SocialNet(int N,String InputFileName){
 5         this.N = N;
 6         this.InputFileName = InputFileName;
 7     }
 8     public String getTimeString(){
 9         WeightedQuickUnionUF wQUF = new WeightedQuickUnionUF(N);
10         In fin = new In(InputFileName);
11         while(fin.hasNextLine()){
12             String[] lineSplit = fin.readLine().split(" ");
13             wQUF.union(Integer.parseInt(lineSplit[1]), Integer.parseInt(lineSplit[2]));
14             if(wQUF.count() == 1){
15                 return lineSplit[0];
16             }
17         }
18         return "Never";
19     }
20     public static void main(String[] args) {
21         // TODO Auto-generated method stub
22         SocialNet sn = new SocialNet(10,"SocialNetInput.txt");
23         System.out.println(sn.getTimeString());
24     }
25 }

 input File:

20130101 0 2
20130201 0 4
20130301 2 3
20130401 3 4
20130501 4 5
20130601 3 6
20130701 5 6
20130801 2 6
20130901 1 2
20131001 7 3
20131101 0 9
20131201 4 8
20131202 4 3

 

posted @ 2014-02-18 22:45 理想空间 阅读(1655) 评论(0) 推荐(0) 编辑
摘要: 很多时候在写一个小的项目不想使用github等工具,只想简单在本地搭建一个版本管理器。那么TortoiseSVN就非常适合。第一步:下载TortoiseSVN,http://tortoisesvn.net/downloads.html 按自己的操作系统下载对应的软件即可,安装过程很简单。一步一步到底,安装完后,右键菜单中会出现TortoiseSVN的选项:第二步,创建一个文件夹用来存放TortoiseSVN的数据,例如创建E:\Coding\SVN该文件夹只能由TortoiseSVN来管理。再在SVN下创建项目文件夹,例如项目名为testproject,则创建一个testproject的文件夹 阅读全文
posted @ 2013-12-16 21:24 理想空间 阅读(7125) 评论(0) 推荐(0) 编辑
摘要: 一直想学习数值分析,无奈每次看到复杂的公式,各种证明就头疼,对于一个不是数学专业的人来说,其实能够掌握基本原理会实现代码就够了。最近看到王能超老师编写的一本《计算方法——算法设计及其Matlab》,感觉挺好的。我喜欢这种薄薄的书,能讲到要点,思路也很清楚,所以打算写下一点读书笔记。 另外,重要能够在博客中写latex公式了,为了这再怎么也要写点东西。 首先要讲的是插值算法,我们再日常工作和研究中常常需要做差值运算,例如图像插值,虽然是二维情况,但是跟一维原理是一样的。所以,插值问题可归结为以下情景,即已知一些列离散的点,$({x}_i,{y}_i),i=1,2,...,n$,目的是利用这... 阅读全文
posted @ 2013-11-28 00:31 理想空间 阅读(1874) 评论(0) 推荐(0) 编辑
摘要: function Car(Name,Age){ this.Name = Name; this.Age = Age;}Car.prototype.showAge = function(){ alert(this.Age);}function MoCar(Name,Age){ Car.call(this,Name,Age);}MoCar.prototype = new Car();MoCar.prototype.showName = function(){ alert(this.Name);}var obj = new MoCar("jianboqi",12);obj.show 阅读全文
posted @ 2013-03-07 11:04 理想空间 阅读(187) 评论(0) 推荐(0) 编辑
摘要: 前几天,闲来无事,想起了好久没有使用的新浪云平台(sae)帐号,于是打开来看,竟然也增加了python的支持,最近刚好对这门语言感兴趣,就用django框架来做了一个简单的新浪微博应用。将主要步骤记录一下。 目前,sae的python还在内测中,不过既然叫内测,也就是多一个申请的步骤而已。申请地址如下:http://python.sinaapp.com/apply填一下你的安全邮箱,然后写两点理由即可。(注意,安全邮箱不一定就是你的新浪微博的邮箱 切记) 首先,来看一下我做的这个新浪微博的应用(叫微微素材),就是要完成哪些任务: 应用地址:http://wsucai.sinaapp.co... 阅读全文
posted @ 2013-01-31 11:48 理想空间 阅读(6266) 评论(3) 推荐(0) 编辑
摘要: 在C++中,我们经常使用STL,那个在那些我们常用的数据结构vector,list的背后,又是如何实现的呢?特别是当我们使用iterator对容器进行遍历的时候,我们也能够想整数一样进行 ++ 运算。下面通过一个例子来建立一个slist,使得它能够通过iterator进行访问。我们要实现的功能是:建立一个list存储任意个数,然后输入一个数,通过find函数查找是否在list中存在。所以我们首先要编写一个find函数,find函数的要求是能够操作多种容器,能够查找多种数据类型(int,double),所以应该建立一个模版函数。(实际在STL中,这类函数应该叫算法,它通过iterator对容器进 阅读全文
posted @ 2013-01-17 22:03 理想空间 阅读(2128) 评论(0) 推荐(0) 编辑
摘要: C++ STL中的Iterator遍历容器的时候,无需知道具体的是哪个容器,只用传入地址即可。容器可以使用数组来模拟。代码如下:/************************************************************************//* implement of find function without stl *//************************************************************************/#include <iost... 阅读全文
posted @ 2013-01-16 17:30 理想空间 阅读(401) 评论(0) 推荐(0) 编辑
摘要: 归并排序 详细分析 阅读全文
posted @ 2013-01-15 00:11 理想空间 阅读(16835) 评论(4) 推荐(0) 编辑
摘要: implements of slection sorting with c++ codetemplate<class T> void selectionSort_1(T srcArr[],int n){ for (int i = 0;i < n ;i++) { int index = i; for (int j = i+1;j < n ;j++) { if (srcArr[j] <= srcArr[i]) { index = j; } ... 阅读全文
posted @ 2013-01-13 23:26 理想空间 阅读(269) 评论(0) 推荐(0) 编辑
摘要: Consider the problem of adding two n-bit binary integers, stored in two n-element arrays A and B. The sum of the two integers should be stored in binary form in the an (n+1) -element array C. State the problem formally and write pseudocode for adding the two integers.[Pseudocode]Adding-A-B(A,B,C) .. 阅读全文
posted @ 2013-01-13 17:07 理想空间 阅读(476) 评论(0) 推荐(0) 编辑
点击右上角即可分享
微信分享提示