I am finding that some functions that accept a graph and return a transformed graph lose all the vertex and edge labels of the graph being transformed. For example, when I define a graph like this:

g = Graph[{
      Property["a" -> "b", EdgeLabels -> "one"], 
      Property["b" -> "c", EdgeLabels -> "two"],
      Property["c" -> "d", EdgeLabels -> "three"],
      Property["a" -> "c", EdgeLabels -> "four"]},
      VertexSize -> Medium, VertexLabels -> Placed["Name", Center]]

The graph g has both edge and vertex labels. Now, I would like to create a neighborhood graph around vertex "a":

NeighborhoodGraph[g, "a"]

When I do this, I find that the new graph created does not have vertex or edge labels. I would like to be able to use these graph manipulation functions, but I need to keep the graph attributes. A graph with no labels is not very useful to me. Any help is appreciated!

  • 1
    $\begingroup$ Welcome to Mathematica.SE! Please consider registering your account so that any upvotes you get on this question are added to those you might get on future questions and answers. That way, over time you will be able to do more on the site (post graphics, edit things, etc). $\endgroup$ – Sjoerd C. de Vries Dec 3 '12 at 23:51
  • $\begingroup$ Thanks everyone for the answers. $\endgroup$ – andrew Dec 4 '12 at 16:21
 NeighborhoodGraph[g, "a", Options[g]]


enter image description here

  • $\begingroup$ Your answer is way better than mine. $\endgroup$ – DavidC Dec 5 '12 at 21:45

Method 1: PropertyValue

This is the standard way for manipulating graph properties:

NeighborhoodGraph[g, "a", 
 EdgeLabels -> PropertyValue[g, EdgeLabels], 
 VertexLabels -> PropertyValue[g, VertexLabels], 
 VertexSize -> PropertyValue[g, VertexSize]]

which can be simplified to:

NeighborhoodGraph[g, "a", 
(# -> PropertyValue[g, #]) & /@ {VertexLabels, EdgeLabels, VertexSize}]


Method 2: HighlightGraph (following Sjoerd's suggestion)

This takes Sjoerd's approach but hides the part of the graph that you do not want to see. I generally prefer it to the above method because it preserves the location of the vertices and the shape of the edges.

HighlightGraph[g, NeighborhoodGraph[g, "a"], GraphHighlightStyle -> "DehighlightHide"]


Method 3: AbsoluteOptions (Not recommended)

This works (usually):

NeighborhoodGraph[g, "a", AbsoluteOptions[g, {EdgeLabels,VertexLabels,VertexSize}]]

AbsoluteOptions behaves inconsistently and thus should probably be avoided.


Though this is probably not what you're looking for, HighLightGraph can be useful to mark subgraphs in the original graph. In your case:

HighlightGraph[g, NeighborhoodGraph[g, "a"]]

Mathematica graphics

  • $\begingroup$ +1 I shamelessly built my second example on your fine approach. $\endgroup$ – DavidC Dec 4 '12 at 0:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.