使用具有命名顶点的边创建Mathematica / Combinatorica图

时间:2010-08-06 02:47:25

标签: graph-theory wolfram-mathematica

如何从具有命名顶点的边创建Mathematica图? EG:

http://pastebin.com/Se1Nhe3M

我已经尝试了上述和几种变体,但Combinatorica从未 非常接受它。显然,Graph []想要坐标 我希望Combinatorica能够找到自己的位置。

AddVertex到EmptyGraph [0](或其他)也失败了。

GraphUtilities不是一个选项,因为我想做相当复杂的事情 我的图表分析。

这似乎是一个简单的问题。 Graphviz可以轻松创建图表 有命名顶点的边缘,所以我确定Mathematica也可以吗?

我读过:

ShowGraph[ { {e1,e2}, {e1, e3} }, {e1,e2,e3} ]; // what is the problem here?

但它似乎对我的具体案例没有帮助。

5 个答案:

答案 0 :(得分:2)

如果您有Mathematica 7,请尝试使用内置GraphPlot

GraphPlot[{"Conga" -> "Egypt", "Egypt" -> "Conga", 
  "Conga" -> "Sarah Desert", "Sarah Desert" -> "Conga", 
  "Egypt" -> "Europe", "Europe" -> "Egypt", "Egypt" -> "Arabia", 
  "Arabia" -> "Egypt", "Egypt" -> "Sarah Desert", 
  "Sarah Desert" -> "Egypt", "UK" -> "Europe", "Europe" -> "UK", 
  "UK" -> "Iceland", "Iceland" -> "UK", "UK" -> "Greenland", 
  "Greenland" -> "UK", "Europe" -> "Arabia", "Arabia" -> "Europe", 
  "Europe" -> "Germany", "Germany" -> "Europe", "Europe" -> "Iceland",
   "Iceland" -> "Europe", "Europe" -> "Sarah Desert", 
  "Sarah Desert" -> "Europe", "Germany" -> "Russia", 
  "Russia" -> "Germany", "Germany" -> "Arabia", "Arabia" -> "Germany",
   "Germany" -> "Iceland", "Iceland" -> "Germany", 
  "Germany" -> "Irakistan", "Irakistan" -> "Germany", 
  "Austr(al)ia" -> "China", "China" -> "Austr(al)ia", 
  "Arabia" -> "Irakistan", "Irakistan" -> "Arabia", 
  "Canada" -> "More Russia", "More Russia" -> "Canada", 
  "Canada" -> "USA", "USA" -> "Canada", 
  "Canada" -> "Andy's Mountains", "Andy's Mountains" -> "Canada", 
  "More Russia" -> "Russia", "Russia" -> "More Russia", 
  "More Russia" -> "China", "China" -> "More Russia", 
  "More Russia" -> "Irakistan", "Irakistan" -> "More Russia", 
  "China" -> "Irakistan", "Irakistan" -> "China", 
  "USA" -> "Greenland", "Greenland" -> "USA", 
  "USA" -> "Andy's Mountains", "Andy's Mountains" -> "USA", 
  "Brazil" -> "Sarah Desert", "Sarah Desert" -> "Brazil", 
  "Brazil" -> "Andy's Mountains", "Andy's Mountains" -> "Brazil", 
  "Russia" -> "Irakistan", "Irakistan" -> "Russia"}, 
 DirectedEdges -> True]

这将为您提供以下内容,例如:

alt text http://img638.imageshack.us/img638/7803/plotm.png

布局,顶点和边缘标签和样式等有很多选项。

答案 1 :(得分:2)

如果粘滞点是表示为字符串的节点,而重型图分析函数希望它们是整数,则可以考虑将字符串映射为整数,反之亦然:

nodes = DeleteDuplicates@Flatten[graph /. Rule -> List]

{"Conga", "Egypt", "Sarah Desert", "Europe", "Arabia", "UK", "Iceland", 
 "Greenland", "Germany", "Russia", "Irakistan", "Austr(al)ia", "China", "Canada",
 "More Russia", "USA", "Andy's Mountains", "Brazil"}

现在你有了节点列表。接下来是与整数的映射:

each[{i_, s_}, Transpose[{Range@Length@nodes, nodes}],
  numify[s] = i;
  namify[i] = s]

您现在可以轻松地将节点转换为整数或从整数转换:

numify["Europe"]

4

namify[4]

"Europe"

像这样转换整个图:

graph /. s_String -> numify[s]

请注意,each是以下实用程序函数,在此处讨论:ForEach loop in Mathematica

SetAttributes[each, HoldAll];               (* each[pattern, list, body]      *)
each[pat_, lst_, bod_] := ReleaseHold[      (*  converts pattern to body for  *)
  Hold[Cases[Evaluate@lst, pat:>bod];]];    (*   each element of list.        *)

答案 2 :(得分:1)

由于你专门询问了Combinatorica,因为我总是犹豫是否开始试图打包一个包的内部细节,所以这可能会对你有所帮助:

使用<<<<<<<或如果你的版本需要这个需要。然后使用您的数据:

edges = {{“Conga” - > “埃及”},{“埃及” - > “Conga”},{“Conga” - > “莎拉沙漠”},   {“莎拉沙漠” - > “Conga”},{“埃及” - > “Europe”},{“Europe” - > “埃及”},   {“埃及” - > “Arabia”},{“Arabia” - > “埃及”},{“埃及” - > “莎拉沙漠”},   {“莎拉沙漠” - > “Egypt”},{“UK” - > “Europe”},{“Europe” - > “英国”},   {“UK” - > “Iceland”},{“Iceland” - > “UK”},{“UK” - > “格陵兰”},   {“格陵兰岛” - > “UK”},{“Europe” - > “Arabia”},{“Arabia” - > “欧洲”},   {“欧洲” - > “德国”},{“德国” - > “Europe”},{“Europe” - > “冰岛”},   {“冰岛” - > “Europe”},{“Europe” - > “莎拉沙漠”},   {“莎拉沙漠” - > “Europe”},{“Germany” - > “俄罗斯”},{“俄罗斯” - > “德国”},   {“德国” - > “Arabia”},{“Arabia” - > “德国”},{“德国” - > “冰岛”},   {“冰岛” - > “德国”},{“德国” - > “伊拉克斯坦”},{“伊拉克斯坦” - > “德国”},   {“Austr(al)ia” - > “中国”},{“中国” - > “Austr(al)ia”},{“Arabia” - > “Irakistan”},   {“伊拉克斯坦” - > “Arabia”},{“Canada” - > “更多俄罗斯”},{“更多俄罗斯” - > “加拿大”},   {“加拿大” - > “USA”},{“USA” - > “Canada”},{“Canada” - > “安迪的山脉”},   {“Andy's Mountains” - > “Canada”},{“More Russia” - > “俄国”},   {“俄罗斯” - > “更多俄罗斯”},{“更多俄罗斯” - > “中国”},{“中国” - > “更多俄罗斯”},   {“更多俄罗斯” - > “伊拉克斯坦”},{“伊拉克斯坦” - > “更多俄罗斯”},   {“中国” - > “伊拉克斯坦”},{“伊拉克斯坦” - > “China”},{“USA” - > “格陵兰”},   {“格陵兰岛” - > “USA”},{“USA” - > “安迪的山脉”},   {“Andy's Mountains” - > “USA”},{“Brazil” - > “莎拉沙漠”},   {“莎拉沙漠” - > “Brazil”},{“Brazil” - > “安迪的山脉”},   {“Andy's Mountains” - > “Brazil”},{“Russia” - > “Irakistan”},   {“伊拉克斯坦” - > “俄罗斯”}} /规则[from_,对_] - > {从,到};

labels = {“Canada”,“USA”,“Greenland”,“Brazil”,“Andy's Mountains”,“UK”,“Iceland”,“Germany”,“Europe”,“Russia”,“More Russia “,”伊拉克斯坦“,”阿拉伯“,”中国“,”澳大利亚“,”埃及“,”莎拉沙漠“,”康加“};

numberededges =分区[拼合[边缘/ .Thread [规则[标签,范围[长度[标签]]]]],2];

ShowGraph [AddEdges [EmptyGraph [长度[标签],numberededges]   VertexLabel->标签,&PlotRange- GT;全部]

现在,如果自动格式化没有破坏这一点,那么我想你可能已经准备好了。

我没有将边缘指向此,我想你可能想要这样做。 希望这足以让你开始。

所有这些都是基于Pemmaraju和Skiena在“Computational Discrete Mathematics:Combinatorics and Graph Theory with Mathematica”中来回寻呼几分钟。我相信任何试图使用Combinatorica但没有在他们面前的人只是坚果。我希望我们可以说服他们推出一个新版本,修复一些拼写错误,并让他们更容易让那些不了解Combinatorica的人能够使用它来开始。

答案 3 :(得分:0)

您可以将边缘转换为邻接矩阵,然后使用Combinatorica的FromAdjacencyMatrix函数。

(* Converts lists of edges into adjacency matrix, saving "nodename->column #" mapping into global variable nodeMap *)
graph2mat[edges_] := Module[{nodes, mat, n},
   nodes = Sequence @@@ edges // Union // Sort;
   nodeMap = (# -> (Position[nodes, #] // Flatten // First)) & /@ 
     nodes;
   mat = (({#1, #2} -> 1) & @@@ (edges /. nodeMap)) // SparseArray // 
     Normal;
   n = Max[Length[#] & /@ {mat, Transpose[mat]}];
   PadRight[mat, {n, n}]
   ];

g = FromAdjacencyMatrix[graph2mat[{"a" -> "b", "b" -> "a"}]];
reverseNodeMap = Reverse /@ nodeMap;
MaximumClique[g] /. reverseNodeMap

答案 4 :(得分:0)

Combinatorica Graph []对象不易处理。你试过www.sagenb.org吗? Sage的Graph对象非常酷。