联合查找解决方案的时间复杂度
Time complexity of a union-find solution
解决以下问题的时间复杂度大约是多少?如果我们假设由于路径压缩,每次调用 self.find() 大致分摊到 ~O(1)
问题陈述:
Given a list accounts, each element accounts[i] is a list of strings,
where the first element accounts[i][0] is a name, and the rest of the
elements are emails representing emails of the account.
Now, we would like to merge these accounts. Two accounts definitely
belong to the same person if there is some email that is common to
both accounts. Note that even if two accounts have the same name, they
may belong to different people as people could have the same name. A
person can have any number of accounts initially, but all of their
accounts definitely have the same name.
After merging the accounts, return the accounts in the following
format: the first element of each account is the name, and the rest of
the elements are emails in sorted order. The accounts themselves can
be returned in any order.
Example: Input: accounts = [["John", "johnsmith@example.com",
"john00@example.com"], ["John", "johnnybravo@example.com"], ["John",
"johnsmith@example.com", "john_newyork@example.com"], ["Mary",
"mary@example.com"]]
Output: [["John", 'john00@example.com', 'john_newyork@example.com',
'johnsmith@example.com'], ["John", "johnnybravo@example.com"], ["Mary",
"mary@example.com"]]
Explanation: The first and third John's are the same person as they
have the common email "johnsmith@example.com". The second John and Mary
are different people as none of their email addresses are used by
other accounts. We could return these lists in any order, for example
the answer [['Mary', 'mary@example.com'], ['John',
'johnnybravo@example.com'], ['John', 'john00@example.com',
'john_newyork@example.com', 'johnsmith@example.com']] would still be
accepted.
class Solution:
def accountsMerge(self, accounts):
"""
:type accounts: List[List[str]]
:rtype: List[List[str]]
"""
owners={}
parents={}
merged=collections.defaultdict(set)
results=[]
for acc in accounts:
for i in range(1,len(acc)):
owners[acc[i]] = acc[0]
parents[acc[i]] = acc[i]
for acc in accounts:
p = self.find(acc[1],parents) #Find parent of the first email in the list.
for i in range(2,len(acc)):
#Perform union find on the rest of the emails across all accounts (regardless of account name, as no common email can exist between different names.)
#Any common emails between any 2 lists will make those 2 lists belong to the same set.
currP = self.find(acc[i],parents)
if p!=currP:
parents[currP] = p
for acc in accounts:
p = self.find(acc[1],parents)
for i in range(1,len(acc)):
merged[p].add(acc[i])
for name,emails in merged.items():
res = [owners[name]] + sorted(emails)
results.append(res)
return results
def find(self,node,parents):
if node!=parents[node]:
parents[node] = self.find(parents[node],parents)
return parents[node]
逐个检查代码:
for acc in accounts:
for i in range(1,len(acc)):
owners[acc[i]] = acc[0]
parents[acc[i]] = acc[i]
这是 O(N),其中 N 是输入文本的大小,因为算法只访问输入的每个部分一次。请注意,每个文本元素可能具有任意大小,但这会被处理,因为 N 是输入文本的大小。
那么:
for acc in accounts:
p = self.find(acc[1],parents) #Find parent of the first email in the list.
for i in range(2,len(acc)):
currP = self.find(acc[i],parents)
if p!=currP:
parents[currP] = p
这也是 O(N),因为:
self.find(acc[i], parents)
被认为是摊销 O(1) 由于路径
压缩.
- 和以前一样 - 每个输入元素都被访问一次。
下一篇:
for acc in accounts:
p = self.find(acc[1],parents)
for i in range(1,len(acc)):
merged[p].add(acc[i])
循环本身取决于 N - 输入文本的大小。 add()
方法对一个集合进行操作,在 python 中被认为是 O(1)。总而言之,这个块也需要 O(N)。
最后:
for name,emails in merged.items():
res = [owners[name]] + sorted(emails)
results.append(res)
令人惊讶的是,这里存在瓶颈(至少在 O 表示法方面)。 emails
中的元素数量是 O(N),因为很可能系统有一个拥有大部分电子邮件的大用户。这意味着 sorted(emails)
可以采用 O(N log N)。
创建res的代码部分,排序后:
res = [owners[name]] + <the sort result>
这只是排序后数据大小的线性关系,小于排序的O(N log N)
虽然在一个循环中,但是总的来说排序的成本加起来不会超过O(N log N),因为O(N log N)的成本是假设有一个大用户。大用户不多。
例如,假设系统中有K个相等的用户。这使得每个用户的排序成本为 O(N/K log(N/K))。整个系统总计为 O(N log (N/K))。如果 K 是常数,这就变成了 O(N log N)。如果 K 是 N 的函数,则 O(N log(N/K)) 小于 O(N log N)。 这不是一个严格的证明,但足以了解为什么它是 O(N log N) 而不是更差的基本概念。
结论:该算法以 O(N log N) 复杂度运行,其中 N 是输入文本的大小。
注意:复杂性计算有一个主要假设,即在 Python 中,通过长度为 L 的字符串键访问映射或集合是一个 O(L) 操作。这通常是正确的,具有完善的散列函数,Python 没有。
解决以下问题的时间复杂度大约是多少?如果我们假设由于路径压缩,每次调用 self.find() 大致分摊到 ~O(1)
问题陈述:
Given a list accounts, each element accounts[i] is a list of strings, where the first element accounts[i][0] is a name, and the rest of the elements are emails representing emails of the account.
Now, we would like to merge these accounts. Two accounts definitely belong to the same person if there is some email that is common to both accounts. Note that even if two accounts have the same name, they may belong to different people as people could have the same name. A person can have any number of accounts initially, but all of their accounts definitely have the same name.
After merging the accounts, return the accounts in the following format: the first element of each account is the name, and the rest of the elements are emails in sorted order. The accounts themselves can be returned in any order.
Example: Input: accounts = [["John", "johnsmith@example.com", "john00@example.com"], ["John", "johnnybravo@example.com"], ["John", "johnsmith@example.com", "john_newyork@example.com"], ["Mary", "mary@example.com"]]
Output: [["John", 'john00@example.com', 'john_newyork@example.com', 'johnsmith@example.com'], ["John", "johnnybravo@example.com"], ["Mary", "mary@example.com"]]
Explanation: The first and third John's are the same person as they have the common email "johnsmith@example.com". The second John and Mary are different people as none of their email addresses are used by other accounts. We could return these lists in any order, for example the answer [['Mary', 'mary@example.com'], ['John', 'johnnybravo@example.com'], ['John', 'john00@example.com', 'john_newyork@example.com', 'johnsmith@example.com']] would still be accepted.
class Solution:
def accountsMerge(self, accounts):
"""
:type accounts: List[List[str]]
:rtype: List[List[str]]
"""
owners={}
parents={}
merged=collections.defaultdict(set)
results=[]
for acc in accounts:
for i in range(1,len(acc)):
owners[acc[i]] = acc[0]
parents[acc[i]] = acc[i]
for acc in accounts:
p = self.find(acc[1],parents) #Find parent of the first email in the list.
for i in range(2,len(acc)):
#Perform union find on the rest of the emails across all accounts (regardless of account name, as no common email can exist between different names.)
#Any common emails between any 2 lists will make those 2 lists belong to the same set.
currP = self.find(acc[i],parents)
if p!=currP:
parents[currP] = p
for acc in accounts:
p = self.find(acc[1],parents)
for i in range(1,len(acc)):
merged[p].add(acc[i])
for name,emails in merged.items():
res = [owners[name]] + sorted(emails)
results.append(res)
return results
def find(self,node,parents):
if node!=parents[node]:
parents[node] = self.find(parents[node],parents)
return parents[node]
逐个检查代码:
for acc in accounts:
for i in range(1,len(acc)):
owners[acc[i]] = acc[0]
parents[acc[i]] = acc[i]
这是 O(N),其中 N 是输入文本的大小,因为算法只访问输入的每个部分一次。请注意,每个文本元素可能具有任意大小,但这会被处理,因为 N 是输入文本的大小。
那么:
for acc in accounts:
p = self.find(acc[1],parents) #Find parent of the first email in the list.
for i in range(2,len(acc)):
currP = self.find(acc[i],parents)
if p!=currP:
parents[currP] = p
这也是 O(N),因为:
self.find(acc[i], parents)
被认为是摊销 O(1) 由于路径 压缩.- 和以前一样 - 每个输入元素都被访问一次。
下一篇:
for acc in accounts:
p = self.find(acc[1],parents)
for i in range(1,len(acc)):
merged[p].add(acc[i])
循环本身取决于 N - 输入文本的大小。 add()
方法对一个集合进行操作,在 python 中被认为是 O(1)。总而言之,这个块也需要 O(N)。
最后:
for name,emails in merged.items():
res = [owners[name]] + sorted(emails)
results.append(res)
令人惊讶的是,这里存在瓶颈(至少在 O 表示法方面)。 emails
中的元素数量是 O(N),因为很可能系统有一个拥有大部分电子邮件的大用户。这意味着 sorted(emails)
可以采用 O(N log N)。
创建res的代码部分,排序后:
res = [owners[name]] + <the sort result>
这只是排序后数据大小的线性关系,小于排序的O(N log N)
虽然在一个循环中,但是总的来说排序的成本加起来不会超过O(N log N),因为O(N log N)的成本是假设有一个大用户。大用户不多。
例如,假设系统中有K个相等的用户。这使得每个用户的排序成本为 O(N/K log(N/K))。整个系统总计为 O(N log (N/K))。如果 K 是常数,这就变成了 O(N log N)。如果 K 是 N 的函数,则 O(N log(N/K)) 小于 O(N log N)。 这不是一个严格的证明,但足以了解为什么它是 O(N log N) 而不是更差的基本概念。
结论:该算法以 O(N log N) 复杂度运行,其中 N 是输入文本的大小。
注意:复杂性计算有一个主要假设,即在 Python 中,通过长度为 L 的字符串键访问映射或集合是一个 O(L) 操作。这通常是正确的,具有完善的散列函数,Python 没有。