Uploaded by abesigabrania6

APSSSS

advertisement
QN: Given any set of n people, is there a pair of people with the same birthday
and time to the second (i) bithdays n>365 (pigeon hole principle)
n>365*24*60*60 (ii) Design a bruteforce algorithm that returns a pair
with the same birth time stamp (iii) Prove the correctness of the
algorithm (iii) Compute the worst case asymptotes complexity (iv)
Redesign your algorithm to reduce the complexity (Always show the
working)
(i) Using the pigeonhole principle, if we have more than 365246060 people, then there must be at least
two people with the same birthday and time to the second. This is because there are only 365246060
possible distinct birthdays and times to the second in a non-leap year.
(ii) Here is a brute-force algorithm that returns a pair with the same birth time stamp:
1. For each person in the set, calculate their birth timestamp (i.e., the number of seconds since a
certain reference point, such as the Unix epoch).
2. Compare each person's birth timestamp with every other person's birth timestamp to find a pair
with the same timestamp.
3. If a pair with the same timestamp is found, return that pair.
4. If no pair is found, return "no pair found."
(iii) To prove the correctness of the algorithm, we need to show that it always returns a pair with the
same birth timestamp if one exists in the set, and "no pair found" otherwise.
If there is a pair of people with the same birth timestamp in the set, then the algorithm will find them
because it compares every person's timestamp with every other person's timestamp. Therefore, the
algorithm will always return a pair with the same birth timestamp if one exists.
If there is no pair of people with the same birth timestamp in the set, then the algorithm will compare
every possible pair of timestamps and never find a match. Therefore, the algorithm will correctly return
"no pair found" in this case.
(iv) The worst-case asymptotic complexity of the brute-force algorithm is O(n^2), where n is the number
of people in the set. This is because we compare each person's birth timestamp with every other
person's birth timestamp, resulting in n^2 comparisons.
(v) A more efficient algorithm to solve this problem would be to use a hash table or a set to store the
birth timestamps of each person as we iterate through the set. If we encounter a person with a birth
timestamp that is already in the hash table or set, then we have found a pair with the same birth
timestamp. This algorithm has a worst-case asymptotic complexity of O(n), which is much better than
the brute-force algorithm.
Here is the Python code for the more efficient algorithm:
def find_same_birthday_timestamp_pair(people):
# Create an empty set to store birth timestamps
timestamps = set()
# Iterate through the people
for person in people:
# Calculate the person's birth timestamp
timestamp = person.birthday.timestamp()
# Check if the timestamp is already in the set
if timestamp in timestamps:
# Found a pair with the same birth timestamp
return person, [p for p in people if p.birthday.timestamp() == timestamp][0]
# Add the timestamp to the set
timestamps.add(timestamp)
# No pair found
return None
The function takes a list of people objects, each with a birthday attribute that is a datetime object
representing the person's birthday. The function returns a tuple of the first pair of people with the same
birth timestamp, or None if no such pair exists.
The function iterates through the people and calculates each person's birth timestamp using the
timestamp() method of the datetime object. It then checks if the timestamp is already in the
timestamps set using the in operator. If it is, then the function returns the pair of people with the same
birth timestamp. If not, the function adds the timestamp to the set and continues iterating through the
people.
The worst-case asymptotic complexity of this algorithm is O(n), where n is
Download