3
$\begingroup$

I would like to actually change the vertices in a grid graph instead of simply changing the label. Ultimately, I want the vertices to be points on a plane.

So if given a simple grid graph

g = GridGraph[{2,2},VertexLabels -> "Name"]

How can I change the vertex 1 to be {0,0}, 2 to be {0,1}, 3 to be {1,0}, and 4 to be {1,1}, so that

VertexList[g] = {{0,0},{0,1},{1,0},{1,1}}?
$\endgroup$
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – Michael E2 Sep 27 '15 at 20:41
3
$\begingroup$

You cand use VertexReplace to replace each vertex with its coordinates or any other values.

g = GridGraph[{2, 3}, VertexLabels -> "Name"];
VertexReplace[g, Thread[VertexList[g] -> GraphEmbedding[g]]]

enter image description here

$\endgroup$
1
$\begingroup$
ClearAll[vReplace]
vReplace  = VertexReplace[g, v_ :> Floor @ GraphEmbedding[g][[v]] - 1]  (* or *)
vReplace =  VertexReplace[#, v_ :> 
  QuotientRemainder[v - 1, First["Dimension" /. Last[GraphLayout /. Options[#]]]]] &;

Examples:

{r, c} = {2, 2};
g = GridGraph[{r, c}, VertexLabels -> "Name", ImagePadding -> 30, ImageSize -> 300];
Row[{g, vReplace @ g}] 

enter image description here

With {r, c} = {3, 5} we get

enter image description here

And with {r, c} = {5, 3} we get

enter image description here

Update: Building the grid graph from scratch:

gridGraph[{r_, c_}, o : OptionsPattern[]] := 
 Module[{v = QuotientRemainder[# - 1, r] & /@ Range[r c], e}, 
  e = If[ManhattanDistance @@ # == 1, UndirectedEdge @@ #, ## &[]] & /@ Subsets[v, {2}];
  Graph[v, v, VertexCoordinates -> Thread[v-> v], o]]

Example:

gridGraph[{5, 6}, VertexLabels -> "Name", ImagePadding -> 25, ImageSize -> 500]

enter image description here

$\endgroup$
0
$\begingroup$
Graph[{
  Labeled[1, "{0,0}"], 
  Labeled[2, "{0,1}"], 
  Labeled[3, "{1,0}"], 
  Labeled[4, "{1,1}"]}, 
  {1 <-> 2, 1 <-> 3, 3 <-> 4, 2 <-> 4},
 VertexCoordinates -> {{0, 0}, {1, 0}, {0, 1}, {1, 1}}]

enter image description here

$\endgroup$
0
$\begingroup$

Update: Use NearestNeighborGraph with exact coordinates.

gridGraph[dims_, opt : OptionsPattern[]] := 
 NearestNeighborGraph[Tuples[Range /@ dims], opt]

gridGraph[{3, 2}, VertexLabels -> "Name"]

enter image description here


Old answer:

Assuming that you don't just want to change the label that is displayed, but actually change the vertex name, you can do this:

Look at in which order GridGraph numbers vertices, generate the corresponding desired names with Tuple, and finally replace the integers with the desired names.

In[]:= g = GridGraph[{2, 3}, VertexLabels -> "Name"]

In[]:= names = Tuples[{Range[3], Range[2]}]
Out[]= {{1, 1}, {1, 2}, {2, 1}, {2, 2}, {3, 1}, {3, 2}}

In[]:= rules = Thread[Range@Length[names] -> names]
Out[]= {1 -> {1, 1}, 2 -> {1, 2}, 3 -> {2, 1}, 4 -> {2, 2}, 5 -> {3, 1}, 6 -> {3, 2}}

In[]:= Graph[
 VertexList[g] /. rules, EdgeList[g] /. rules,
 VertexLabels -> "Name"
 ]
$\endgroup$

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.