0% found this document useful (0 votes)
2 views

Design and analysis of algo

The document outlines algorithms for solving various computational problems, including finding vertices of a convex polygon, determining the nth smallest element in combined sorted arrays, checking for frequent elements in an array, and testing for a majority element. Each problem is accompanied by a step-by-step explanation of the algorithm and an analysis of its time complexity. The algorithms are designed to run in O(n log n) or O(n) time, demonstrating efficient approaches to these problems.

Uploaded by

f20220103
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Design and analysis of algo

The document outlines algorithms for solving various computational problems, including finding vertices of a convex polygon, determining the nth smallest element in combined sorted arrays, checking for frequent elements in an array, and testing for a majority element. Each problem is accompanied by a step-by-step explanation of the algorithm and an analysis of its time complexity. The algorithms are designed to run in O(n log n) or O(n) time, demonstrating efficient approaches to these problems.

Uploaded by

f20220103
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Birla Institute of Technology and Science-Pilani, Hyderabad Campus

Second Semester 2024-2025


Tutorial-4
Course No CS F364 Course Title: Design and Analysis of Algorithm

General Instructions: Argue logically. Write it in a manner that explains your logic very
clearly. Do not miss steps in between.
Q1. Let E be an unsorted set of n segments that are the edges of the convex polygon. Describe
an O(nlog(n)) algorithm that computes from E a list containing all vertices of the polygon, sorted in
counter-clockwise order.

1. Describe each step of your algorithm in plain English.

2. Analyze the running time of your algorithm and justify why it is O(nlog(n)).

Solution A convex polygon has the property that you can triangulate it by drawing edges from any one
vertex. Thus, we can sort the endpointsof all segments relative to the any vertex (which is one of the
segments.)
Given {s1 , s2 , . . . , sn }
Algorithm:

1. Let p be one point of s1 .

2. Search through all segments for the segment which also contains p. Call that segment sj .

3. Find the ”other endpoint” of s1 , call it a. Since the segments are already the boundary of a convex
polygon, it must be the case that all endpoints are either to the left of (a, p) or to the right of (a,
p).

4. Check any other point. If it is to the right of (a, p), the swap direction so it is to the left of ap.

5. For all endpoints q of all segments, sort them by angle [a p q].

6. List the endpoints in this order, dropping duplicates.

RUNTIME:
Steps 1, 2, 3 are constant
Steps 2, 6 are O(n)
Step 5 is O(nlogn)
total: O(nlogn) .
Q2]. Let A[1..n] and B[1..n] be two arrays of integers, each sorted in increasing order. Write a divide
and conquer algorithm that finds the nth smallest element of the 2n combined elements. Your algorithm
must run in O(log n) time. You may assume that all the 2n elements are distinct.

1. Explain your algorithm in plain English

2. Analyze the running time of your algorithm and justify why it is O(logn).

1
Solution Algorithm: We are searching the nth smallest elemnt sn in the union of the arrays of a[1, . . . , n]
and b[1, . . . , n].
Our algorithm starts off by comparing a[⌊ n2 ⌋] and b[⌊ n2 ⌋] :

1. If a[⌊ n2 ⌋] > b[⌊ n2 ⌋], then sn will be the ⌊ n2 ⌋th smallest element of the union of the subarrays
a[1, . . . , ⌊ n2 ⌋] and b[⌈ n2 ⌉ + 1, . . . , n].

2. If a[⌊ n2 ⌋] < b[⌊ n2 ⌋], then sn will be the ⌈ n2 ⌉th smallest element of the union of the subarrays a[⌊ n2 ⌋ +
1, . . . , n] and b[1, . . . , ⌈ n2 ⌉].

3. The base case of the recursion will be when there is one element in a and one element in b. Then
return the smallest

Time complexity. Our algorithm is characterized by the recurrence:

n
T (n) = T + O(1)
2
(1)

Using the Master Theorem with a = 1, b = 2 and d = 0, as logb a = 0 = d we get

T (n) = O(logn) (2)

Q3 Describe an agorithm to determine in O(n) time whether an arbitrary array A[1 . . . n] contains more
than n4 copies of any value (Do not use hashing, or radix sort, or any other method that depends on the
precise input values, as opposed to their order). Argue why your algorithm is of time complexity O(n).
Solution An element that occurs at least n/4 times must be one of the following four elements: n/4-th
smallest, 2n/4-th smallest, 3n/4-th smallest element. The reason is, suppose there is an element x that
occurs at least n/4 times in A. Let AS be the array that results when A is sorted. Let i be the least
index where x appears in AS. Then, AS[i] to AS[i + n/4 − 1] are all equal to x.
The algorithm: Use the linear time algorithm DETERMINISTIC SELECT for computing the k-th small-
est element to compute the n/4 th smallest, 2n/4-th smallest, 3n/4-th smallest elements. For each one
go through the array A to see if it occurs n/4 times. If none of these elements occur n/4 times then there
is no such element.
The algorithm takes O(n) time as we are calling DETERMINISTIC SELECT three times, and each call
takes O(n) time.
Q4 Let A be a list of n (not necessarily distinct) integers. Describe an O(n)-algorithm to test whether
any element occurs more than ⌊n/2⌋ times in A.
Solution If an element occurs more than ⌈n/2⌉ times in A then it must be the median of A. However,
the reverse is not true, so once the median is found, you must check to see how many times it occurs in
A. The algorithm takes O(n) time provided you use linear selection and O(n) space.

You might also like