Algorithms,part1 week1
来源:互联网 发布:淘宝最好的推广方法 编辑:程序博客网 时间:2024/06/17 08:29
Interview Questions: Union-Find
Question 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 .
hint-union-find.
use another array to keep track of the number of each subtree for each node.
Then, use binary search (lgN) to search N for each time-stamp and union pair (M).
Question 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 the connected component containing i. The operations, union(), connected(), and find() should all take logarithmic time or better.
Hint: maintain an extra array to the weighted quick-union data structure that stores for each root i the large element in the connected component containing i.
For example, if one of the connected components is
Question 3
Successor with delete. Given a set of N integers S={0,1,...,N−1} and a sequence of requests of the following form:
Hint: use the modification of the union-find data discussed in the previous question.- Remove
x fromS - Find the successor of
x : the smallesty inS such thaty≥x .
Question 4
Union-by-size. Develop a union-find implementation that uses the same basic strategy as weighted quick-union but keeps track of tree height and always links the shorter tree to the taller one. Prove a lgN upper bound on the height of the trees for N sites with your algorithm.
Hint: replace the sz[] array with a ht[] array such that ht[i] stores the height of the subtree rooted at i.Interview Questions: Analysis of Algorithms
Question 1
3-SUM in quadratic time. Design an algorithm for the 3-SUM problem that takes time proportional to N2 in the worst case. You may assume that you can sort the N integers in time proportional to N2 or better.
Question 2
Search in a bitonic array. An array is bitonic if it is comprised of an increasing sequence of integers followed immediately by a decreasing sequence of integers. Write a program that, given a bitonic array of N distinct integer values, determines whether a given integer is in the array. Your program should use ∼3lgN compares in the worst case.
Question 3
Egg drop. Suppose that you have an N -story building and plenty of eggs. An egg breaks if it is dropped from floor T or higher and does not break otherwise. Your goal is to devise a strategy to determine the value of T given the following limitations on the number of eggs and tosses:
- Version 0: 1 egg,
≤T tosses. - Version 1:
∼1lgN eggs and∼1lgN tosses. - Version 2:
∼lgT eggs and∼2lgT tosses. - Version 3:
2 eggs and∼2N−−√ tosses. - Version 4:
2 eggs and≤cT−−√ tosses for some fixed constantc .
- Algorithms,part1 week1
- Sorting Algorithms Part1
- Week1 Assignment - Princeton-Algorithms-PartI
- week1-10Theory of algorithms
- Algorithms, Part I WEEK1 Percolation
- Coursera Algorithms week1 Interview Questions: Egg drop
- 《大学算法教程Algorithms》笔记--Chapter1--Part1
- 算法设计Week1 LeetCode Algorithms Problem #1 Two Sum
- 算法设计Week1 LeetCode Algorithms Problem #2 Add Two Numbers
- Coursera Algorithms week1 练习测验1:Social network connectivity
- Coursera Algorithms week1 练习测验3:Successor with delete
- Coursera Algorithms week1 Interview Questions: 3Sum in quadratic time
- Coursera Algorithms week1 Interview Questions: Search in a bitonic array
- week1
- week1
- week1
- week1
- week1
- 计算机端口及设置
- flush让页面分块,逐步呈现
- Android ApiDemo学习(四)Views——7 Layout Animation
- LAST_QUERY in Form and OAF Page
- Linux php的运行模式与其相关名词术语
- Algorithms,part1 week1
- lucene管理IndexReader和IndexWriter的最佳实践
- C++和Objective-C混编(官方文档翻译)
- 修复metasploit v4.2.0 ,启用db_autopwn命令问题(t00ls)
- 【Eclipse】使用的插件
- 客户端使用 pl/sql developer 无法以sys用户登录
- java实现数字千分位的显示
- JAVA——DataOutputStream写文本和换行
- msp430串口遇到的问题