Question

In: Computer Science

Describe at least three algorithms (python only) to determine whether an arbitrary array A[1 .. n]...

Describe at least three algorithms (python only) to determine whether an arbitrary array A[1 .. n] contains more than n/4 copies of any value. Analyze the running time complexity of all the three methods.

Solutions

Expert Solution

#---------- algos.py ------------
import random,time
#function that takes unsorted Array A
'''
Time complexity: O(n^2)
'''
def linearFinding(A):
   length = len(A)
   quarter = length//4
   #to store repeated values more than the quarter.
   vals = []
   #iterate through list
   for i in range(length):
       val_i = A[i]
       #set count of value at i to 1.
       count = 1
       #check the rest of the array
       #and get count.
       for j in range (i+1,length):
           if(val_i == A[j]):
               count+=1
       #if count is > quarter then
       if(count > quarter):
           #check if val_i not checked already.
           if(val_i not in vals):
               vals.append(val_i)
   print("\nValues that are repeated more than n/4(",quarter,") are: \n")
   length = len(vals)
   if(length == 0):
       print("\nNo values are there that are repeated")
       return 0
   for i in range(length):
       print(vals[i],end=" ")
   print("")
'''
Function that takes unsorted array.
Description: O(n log n)
'''
def sortedLinear(A):
   #copy the array A which takes O(N)
   A_copy = A.copy()
   #sort the array O(n log n)
   A_copy.sort()
  
   length = len(A_copy)
   quarter = length//4
   found = False
   print("\nValues that are repeated more than n/4(",quarter,") are: \n")
   i =0
   #iterate through array
   #below while loop overall time complexity will be O(N)
  
   while(i<length):
       #since the array is sorted.
       #all the values are in sequence.
       val_i = A_copy[i]
       #set count of val_i to 1.
       count = 1
       j = i+1
       #till j<length and j_th value is equal to val_i
       while(j<length and A_copy[j] == val_i):
           #increment count
           count+=1
           j+=1
       #if count > quarter
       if(count > quarter):
           #print it.
           print(val_i,end=" ")
           #since one value is found set found to true.
           found = True
           #increment the i by count
           i+=count
       else:
           #if not increment i by 1
           i+=1
   #if not found print no values are found
   if(not found):
       print("\nNo values are there that are repeated")
   print("")
  

'''
function that takes unsorted array.
Time Complexity: O(N)
'''
def dictSearch(A):
   vals = {}
   length = len(A)
   #iterate through list. O(N)
   for i in range(length):
       val_i = A[i]
       #time complexity of python dict.keys() is O(1)
       #you can check it.
       if(val_i in vals.keys()):
           #if val_i already is found then increment it by 1.
           vals[val_i] += 1
       else:
           #if not create new key by value 1.
           vals[val_i] = 1
  
   #then print the values in dictionary whose values are > count.
   #below opeartion depends on number of values that are repeated > quarter.
   #and it will be < O(N) so overall time complexity will be O(N) which is iterating
   #through list by using above for loop.
   quarter = length/4
   print("\nValues that are repeated more than n/4(",quarter,") are: \n")
   found = False
   for key,val in vals.items():
       if(val > quarter):
           print(key,end=" ")
           found = True
  
   if(not found):
       print("\nNo values are there that are repeated")
A = []
for i in range(10000):
   A.append(random.randint(0,3))
print("Array Size: ",len(A))

print("\n Method - 1")
st = time.time()
linearFinding(A)
end = time.time()- st
print("Time taken %.2f"%end)

print("\n Method - 2")
st = time.time()
sortedLinear(A)
end = time.time() - st
print("Time taken %.2f"%end)

print("\n Method - 3")
st = time.time()
dictSearch(A)
end = time.time() - st

print("Time taken %.2f"%end)


Related Solutions

Describe at least three indicators that you would use to determine whether the stocks are inexpensive.   ...
Describe at least three indicators that you would use to determine whether the stocks are inexpensive.    How would use these indicators?
Consider the following pseudocode for insertion-sort algorithm. The algorithm sorts an arbitrary array A[0..n − 1]...
Consider the following pseudocode for insertion-sort algorithm. The algorithm sorts an arbitrary array A[0..n − 1] of n elements. void ISORT (dtype A[ ], int n) { int i, j; for i = 1 to n – 1 {     //Insert A[i] into the sorted part A[0..i – 1]     j = i;     while (j > 0 and A[j] < A[j – 1]) {         SWAP (A[j], A[j – 1]);         j = j – 1 }     }...
1. We have an array A of size n. There are only positive integers in this...
1. We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. Design an efficient algorithm to find the maximum difference between any two...
1. We have an array A of size n. There are only positive integers in this...
1. We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. Design an efficient algorithm to find the maximum difference between any two...
We have an array A of size n. There are only positive integers in this array....
We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. a. Design an efficient algorithm to find the maximum difference between any two...
We have an array A of size n. There are only positive integers in this array....
We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. a. Design an efficient algorithm to find the maximum difference between any two...
Write a Python function that accepts three arguments: an array, the size of the array, and...
Write a Python function that accepts three arguments: an array, the size of the array, and a number n. Assume that array contains integers. The function should display all integers in the array that are greater than the number n. Test your function.
Determine whether there is an integer n > 1 such that there is a projective plane...
Determine whether there is an integer n > 1 such that there is a projective plane of order n (i.e. with n + 1 points on each line) such that n ̸= pk for any prime number p and integer k ≥ 1.
Question 1 a) Determine whether the language {a n b m c n | n >...
Question 1 a) Determine whether the language {a n b m c n | n > 0} is regular or not using pumping Lemma. b) Prove that the language {(ai bn | i, n > 0, i = n or i = 2n} is not regular using the Pumping Lemma.
Determine whether the series converges absolutely, conditionally, or not at all. ∞ n = 1 (−1)n...
Determine whether the series converges absolutely, conditionally, or not at all. ∞ n = 1 (−1)n 1 + n3
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT