Skip to content

Commit

Permalink
add IGVertexContract
Browse files Browse the repository at this point in the history
  • Loading branch information
szhorvat committed Sep 19, 2016
1 parent 0e92e06 commit a7461e3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
28 changes: 27 additions & 1 deletion IGraphM/IGraphM.m
Expand Up @@ -362,6 +362,8 @@

IGBipartitePartitions::usage = "IGBipartitePartitions[graph] partitions the vertices of a bipartite graph.";

IGVertexContract::usage = "IGVertexContract[g, {{v1, v2, \[Ellipsis]}, \[Ellipsis]}] returns graph in which the specified vertex sets are contracted into single vertices.";


Begin["`Private`"];

Expand Down Expand Up @@ -691,7 +693,10 @@

(* Bipartite graphs *)

LFun["bipartitePartitions", {}, {Integer, 1}]
LFun["bipartitePartitions", {}, {Integer, 1}],

(* Vertex contraction *)
LFun["contractVertices", {{Real, 1}}, "Void"]
}
]
}
Expand Down Expand Up @@ -3139,6 +3144,27 @@
]


(* Vertex contraction *)

IGVertexContract::inv = "The vertices `` are not present in the graph.";
IGVertexContract::vset = "`` must be a list of vertex sets.";

Options[IGVertexContract] = { SelfLoops -> False };
SyntaxInformation[IGVertexContract] = {"ArgumentsPattern" -> {_, _, OptionsPattern[]}};
IGVertexContract[graph_?igGraphQ, sets : {___List}, opt : OptionsPattern[]] :=
catch@Block[{ig = igMakeFast[graph], allElements = Join @@ sets},
If[Not@SubsetQ[VertexList[graph], allElements],
Message[IGVertexContract::inv, Complement[allElements, VertexList[graph]]];
Return[$Failed]
];
check@ig@"contractVertices"@communitiesToMembership[
VertexList[graph],
Join[sets, List /@ Complement[VertexList[graph], allElements]]
];
If[TrueQ@OptionValue[SelfLoops], Identity, SimpleGraph]@igToGraph[ig]
]

IGVertexContract[graph_?igGraphQ, arg_, opt : OptionsPattern[]] := Null /; Message[IGVertexContract::vset, arg]

(***** Finalize *****)

Expand Down
8 changes: 8 additions & 0 deletions IGraphM/LibraryResources/Source/IG.h
Expand Up @@ -1781,6 +1781,14 @@ class IG {

return map.makeMTensor();
}

// Contract vertices

// note this is a constructor!
void contractVertices(mma::RealTensorRef t) {
igraph_vector_t mapping = igVectorView(t);
igCheck(igraph_contract_vertices(&graph, &mapping, NULL));
}
};

#endif // IG_H
Expand Down

0 comments on commit a7461e3

Please sign in to comment.