# combining NeighborhoodGraph and RandomGraph

I have Combinatorica and GraphUtilities loaded in version 9 of Mathematica. If I then set:

G = RandomGraph[20,0.5];
G1 = NeighborhoodGraph[G,1];


I get the error message that NeighborhoodGraph requires a graph object at position 1. How do I convert G into a graph object?

I can get this process to work with named graphs in Mathematica using GraphData but am clearly missing something with random graphs.

-
You do not need either Combinatorica or GraphUtilities for this. In fact Combinatorica may conflict with built-in functions, so do not load it unless you know you need it. Use the built-in RandomGraph and NeighborhoodGraph instead (please check the documentation for the correct syntax for RandomGraph[{m,n}]) –  Szabolcs Mar 16 at 19:17
If you do need Combinatorica for some reason, I recommend this way of loading it and referring to function names together with their context. –  Szabolcs Mar 16 at 19:19
Thanks but when I delete Combinatorica and GraphUtilities I still get the same error message. Clive –  clive elphick Mar 16 at 19:32
I hope by "deleting" you mean "restarting the kernel and not loading them" :) The likely reason you get that message is that you are using an incorrect syntax for the built-in RandomGraph (which is different from CombinatoricaRandomGraph). Try g = RandomGraph[{10, 20}], then NeighborhoodGraph[g, 1]. –  Szabolcs Mar 16 at 19:34
To explain a bit more: The reason it did not work when you loaded Combinatorica is that Combinatorica uses a different graph representation then the one available by default in version 8 and 9 of Mathematica. Comnbinatorica also has a function called RandomGraph which will shadow the built-in RandomGraph. Combinatorica's version requires a different syntax and returns a different kind of graph representation. –  Szabolcs Mar 16 at 19:37
show 1 more comment

First, the solution I recommend: do not load either Combinatorica or GraphUtilities, and use the built-in RandomGraph function with the appropriate syntax:

g = RandomGraph[{10, 20}]
NeighborhoodGraph[g, 1]


The reason it doesn't work is because when you load Combinatorica, it creates a new RandomGraph function which is incompatible with the RandomGraph built into versions 8 and 9. The situation of graph-related functions is a bit messy at the moment, and there are three different graph representations that are used. There's the Combinatorica package's own graph representation (since about 1996 if I'm correct), there's the edge-list representation used by the GraphPlot and related functions (since around v5) and there's the new, extended graph functionality introduced in version 8 which uses atomic Graph[...] objects. Many of the new v8 graph functions conflict with Combinatorica in the sense that they will be shadowed by Combinatorica functions. Because of this it's better load load Combinatorica like this and refer to Combinatorica functions with their full names: RandomGraph will be the built-in one and CombinatoricaRandomGraph will be the one defined in Combinatorica.

What happened in your case that you used Combinantorica's RandomGraph which returns a graph object in a different format than what the new, built-on NeighborhoodGraph expects. It can be of course converted, but it's probably not worth the trouble since there's the built-in RandomGraph now.

The GraphUtilities package is for working with edge-list representations that GraphPlot` expects, and for converting these to the Combinatorica format. There should be no need for this package at all in version 9. (Combinatorica contains a few functions not yet available as built-ins but GraphUtilities doesn't.)

-