Skip to content

Add utility to extract faces from PlanarEmbedding - #8512

Merged
rossbar merged 7 commits into
networkx:mainfrom
VarandArakelian:add-faces-from-planar-embedding
Mar 24, 2026
Merged

rossbar merged 7 commits into
networkx:mainfrom
VarandArakelian:add-faces-from-planar-embedding

Conversation

@VarandArakelian

Copy link
Copy Markdown
Contributor

This PR adds a small utility function, faces_from_planar_embedding, that enumerates facial boundary walks from a PlanarEmbedding.

Motivation

While NetworkX can compute planar embeddings, it does not currently provide a public helper to enumerate the faces induced by a given combinatorial embedding. Several algorithms and applications work directly at the level of the rotation system (rather than a geometric drawing) and require access to facial boundary walks.

What this adds

  • A public function faces_from_planar_embedding(embedding) in networkx.algorithms.planarity
  • The function traverses the embedding’s rotation system and visits each directed half-edge exactly once
  • It returns facial boundary walks in cyclic order; walks may repeat vertices, e.g. around bridges or articulation points
  • For disconnected embeddings, faces are returned per connected component (no distinguished global outer face)

Tests

New tests verify that:

  • Directed half-edges are partitioned exactly once among faces
  • Expected face counts hold for cycles, trees, and K₄
  • Disconnected graphs return component-local faces
  • Isolated nodes produce no faces
  • Non-PlanarEmbedding inputs raise a TypeError

This change is additive and does not modify existing APIs.

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this!

Big picture comments:

Can you provide a reference for this algorithm? Is there a publication/wikipedia page where facial boundary walks are discussed or used?

How would you see the result being used? Is a list of sets a convenient data structure for the result?

I guess it would be possible to make this a method of the class, but I like this function interface. It's not complicated code -- so I suppose the user could put it together themselves, but it takes a fair amount of thinking, and it requires close knowledge of the embedding data class. So it seems reasonable to have a separate function.

As far as where this function goes/lives, the tests would traditionally live in the test_planarity.py module. Is there a reason you put them separately? Thoughts?

Comment thread networkx/algorithms/planarity.py Resolved
Comment thread networkx/algorithms/planarity.py Resolved
@boothby

boothby commented Feb 13, 2026 •

Copy link
Copy Markdown
Contributor

Voicing support for this PR though I haven't gone over it in detail -- it's something I was going to need to add for #8489 anyway. The algorithm is bog-standard and will appear (or be taken for granted) in any reference that mentions the algorithm implemented in PlanarEmbedding.traverse_face -- this is just the clean way to enumerate all the faces of an embedding. An easy (and by my eye, pretty good) list of references can be found here: https://en.wikipedia.org/wiki/Combinatorial_map

Is a list of sets a convenient data structure for the result?

No, it should be a list of lists (perhaps lazier and less mutable; an iterable of tuples?) -- facial walks can visit nodes multiple times, and it's important to know what order that happens in.

@rossbar

rossbar commented Feb 13, 2026

Copy link
Copy Markdown
Contributor

Thanks for the insight @boothby - a quick API question: IYO is this better suited as a method of the PlanarEmbedding class or a standalone function? The pros/cons as I see them:

Function

  • PRO: Matches the typical NetworkX pattern
  • CON: extra isinstance boilerplate

Method

  • No boilerplate, just pure face-extraction in the implementation
  • Methods on class are not very networkx-y, but the PlanarEmbedding class itself is already breaks from the everything-should-be-functions pattern (cf. e.g. the traverse_face method).

I have no strong opinion, but since you've been thinking about this already it'd be great to hear what you'd want the UX to be!

@boothby

boothby commented Feb 16, 2026

Copy link
Copy Markdown
Contributor

Great question, Ross. I'm not personally married to the notion that classes shouldn't have methods, but I think the line should be "don't make a standalone function that must depend on the guts of a class." And in this case, mark_half_edges depends on the internals of the combinatorial embedding data structure that the PlanarEmbedding class maintains*. I would be inclined to make this a method.

* specifically if we adopt a different representation of embeddings that can handle surfaces other than the plane, one might want to find facial walks and making such a class conform to the mark_half_edges interface might be a wart

@boothby

boothby commented Feb 16, 2026

Copy link
Copy Markdown
Contributor

Having looked in detail; if Dan's objections are satisfied I think this is good

@VarandArakelian

VarandArakelian commented Feb 16, 2026 •

Copy link
Copy Markdown
Contributor Author

@dschult About the Big Picture

This function is about finding faces of a graph when the embedding is planar.

To put it in context: there are three cases actually. The number of faces of a graph depends on the embedding. Embedding can be planar or non-planar. And if it is non-planar, the manifold onto which the graph is embedded can be either orientable or non-orientable. (If it is planar, it is orientable, as all planes in \mathbb{R}^2 are orientable, so we end up with three cases.)

The math here involves rotation systems (I see the Wikipedia page for it was posted). Rotation systems are a standard textbook topic. This is not research-level. Also, rotation systems can be used to find the faces in any of the three cases.

This function is for the case when the graph is embedded onto a plane. It is short because embedding.neighbors_cw_order(u) gives the cyclic ordering of neighbors for the vertex u, which is essentially the rotation system for that vertex. This is contained in PlanarEmbedding. To answer @rossbar : this is not boilerplate. We need to make sure that the object is there. It is where the rotation system comes from.

How I see it being used
Actually, I was writing the toric code, so I needed to find edges, nodes, and faces. These are elementary things for a graph. You can find nodes and edges easily, but surprisingly, there isn’t a built-in way to find the faces. I wrote three functions for the three cases:
• faces_from_planar_embedding – for the planar case
• faces_from_rotation – for when a graph is embedded onto an orientable manifold. The function is called this way because it requires the user to input the rotation system.
• faces_from_signed_rotation – for when a graph is embedded onto a non-orientable manifold. Here, the user inputs not only the rotation system (which won’t be enough) but also the twist.

I uploaded the planar case only because my understanding is that the others could be out of the scope of NetworkX, but I have a wrapper that computes faces, whichever case it might be.

Is a list of sets a convenient data structure for the result?
I see the argument that a list of lists would preserve traversal. But the faces of a graph after an embedding are unique. You can get [2,3,4,1] instead of [1,2,3,4], but they are always permutations of the same elements. Also, take the case of the edges. We find edges by G.edges(). Does it make a difference how the list is ordered?

@boothby I understand some might want a specific path traversal, but how about some other function for that? This idea could be generalized, and wanting a list of edges for all the faces given a constraint would be a specific version of a more general path traversal algorithm on a graph.

Should these be functions or methods?
What is a graph? A graph is defined as a collection of edges and vertices. Faces are not inherent to a graph. So should we have methods like G.faces_from_planar_embedding or G.faces_from_rotation? I’d argue no. A face is not a feature of the graph itself. Faces are meaningful only when there is an embedding. Without an embedding, a graph is just a collection of edges and vertices, so a graph G doesn’t have face information—it will have it after it is embedded.

PS. Thanks everyone for the comments

@rossbar

rossbar commented Feb 16, 2026

Copy link
Copy Markdown
Contributor

Thanks everyone for the fruitful discussion! To summarize the action items to get us towards the (seemingly) forming consensus:

  • Let's implement this as a method on PlanarEmbedding rather than a standalone function
  • Let's implement as a generator
  • Make sure that the in-line comments (i.e. the code suggestions) from the previous review are addressed!

@rossbar

rossbar commented Mar 2, 2026

Copy link
Copy Markdown
Contributor

Gentle ping @VarandArakelian are you still planning on working on this? There's a clear path forward summarized above!

@VarandArakelian

Copy link
Copy Markdown
Contributor Author

@rossbar Yes. I haven't had time. The plan is to begin doing what remains here after March 15th

@VarandArakelian

VarandArakelian commented Mar 18, 2026 •

Copy link
Copy Markdown
Contributor Author

@dschult, @rossbar, @boothby Implemented this as PlanarEmbedding.faces() per the consensus

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good. Thanks @VarandArakelian

@VarandArakelian

Copy link
Copy Markdown
Contributor Author

@dschult Thank you!! I really appreciate it! I’m thinking of a class GeneralEmbedding for general embeddings on surfaces with face extraction. I’ll start working on it as soon as this is merged. Thanks again to @rossbar and @boothby! It’s been a really pleasant experience.

@amcandio amcandio left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@rossbar rossbar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took the liberty of removing some of the "helper" functions in the test suite, but otherwise LGTM. Thanks @VarandArakelian and everyone for the reviews!

@rossbar
rossbar merged commit 6f0fa50 into networkx:main Mar 24, 2026
44 checks passed
@dschult dschult added this to the 3.7 milestone Mar 24, 2026
mcbosch pushed a commit to mcbosch/networkx that referenced this pull request Apr 7, 2026
* Add utility to extract faces from PlanarEmbedding

* Style fixes from pre-commit

* Fix typing and import order for faces_from_planar_embedding

* Add faces() method to PlanarEmbedding

* MAINT: Rm indirection.

* Rm more indirection.

* STY: Undo style changes.

---------

Co-authored-by: Ross Barnowski <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

5 participants