2
$\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$
2
  • 1
    $\begingroup$ Did you contact Wolfram to report this bug? If not, please do so. $\endgroup$
    – Szabolcs
    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$ Oct 25, 2021 at 19:12

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, privacy policy and cookie policy

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