如何以编程方式访问 Mathematica 8 中“Graph”对象的信息?

发布于 2024-11-15 11:41:38 字数 1252 浏览 2 评论 0原文

我正在尝试访问 Mathematica 8 中 Graph 对象内的信息。由于某种原因,Part 命令似乎不起作用。

myGraph 是我想要访问的对象。

下面的第一行显示 myGraph。其他人的作用是检查它。

myGraph

myGraph // FullForm  
myGraph // InputForm  
myGraph // OutputForm    
myGraph[[1]]
myGraph[[2]]  

myGraph

为什么 myGraph[[1]] 不返回 List[1, 3,4,2,5]? [我检查到级别 2,以防 Graph 被某些不可见的包装器包装。 Level[myGraph,1],仅返回 {}FullForm[myGraph][[1]] 返回图表本身的图片。

我一定是忽略了一些显而易见的事情。


编辑

这是我用来生成图表的代码。其中大部分与当前的问题无关。 但至少您将使用我正在使用的相同代码。

ClearAll[edges, compatibleQ, adjacentCourses, g];
edges[w_, b_] := 
 Most /@ Accumulate /@ 
   Flatten[Permutations[#] & /@ IntegerPartitions[w, All, b], 1]

compatibleQ[j_, k_, edg_] := 
 If[Intersection[edg[[j]], edg[[k]]] == {}, {j, k}, False]

adjacentCourses[edg_] := 
 Module[{len = Length[edg]},
  Cases[Flatten[Table[compatibleQ[j, k, edg], {j, len}, {k, j, len}], 
    1], {v_, w_} :>  v \[UndirectedEdge] w]]

myGraph =  Graph[adjacentCourses[edges[9, {2, 3}]], VertexLabels -> "Name", 
ImagePadding -> 10]

I'm trying to access information within a Graph object in Mathematica 8. For some reason, the Part command does not seem to work.

myGraph is the object I want to gain access to.

The first line below displays myGraph. The others serve to inspect it.

myGraph

myGraph // FullForm  
myGraph // InputForm  
myGraph // OutputForm    
myGraph[[1]]
myGraph[[2]]  

myGraph

Why doesn't myGraph[[1]] return List[1,3,4,2,5] ?
[I checked to level 2 just in case Graph were wrapped by some invisible wrapper. Level[myGraph,1], simply returns {}. And FullForm[myGraph][[1]] returns a picture of the graph itself.

I must be overlooking something obvious.


Edit

Here's the code I used to produce the graph. Most of it is irrelevant to the issue at hand.
But at least you'll be working with the same code I am using.

ClearAll[edges, compatibleQ, adjacentCourses, g];
edges[w_, b_] := 
 Most /@ Accumulate /@ 
   Flatten[Permutations[#] & /@ IntegerPartitions[w, All, b], 1]

compatibleQ[j_, k_, edg_] := 
 If[Intersection[edg[[j]], edg[[k]]] == {}, {j, k}, False]

adjacentCourses[edg_] := 
 Module[{len = Length[edg]},
  Cases[Flatten[Table[compatibleQ[j, k, edg], {j, len}, {k, j, len}], 
    1], {v_, w_} :>  v \[UndirectedEdge] w]]

myGraph =  Graph[adjacentCourses[edges[9, {2, 3}]], VertexLabels -> "Name", 
ImagePadding -> 10]

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

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

发布评论

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

评论(3

夏夜暖风 2024-11-22 11:41:38

不管外表如何,Mathematica 8 中引入的图形对象并不是“正常”的符号表达式。以下SO问题详细讨论了这个问题和其他此类问题,包括提取部分图形定义的方法:

Mathematica 8.0中的新图形

Despite appearances, the graph objects introduced in Mathematica 8 are not "normal" symbolic expressions. The following SO question discusses this and other such problems in detail, including ways to extract parts of the graph definition:

new Graph in Mathematica 8.0

奶气 2024-11-22 11:41:38

事实证明,我的问题有一些简单的答案。

Graph 的文档包含多种从 Graph 对象检索信息的方法。 (对我没有检查感到羞耻。)在我看来,最有用的命令是:

VertexList[]
VertexCount[]
EdgeList[]
EdgeCount[]
EdgeRules[] 
VertexIndex[]
EdgeIndex[]
PropertyValue[]

通常在操作图形对象之后,我们需要从图形对象中获取信息。
我可以轻松找到我构建的图表中包含哪些信息,但如果输出派生图表(例如从 NeighborhoodGraph 中),则无需探测我就无法知道其属性。

感谢 @dbJohn 提供 Wolfram 文档的链接。

特别感谢 @WReach 在之前的 SO 讨论中提供了他的评论的链接关于Graph对象。

Turns out that there were some straightforward answers to my question.

The documentation for Graph contains several ways of retrieving information from a Graph object. (Shame on me for not checking.) The most useful commands, in my view, are:

VertexList[]
VertexCount[]
EdgeList[]
EdgeCount[]
EdgeRules[] 
VertexIndex[]
EdgeIndex[]
PropertyValue[]

We need to get information OUT of the graph object typically after we've manipulated it.
I can easily find what information went into a Graph I build but if a derivative Graph is output, e.g. from NeighborhoodGraph, I won't know its properties without probing.

Thanks to @dbJohn for the link to the Wolfram documentation.

Special thanks to @WReach for the link to his comments in a prior SO discussion about the Graph object.

空心↖ 2024-11-22 11:41:38

如何以编程方式访问有关 Mathematica 8 中“Graph”对象的信息?

似乎有许多新函数可用于获取有关图表的信息,如下所示:
https://reference.wolfram.com/language/guide/GraphConstructionAndRepresentation.html

在您的示例中,您似乎希望图表的顶点列表按正确的顺序排列。函数 VertexList 似乎可以做到这一点。
这是属性和属性的屏幕截图文档中的关系部分:

在此处输入图像描述

How can I programmatically access information about a 'Graph` object in Mathematica 8?

There seems to be a number of new functions for getting bits of information about graphs as listed here:
https://reference.wolfram.com/language/guide/GraphConstructionAndRepresentation.html.

In your example you seem to want the list of vertices of the graph in the correct order. The function VertexList seems to do this.
Here is a screenshot from Properties & Relations section in doc:

enter image description here

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