15
$\begingroup$

Bug introduced in version 8 or earlier and fixed in 10.0


I have created a notebook with two cells. This is the content of the first:

g = Graph[{1 \[UndirectedEdge] 2, 2 \[UndirectedEdge] 3, 1 \[UndirectedEdge] 3, 1 \[UndirectedEdge] 4, 4 \[UndirectedEdge] 5, 4 \[UndirectedEdge] 6}]

And this is the content of the second:

g
listDegree = VertexDegree[g]
vl = VertexList[g]
nodeMaxDegree = Pick[vl, listDegree, Max[VertexDegree[g]]][[1]]
aM = AdjacencyMatrix[g];
vLM = aM[[VertexIndex[g, nodeMaxDegree]]];
nN = Pick[vl, vLM, 0]

If I evaluate the second cell (after processing the first) for a second time:

  1. the first time no problem, the results are correct;
  2. the second time the vertex list of g is inexplicably wrong but the graph remain correct!!

I don't understand the cause because the graph g is never touched.

Thanks in advance

$\endgroup$
14
  • 1
    $\begingroup$ Interesting: FullForm[vl] gives List[1, System`Private`InternSequence[], System`Private`InternSequence[], System`Private`InternSequence[], 5, 6]. $\endgroup$ Commented Mar 27, 2014 at 18:34
  • 1
    $\begingroup$ @JacobAkkerboom, I expect that comes from Typeset`MakeBoxes[_, StandardForm] not having a rule for System'Private'InternSequence[] like it does for Sequence[] $\endgroup$ Commented Mar 27, 2014 at 20:04
  • 3
    $\begingroup$ I reported the issue to WRI. $\endgroup$
    – Adam Reith
    Commented Mar 27, 2014 at 20:16
  • 1
    $\begingroup$ @Jacob That is not a problem of Simon's Spelunk but the way how MakeBoxes definitions are returned and converted to boxformat: just try for example Attributes[LabeledSlider] = {}; FullDefinition@LabeledSlider and be prepared for LOTS of errors... $\endgroup$ Commented Mar 27, 2014 at 20:16
  • 1
    $\begingroup$ The bug seems to be no longer present in version 10! $\endgroup$ Commented Jul 9, 2014 at 21:10

2 Answers 2

10
$\begingroup$

This is a bug in Pick caused by SparseArray, has nothing to do with Graph. Minimal example (SparseArray object is the fullform version of your vLM):

x = {1, 2, 3, 4, 5, 6};
Pick[x, SparseArray[Automatic, {6}, 0, {1, {{0, 3}, {{2}, {3}, {4}}}, {1, 1, 1}}], 0];
FullForm@x
{1, System`Private`InternSequence[], System`Private`InternSequence[], 
    System`Private`InternSequence[], 5, 6}

As you can observe, the value of x gets updated though no assignment is done: those members are replaced in x which are listed in the SparseArray (2, 3 and 4).

One obvious solution for your case is to wrap the AdjacencyMatrix into Normal so its result won't be a SparseArray.

$\endgroup$
15
  • 2
    $\begingroup$ Is InternSequence used Privately to splice interns into the WRI System? :D $\endgroup$
    – rm -rf
    Commented Mar 27, 2014 at 19:06
  • 3
    $\begingroup$ Nice discovery. Horrible bug - it happens even if x is Protected and Locked. $\endgroup$ Commented Mar 27, 2014 at 20:06
  • 3
    $\begingroup$ Nice work! And here is my guess of the syntax of the SparseArray! :) $\endgroup$
    – Silvia
    Commented Mar 27, 2014 at 22:29
  • 1
    $\begingroup$ @Silvia Plausible hypothesis, it deserves its own thread! $\endgroup$ Commented Mar 27, 2014 at 23:29
  • 2
    $\begingroup$ @Silvia and Istvan, the 1 is for the sparse array version number $\endgroup$
    – rm -rf
    Commented Mar 28, 2014 at 15:16
4
$\begingroup$

This looks like a bug to me. Here is a slightly more minimal example.

ue = UndirectedEdge;
g = Graph[ue @@@ {{1, 2}, {2, 3}, {1, 3}, {1, 4}, {4, 5}, {4, 6}}];
vl = VertexList[g]
aM = AdjacencyMatrix[g];
vLM = aM[[VertexIndex[g, 1]]];
Pick[vl, vLM, 0];
VertexList[g]

Output

{1, 2, 3, 4, 5, 6}  
{1, 5, 6}

You can solve the error by making a copy of the vertexlist yourself. This can be done by using Append and Delete as follows

ue = UndirectedEdge;
g = Graph[ue @@@ {{1, 2}, {2, 3}, {1, 3}, {1, 4}, {4, 5}, {4, 6}}]
vl = Delete[Append[VertexList[g], 0], -1]
aM = AdjacencyMatrix[g];
vLM = aM[[VertexIndex[g, 1]]];
Pick[vl, vLM, 0];
v1 = VertexList[g]

Output

{1, 2, 3, 4, 5, 6}  
{1, 2, 3, 4, 5, 6}

So in your case you could do

copy[list_] := Delete[Append[list, 0], -1];

g
listDegree = VertexDegree[g]

Block[
 {punchingBag},
 punchingBag = copy[VertexList[g]]; 
 nodeMaxDegree = 
  Pick[punchingBag, listDegree, Max[VertexDegree[g]]][[1]]; 
 aM = AdjacencyMatrix[g]; vLM = aM[[VertexIndex[g, nodeMaxDegree]]]; 
 nN = Pick[punchingBag, vLM, 0];
 ]
vl = VertexList[g]

Another (probably better) solution would be to use Developer`ToPackedArray on VertexList[g], this avoids the strange behaviour from occurring altogether.

$\endgroup$
2
  • $\begingroup$ There are alternatives to the Pick function to do the same job (in this case find all the vertices that aren't neighbor to a given vertex?)? $\endgroup$
    – Adam Reith
    Commented Mar 27, 2014 at 18:46
  • $\begingroup$ @AdamReith sure! Also you can probably prevent the error altogether. One moment $\endgroup$ Commented Mar 27, 2014 at 18:47

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.