Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

share|improve this question
1  
Interesting: FullForm[vl] gives List[1, System`Private`InternSequence[], System`Private`InternSequence[], System`Private`InternSequence[], 5, 6]. –  István Zachar Mar 27 at 18:34
    
What does it means "System`Private`InternSequence[]"? –  Adam Reith Mar 27 at 18:49
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 at 20:04
3  
I reported the issue to WRI. –  Adam Reith Mar 27 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 at 20:16
show 7 more comments

2 Answers 2

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.

share|improve this answer
    
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?)? –  Adam Reith Mar 27 at 18:46
    
@AdamReith sure! Also you can probably prevent the error altogether. One moment –  Jacob Akkerboom Mar 27 at 18:47
add comment

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.

share|improve this answer
1  
Is InternSequence used Privately to splice interns into the WRI System? :D –  rm -rf Mar 27 at 19:06
3  
Nice discovery. Horrible bug - it happens even if x is Protected and Locked. –  Simon Woods Mar 27 at 20:06
2  
Nice work! And here is my guess of the syntax of the SparseArray! :) –  Silvia Mar 27 at 22:29
1  
@Silvia Plausible hypothesis, it deserves its own thread! –  István Zachar Mar 27 at 23:29
2  
@Silvia and Istvan, the 1 is for the sparse array version number –  rm -rf Mar 28 at 15:16
show 10 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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