Mathematica处理图论算法的一些问答
来源:互联网 发布:虎扑推荐淘宝匡威店铺 编辑:程序博客网 时间:2024/06/06 14:20
http://mathematica.stackexchange.com/questions/15346/combinatorica-package-and-graph-theoretical-issues?rq=1
Combinatorica package and graph-theoretical issues
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"]
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)
2 Answers
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]
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])}]
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.
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]]
You lose the labels in the process.
- Mathematica处理图论算法的一些问答
- BW的一些问答
- mathematica的图像处理0--七夕特刊
- 关于jsplet的一些问答
- 关于citrix的一些问答
- 有关EventBus的一些问答
- 一些问答系统的资料
- Mathematica图像处理
- 关于mathematica中添加注释的一些方法
- 一些问答
- 一些问答
- 收集的一些C#的基础知识问答
- 关于“自动编程软件”的一些问答
- 一些关于Collection FrameWork的问答
- BW--收集到BW的一些问答
- BW--收集到BW的一些问答
- 关于电力方面的一些问答
- 对VPS一些常见问题的问答
- sed--文本编辑
- [C#]基于HttpWebRequest和HttpWebResponse的HttpHelper
- Cookie
- java的模板类
- SSH框架整合问题,java.lang.reflect.InvocationTargetException
- Mathematica处理图论算法的一些问答
- a Line Segment Detector
- DFT/DTFT
- 马云在京员工大会演讲
- MVC5开发前端框架AngularJS快速入门之Service
- 黑马程序员_java语言基础_常量变量运算符
- Dom4j框架的使用
- 马化腾“互联网+中国”峰会演讲
- Android选取相机、相册图片进行裁剪,并更新UI
Read the FAQs
! 3) When you see good Q&A, vote them up byclicking 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