Skip to content

Commit

Permalink
Trac #24118: Add quick check for planarity
Browse files Browse the repository at this point in the history
Functions to check if a graph is planar or circular planar should first
check if the graph has "too many" edges compared to number of vertices
and then return `False` directly.

URL: https://trac.sagemath.org/24118
Reported by: jmantysalo
Ticket author(s): Jori Mäntysalo
Reviewer(s): David Coudert
  • Loading branch information
Release Manager authored and vbraun committed Jan 17, 2018
2 parents 18f63ea + 3db3fb3 commit 21e1d6b
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/sage/graphs/generic_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -4239,7 +4239,20 @@ def is_planar(self, on_embedding=None, kuratowski=False, set_embedding=False, se

sage: posets.BooleanLattice(3).cover_relations_graph().is_planar()
True

Corner cases::

sage: graphs.EmptyGraph().is_planar()
True
sage: Graph(1).is_planar()
True
"""
# Quick check first
if (on_embedding is None and not kuratowski and not set_embedding and not set_pos
and not self.allows_loops() and not self.allows_multiple_edges()):
if self.order() > 4 and self.size() > 3*self.order()-6:
return False

if self.has_multiple_edges() or self.has_loops():
if set_embedding or (on_embedding is not None) or set_pos:
raise NotImplementedError("Cannot compute with embeddings of multiple-edged or looped graphs.")
Expand Down Expand Up @@ -4267,7 +4280,7 @@ def is_planar(self, on_embedding=None, kuratowski=False, set_embedding=False, se
return planar

def is_circular_planar(self, on_embedding=None, kuratowski=False,
set_embedding=True, boundary = None,
set_embedding=True, boundary=None,
ordered=False, set_pos=False):
"""
Tests whether the graph is circular planar (outerplanar)
Expand Down Expand Up @@ -4373,10 +4386,26 @@ def is_circular_planar(self, on_embedding=None, kuratowski=False,

sage: K23.is_circular_planar(set_embedding=True, boundary = [0,2,1,3])
True

TESTS:

Corner cases::

sage: graphs.EmptyGraph().is_circular_planar()
True
sage: Graph(1).is_circular_planar()
True
"""
if ordered and boundary is None:
raise ValueError("boundary must be set when ordered is True.")

# Quick check first
if (on_embedding is None and not kuratowski and set_embedding and
boundary is None and not ordered and not set_pos and
not self.allows_loops() and not self.allows_multiple_edges()):
if self.order() > 3 and self.size() > 2*self.order()-3:
return False

if boundary is None:
boundary = self

Expand Down

0 comments on commit 21e1d6b

Please sign in to comment.