Mathematica 8 中的多重图

发布于 2024-10-27 16:29:39 字数 1104 浏览 3 评论 0原文

我刚刚花了几个小时尝试转换使用 Mathematica 7 的 GraphPlot一些旧代码 /code> 使用新的 Mathematica 8 图形函数。这似乎是明智的,因为新的图形绘制更好,并且它具有诸如 AdjacencyMatrixKirchhoffMatrix 内置。

问题是我无法弄清楚如何让具有多个边的图在 Mma 8 中工作。

我用作规范的费曼图示例是双环真空图

GraphPlot[{1 -> 2, 1 -> 2, 1 -> 2}, MultiedgeStyle -> .5, 
          DirectedEdges -> True, VertexCoordinateRules -> {{-1, 0}, {1, 0}}]

双环真空日落图

尝试在 Mma 8 中制作类似的图

Graph[{DirectedEdge[1, 2], DirectedEdge[1, 2], DirectedEdge[1, 2]}, 
      VertexCoordinates -> {{-1, 0}, {1, 0}}]

会产生错误message

Graph::supp: Mixed graphs and multigraphs are not supported. >>

如何使用 Mathematica 8 的 Graph[] 对象构建(并使用)类似的图形?

编辑:这个问题在 Mathematica 9 中仍然存在

I just spent a couple of hours trying to convert some old code that uses Mathematica 7's GraphPlot to use the new Mathematica 8 Graph functions. It seemed sensible since the new graph drawing is much nicer and it has things like AdjacencyMatrix and KirchhoffMatrix built in.

The problem is that I can not figure out how to get graphs with multiple edges to work in Mma 8.

The Feynman graph that I use as my canonical example is the two-loop vacuum graph

GraphPlot[{1 -> 2, 1 -> 2, 1 -> 2}, MultiedgeStyle -> .5, 
          DirectedEdges -> True, VertexCoordinateRules -> {{-1, 0}, {1, 0}}]

two-loop vacuum sunset graph

Trying to make the similar graph in Mma 8

Graph[{DirectedEdge[1, 2], DirectedEdge[1, 2], DirectedEdge[1, 2]}, 
      VertexCoordinates -> {{-1, 0}, {1, 0}}]

yields the error message

Graph::supp: Mixed graphs and multigraphs are not supported. >>

How can I construct (and work with) a similar graph using Mathematica 8's Graph[] objects?

Edit: This problem still exists in Mathematica 9

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

永言不败 2024-11-03 16:29:39

我经历了类似的过程,尝试使用 Graph 来处理所有事情,发现它并不能取代 Combinatorica 和 GraphPlot。 Graph 的最佳用途是将其用作存储顶点+边+坐标的容器类型。

例如,Combinatorica 教程的“算法图论”中的大多数函数不适用于新的 Graph 对象。当我与 WRI 开发人员讨论 Graph 项目时,我的理解是为 Graph 提供所有 Combinatorica 功能并不是优先考虑的事情,因为设计目标是提供以与算法无关的方式解决任务的方法。例如,您可能有方法为新的 Graph 对象查找顶点覆盖和图形着色,但对于 Brelaz 着色和贪婪顶点覆盖等算法特定任务,您可能始终必须遵循 Combinatorica 。

除了多图之外,某些图形布局不适用于 Graph 对象。您无法修复某些顶点坐标并让自动布局完成其余的工作。此外,LayeredGraphPlot 的布局不可用,有时优于 GraphLayeredDrawing

充分利用这三个世界的方法是使用 Graph 对象作为图形存储的主要工具,并为 GraphPlot、Combinatorica 和 Combinatorica 制作包装器。接受 Graph 对象的 code>GraphUtilities 函数

一些用例:

  • 您需要 CombinatoricaGraphUtilities 中的一些算法 --制作一个包装器someAlgorithm,它接受Graph对象,将其转换为边列表或Combinatorica图(GraphUtilities'ToCombinatoricaGraph很有帮助),运行算法,将其转换回 Graph 对象,注意从原始图形对象中设置正确的 GraphStyle 和 VertexCoordinates 。由于存在冲突,请确保 CombinatoricaGraphUtilities 不在上下文路径上,我 使用 $Pre 进行操作

  • 您需要一些自定义图表,例如 这里,或者多边图——制作一个包装函数 someGraphPlot 接受 Graph 对象,将其转换为正确的表示,然后使用 GraphPlot 或创建一个带有自定义顶点的临时 Graph 对象/用于这一图的边缘形状。请注意,您可以使用 SetProperty 将属性附加到边缘,这样您就可以以这种方式将多重图存储在 Graph 中。

  • 您想要使用 GraphPlot 布局之一并将坐标存储在 Graph 中 - 使用类似 此处GraphPlot布局中获取顶点坐标,并将其存储在Graph<中使用 VertexCoordinates

    的 /code> 对象

,这是一个 笔记本 演示这些用例和其他一些用例

I went through a similar process of trying to use Graph for everything, and found that it it does not replace Combinatorica and GraphPlot. The best use for Graph is to use it as a container type to store vertices + edges + coordinates.

For example, most of the functions from "Algorithmic Graph Theory" of Combinatorica tutorial are not available for new Graph objects. When I talked with a WRI developer on Graph project, my understanding was providing all of Combinatorica functions for Graph is not a priority because the design goal is to provide methods that solve tasks in algorithmic agnostic way. For instance, you may have method to find vertex cover and graph coloring for new Graph object, but for algorithmic specific tasks like Brelaz coloring and Greedy Vertex Cover, you may always have to defer to Combinatorica.

In addition to multi-graphs, some graph layouts are not available for Graph objects. You can not fix some vertex coordinates and let automatic layout do the rest. Also, layout of LayeredGraphPlot is not available and is sometimes preferred over Graph's LayeredDrawing.

The way to get the best of 3 worlds is to use Graph objects as main vehicle for graph storage and make wrappers for GraphPlot, Combinatorica and GraphUtilities functions that accept Graph objects

Some use cases:

  • You need some algorithm from Combinatorica or GraphUtilities -- make a wrapper someAlgorithm that takes Graph object, converts it to list of edges or Combinatorica graph (GraphUtilities'ToCombinatoricaGraph is helpful), runs the algorithm, converts it back to Graph object, taking care to set correct GraphStyle and VertexCoordinates from the original graph object. Because of conflicts, make sure Combinatorica and GraphUtilities are not on context path, I do it using $Pre

  • You need some custom graph plot like here, or the multi-edge graph -- make a wrapper function someGraphPlot that accepts Graph object, converts it to correct representation, then uses GraphPlot or perhaps creates a temporary Graph object with custom vertex/edge shapes for the purpose of this one plot. Note that you can attach properties to edges using SetProperty so you can store your multigraphs in Graph that way.

  • You want to use one of GraphPlot layouts and store coordinates in Graph -- use function like here to get vertex coordinates from GraphPlot layout, and store them in Graph object using VertexCoordinates

Here's a notebook demonstrating these use cases and a few others

独木成林 2024-11-03 16:29:39

GraphPlot 函数在 mma 8 中仍然有效。Combinatorica

的函数也不支持多重图。在邻接矩阵中实现起来也相当困难。也许使用EdgeWeight可以在计算中发挥作用?

对于绘制多个链接,我可以想象“EdgeShapeFunction”可能会帮助您。

ef[pts_List, e_] :=
 Block[{g1 = 
    Insert[pts, (pts[[1]] + pts[[-1]])/
      2 + ({x, y}/5 /. 
        Solve[{Norm[{x, y}] == 1, (pts[[1]] - pts[[-1]]).{x, y} == 
            0}, {x, y}][[1]]), Round[(Length[pts] + 1)/2]],
   g2 = Insert[
     pts, (pts[[1]] + pts[[-1]])/
      2 + (-{x, y}/5 /. 
        Solve[{Norm[{x, y}] == 1, (pts[[1]] - pts[[-1]]).{x, y} == 
            0}, {x, y}][[1]]), Round[(Length[pts] + 1)/2]]}, {Arrow[
    BSplineCurve[g1]], Arrow[BSplineCurve[g2]], Arrow[pts]}]

Graph[{1 \[DirectedEdge] 2, 2 \[DirectedEdge] 3, 3 \[DirectedEdge] 1},
  EdgeShapeFunction -> ef]

在此处输入图像描述

或对于选定的边缘:

Graph[{1 \[DirectedEdge] 2, 2 \[DirectedEdge] 3, 3 \[DirectedEdge] 1},
  EdgeShapeFunction -> {3 \[DirectedEdge] 1 -> ef}]

在此处输入图像描述

可以轻松地对函数 ef 进行参数化以获取要绘制的边数。

The GraphPlot function still works in mma 8.

Multigraphs weren't supported in Combinatorica's functions either. Pretty difficult to implement in an adjecency matrix too. Perhaps working with EdgeWeight may work in calculations?

For drawing multiple links I can imagine that 'EdgeShapeFunction' may help you.

ef[pts_List, e_] :=
 Block[{g1 = 
    Insert[pts, (pts[[1]] + pts[[-1]])/
      2 + ({x, y}/5 /. 
        Solve[{Norm[{x, y}] == 1, (pts[[1]] - pts[[-1]]).{x, y} == 
            0}, {x, y}][[1]]), Round[(Length[pts] + 1)/2]],
   g2 = Insert[
     pts, (pts[[1]] + pts[[-1]])/
      2 + (-{x, y}/5 /. 
        Solve[{Norm[{x, y}] == 1, (pts[[1]] - pts[[-1]]).{x, y} == 
            0}, {x, y}][[1]]), Round[(Length[pts] + 1)/2]]}, {Arrow[
    BSplineCurve[g1]], Arrow[BSplineCurve[g2]], Arrow[pts]}]

Graph[{1 \[DirectedEdge] 2, 2 \[DirectedEdge] 3, 3 \[DirectedEdge] 1},
  EdgeShapeFunction -> ef]

enter image description here

or for selected edges :

Graph[{1 \[DirectedEdge] 2, 2 \[DirectedEdge] 3, 3 \[DirectedEdge] 1},
  EdgeShapeFunction -> {3 \[DirectedEdge] 1 -> ef}]

enter image description here

The function ef can be easily parametrized for the number of edges to draw.

鸩远一方 2024-11-03 16:29:39

我猜这些尚未得到支持:

In[201]:= AdjacencyGraph[{{0, 3}, {0, 0}}]

During evaluation of In[201]:= Graph::supp: Mixed graphs and multigraphs are not supported. >>

Out[201]= AdjacencyGraph[{{0, 3}, {0, 0}}]

尽管这可能不是您希望得到的答案。

These are not yet supported, I guess:

In[201]:= AdjacencyGraph[{{0, 3}, {0, 0}}]

During evaluation of In[201]:= Graph::supp: Mixed graphs and multigraphs are not supported. >>

Out[201]= AdjacencyGraph[{{0, 3}, {0, 0}}]

although this might not be the answer you hope to get.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文