Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Say you have the skeleton of a graph:

nodeset= PolyhedronData["Icosahedron", "SkeletonRules"]

and you've an arbitrary list of some fraction of those nodes, say given = {1,4,10}, which you want to turn into the list of all nodes connected directly to those given (along with the number of given each is connected to).


connCheckTool[node1_, node2_] := Count[nodeset, (node1 -> node2)] + Count[nodeset,(node2 -> node1)]

The following function achieves this:

       Total /@ 
    Table[connCheckTool[i, given[[z]]], {i, 1, 120, 1}, {z, 
      1, Length@given}]} /. {x_Integer, 0} -> delete, delete], {x_, y_} /; 
MemberQ[given, x] == True];

However, the inner connCheckTool is not very efficient, especially with larger graphs.

Can anyone think of a way to accomplish this in a better way?

I suspect it will involve the AdjacencyMatrix of the original, whole graph, as opposed to the list-search mechanism, but can't quite get it...

share|improve this question
Checked AdjacencyList in the docs? And NeighborhoodGraph and SubGraph? – kglr Apr 10 '14 at 10:30
I had an apparently undue paranoia of using Graph objects more than needed. Thank you for correcting that. – Ghersic Apr 11 '14 at 2:58
up vote 2 down vote accepted
Tally[Join @@ (AdjacencyList[Graph[nodeset], #] & /@ given)]


(* {{1, 1}, {4, 1}, {5, 1}, {6, 1}, {7, 1}, {11, 1}, {17, 1},
     {20,  1}, {41, 1}, {52, 1}, {71, 1}, {76, 1}, {105, 1},{109, 1}, {113, 1}} *)
share|improve this answer

Your Answer


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.