Skip to main content
Tweeted twitter.com/StackMma/status/777676453613867009
added 116 characters in body
Source Link
Szabolcs
  • 236.5k
  • 31
  • 641
  • 1.3k

From the documentation,

VertexContract[g, {v1, v2, ...}] contracts a collection of vertices v1, v2, ... into a single vertex of the graph g.

Let's try it:

VertexContract[CycleGraph[6], {3,4}] // EdgeList

(* {1 <-> 2, 1 <-> 6, 2 <-> 3, 5 <-> 6, 5 <-> 3} *)

Vertices 3 and 4 were contracted into a single vertex now named 3. So Effectively we have a cycle on vertices 1, 2, 3, 5, 6. So far so good.

VertexContract[g, {{v1, v2, ...}, ...}] contracts several collections of vertices.

I am not getting this one. I expected

VertexContract[CycleGraph[6], {{1, 2}, {3, 4}, {5, 6}}]

to contract the pairs {1,2}, {3,4} and {5,6}, each, and get this graph:

{1 <-> 3, 3 <-> 5, 5 <-> 1}

I.e. a cycle on 1, 3, 5.

Instead I get a graph with a single vertex and no edges. Why? What does this command actually do?


I can do

Fold[VertexContract[#1, #2] &, g, {{v1, v2, ...}, ...}] 

to achieve the result I expected, but it's pretty slow when there's a lot to contract.

From the documentation,

VertexContract[g, {v1, v2, ...}] contracts a collection of vertices v1, v2, ... into a single vertex of the graph g.

VertexContract[CycleGraph[6], {3,4}] // EdgeList

(* {1 <-> 2, 1 <-> 6, 2 <-> 3, 5 <-> 6, 5 <-> 3} *)

Vertices 3 and 4 were contracted into a single vertex now named 3. So far so good.

VertexContract[g, {{v1, v2, ...}, ...}] contracts several collections of vertices.

I am not getting this one. I expected

VertexContract[CycleGraph[6], {{1, 2}, {3, 4}, {5, 6}}]

to contract the pairs {1,2}, {3,4} and {5,6}, each, and get this graph:

{1 <-> 3, 3 <-> 5, 5 <-> 1}

Instead I get a graph with a single vertex and no edges. Why? What does this command actually do?


I can do

Fold[VertexContract[#1, #2] &, g, {{v1, v2, ...}, ...}] 

to achieve the result I expected, but it's pretty slow when there's a lot to contract.

From the documentation,

VertexContract[g, {v1, v2, ...}] contracts a collection of vertices v1, v2, ... into a single vertex of the graph g.

Let's try it:

VertexContract[CycleGraph[6], {3,4}] // EdgeList

(* {1 <-> 2, 1 <-> 6, 2 <-> 3, 5 <-> 6, 5 <-> 3} *)

Vertices 3 and 4 were contracted into a single vertex now named 3. Effectively we have a cycle on vertices 1, 2, 3, 5, 6. So far so good.

VertexContract[g, {{v1, v2, ...}, ...}] contracts several collections of vertices.

I am not getting this one. I expected

VertexContract[CycleGraph[6], {{1, 2}, {3, 4}, {5, 6}}]

to contract the pairs {1,2}, {3,4} and {5,6}, each, and get this graph:

{1 <-> 3, 3 <-> 5, 5 <-> 1}

I.e. a cycle on 1, 3, 5.

Instead I get a graph with a single vertex and no edges. Why? What does this command actually do?


I can do

Fold[VertexContract[#1, #2] &, g, {{v1, v2, ...}, ...}] 

to achieve the result I expected, but it's pretty slow when there's a lot to contract.

Source Link
Szabolcs
  • 236.5k
  • 31
  • 641
  • 1.3k

What does VertexContract do?

From the documentation,

VertexContract[g, {v1, v2, ...}] contracts a collection of vertices v1, v2, ... into a single vertex of the graph g.

VertexContract[CycleGraph[6], {3,4}] // EdgeList

(* {1 <-> 2, 1 <-> 6, 2 <-> 3, 5 <-> 6, 5 <-> 3} *)

Vertices 3 and 4 were contracted into a single vertex now named 3. So far so good.

VertexContract[g, {{v1, v2, ...}, ...}] contracts several collections of vertices.

I am not getting this one. I expected

VertexContract[CycleGraph[6], {{1, 2}, {3, 4}, {5, 6}}]

to contract the pairs {1,2}, {3,4} and {5,6}, each, and get this graph:

{1 <-> 3, 3 <-> 5, 5 <-> 1}

Instead I get a graph with a single vertex and no edges. Why? What does this command actually do?


I can do

Fold[VertexContract[#1, #2] &, g, {{v1, v2, ...}, ...}] 

to achieve the result I expected, but it's pretty slow when there's a lot to contract.