简体   繁体   English

如何平衡图中的周期?

[英]How to balance cycles in a graph?

Consider this graph: 考虑以下图表:

在此处输入图片说明

The second image (with weights in parenthesis) is "balanced", ie each node sends the right amount of weight to the other nodes, and the end node has the same weight as the start node. 第二个图像(在括号中带有权重)是“平衡的”,即每个节点向其他节点发送正确的权重,并且结束节点的权重与开始节点的权重相同。

Now let's say my graph is empty (the edges know how much % weight to send to other nodes but there is no weight yet in the graph). 现在让我们说我的图是空的(边缘知道要发送到其他节点的重量百分比,但是图中还没有重量)。 If I put a weight of 20 on the start node, how can I make it so that the end graph will be balanced like shown in the second image? 如果将20的权重放在起始节点上,如何使它最终平衡,如第二幅图所示? It seems to me that I should recursively update the graph until the weight becomes constant in every node, but is it the best way to do it? 在我看来,我应该递归更新图,直到权重在每个节点中保持恒定为止,但这是最好的方法吗?

Edit: Here is the Java code related to this problem for the project I was working on, if it can ever help someone. 编辑:这是与我正在研究的项目有关的Java代码,如果它可以帮助某人。 I understand there is no information on some functions used in this code, but you should get the big picture. 我了解此代码中没有使用某些功能的信息,但您应该对此有所了解。

Note: many variable names are in French, here is a quick translation for some of them: 注意:许多变量名称都是法语的,下面是其中一些的快速翻译:
nb_stations: amount of nodes nb_stations:节点数
m_listeStations: list of all the nodes m_listeStations:所有节点的列表
ID_ARRIVEE: ID of the start node ("Start" in the graph) ID_ARRIVEE:起始节点的ID(图中的“开始”)
Reseau: the graph Reseau:图表
poids: weight 诗集:体重
arc: edge 弧线:边缘

// build the distribution matrix
public float[] buildDistribution() {
    int nb_stations = m_listeStations.size();
    float[] distribution = new float[nb_stations];
    Arrays.fill(distribution, 0);
    distribution[findIndexStation(findStation(Reseau.ID_ARRIVEE))] = findStation(Reseau.ID_ARRIVEE).getPoids();
    return distribution;
}

// build the transition matrix
public float[][] buildTransition() {
    int nb_stations = m_listeStations.size();
    float[][] transition = new float[nb_stations][nb_stations];
    for (float[] ligne : transition) {
        Arrays.fill(ligne, 0);
    }

    for (int i = 0; i < nb_stations; ++i) {
        List<Arc> arcsOut = m_listeStations.get(i).getArcsOut();
        for (int j = 0; j < arcsOut.size(); ++j) {
            transition[i][findIndexStation(arcsOut.get(j).getCible())] = arcsOut.get(j).getPoidsRelatif();
        }
    }

    int indexArrivee = findIndexStation(findStation(Reseau.ID_ARRIVEE));
    transition[indexArrivee][indexArrivee] = 1; // arrivée continuelle

    return transition;
}

// this function does one iteration of distribution*transition to converge toward the real weights
public float[] convergeDistribution(float[] in_distribution, float[][] in_transition) {
    int nb_stations = m_listeStations.size();
    float[] converge = new float[nb_stations];
    Arrays.fill(converge, 0);
    for (int i = 0; i < nb_stations; ++i) {
        for (int j = 0; j < nb_stations; ++j) {
            converge[i] += in_distribution[j] * in_transition[j][i];
        }
    }
    return converge;
}

Your graph is what is known as a Markov chain . 您的图就是所谓的马尔可夫链 What you are looking to find is the stationary distribution of the Markov chain. 您正在寻找的是马尔可夫链的平稳分布

If P is your transition matrix, the stationary distribution x is the solution to the equation x*P = x. 如果P是您的转移矩阵,则平稳分布x是方程x * P = x的解。

The rate of converge is discussed here . 这里讨论收敛速度。

Once you have the stationary distribution, you can solve the problem you describe very easily. 一旦有了固定分布,就可以很轻松地解决您描述的问题。 Just fix the weight on one node/edge and then all of the other node/edge weights are proportional to it, based on the stationary distribution and the transition matrix. 只需将权重固定在一个节点/边缘上,然后根据固定分布和过渡矩阵,所有其他节点/边缘的权重都与它成比例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM