4
$\begingroup$

I extract path subgraphs $P_4$ of an undirected graph $G$. And want to list the VertexNames of the subgraphs in the same order as they are connected.

Here is an example:

g = Graph[{6 <-> 7, 1 <-> 6, 3 <-> 1, 3 <-> 2, 5 <-> 4, 3 <-> 4}, 
VertexLabels -> "Name", ImagePadding -> 10];
h = Subgraph[g, {1, 3, 6, 7}, VertexLabels -> "Name", ImagePadding -> 10]
HighlightGraph[g, h]
VertexList[h]

GraphImage

VertexList[h] always gives the vertex names in the order specified in line 5. Is it possible to extract the vertex names in the order of connectivity? The given example should give {3,1,6,7} or the reverse instead of {1,3,6,7}.

$\endgroup$
0

2 Answers 2

2
$\begingroup$

The following will only work with Subgraph being a PathGraph.

g = Graph[{6 <-> 7, 1 <-> 6, 3 <-> 1, 3 <-> 2, 5 <-> 4, 3 <-> 4}];
h = Subgraph[g, {1, 3, 6, 7}];
SortBy[
  Thread[{VertexList@h, Chop@AbsoluteOptions[h, VertexCoordinates][[2]]}], 
  Last][[All, 1]]
{3,1,6,7}
$\endgroup$
3
  • $\begingroup$ This doesn't work. Replace {1, 3, 6, 7} with {1, 6, 3, 7} and you get the wrong answer. The answer should be invariant with respect to the specification of the subgraph vertices $\endgroup$
    – paw
    Jun 22, 2014 at 15:08
  • 1
    $\begingroup$ @user4539 Should be better now :) $\endgroup$
    – Öskå
    Jun 22, 2014 at 15:45
  • $\begingroup$ pretty clever, Thanks! $\endgroup$
    – paw
    Jun 22, 2014 at 16:12
4
$\begingroup$

Update:

sortedVF[g_?UndirectedGraphQ] := 
 Module[{c1 = First@GraphPeriphery[g], c2 = Last@GraphPeriphery[g]}, 
   SortBy[VertexList[g], {GraphDistance[g, c1, #] &, - GraphDistance[g, c2, #] &}]];

sortedVF[h]
(* {3, 1, 6, 7} *)

DeleteDuplicates[sortedVF /@ (Subgraph[g, #] & /@ Permutations[{1, 3, 6, 7}])]
(* {{3, 1, 6, 7}, {7, 6, 1, 3}} *)

Another example:

g2 = PetersenGraph[6, 2, VertexLabels -> "Name", ImagePadding -> 10];
h2 = Subgraph[g2, {1, 3, 5, 11, 7, 8}];
Row[{g2, HighlightGraph[g2, h2]}]

enter image description here

DeleteDuplicates[sortedVF /@ (Subgraph[g2, #] & /@ Permutations[{1, 3, 5, 11, 7, 8}])]
(* {{11, 5, 3, 1, 7, 8}, {8, 7, 1, 3, 5, 11}}  *)

Note: the original post below skips nodes that are not on the shortest-path.

The following function from the docs GraphDiameter>>Applications seems to do what is needed:

findDiameterPath[g_?UndirectedGraphQ] := Module[{d = GraphDistanceMatrix[g], u, v, pos}, 
   pos = First@Position[d, Max[d]];
   {u, v} = Part[VertexList[g], pos];
   PathGraph@FindShortestPath[g, u, v]];

g = Graph[{6 \[UndirectedEdge] 7, 1 \[UndirectedEdge] 6, 
    3 \[UndirectedEdge] 1, 3 \[UndirectedEdge] 2, 
    5 \[UndirectedEdge] 4, 3 \[UndirectedEdge] 4}, 
      VertexLabels -> "Name", ImagePadding -> 10];
h = Subgraph[g, {1, 3, 6, 7}, VertexLabels -> "Name", ImagePadding -> 10];

VertexList[findDiameterPath[Subgraph[g, h]]]
(* {3, 1, 6, 7} *)

DeleteDuplicates[VertexList[findDiameterPath[#]] & /@ (Subgraph[g, #] & /@ 
    Permutations[{1, 3, 6, 7}])] 
(*  {{3, 1, 6, 7}, {7, 6, 1, 3}} *)
$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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