3
$\begingroup$

The obsolete Combinatorica package has a command Contract for contracting an edge in a graph. Below is my very clunky attempt to work with up-to-date graph objects and yet use the Contract command from Combinatorica. The idea is to convert into and out of adjaceny matrices.

My questions are:

Is there a modern analogue of the Contract command ?

If not, then would readers advise me to continue working with the Combinatorica Contract command, or to write my own command for contracting edges, and not bother trying to use Combinatorica ?

Needs["Combinatorica`"]

graph = System`Graph[{1 \[UndirectedEdge] 2, 1 \[UndirectedEdge] 3, 
2 \[UndirectedEdge] 3, 2 \[UndirectedEdge] 4, 
3 \[UndirectedEdge] 4}, VertexLabels -> "Name"]

convertgraphtocombinatoricastyle[graph_] := Module[{},
FromAdjacencyMatrix[Normal[AdjacencyMatrix[graph]]]];

combinatoricastylegraph = convertgraphtocombinatoricastyle[graph];
ShowGraph[combinatoricastylegraph, VertexLabel -> True]

contractedcombinatoricagraph = 
Contract[combinatoricastylegraph, {1, 2}];
ShowGraph[contractedcombinatoricagraph, VertexLabel -> True]
$\endgroup$
  • 4
    $\begingroup$ Are you looking for EdgeContract? $\endgroup$ – hftf Oct 17 '15 at 19:25
  • $\begingroup$ Is that new in version 10 ? I only have 9 :D Aha ! I see that it is indeed new in version 10. Thank you for your response, hftf ! $\endgroup$ – Simon Oct 17 '15 at 19:36
  • $\begingroup$ Version 9 doesn't support multigraphs, so you can't reproduce exactly the same behavior $\endgroup$ – Dr. belisarius Oct 18 '15 at 1:11
  • $\begingroup$ Thank you, belisarius is forth. I remember that I wrote code to deal with multigraphs before. I represented them by their adjacency matrices, but the elements of the matrices were lists, corresponding to a labeling of the (possibly many) edges between pairs of vertices. $\endgroup$ – Simon Oct 18 '15 at 16:20

Your Answer

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

Browse other questions tagged or ask your own question.