views:

150

answers:

3

doesn't show specified graph in Mathematica Graph Theory package 'Combinatorica'.

+3  A: 

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.

dreeves
Combinatorica Tutorial says,'A graph is defined as a set of vertices with a set of edges, where an edge is defined as a pair of vertices.' Let me upload a picture to make the point clear.
Sazzad
Can you tell me why the following doesn't work: g = EmptyGraph[3]; g = AddEdges[{{1, 2}, {1, 3}}]; ShowGraph[g];
Sazzad
+1 for pointing out EmptyGraph[]
belisarius
@Sazzad It doesn't work because the AddEdges syntax is wrong. Try ..g = EmptyGraph[3]; g = AddEdges[g, {{1, 2}, {1, 3}}];
belisarius
+4  A: 

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]
Janus
Doesn't work in Mathematica 5. Probably a new addition to Mathematica 7.
Sazzad
+1 for pointing out that there's one more graph plotting package: GraphUtilities. But it seems to be loaded automatically. No need for Needs["GraphUtilities`"]?
Max
+1  A: 

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!

Sazzad