3
$\begingroup$

Graphs often compress a great deal of information into a simple visualizable structure. So it is not uncommon that vertex labels include more information than a simple index. For example, vertex labels might be lists. Graph handles this brilliantly:

graph = Graph[{{1, 2} \[DirectedEdge] {1, 3},
               {1, 3} \[DirectedEdge] {2, 3}, 
               {2, 3} \[DirectedEdge] {3, 2},
               {3, 2} \[DirectedEdge] {1, 2}}, VertexLabels -> "Name"] 

Nicely yields the graph

A directed graph with vertices given as lists

The issue here is EdgeContract. It does not seem to operate on such graphs. For example:

EdgeContract[graph, First[EdgeList[graph]]]

returns exactly the same graph. It does not contract the first edge.

$\endgroup$
3
  • 1
    $\begingroup$ Did you contact Wolfram to report this bug? If not, please do so. $\endgroup$
    – Szabolcs
    Commented Oct 25, 2021 at 17:36
  • 1
    $\begingroup$ Not fixed in 13.0 Prerelease 1. 13.0.0 for Mac OS X x86 (64-bit) (October 11, 2021). $\endgroup$ Commented Oct 25, 2021 at 19:12
  • $\begingroup$ Now fixed in Version 13.2 for Microsoft Windows (64-bit). $\endgroup$
    – A. Kato
    Commented Apr 8, 2023 at 2:27

1 Answer 1

1
$\begingroup$

One work-around is to covert the vertex names into strings:

sgraph = Graph[Map[ToString[First[#]] \[DirectedEdge] ToString[Last[#]] &, 
                    EdgeList[graph]], VertexLabels -> "Name"]

(I'm sure someone has a more elegant method for doing this) That produces the same graph but with the vertex names as strings instead of lists.

Now EdgeContract works:

EdgeContract[sgraph, First[EdgeList[sgraph]]]

correctly yields:

Directed graph with the first edge contracted

I suppose another alternative is a dictionary that maps labels to their information. But wouldn't it be nice if this added step weren't required?

$\endgroup$

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.