doesn't show specified graph in Mathematica Graph Theory package 'Combinatorica'.
views:
150answers:
3If 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.
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]
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!