找到配对,使权重之和最小化?

时间:2015-12-04 10:08:43

标签: java algorithm graph-theory shortest-path

在解决中国邮递员问题(路线检查问题)时,我们如何才能找到配对(在奇数顶点之间),以使权重之和最小化?

这是成功解决非欧拉图的中国邮递员问题的算法中最关键的一步。虽然它很容易在纸上实现,但我在Java中实现时遇到了困难。

我正在考虑如何找到所有可能的对,但是如果一个在所有奇数顶点上运行第一个循环而在所有其他可能的对上运行下一个循环。这只会给出一对,找到所有其他对,你需要另外两个循环,依此类推。这是相当奇怪的,因为人们将粗略地“循环”循环。有没有更好的方法来解决这个问题。

我已经阅读过Edmonds-Jonhson算法,但我不明白构建二分图背后的动机。我也读过中文邮递员问题:在奇度节点之间找到最佳连接,但作者没有解释如何实现暴力算法。 还有以下问题:我应该如何为中国邮递员问题生成分区/对?之前已经被Stack溢出的用户询问过,但是对帖子的回复给出了代码的python实现。我不熟悉python,我会请求任何社区成员用Java重写代码,或者如果可能的话解释算法。

谢谢。

2 个答案:

答案 0 :(得分:0)

经济递归

这些元组通常被称为边缘,不是吗?

你需要一个递归。

0. Create main stack of edge lists.
1. take all edges into a current edge list. Null the found edge stack.
2. take a next current edge for the current edge list and add it in the found edge stack.
3. Create the next edge list from the current edge list. push the current edge list into the main stack. Make next edge list current.
4. Clean current edge list from all adjacent to current edge and from current edge.
5. If the current edge list is not empty, loop to 2.
6. Remember the current state of found edge stack - it is the next result set of edges that you need.
7. Pop the the found edge stack into current edge. Pop the main stack into current edge list. If stacks are empty, return. Repeat until current edge has a next edge after it. 
8. loop to 2.

因此,您拥有所有可能的边缘集,并且您永远不需要检查“我是否在不同的顺序中看到了相同的集合?”

答案 1 :(得分:0)

当你绕过它时,它实际上相当简单。我只是在这里分享一些代码,希望它能帮助下一个人!

下面的函数返回所有有效的奇数顶点组合,然后需要检查最短的顶点组合。

GlobDebug=FALSE
"{BOPO}"BOPSRace1 ト "{BOPO}"BOP2.code "{BOPS}"NewResrc.r
    rez "{BOPS}"NewResrc.r -o "DataFrame:BOP2_ト:BOP2"

"{BOPO}"BOP2.code ト "{BOPO}"Globals.p.o カ
                    "{BOPO}"AsmCode.a.o カ
                    "{BOPO}"Init.p.o カ
                    "{BOPO}"Titles.p.o カ
                    "{BOPO}"Background.p.o カ
                    "{BOPO}"StdRoutines.p.o カ
                    "{BOPO}"History.p.o カ
                    "{BOPO}"PlanMove.p.o カ
                    "{BOPO}"CrisisU.p.o カ
                    "{BOPO}"Main.p.o
    link "{Libraries}"Interface.o カ
         "{Libraries}"Runtime.o カ
         "{PLibraries}"Paslib.o カ
         "{BOPO}"Globals.p.o カ
         "{BOPO}"AsmCode.a.o カ
         "{BOPO}"Init.p.o カ
         "{BOPO}"Titles.p.o カ
         "{BOPO}"Background.p.o カ
         "{BOPO}"StdRoutines.p.o カ
         "{BOPO}"History.p.o カ
         "{BOPO}"PlanMove.p.o カ
         "{BOPO}"CrisisU.p.o カ
         "{BOPO}"Main.p.o カ
         -o "{BOPO}"BOP2.code

"{BOPO}"AsmCode.a.o ト "{BOPS}"AsmCode.a      
    Asm -o "{BOPO}" "{BOPS}"AsmCode.a       

"{BOPO}"Globals.p.o ト "{BOPS}"Globals.p      
    Pascal -d DebugFlg={GlobDebug} -o "{BOPO}" "{BOPS}"Globals.p

"{BOPO}"Init.p.o ト "{BOPS}"Init.p "{BOPS}"Globals.p          
    Pascal -d DebugFlg={GlobDebug} -o "{BOPO}" "{BOPS}"Init.p

"{BOPO}"Titles.p.o ト "{BOPS}"Titles.p "{BOPS}"Globals.p          
    Pascal -d DebugFlg={GlobDebug} -o "{BOPO}" "{BOPS}"Titles.p

"{BOPO}"Background.p.o ト "{BOPS}"Background.p "{BOPS}"Globals.p          
    Pascal -d DebugFlg={GlobDebug} -o "{BOPO}" "{BOPS}"Background.p

"{BOPO}"StdRoutines.p.o ト "{BOPS}"StdRoutines.p "{BOPS}"Globals.p            
    Pascal -d DebugFlg={GlobDebug} -o "{BOPO}" "{BOPS}"StdRoutines.p

"{BOPO}"History.p.o ト "{BOPS}"History.p "{BOPS}"Globals.p            
    Pascal -d DebugFlg={GlobDebug} -o "{BOPO}" "{BOPS}"History.p

"{BOPO}"PlanMove.p.o ト "{BOPS}"PlanMove.p "{BOPS}"Globals.p          
    Pascal -d DebugFlg={GlobDebug} -o "{BOPO}" "{BOPS}"PlanMove.p

"{BOPO}"CrisisU.p.o ト "{BOPS}"CrisisU.p "{BOPS}"Globals.p            
    Pascal -d DebugFlg={GlobDebug} -o "{BOPO}" "{BOPS}"CrisisU.p

"{BOPO}"Main.p.o ト "{BOPS}"Main.p "{BOPS}"Globals.p 
    Pascal -d DebugFlg={GlobDebug} -o "{BOPO}" "{BOPS}"Main.p