【问题标题】:How to make a recursive function for find all Euler path in a graph?如何创建一个递归函数来查找图中的所有欧拉路径?
【发布时间】:2014-08-22 08:07:27
【问题描述】:

我正在尝试在图中查找所有欧拉路径。为此,我使用基于此的 java 代码:http://www.sanfoundry.com/java-program-implement-euler-circuit-problem/(此示例仅找到一个欧拉路径)。

基本上,我对 PrintEulerUtil 方法(如下)进行了一些更改,但这给算法带来了一些问题,我找不到可行的解决方案。

代码如下:

public void printEulerTourUtil(int vertex, int[][] adjacencyMatrix, String trail) {

        // variable that stores (in every recursive call) the values of the adj matrix
        int[][] localAdjacencyMatrix = new int[this.numberOfNodes + 1][this.numberOfNodes + 1];
        // verifies if there is some edge unvisited. if not, then the euler path is in variable "trail"
        int verificationSum = 0;

        // copy values of variable, not only reference
        for (int i = 0; i <= numberOfNodes; i++) {
            for (int j = 0; j <= numberOfNodes; j++) {
                localAdjacencyMatrix[i][j] = adjacencyMatrix[i][j];
                verificationSum += localAdjacencyMatrix[i][j];
            }
        }

        Integer destination = 1;

        // if verificationSum != 0, then, at least one edge is in the adj matrix
        if (verificationSum != 0) {
           // test for every destination possible if is valid (isValidNextEdge) and if has connection between the actual vertex and the destination.
            for (destination = 1; destination <= numberOfNodes; destination++) {
                if (localAdjacencyMatrix[vertex][destination] == 1 && isValidNextEdge(vertex, destination, localAdjacencyMatrix)) {
                    // remove the edge for the next recursion call (and not loop the program)
                    removeEdge(vertex, destination, localAdjacencyMatrix);
                    trail = trail.concat(destination.toString());
                    // recursive call 
                    printEulerTourUtil(destination, localAdjacencyMatrix, trail);
                }
            }
        } else {
            System.out.println("Euler path: " + trail);
        }
    }

问题是:当递归调用返回,并且目的地增加时,图(邻接矩阵)会发生一些变化,无法找到新的(下一个)欧拉路径。举个例子就更容易了,所以:

如您所见,例如,在三者的第二层中,当destination 等于4 时,边1-2 和1-3 已经被之前的递归调用移除。然后,图表与开始的不一样......这使得在第一个之后无法找到欧拉路径(因为图表不正确)。

有什么想法吗?如果有人想要我的整个代码,请问。任何帮助都会非常有用。非常感谢,抱歉帖子的大小。

【问题讨论】:

    标签: java algorithm recursion graph


    【解决方案1】:

    你已经发现问题了!

    当递归调用返回,目的地递增时,图(邻接矩阵)会发生一些变化

    在递归函数开始时,您当前正在获取已传递给您的adjacencyMatrix 的副本。但是,当您遍历可能的目标边缘时,您会损坏这个本地副本。正如您所观察到的,在沿 first 目标边缘的递归调用返回后,您的 localAdjacencyMatrix 已经更改 - 将其传递给 的递归调用不再正确第二个目标边缘。

    要解决您的问题,您需要更好地跟踪 in 传递给您的邻接矩阵,以及您传递的多个 不同 邻接矩阵。

    我没有检查这个,但我想说你需要:

    • // test for every destination possible if is valid (isValidNextEdge) and if has connection between the actual vertex and the destination 之后更改测试,使其适用于 adjacencyMatrix,即传入的不变矩阵

    • 复制将值从adjacencyMatrix 复制到localAdjacencyMatrix 的代码到// remove the edge for the next recursion call (and not loop the program) 之前。这将确保当您删除一条边时,您是从输入邻接矩阵的副本中删除的

    【讨论】:

    • 感谢您的回答!实际上,当我写这篇文章时,我不确定我的结论:) 从昨天开始我想了很多,终于找到了一个正确的递归方法来解决这个问题。你的回答对我帮助很大!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 2017-08-27
    • 1970-01-01
    • 2013-03-14
    • 1970-01-01
    相关资源
    最近更新 更多