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

 1 import java.io.FileInputStream;
 2 import java.io.FileNotFoundException;
 3 import java.util.Scanner;
 4 
 5 import edu.princeton.cs.algs4.StdOut;
 6 import edu.princeton.cs.algs4.WeightedQuickUnionUF;
 7 
 8 
 9 public class SocialNetworkConnUF {
10     private FileInputStream ins;
11     private WeightedQuickUnionUF uf;
12     public SocialNetworkConnUF(int num, FileInputStream ins){
13         this.ins = ins;
14         uf = new WeightedQuickUnionUF(num);
15     }
16     
17     @SuppressWarnings("resource")
18     public String getEarliestConTime(){
19         Scanner scanner = new Scanner(ins,"utf-8");
20         String earliestConTime = null;
21         while(scanner.hasNextLine()){
22             String line = scanner.nextLine();
23             if(line != null && !line.trim().equals("")){
24                 String[] lineArray = line.split(" ");
25                 if(lineArray.length == 3){
26                     String timestamp = lineArray[0];
27                     int p = Integer.parseInt(lineArray[1]);
28                     int q = Integer.parseInt(lineArray[2]);
29                     if(uf.connected(p, q)) continue;
30                     uf.union(p,q);
31                     if(uf.count() == 1) {
32                         earliestConTime = timestamp;
33                         break;
34                     }
35                 }
36             }
37             
38         }
39         return earliestConTime;
40     }
41     public static void main(String[] args){
42         FileInputStream ins;
43         try {
44             ins = new FileInputStream("socialNetworkLog.txt");
45             SocialNetworkConnUF socialNet = new SocialNetworkConnUF(10, ins);
46             String earliestConnTime = socialNet.getEarliestConTime();
47             StdOut.println(" the earliest connected time is :" + earliestConnTime);
48         } catch (FileNotFoundException e) {
49             e.printStackTrace();
50         }
51         
52     }
53     /*
54      * socialNetworkLog.txt
55      * 20170714001 0 1
56      * 20170714002 4 5
57      * 20170714003 8 9
58      * 20170714004 2 4
59      * 20170714005 5 6
60      * 20170714006 7 8
61      * 20170714007 2 5
62      * 20170714008 6 7
63      * 20170714009 1 2
64      * 20170714010 0 3
65      * 20170714011 1 9
66      * 20170714012 3 7
67      *
68      */
69 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM