Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
859 views
in Technique[技术] by (71.8m points)

new Graph in Mathematica 8.0

  1. Has anybody figured out a way to modify Graph objects in Mathematica 8? In particular, how to get the same functionality you get when you right click on the graph.

  2. Some of the new graph functions conflict with Combinatorica, is there a way to force Mathematica to use a built-in version of the function? In other words, is there a way to get access to built-in CompleteGraph after I do Needs["Combinatorica"] which imports Combinatorica version of CompleteGraph?

To clarify 1, Context Menu on Graph lets you change GraphStyle and GraphLayout, and I'd like to be able to change them programmatically. Here's one way I found to change GraphStyle of Graph object

g = GridGraph[{4, 4}];
BooleanGraph[Or, g, g, GraphStyle -> "DiagramBlack"]

However, that forgets options of the original graph like VertexCoordinates

Trying Brett's recipe on grid graph

g = GridGraph[{3, 2}, ImageSize -> Tiny]
coords = PropertyValue[{g, #}, VertexCoordinates] & /@ VertexList[g];
Graph[EdgeList[g], GraphStyle -> "BasicGold", 
 VertexCoordinates -> coords, ImageSize -> Tiny]


(source: yaroslavvb.com)

There seems to be a bug with how Mathematica handles Graph coordinates on graph operations. First line below permutes coordinates, second gives internal warning, probably related to coordinates. Using non-integer vertices and explicit coordinates for each vertex doesn't help. One solution is to store coordinates globally and have fixCoordinates function to reassign correct coordinates to Graph after modifications

VertexDelete[GridGraph[{3, 3}], 1]
NeighborhoodGraph[VertexDelete[GridGraph[{3, 3}], 1], 2]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The new Graph objects are atomic in Mathematica 8. Thus, like strings or images they do not have internal structure that can be manipulated in the normal fashion. What is particularly unusual is that the new objects have a FullForm that looks like it can be manipulated symbolically. But appearances can be deceiving -- not only is that representation inaccessible to pattern-matching, but it is not even a valid graph specification if you feed it back to Mathematica using copy-and-paste.

I found a couple of hacks that can be used to manipulate graph structure. The first tries to use the "official" channels to extract the properties of graphs:

adjustedGraph[g_, newOptions___] :=
  Graph[
    VertexList@g,
    EdgeList@g,
    newOptions,
    Sequence@@Table[p -> PropertyValue[g, p], {p, PropertyList[g]}]
  ]

You can use this function like this:

g = GridGraph[{4, 4}, GraphStyle -> "DiagramBlack", ImageSize -> Tiny]
adjustedGraph[g, GraphStyle -> "BasicGold"]

This function uses VertexList, EdgeList and PropertyValue to extract the graph properties. Unfortunately, some options are not recoverable by this means. For example, the Graphics option ImageSize will be lost using this method.

An even more heinous hack exploits the pseudo-symbolic representation of FullForm:

adjustedGraph2[g_, newOptions___] :=
  "Hold@" ~~ ToString[g, InputForm] //
  ToExpression //
  #[[1, 3]] & //
  Graph[VertexList@g, EdgeList@g, newOptions, Sequence @@ #] &

Despite its evil nature, this second function performs more satisfactorily as it appears to retain most graph options. I say "most", because I have not yet experimented with more esoteric options like wrappers, shape functions and graph properties assigned after the fact. There are no guarantees that this method will work unchanged as Wolfram changes the representation of graph objects (or even that it works for all possible graph definitions now).

There ought to be a way to achieve this without hacks. I still hold out hope that there is some function lurking out there that will give the complete symbolic representation of a graph object.

As for the symbol conflicts that arise after loading the Combinatorica package, you can still access the original symbols by explicitly specifying the package name, e.g. System`CompleteGraph. If you prefer to have the system symbols take precedence over the Combinatorica symbols, you could evaluate the following expression to change the package search order:

$ContextPath =
  $ContextPath /.
    {x___, c : "Combinatorica`", y___, s:"System`", z___} :> {x, y, s, c, z}

I note that Wolfram is effectively deprecating the Combinatorica package by issuing a scary warning message when you load the package.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...