Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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]
like image 845
Yaroslav Bulatov Avatar asked Nov 29 '10 08:11

Yaroslav Bulatov


3 Answers

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.

like image 139
WReach Avatar answered Oct 24 '22 02:10

WReach


The following will preserve the vertex coordinates of the original graph.

g = CompleteGraph[5];
coords = PropertyValue[{g, #}, VertexCoordinates] & /@ VertexList[g];
Graph[VertexList[g], EdgeList[g], GraphStyle -> "BasicGold", 
 VertexCoordinates -> coords]

Mathematica graphics

I would think something similar could preserve other options as well, though I haven't tried it.

like image 21
Brett Champion Avatar answered Oct 24 '22 02:10

Brett Champion


For #2, you should be able use distinguish between the two using the context. Thus, System`CompleteGraph[5] creates a new V8 graph, while Combinatorica`CompleteGraph[5] creates an old Combinatorica graph.

I'm not sure exactly what you mean in #1, but you can convert the Graph to Graphics, then the contextual menu will appear as before. I'm not so sure that this is an improvement, though.

like image 44
Mark McClure Avatar answered Oct 24 '22 03:10

Mark McClure