发现最短“路径” Facebook 上的两个人之间

发布于 2024-11-03 09:10:09 字数 1431 浏览 1 评论 0原文

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

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

发布评论

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

评论(2

梦在夏天 2024-11-10 09:10:09

当谈到语言时,您应该使用您最熟悉的语言。例如,他们有 PHP 的示例代码,因此如果您了解 PHP,则可以从它开始。 Java 也可以工作。

现在,我不知道 fbook API 是否已经有一些执行此任务的函数。但是,正如您已经提到的,您想要找到“最短路径”。事实上,有很多算法可以找到图的两个节点之间的最短路径。

您正在寻找图的两个节点之间的最短路径。什么是图表?

顾名思义,图就是节点和边的集合。在这种情况下,每个人都是一个节点。连接节点的边是由友谊形成的。

假设您有朋友 X,他有朋友 {A、B、C、D} 和 朋友 Y,他有朋友 (B、D、E、F}您首先创建所有朋友的图表(即,取两个集合的并集 {A, B, C, D, E, F, X, Y} We)。包括 X 和 Y,因为我们最终想要找到它们之间的最短距离2.

一旦你获得了每个朋友的社交图(谁是他们的朋友,他们彼此是朋友吗?等等),你就可以将他们放入一个图结构中,我不会谈论如何做到这一点 - 只是变得更大。 - 这里的图片

表示的一种方法是使用邻接矩阵:

  A B C D E F X Y
A 1 0 0 0 0 0 1 0
B      ...
C
D
E
F
X
Y

即,查看每个网格项,如果两个人是朋友,则在他们的横截面中输入“1”,否则

现在应用 “0”。您可以使用该数据的最短路径算法。 Dijkstra 算法可以实现这一点。

因此:您需要了解一些图、邻接矩阵和最短路径算法的背景知识,甚至可能有一个 Java 库可以为您完成所有这些工作。甚至是 PHP 或 R 库。但从较高的层面来看,这就是你想要实现的目标。我什至不确定 fbook API 是否会为您提供解决此问题所需的所有数据。

祝你好运!

When it comes to the language, you should use whatever you are most comfortable with. They have sample code for PHP, for example, so if you know PHP you could start with that. Java would work too.

Now, I don't know if the fbook API already has some function which performs this task. But, as you have already alluded to, you want to find the "shortest path." In fact, there are many algorithms out there which will find the shortest path between two nodes of a graph.

You are looking for the shortest path between two nodes of a graph. What's a graph?

A graph just what it sounds like - a collection of nodes and edges. In this case, each person would be a node. And the edges, which connect nodes, are formed by friendships.

So lets say you have Friend X, who has friends {A, B, C, D} and Friend Y, who has friends (B, D, E, F}. You's start by creating a graph of all of the friends (that is, take the union of the two sets). {A, B, C, D, E, F, X, Y} We include X and Y because we ultimately want to find the shortest distance between those two.

Once you get the social graph of each friend (who are their friends, are they friends with each other, etc) then you can place them into a graph structure. I won't talk about how to do that - just going big-picture here.

One way to represent that is with an adjacency matrix:

  A B C D E F X Y
A 1 0 0 0 0 0 1 0
B      ...
C
D
E
F
X
Y

That is, look at each grid item. If the two people are friends, put a "1" in their cross-section, otherwise a "0".

Now apply a shortest-path algorithm to that data. You could use Dijkstra's Algorithm to accomplish this.

So: you need to have a little background on graphs, adjacency matrices, and shortest path algorithms. There might even be a Java library that does all this for you. Or even a PHP or R library. But at a high level, this is what you are trying to accomplish. I'm not even sure if the fbook API will give you all the data you need to solve this.

Best of luck!

夜光 2024-11-10 09:10:09

我应该选择什么语言?

您可以轻松使用的任何语言。

我应该使用哪些库?我应该朝什么方向发展?

尝试:BFS(队列)和DFS(堆栈或递归)。

What language should I choose?

Any language your are comfortable to use.

What libraries should I use? What general direction should I go in?

Try: BFS (queue) and DFS(Stack or recursive).

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