I'm facing some issues when trying to use the CommunityStructureAssignment function from the GraphUtilities package in Mathematica 8.

I'd like to output a community structure assignment for my random graph, but it seems the function is unrecognized by Mathematica. In particular with the following code I'm creating a random weighted graph G with 10 nodes and 30 edges and I visualize the weighted adjacency matrix:

nNodes = 10;
nEdges = 30;
G = Graph[EdgeList[RandomGraph[{nNodes, nEdges}]], EdgeWeight -> RandomReal[{0.1, 5}, nEdges]];

W = WeightedAdjacencyMatrix[G];
W // MatrixForm

After that, following the documentation of CommunityStructureAssignment, I'd imagine that feeding the EdgeList of G directly to CommunityStructureAssignment would result in a list but:

CommunityStructureAssignment[EdgeList[G], Weighted -> True]

returns it self:

CommunityStructureAssignment[{1 \[UndirectedEdge] 4, 
  1 \[UndirectedEdge] 5, 2 \[UndirectedEdge] 4, 2 \[UndirectedEdge] 5,
   2 \[UndirectedEdge] 1, 2 \[UndirectedEdge] 3, 
  3 \[UndirectedEdge] 5, 3 \[UndirectedEdge] 1, 4 \[UndirectedEdge] 5,
   4 \[UndirectedEdge] 3}, Weighted -> True]

while the documentation clearly says that:

g = {3 -> 2, 2 -> 1, 1 -> 3, 3 -> 5, 5 -> 6, 6 -> 7, 7 -> 5};

should correctly work.

Now the question is: how can I feed the correct input graph into CommunityStructureAssignment? And also how to include the edge weights into the EdgeList?

Also I have to say, that once i import the GraphUtilities package, the definition of EdgeList turns red. Is this because there is a conflict of names?


1 Answer 1


I am using Mathematica 10 but I suppose the same issues will apply. I believe that the GraphUtilites package is not designed to work with Graph objects but instead other, earlier formats.

After loading Needs["GraphUtilities`"] the Symbol EdgeList is highlighted in red indicating shadowing, meaning that this Symbol exists in multiple contexts in the $ContextPath. If the one from the package context is used it does not recognize G:


enter image description here

Likewise it seems that CommunityStructureAssignment does not recognize \[UndirectedEdge] as it was presumably written before the introduction of that functionality. If we use the System` EdgeList function and replace UndirectedEdge with Rule:

CommunityStructureAssignment[Rule @@@ System`EdgeList[G], Weighted -> True]
{1, 2, 3, 4, 2, 2, 2, 2, 2, 2}
  • $\begingroup$ That's magic! It works, thanks, you saved my day! $\endgroup$
    – linello
    Commented Sep 19, 2014 at 14:18

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.