# Preserving labels when using graph functions

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!

• 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). Commented Dec 3, 2012 at 23:51
• Thanks everyone for the answers. Commented Dec 4, 2012 at 16:21

 NeighborhoodGraph[g, "a", Options[g]]


gives

• Your answer is way better than mine. Commented Dec 5, 2012 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"]]


• +1 I shamelessly built my second example on your fine approach. Commented Dec 4, 2012 at 0:24

I ran across this question some ten years after it was asked and answered. I want to add this "answer" from a functional programming perspective, since I believe it may be useful to others.

The following function has been a work-horse in many applications of my own work which focuses on the diagrammatic expression of dynamics as they occur in tissue regeneration such as blood formation (hematopoiesis):

hsPreserveGraphOptions[g_Graph]:=Graph[#, Options[g]] &


This convenience function is part of the (not yet released) package QWolF (Quantal Wolfram Language).

With respect to the example

hsPreserveGraphOptions[g]@NeighborhoodGraph[g, "a"]


and, as another example

hsPreserveGraphOptions[g]@IGRandomSpanningTree[g]


gives a random spanning tree via IGraphM by Szabolcs Horvat

respectively.

The function works nicely with "pipes" - which I am using extensively - such as in

NeighborhoodGraph[g, "a"] \[VerticalEllipsis] hsPreserveGraphOptions[g]


Here I use the [VerticalEllipsis] sign via an appropriate InputAlias as my pipe symbol to avoid confusion with the WL symbol for Alternatives[].