Mathematica处理图论算法的一些问答

http://mathematica.stackexchange.com/questions/15346/combinatorica-package-and-graph-theoretical-issues?rq=1


I've searched for a solution to my problem a couple of days, and I didn't find one. Hope you guys can help me.

I'm currently trying to solve some graph-theoretical problems. One of them is to find the maximum flow throughout a graph. I've managed to draw the graph in Mathematica, and would now like to use the built-in function NetworkFlow. To do this I need to load the package Combinatorica, but that gives me problems with "shadowed" functions. I've tried to refer to them by their fully qualified names, e.g., Combinatorica`NetworkFlow, etc., but I still can't get it to work.

I've pasted in some code which I hope will make it easier to see what I'm doing wrong:

Graph[{Kø -> Ro, Kø -> Pu, Kø -> Pa, Ro -> Ha, Ro -> Be, Pu -> Ha, 
  Pu -> Be, Pa -> Ha, Ha -> Be, Ha -> St, Ha -> Br, Ha -> Li, 
  Be -> St, Be -> Br, Be -> Li, St -> Br, St -> Li, St -> Par, 
  Br -> Li, Br -> Par, Li -> Par}, 
 EdgeWeight -> {"2", "11", "8", "3", "5", "14", "11", "10", "12", 
   "12", "10", "10", "4", "4", "12", "4", "12", "11", "23", "16", 
   "10"}, VertexLabels -> "Name", ImagePadding -> 10, 
 GraphLayout -> "SpringEmbedding"]

Mathematica graphics

Which gives me a nice graph. Below is the "real" problem:

Needs["Combinatorica`"]
Graph[{Kø -> Ro, Kø -> Pu, Kø -> Pa, Ro -> Ha, Ro -> Be, Pu -> Ha, 
  Pu -> Be, Pa -> Ha, Ha -> Be, Ha -> St, Ha -> Br, Ha -> Li, 
  Be -> St, Be -> Br, Be -> Li, St -> Br, St -> Li, St -> Par, 
  Br -> Li, Br -> Par, Li -> Par}, 
 EdgeWeight -> {"2", "11", "8", "3", "5", "14", "11", "10", "12", 
   "12", "10", "10", "4", "4", "12", "4", "12", "11", "23", "16", 
   "10"}, VertexLabels -> "Name", ImagePadding -> 10, 
 GraphLayout -> "SpringEmbedding"]
Combinatorica`NetworkFlow[%, Kø, Par]

During evaluation of In[28]:= General::compat: Combinatorica Graph and Permutations functionality has been superseded by preloaded functionaliy. The package now being loaded may conflict with this. Please see the Compatibility Guide for details.

Out[29]= Graph({Kø->Ro,Kø->Pu,Kø->Pa,Ro->Ha,Ro->Be,Pu->Ha,Pu->Be,Pa->Ha,Ha->Be,Ha->St,Ha->Br,Ha->Li,Be->St,Be->Br,Be->Li,St->Br,St->Li,St->Par,Br->Li,Br->Par,Li->Par},EdgeWeight->{2,11,8,3,5,14,11,10,12,12,10,10,4,4,12,4,12,11,23,16,10},VertexLabels->Name,ImagePadding->10,GraphLayout->SpringEmbedding)

Out[30]= NetworkFlow(Graph({Kø->Ro,Kø->Pu,Kø->Pa,Ro->Ha,Ro->Be,Pu->Ha,Pu->Be,Pa->Ha,Ha->Be,Ha->St,Ha->Br,Ha->Li,Be->St,Be->Br,Be->Li,St->Br,St->Li,St->Par,Br->Li,Br->Par,Li->Par},EdgeWeight->{2,11,8,3,5,14,11,10,12,12,10,10,4,4,12,4,12,11,23,16,10},VertexLabels->Name,ImagePadding->10,GraphLayout->SpringEmbedding),Kø,Par)
share edit flag
 
 
  
I am curious - what is background of this problem? - could you enlighten us please?  –    Vitaliy Kaurov   Nov 28 '12 at 22:25
1 
  
Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, byanswering questions in your area of expertise. 2) Read the FAQs! 3) When you see good Q&A, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. ALSO, remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign`  –    Vitaliy Kaurov   Nov 28 '12 at 23:20
 
 

2 Answers

up vote 10 down vote accepted

You do not need the package if you have Mathematica 9. Something very important - you cannot use strings on EdgeWeight - you need numerical values there. So corrected your code is:

g = Graph[{Kø -> Ro, Kø -> Pu, Kø -> Pa, Ro -> Ha, Ro -> Be, Pu -> Ha,
    Pu -> Be, Pa -> Ha, Ha -> Be, Ha -> St, Ha -> Br, Ha -> Li, 
   Be -> St, Be -> Br, Be -> Li, St -> Br, St -> Li, St -> Par, 
   Br -> Li, Br -> Par, Li -> Par}, 
  EdgeWeight -> {2, 11, 8, 3, 5, 14, 11, 10, 12, 12, 10, 10, 4, 4, 12,
     4, 12, 11, 23, 16, 10}, ImagePadding -> 10, 
  GraphLayout -> "SpringEmbedding", GraphStyle -> "SmallNetwork", 
  EdgeLabels -> "EdgeWeight", VertexSize -> .2]

enter image description here

Edge labels are edge weights. For the flow we do:

OF = FindMaximumFlow[g, Kø, Par, "OptimumFlowData", EdgeCapacity -> EdgeWeight];

OF["FlowValue"]

21

SetProperty[OF["FlowGraph"], {EdgeLabelStyle -> Directive[Red, 13],
 EdgeLabels -> (# -> Row[{OF[#], "/", 
 PropertyValue[{g, #}, EdgeWeight]}] & /@ EdgeList[g])}]

enter image description here

Now you have "edge flow / edge capacity". Edge opacity is also edge flow. There is a free trial version of Mathematica 9 if you do not have it yet.

share edit flag
 
 
  
I'm having some trouble installing the new version 9.0, but your answer was perfect - exactly what I'm looking for, thanks. I need to use it for a new class I'm taking. My teacher want me to calculate it by hand, so I need something to check my answers!  –    Marco Dal Farra   Nov 29 '12 at 8:29  
 
  
@MarcoDalFarra I am curious - what is background of this problem? - could you explain please? Some sort of chemical processes?  –    Vitaliy Kaurov   Nov 29 '12 at 8:35  
 
  
The Vertexes represent major cities in europe, eg. Kø = København, danish for Copenhagen, Par = Paris, etc. So I need to figure out how to move max passengers from Copenhagen to Paris given a railroad capacity between each city.  –    Marco Dal Farra   Nov 29 '12 at 8:45
 

Precede Graph with the System` context and it should work out fine. Please note that you can't use system 8 Graphs as input in Combinatorica graph functions. They have to be converted first, for instance by converting the v8 Graph to an adjacency matrix, which can be converted to a Combinatorica graph.

ShowGraph[SpringEmbedding@FromAdjacencyMatrix[ AdjacencyMatrix@g // Normal]]

Mathematica graphics

You lose the labels in the process.

share edit flag
 

Your Answer


















  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值