15

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

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
Adam Reith
  • 151
  • 4
  • 1
    Interesting: FullForm[vl] gives List[1, System`Private`InternSequence[], System`Private`InternSequence[], System`Private`InternSequence[], 5, 6]. – István Zachar Mar 27 '14 at 18:34
  • What does it means "SystemPrivateInternSequence[]"? – Adam Reith Mar 27 '14 at 18:49
  • Adam, we were probably never supposed to see that symbol. I bet most WRI employees don't even know what it is does as it is in a private context. Who knows though :) – Jacob Akkerboom Mar 27 '14 at 19:01
  • Adam, I've modified your title to better reflect the case. Could you please submit this as a bug report for the TechSupport at WRI? – István Zachar Mar 27 '14 at 19:07
  • Sure! Can you tell me how? I'm still a newbie :D – Adam Reith Mar 27 '14 at 19:26
  • By the way, System'Private'InternSequence[](by itself, without a wrapper like List) "evaluates to" StandardForm. Peculiar :P – Jacob Akkerboom Mar 27 '14 at 19:46
  • Adam, can you please report this to support? This is not an official Wolfram site, so they might never see the bug if it's not reported through the official channels (support @ wolfram.com). We would of course all like to see it fixed, so do report it please! – Szabolcs Mar 27 '14 at 19:54
  • 1
    @JacobAkkerboom, I expect that comes from Typeset`MakeBoxes[_, StandardForm] not having a rule for System'Private'InternSequence[] like it does for Sequence[] – Simon Woods Mar 27 '14 at 20:04
  • @SimonWoods hmm let's call that function sPI for short. If you use a rule to transform sPI[], like in sPI[] /. x_ :> x, it will evaluate to Sequence[]. I thought it might have to do with that. I've never looked at Typeset'Makeboxes, your guess is probably better than mine. By the way your Spelunk gives an error for that one. – Jacob Akkerboom Mar 27 '14 at 20:13
  • 3
    I reported the issue to WRI. – Adam Reith Mar 27 '14 at 20:16
  • 1
    @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... – István Zachar Mar 27 '14 at 20:16
  • 1
    The bug seems to be no longer present in version 10! – Jacob Akkerboom Jul 09 '14 at 21:10
  • @Jacob Do you know in which version this bug first appeared? I'd like to add the appropriate version banner to this post. – Mr.Wizard Feb 03 '15 at 09:08
  • 1
    @Mr.Wizard well, the bug with SparseArray, as illustrated by istvan's answer, is present in version 7 (I could not test earlier versions). The code by the OP first causes trouble in version 8, as the `Combinatorica`` package in version 7 had different syntax. – Jacob Akkerboom Feb 03 '15 at 11:03

2 Answers2

10

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.

István Zachar
  • 47,032
  • 20
  • 143
  • 291
4

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.

Jacob Akkerboom
  • 12,215
  • 45
  • 79