Is it a Cactus?

23

4

In graph theory, a Cactus is a connected graph such that any distinct two simple cycles in the graph share at most one vertex.

Here is a Cactus with 3 simple cycles outlined with dashed lines.

Cactus Graph

The following graph is similar to the one pictured above but is not a Cactus because the two vertices labeled in red are shared by two simple cycles.

Not Cactus Graph

Things can get a little bit trickier, for example the following graph:

Also not a Cactus graph

Might look like a Cactus but it is not. This can be shown by highlighting the following cycle:

Highlighted cycle

This cycle shares more than one point with a lot of the more obvious cycles in the graph.

Definitions

  • A connected graph is a graph such that there exists at least one path between any two vertices.

  • A simple cycle is a path on a graph that starts and ends at the same vertex and visits no vertex more than once.

  • A simple graph is an undirected, unweighted graph such that no vertices are connected two each other by more than one edge and no vertex is connected to itself. A simple graph is the most basic type of graph and is what most people mean when they say graph.

Task

Take a simple graph as input and decide whether it is a Cactus graph. You should output two distinct values one for True and one for False. You may take input in any format you see fit.

This is so you should aim to minimize the byte count of your answers.

Test Cases

Test Cases as Adjacency Matrices

Post Rock Garf Hunter

Posted 2017-05-14T18:50:48.463

Reputation: 55 382

Can you have a look at my solution, let me know if it's valid? I fell like the obvious pattern was too obvious and that I've missed something. – Shaggy – 2017-05-18T10:24:07.270

@Shaggy I can't read JavaScript, If you explain it I might be able to. – Post Rock Garf Hunter – 2017-05-18T15:01:18.857

I can try. I'm checking for 2 things: 1) Does e contain exactly one element AND does v contain exactly 2 AND is v equal to the first element of e? 2) OR Is v equal to the union set of the first elements of each element in e? The second test case passes the first check (v=[1,2]=e[0]=[1,2]) and the other test cases that should be true match the second, e.g. case#4: v=[1,2,3,4,5,6]=[e[0][0],e[1][0],e[2][0],e[4][0]]=[1,2,3,4,5,6]. – Shaggy – 2017-05-18T15:10:15.107

@Shaggy This does not work for example the first diagram provided fails. console.log(f([1,2,3,4,5,6,7,8,9,10,11,12,13])([[1,2],[1,3],[3,4],[2,4],[3,5],[5,6],[6,7],[7,8],[8,5],[7,9],[9,10],[10,11],[11,7],[8,12],[8,13]])) – Post Rock Garf Hunter – 2017-05-18T15:31:11.417

Should that return true or false? – Shaggy – 2017-05-18T15:34:44.960

@Shaggy It should be true. – Post Rock Garf Hunter – 2017-05-18T15:35:30.670

Ah, nuts! Could you add that to the test cases, please? – Shaggy – 2017-05-18T15:46:46.923

Answers

9

Mathematica, 62 bytes

Sort@#==#⋃#&[Join@@FindCycle[#,∞,All]]&&ConnectedGraphQ@#&

Checks: (find all cycles, there are no duplicate edges) and (The graph is a connected graph)

JungHwan Min

Posted 2017-05-14T18:50:48.463

Reputation: 13 290

1g should be #, right? – ngenisis – 2017-05-18T10:47:16.017

6So you're telling me there's no isCactus builtin? I'm disappointed. – Aaron – 2017-05-18T13:53:46.600

Someone should write one. – Draco18s no longer trusts SE – 2017-05-18T18:24:16.303

You should put Mathematica Simplified as a separate answer. – mbomb007 – 2017-05-18T19:03:47.123

3@Aaron It would be CactusQ if it existed, I believe. – NieDzejkob – 2017-09-17T07:41:43.953