Skip to content

Commit

Permalink
Trac #24113: Add test for cactus graph
Browse files Browse the repository at this point in the history
This patch adds a function to check if a graph is cactus graph.

URL: https://trac.sagemath.org/24113
Reported by: jmantysalo
Ticket author(s): Jori Mäntysalo
Reviewer(s): David Coudert
  • Loading branch information
Release Manager authored and vbraun committed Oct 30, 2017
2 parents 326106e + 8b789f6 commit 9896ca2
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/sage/graphs/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -1798,6 +1798,59 @@ def is_forest(self, certificate=False, output='vertex'):
if not isit:
return (False, cycle)

@doc_index("Graph properties")
def is_cactus(self):
"""
Check whether the graph is cactus graph.
A graph is called *cactus graph* if it is connected and every pair of
simple cycles have at most one common vertex.
There are other definitions, see :wikipedia:`Cactus_graph`.
EXAMPLES::
sage: g = Graph({1: [2], 2: [3, 4], 3: [4, 5, 6, 7], 8: [3, 5], 9: [6, 7]})
sage: g.is_cactus()
True
sage: c6 = graphs.CycleGraph(6)
sage: naphthalene = c6 + c6
sage: naphthalene.is_cactus() # Not connected
False
sage: naphthalene.merge_vertices([0, 6])
sage: naphthalene.is_cactus()
True
sage: naphthalene.merge_vertices([1, 7])
sage: naphthalene.is_cactus()
False
TESTS::
sage: all(graphs.PathGraph(i).is_cactus() for i in range(5))
True
sage: Graph('Fli@?').is_cactus()
False
"""
self._scream_if_not_simple()

# Special cases
if self.order() < 4:
return True

# Every cactus graph is outerplanar, and outerplanar
# graphs have limited number of edges.
if self.size() > self.order()*2-3:
return False

if not self.is_connected():
return False

# the number of faces is 1 plus the number of blocks of order > 2
B = self.blocks_and_cut_vertices()[0]
return len(self.faces()) == sum(1 for b in B if len(b) > 2) + 1

@doc_index("Graph properties")
def is_biconnected(self):
"""
Expand Down
1 change: 1 addition & 0 deletions src/sage/graphs/isgci.py
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,7 @@ def _XML_to_dict(root):
graph_classes.BinaryTrees = GraphClass("BinaryTrees", "gc_847")
graph_classes.Bipartite = GraphClass("Bipartite", "gc_69", recognition_function = lambda x:x.is_bipartite())
graph_classes.Block = GraphClass("Block", "gc_93", recognition_function = lambda x:x.is_block_graph())
graph_classes.Cactus = GraphClass("Cactus", "gc_108", recognition_function = lambda x:x.is_cactus())
graph_classes.Chordal = GraphClass("Chordal", "gc_32", recognition_function = lambda x:x.is_chordal())
graph_classes.ClawFree = GraphClass("Claw-free", "gc_62")
graph_classes.CoGraph = GraphClass("CoGraph", "gc_151", recognition_function = lambda x:x.is_cograph())
Expand Down

0 comments on commit 9896ca2

Please sign in to comment.