如何解有环的图
我正在尝试在 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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
求解线性方程似乎是一个非常好的方法。
您可以尝试使用高斯消除。我非常确定您可以在网络上找到已经编写的 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.
注意:对于循环图,求解一个线性方程组不会给出概率。它为您提供了预期的多样性。
好的,问题给出了一个图G,对于每个节点来计算该节点被访问的概率。这是一个精确的算法。
该算法在图的大小方面基本上呈线性关系,但在 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.
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.