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

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


Combinatorica package and graph-theoretical issues

up vote 12 down vote favorite
5

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)
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
 
 
start a bounty

2 Answers

active oldest votes
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]

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.

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
 
up vote 3 down vote

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.

share edit flag
 

Your Answer


















你可能感兴趣的:(学习学习,mathematica,算法,数学)