Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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
1  
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
add comment

3 Answers 3

up vote 3 down vote accepted
 NeighborhoodGraph[g, "a", Options[g]]

gives

enter image description here

share|improve this answer
    
Your answer is way better than mine. –  David Carraher Dec 5 '12 at 21:45
add comment

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}]

neighborhood


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"]

subgraph


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
add comment

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. –  David Carraher Dec 4 '12 at 0:24
add comment

Your Answer

 
discard

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.