高效的算法,采用 Twitter 用户并按照其关注者数量的顺序找到顶级用户

发布于 2024-12-28 04:38:07 字数 353 浏览 1 评论 0原文

标题很罗嗦。所以我会用一个例子来解释。

我们有一个包含 10,000 个 Twitter 用户的数据库,每个用户最多可关注 2000 个用户。该算法将一个从未见过的随机用户(包括关注他的人)作为输入,并按照关注者数量的顺序从数据库中返回 Twitter 用户。

IE 我们有:

用户 A 关注 1,2,3,4

用户 B 关注 3,4,5,6

用户 C 关注 4,8,9

我们输入用户 X,有用户 3,4,5 关注他。

该算法应返回:

B: 3 个匹配 (3,4,5)

A: 2 个匹配 (3,4)

C: 1 个匹配 (4)

The title is very wordy. So I'll explain with an example.

We have a database of 10,000 twitter users with each following up to 2000 users. The algorithm takes as input one random never before seen user (including the people that follow him), and returns the twitter users from the database by order of how many of his followers they follow.

i.e.
We have:

User A follows 1,2,3,4

User B follows 3,4,5,6

User C follows 4,8,9

We enter user X who has users 3,4,5 following him.

The algorithm should return:

B: 3 matches (3,4,5)

A: 2 matches (3,4)

C: 1 match (4)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

べ繥欢鉨o。 2025-01-04 04:38:07

将数据存储为大小为 10^5x10^5 的稀疏整数矩阵 A,并在适当的位置添加 1。然后,给定用户i,计算A[i,] * A(矩阵乘法)。然后排序。

Store the data as a sparse integer matrix A of size 10^5x10^5 with ones at the appropriate places. Then, given a user i, compute A[i,] * A (matrix multiplication). Then sort.

极致的悲 2025-01-04 04:38:07

假设您有一个与此类似的表结构:

Table Users

Id (PK, uniqueidentifier, not null)
Username (nvarchar(50), not null)

Table UserFollowers

UserId (FK, uniqueidentifier, not null)
FollowerId (uniqueidentifier, not null)

您可以使用以下查询来获取关注者的common父级查询中用户的关注者

SELECT     Users_Inner.Username, COUNT(Users_Inner.Id) AS [Total Common Parents]
FROM         Users INNER JOIN
                  UserFollowers ON Users.Id = UserFollowers.FollowerId INNER JOIN
                  UserFollowers AS UserFollowers_Inner ON UserFollowers.FollowerId = UserFollowers_Inner.UserId INNER JOIN
                  Users AS Users_Inner ON UserFollowers_Inner.FollowerId = Users_Computed.Id
WHERE     (UserFollowers.UserId = 'BD34A1FF-FCF5-4D35-B8A3-EFFB1587A874')
GROUP BY Users_Inner.Username
ORDER BY COUNT(Users_Inner.Id) DESC

Assuming you have a table structure similar to this:

Table Users

Id (PK, uniqueidentifier, not null)
Username (nvarchar(50), not null)

Table UserFollowers

UserId (FK, uniqueidentifier, not null)
FollowerId (uniqueidentifier, not null)

You can use the following query to get the common parents of followers of the followers of the user in query

SELECT     Users_Inner.Username, COUNT(Users_Inner.Id) AS [Total Common Parents]
FROM         Users INNER JOIN
                  UserFollowers ON Users.Id = UserFollowers.FollowerId INNER JOIN
                  UserFollowers AS UserFollowers_Inner ON UserFollowers.FollowerId = UserFollowers_Inner.UserId INNER JOIN
                  Users AS Users_Inner ON UserFollowers_Inner.FollowerId = Users_Computed.Id
WHERE     (UserFollowers.UserId = 'BD34A1FF-FCF5-4D35-B8A3-EFFB1587A874')
GROUP BY Users_Inner.Username
ORDER BY COUNT(Users_Inner.Id) DESC
安静被遗忘 2025-01-04 04:38:07

像这样的东西会起作用吗?

for f in followers(x)
    for ff in followers(f)
        count[ff]++  // assume it is initially 0

sort the ff-s by their counts 

与矩阵解决方案不同,其复杂性与涉及的人数成正比,而不是与 Twitter 上的用户数量成正比。

would something like this work?

for f in followers(x)
    for ff in followers(f)
        count[ff]++  // assume it is initially 0

sort the ff-s by their counts 

Unlike the matrix solution, the complexity of this is proportional to the number of people involved rather than the number of users on twitter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文