开发者

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

doesn't show s开发者_开发技巧pecified graph in Mathematica Graph Theory package 'Combinatorica'.


An important thing to note is that Mathematica has two more or less independent graphing frameworks: the one included in the Combinatorica package and the GraphUtilities package.

I my limited experience, the Combinatorica package is quite a big machine to swing into action and you really have to grok the framework to use it. GraphUtilities on the other hand is quite intuitive, has more customization options for graphics and more of a Mathematica feel to it.

To plot your example graph, all you need is

Needs["GraphUtilities`"]
GraphPlot[{e1 -> e2, e1 -> e3}, VertexLabeling -> True, DirectedEdges -> True]


If you want a generic undirected graph with 3 vertices with edges between vertex 1 and vertex 2 and between vertex 1 and vertex 3, the following will do that:

Needs["Combinatorica`"]
ShowGraph@AddEdges[EmptyGraph[3], {{1, 2}, {1, 3}}]

For much more on generating, displaying, and analyzing graphs, jump down to "Representing Graphs" in the Mathematica's Combinatorica Tutorial.


Ok, thanks all. Let me pack up all these discussions. First of all as Janus mentioned, there are two ways to draw graphs in Mathematica. Firstly, Combinatorica way, which comes as an add in. Secondly, GraphPlot way which is a native Mathematica implementation to draw graphs. GraphPlot employs Spring layout to draw graphs. As dreeves said we use ShowGraph[] to draw graphs in Combinatorica way.

To understand why the mentioned code doesn't work we have to understand ShowGraph[] command very well. Mathematica 7 defines,

ShowGraph[g] displays the graph g.

Now, what is graph?

Graph[e , v, opts] represents a graph object where e is the list of edges annotated with graphics options, v is a list of vertices annotated with graphics options, and opts is a set of global graph options"

The problem is Combinatorica doesn't seem to work exactly the above defined way. Let's examine... suppose you've created a Combinatorica graph named g. Now g[[0]] would print Graph, g[[1]] would print list of edges, g[[2]] would print opts. Where is list of vertices??

As you can see above, you can manually create g[[1]], and g[[2]] and feed it to Graph. Let's create it:

e = {{1,2}, {{1,3}}}
opts = {{{0,0}}, {{-5,5}}, {{5,5}}}
g = Graph[e, opts]

This will create a graph with vertices 1, 2 and 3. Now, how can you label them? In Combinatorica, you do this using VertexLabel option of ShowGraph[]. If you use VertexLabel->True it will only show default labels, that is 1, 2 and 3 here. To use your own labels do the following:

ShowGraph[g, VertexLabel->{a,b,c}]

Interesting, right? :).

Now to the exact answer of my question. The problem of the mentioned code is due to the use of literal values instead of integers. Combinatorica only excepts integer values as edge or vertex list.

Remember, although it is possible it is only useful to create small graphs. For larger graphs you should use default graph generators to create your graph. Like, for a random graph use:

g = Random[10, 0.23]

Will create a graph with 10 vertices, with edge probability of 0.23.

Waka, waka,
this time for Combinatorica!


ShowGraph[
 Graph[
 {{{1, 2}}, {{2, 3}}, {{3, 1}}}, 
 {{{0, 0}, VertexLabel -> v1}, 
  {{2, 5}, VertexLabel -> v2}, 
  {{5, 5}, VertexLabel -> v3}}
 ]
]

I think the above is what you were looking for.

The way Combinatorica creates the Graph object, seems very cumbersome. I would think it easier to create an intermediary function, that would yield the Graph object, with similar syntax to GraphPlot. That way you would not have to explicitly write out the vertex coordinates, nor use the cumbersome {{x,y},opts} notation which becomes {{x,y}} when you have no opts to specify.

So, you could have a function such as:

myGraphPlot[{{e1,e2},{e1,e3}},{e1,e2,e3}]

yielding the code at the top.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜