简体   繁体   English

使用负负权重的Bellman-Ford追踪最长路径

[英]Tracing the longest path using Bellman-Ford with negative edge weights

I'm currently finding the longest path in a directed acyclic positive-weighted graph by negating all edge weights and running Bellman-Ford algorithm. 我目前正在通过排除所有边缘权重并运行Bellman-Ford算法,找到有向无环正加权图中的最长路径。 This is working great. 这很好。

However, I'd like to print the trace of which nodes/edges were used. 但是,我想打印出使用了哪些节点/边的轨迹。 How can I do that? 我怎样才能做到这一点?

The program takes as input the number of nodes, a source, destination and edge weight. 该程序将节点数,源,目标和边缘权重作为输入。 Input halts on -1 -1 -1 . -1 -1 -1上停止输入。 My code is as follows: 我的代码如下:

import java.util.Arrays;
import java.util.Vector;
import java.util.Scanner;

public class BellmanFord {
    public static int INF = Integer.MAX_VALUE;

    // this class represents an edge between two nodes
    static class Edge {
        int source; // source node
        int destination; // destination node
        int weight; // weight of the edge
        public Edge() {}; // default constructor
        public Edge(int s, int d, int w) { source = s; destination = d; weight = (w*(-1)); }
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int inputgood = 1;
        int tail;
        int head;
        int weight;
        int count = -1;
        Vector<Edge> edges = new Vector<Edge>(); // data structure to hold graph
        int nnodes = input.nextInt();
        while (inputgood == 1) {
            tail = input.nextInt();
            head = input.nextInt();
            weight = input.nextInt();

            if (tail != -1) {
                edges.add(new Edge(tail, head, weight));
                count++;
            }
            if (tail == -1)
                inputgood = 0;
        }
        int start = edges.get(0).source;
        bellmanFord(edges, nnodes, start);
    }

    public static void bellmanFord(Vector<Edge> edges, int nnodes, int source) {
        // the 'distance' array contains the distances from the main source to all other nodes
        int[] distance = new int[nnodes];
        // at the start - all distances are initiated to infinity
        Arrays.fill(distance, INF);
        // the distance from the main source to itself is 0
        distance[source] = 0;
        // in the next loop we run the relaxation 'nnodes' times to ensure that
        // we have found new distances for ALL nodes
        for (int i = 0; i < nnodes; ++i)
            // relax every edge in 'edges'
            for (int j = 0; j < edges.size(); ++j) {
                // analyze the current edge (SOURCE == edges.get(j).source, DESTINATION == edges.get(j).destination):
                // if the distance to the SOURCE node is equal to INF then there's no shorter path from our main source to DESTINATION through SOURCE
                if (distance[edges.get(j).source] == INF) continue;
                // newDistance represents the distance from our main source to DESTINATION through SOURCE (i.e. using current edge - 'edges.get(j)')
                int newDistance = distance[edges.get(j).source] + edges.get(j).weight;
                // if the newDistance is less than previous longest distance from our main source to DESTINATION
                // then record that new longest distance from the main source to DESTINATION
                if (newDistance < distance[edges.get(j).destination])
                    distance[edges.get(j).destination] = newDistance;
            }
        // next loop analyzes the graph for cycles
        for (int i = 0; i < edges.size(); ++i)
            // 'if (distance[edges.get(i).source] != INF)' means:
            // "
            //    if the distance from the main source node to the DESTINATION node is equal to infinity then there's no path between them
            // "
            // 'if (distance[edges.get(i).destination] > distance[edges.get(i).source] + edges.get(i).weight)' says that there's a negative edge weight cycle in the graph
            if (distance[edges.get(i).source] != INF && distance[edges.get(i).destination] > distance[edges.get(i).source] + edges.get(i).weight) {
                System.out.println("Cycles detected!");
                return;
            }
        // this loop outputs the distances from the main source node to all other nodes of the graph
        for (int i = 0; i < distance.length; ++i)
            if (distance[i] == INF)
                System.out.println("There's no path between " + source + " and " + i);
            else
                System.out.println("The Longest distance between nodes " + source + " and " + i + " is " + distance[i]);
    }
}

You need to slightly modify what you do in the Bellman Ford implementation: 您需要稍微修改在Bellman Ford实现中的操作:

...
int[] lastNode = new int[nnodes];
lastNode[source] = source;
for (int i = 0; i < nnodes; ++i)
    for (int j = 0; j < edges.size(); ++j) {
        if (distance[edges.get(j).source] == INF) continue;
        int newDistance = distance[edges.get(j).source] + edges.get(j).weight;
        if (newDistance < distance[edges.get(j).destination])
        {
            distance[edges.get(j).destination] = newDistance;
            lastNode[edges.get(j).destination] = edges.get(j).source;
        }
    }

Printing individual paths then becomes: 打印单个路径将变为:

static void printPath(int source, int end, int[] lastNodes)
{
    if(source!=end)
        printPath(source, lastNodes[end], lastNodes);
    System.out.print(end+" ");
}

Which prints the path in order from source node to end node. 它将按顺序打印从源节点到结束节点的路径。

The common solution for graph algorithms is to maintain parent[edge] -> edge mapping. 图算法的常见解决方案是维护parent[edge] -> edge mapping。 For edge e the value of parent[e] is the node from which we traverse to e when we create a path optimal in some way. 对于边缘eparent[e]的值是当我们以某种方式创建最佳路径时从其遍历到e的节点。

The array is usually updated in the same way you update index in algorithm to find the largest element in the array, ie within if condition when you compare fitness of candidate path with that of current path. 通常以与更新算法索引以查找数组中最大元素相同的方式来更新数组,即在将候选路径的适用性与当前路径的适用性进行比较时处于if条件内。

In your case, it's here: 就您而言,它在这里:

if (newDistance < distance[edges.get(j).destination]) {
   distance[edges.get(j).destination] = newDistance;
   parent[edges.get(j).destination] = edges.get(j).source;
}

After you comupted parent mapping, you may take destination node and traverse it back, recursively building an array [dest, parent[dest], parent[parent[dest]], ... source . 转换parent映射后,您可以获取目标节点并将其遍历,递归地构建一个数组[dest, parent[dest], parent[parent[dest]], ... source

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

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