1
$\begingroup$

I first generate some random graphs, and then the following code can filter graphs with maximum degree 8.

glist= Table[RandomGraph[{10, 25}], {i, 1, 10}];
Select[glist, Max@VertexDegree[#] == 8 &]

But now I want to see the positions of the filtering graphs in glist. It seems 'Position' is ok, but I lost in many times.

Position[glist, Max@VertexDegree[#] == 8 &] (*always nothing*)
Position[glist, x_ /; Max@VertexDegree@x == 8]

The second did produce results, but it also gave an error warning.

enter image description here

$\endgroup$
3
  • 1
    $\begingroup$ Position[glist, _?(Max@VertexDegree@# == 8 &)] . You are almost there. $\endgroup$
    – Syed
    Commented Jan 16 at 9:51
  • 1
    $\begingroup$ The second form should be: Position[glist, x_Graph /; Max@VertexDegree@x == 8]. It is because it is expecting a Graph object and x is a general pattern object. $\endgroup$
    – Syed
    Commented Jan 16 at 10:11
  • $\begingroup$ Nice. You can write an answer. $\endgroup$
    – licheng
    Commented Jan 16 at 10:24

1 Answer 1

1
$\begingroup$

The first form requires PatternTest as Position requires a pattern for matching.

As opposed to this the Select command requires a function that operates on elements to return a True or False value.

Parentheses are important.

Position[glist, _Graph?(Max@VertexDegree@# == 8 &)]

The second attemp using Condition requires a Graph object for which the VertexDegree function can be executed without warning. The correct form is:

Position[glist, x_Graph /; Max@VertexDegree@x == 8]

Result:

Depending on SeedRandom[], the positions of Graph objects matching the stated criteria about the VertexDegree will be returned. E.g.,

{{5}, {9}}
$\endgroup$
2
  • 1
    $\begingroup$ Your first one also shows a warning on my computer (Mathematica 13.3). Position[glist, _Graph?(Max@VertexDegree@# == 8 &)] is well. $\endgroup$
    – licheng
    Commented Jan 16 at 11:15
  • 1
    $\begingroup$ Thanks for the accept and the heads up. I have updated the answer. $\endgroup$
    – Syed
    Commented Jan 16 at 11:22

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.