3
$\begingroup$

I am trying to change the Head of b (a NeighborhoodGraph) from Graph into List but Apply seems to fail. I start with a list of connected vertices:

a={1\[UndirectedEdge]1,2\[UndirectedEdge]2,1\[UndirectedEdge]3,3\[UndirectedEdge]3,
4\[UndirectedEdge]4,3\[UndirectedEdge]4,5\[UndirectedEdge]5,6\[UndirectedEdge]6,
6\[UndirectedEdge]2,7\[UndirectedEdge]7,8\[UndirectedEdge]5,8\[UndirectedEdge]8,
9\[UndirectedEdge]9,10\[UndirectedEdge]10,10\[UndirectedEdge]4};

I calculate the neighborhood graph of node 3. Its Head is Graph:

b=NeighborhoodGraph[a//Graph,3]
b//Head
(* ==> Graph *)

I use Apply to change the Head to List:

c=Apply[List,b];

However, the Head of c is still Graph:

c//Head
(* ==> Graph *)

My real question is how to access the list of edges in a NeighborhoodGraph.

$\endgroup$
  • 7
    $\begingroup$ As Graph[] is an atomic object, you cannot use Apply[] to extract the edges; instead, look up EdgeList[]. $\endgroup$ – J. M.'s technical difficulties Jun 3 '13 at 18:23
  • 1
    $\begingroup$ Thanks! EdgeList seems to be very well hidden. I could not find it under See Also in Graph, NeighborhoodGraph SubGraph, nor in GraphConstruction, guide/GraphModifications... Which makes me especially grateful that you pointed it to me. $\endgroup$ – Themis Jun 3 '13 at 19:18
  • $\begingroup$ It can be found on the Graph Representation and Properties guide page, a link to which can be found on the bottom of the Graph help page in the "Related guides" section. So, it wasn't really that far away. $\endgroup$ – Sjoerd C. de Vries Jun 3 '13 at 21:35
6
$\begingroup$

Apply does not work on Graph objects because they are atomic (you can verify this using AtomQ).

You can use EdgeList to retrieve the edge list of any graph.


To get access to the compound representation of an atomic Graph, you can use the general techniques described here and here. I recommend using Carl Woll's Nucleus, which wraps this up into a nice easy-to-use function. Be warned that manipulating this representation of Graph takes you into undocumented territory. Therefore, try to avoid it if you can. Sometimes it's necessary though.

$\endgroup$
0
$\begingroup$

Update due to valid comment by Szabolcs. I create the Graph

$$BuckyBall = ResourceFunction["BuckyballGraph"][0, VertexCoordinates -> "Embedded"]

I disassemble $$BuckyBall:

    {$$BBVertexList, $$BBVertexCount, $$BBEdgeList, $$BBVertexCoordinates} =
         {$$BuckyBall // VertexList, $$BuckyBall // VertexCount,
$$BuckyBall // EdgeList, $$BuckyBall // GraphEmbedding}

Work on the vertex coordinates and then re-assemble as Graph3D:

Graph3D[$$BBVertexList,$$BBEdgeList,VertexCoordinates->$$BBVertexCoordinates]

Original post. Still valid, but undoubtedly a desperate hack.

$$BuckyBall is the name of a Graph

 $$BB = StringReplace[$$BuckyBall // InputForm // ToString, 
    "Graph[" -> "List[", 1] // ToExpression;

Any simplification is appreciated. The term , 1] is important, otherwise all "Graph" instances are replaced! Valid Graph expressions are created with

GraphicsRow[{Graph[$$BB[[1]], $$BB[[2]], $$BB[[3]]], 
  Graph[Sequence @@ $$BB], Graph @@ $$BB}]

I use this to extract the list of vertex coordinates, transform/manipulate them and replace the list of vertex coordinates in a copy of $$BB.

| improve this answer | |
$\endgroup$
  • $\begingroup$ This is a nasty hack that will no doubt cause trouble sooner or later. You can get vertex coordinates in multiple ways, e.g. using GraphEmbedding. You can set them using SetProperty. Using the IGraph/M package, you can easily transform them in one go. For example, invert the x coordinate: IGVertexMap[{-1,1} # &, VertexCoordinates, graph]. $\endgroup$ – Szabolcs Mar 2 at 14:08
  • $\begingroup$ If you want to get the compound representation of an atomic expression like Graph, you can use the method I showed here: mathematica.stackexchange.com/a/97332/12 Check Carl Woll's Nucleus function, based on the same concept: it makes the task easier. mathematica.stackexchange.com/a/157198/12 But please do not do such string replacements! They are not safe. $\endgroup$ – Szabolcs Mar 2 at 14:15
  • $\begingroup$ I am curious about what sorts of transformations you are doing on the vertex coordinates. Can it be done easily with IGVertexMap from IGraph/M? If not, I'd like to know about it so I could improve IGraph/M in the future. I will admit that internally IGraph/M does (unhappily) manipulate the compound expression of Graph. The way it does it (analogous to Nucleus) is safer than string manipulation, but not entirely safe, as Wolfram may change the representation they use any time without notice. In fact, it happened for version 12.1, for which I had to adapt IGraph/M a bit. $\endgroup$ – Szabolcs Mar 2 at 16:17
  • $\begingroup$ I am using the Graph as a starting point for the test of a simulation of the growth of organoids and spheroids, i.e. multi-cellular clusters. I currently convert the vertex coordinates to polar coordinates, vary them statistically to simulate growth and movement and convert them back to Cartesian coordinates for display. The edges define neighborhood etc. In the next step I will implement FE analyses. We are already doing this with other software. This another implementation to check some aspects and priors. $\endgroup$ – Ernst Stelzer Mar 4 at 12:45
  • $\begingroup$ It sounds like IGVertexMap from IGraph/M would be perfectly suitable for this, and would provide the most concise solution. BTW for generating points on spheres, you can use something like DiscretizeRegion[Sphere[], PrecisionGoal -> 1, MaxCellMeasure -> 0.1] Vary MaxCellMeasure to change the number of points. The PrecisionGoal -> 1 is just to allow very large cells. In practice, this also seems to make Goldberg polyhedron. If you need a Graph, you can use IGMeshGraph. But it sounds like you only need the points, so you can use MeshCoordinates instead. $\endgroup$ – Szabolcs Mar 4 at 16:41

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.