Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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!

share|improve this question
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). – Sjoerd C. de Vries Dec 3 '12 at 23:51
Thanks everyone for the answers. – andrew Dec 4 '12 at 16:21
up vote 5 down vote accepted
 NeighborhoodGraph[g, "a", Options[g]]


enter image description here

share|improve this answer
Your answer is way better than mine. – 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.

share|improve this answer

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

share|improve this answer
+1 I shamelessly built my second example on your fine approach. – DavidC Dec 4 '12 at 0:24

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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