1. You are given two sorted lists of size m and n. Give an O(logm + logn) time a
ID: 3731121 • Letter: 1
Question
1. You are given two sorted lists of size m and n. Give an O(logm + logn) time algorithm for computing the k-th smallest element in the union of the two lists. Note that the only way you can access these values is through queries to the databases. Ina single query, you can specify a value k to one of the two databases, and the chosen database will return the k-th smallest value that it contains. Since queries are expensive, you would like to compute the median using as few queries as possible. Give an algorithm that nds the median value using at most O(logm + logn) queries.
2. We are given a directed graph G = (V,E) on which each edge (u,v) E has an associated value r(u,v), which is a real number in the range 0 r(u,v) 1 that represents the reliability of a communication channel from vertex u to vertex v. We interpret r(u,v) as the probability that the channel from u to v will not fail, and we assume that these probabilities are independent. Give an ecient algorithm to nd the most reliable path between two given vertices. You do not have to analyze the algorithm’s running time. It should be a polynomial running-time algorithm.
Explanation / Answer
Solution:
The first question is done as per Chegg guidelines, please repost others.
Algorithm:
function smallelement(x[1,…,m], y[1,…,n], k)
if m = 0: return y[k]
if n = 0: return x[k]
if x[m/2] > y[n/2]:
if k < (m + n)/2:
return smallelement(x[1..m/2], y[1…n], k)
else:
return smallelement(x[1..m], y[(n/2) + 1…n], k –n/2)
else:
if k < (m + n)/2:
return smallelement(x[1..m], y[1…n/2], k)
else:
return smallelement(x[(m/2)+ 1..m], y[1…n], k –m/2)
Correctness: If x[m/2] more than y[n/2] then the top half of array x is greater than the bottom halves of both x & y arrays.Therefore the entire top half of array x must lie above the median of combined arrays. The entire bottom half of array y must lie below the median of the combined arrays. By comparing k to (m+n)/2,we will eliminate one of these two half arrays.
Running time: O(logm + logn), a constant amount of time is taken either m or n small halved values. It will happen at mostlogm+logn time before one of them reaches zero
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.