如何解有环的图

发布于 2024-10-31 22:33:26 字数 843 浏览 0 评论 0原文

我正在尝试在 Java 的帮助下找到下一个问题的解决方案。我有一个图表,这是它的外观的一个很好的例子:

在此处输入图像描述

有它的符号:

[ {A = {C = 0.7},{D = 0.3}}, {C = {输出 = 0.2},{F = 0.8}}, {D = {C = 0.1}、{F = 0.2}、{G = 0.3}、{E = 0.4}}、 {S = {A = 0.4},{B = 0.6}},
{E = {G = 0.3},{out = 0.7}}, {G = {B = 0.2}{out = 0.8}}, ...

S - 是起始节点(S = 1),out - 是离开图表的路。

我想跟踪图表并知道每个节点有多少百分比。 例如,A = 0.4*S (S = 1)、C = 0.7A + 0.1D、D = 0.3A + 0.7B

我认为可以通过递归(有向图的 DFS,特别是 Tarjan 的算法)来做到这一点,但是虽然存在循环,但我认为这没有帮助。另一种解决方案是求解线性方程组。 我不知道什么更好,也许有一些针对此类任务的解决方案。 这个例子只是一个例子,但我应该考虑到我有类似的appr。 2000 个节点(谁知道有多少个周期)。

你会怎么做?

I am trying to find a solution for the next problem with help of Java. I have a graph, this is a good example of how it could look:

enter image description here

There is its notation:

[{A = {C = 0.7}, {D = 0.3}},
{C = {out = 0.2}, {F = 0.8}},
{D = {C = 0.1}, {F = 0.2}, {G = 0.3}, {E = 0.4}},
{S = {A = 0.4},{B = 0.6}},
{E = {G = 0.3},{out = 0.7}},
{G = {B = 0.2}{out = 0.8}},
...

S - is a start node (S = 1), out - is a way out of the graph.

I want to trace the graph and know how much percentage each node has.
In instance, A = 0.4*S (S = 1), C = 0.7A + 0.1D , D = 0.3A + 0.7B

I thought it is possible to do it with recursion(DFS for directed graphs, in particular Tarjan's alg.), but while there are cycles I do not think it helps. Another solution is to solve a system of linear equations.
I do not know what is better that would work, and maybe there are some solutions exist for this kind of tasks.
This example is just an example, but I should consider that I have like appr. 2000 nodes (and who know how many cycles).

How would you do it?

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

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

发布评论

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

评论(2

离旧人 2024-11-07 22:33:26

求解线性方程似乎是一个非常好的方法。

您可以尝试使用高斯消除。我非常确定您可以在网络上找到已经编写的 Java 代码来为您完成此操作。

Solving linear equations seems to be a very good approach.

You can try to use Gaussian Elimination. I am pretty sure you can just find already written Java code to do it for you, on the web.

似最初 2024-11-07 22:33:26

注意:对于循环图,求解一个线性方程组不会给出概率。它为您提供了预期的多样性。

好的,问题给出了一个图G,对于每个节点来计算该节点被访问的概率。这是一个精确的算法。

  1. 计算图的强连通分量 (SCC)。
  2. 对于每个 SCC C,对于 C 中每个可能的起始节点 v,通过求解线性方程组计算 (a) 离开的弧的分布C 和 (b) C 中每个节点被访问的概率。我知道实现(b)的最好方法是考虑一个节点成对的产品图。该对的第一个元素是 C 中的一个节点。第二个元素是 C 中已访问过的节点的子集。弧继承自C。求解一些线性方程,找出这个新图中最后一个节点的分布。
  3. G 的顶点上准备一个新图 H,当 v< 时,弧从 vw /em> 和 w 位于 G 的不同组件中,并且存在从 vw 的路径。该弧的概率在步骤 2(a) 中确定。
  4. 解决 H 上的非循环问题。
  5. 对于每个节点,计算步骤 2(b) 中概率的加权和。

该算法在图的大小方面基本上呈线性关系,但在 SCC 的大小方面呈指数分布。我不确定你的周期是什么样的。

Note: for cyclic graphs, solving one system of linear equations does not give you the probabilities. It gives you the expected multiplicity.

Okay, the problem is given a graph G, for each node to compute the probability with which that node is visited. Here's an exact algorithm.

  1. Compute the strongly connected components (SCCs) of the graph.
  2. For each SCC C, for each possible starting node v in C, compute via solving systems of linear equations (a) the distribution of arcs leaving C and (b) the probability with which each node in C is visited. The best way I know to achieve (b) is to consider a product graph whose nodes are pairs. The first element of the pair is a node in C. The second element is a subset of nodes in C that have been visited. Arcs are inherited from C. Solve some linear equations to find out the distribution of last nodes in this new graph.
  3. Prepare a new graph H on the vertices of G with arcs from v to w when v and w are in different components of G and there's a path from v to w. The probability of this arc is as determined in Step 2(a).
  4. Solve the acyclic problem on H.
  5. For each node, compute the weighted sum of probabilities from Step 2(b).

This algorithm is basically linear in the size of the graph but exponential in the size of the SCCs. I'm not sure what your cycles look like.

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