2
$\begingroup$

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).

With,

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

The following function achieves this:

DeleteCases[
   DeleteCases[
    Transpose@{Range[20], 
       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...

$\endgroup$
  • $\begingroup$ Checked AdjacencyList in the docs? And NeighborhoodGraph and SubGraph? $\endgroup$ – kglr Apr 10 '14 at 10:30
  • $\begingroup$ I had an apparently undue paranoia of using Graph objects more than needed. Thank you for correcting that. $\endgroup$ – Ghersic Apr 11 '14 at 2:58
2
$\begingroup$
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}}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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