Back to Data Structures & Algorithms
2026-03-285 min read

Finding faces of a planar graph (Data Structures & Algorithms)

Learn Finding faces of a planar graph (Data Structures & Algorithms) step by step with clear examples and exercises.

Why This Matters

This lesson will provide you with a comprehensive understanding of finding faces in a planar graph using Python and its significance in various fields such as computer science, network design, electronic circuit layouts, and computer graphics.

The Importance of Planar Graph Algorithms

Planar graphs play an essential role in computer science due to their simplicity and ease of visualization. Understanding how to find faces in a planar graph is crucial for competitive programming and practical applications like map labeling, VLSI design, network routing, and more.

Prerequisites

To fully grasp this lesson, you should have a good understanding of:

  1. Basic Python programming concepts (variables, functions, loops, lists)
  2. Data structures (lists, sets, dictionaries)
  3. Graph theory basics (adjacency list representation, depth-first search)
  4. Familiarity with the concept of planar graphs and their properties
  5. Understanding of recursion and stack data structure

Core Concept

A planar graph can be drawn in a plane without any edges crossing each other. We will use Depth-First Search (DFS) to find faces of a planar graph by visiting vertices, marking them as visited, and keeping track of the current face we are exploring using a stack.

Planar Graph Representation

We represent a planar graph using an adjacency list, where each vertex has a list of its adjacent vertices.

graph = {
'A': ['B', 'C'],
'B': ['A', 'D'],
'C': ['A', 'D', 'E'],
'D': ['B', 'C'],
'E': ['C']
}

Finding Faces of a Planar Graph (DFS)

We will use DFS to traverse the graph and find faces. The key is to keep track of the current face we are exploring, which can be done by maintaining a stack and a visited dictionary. Initially, we start from an arbitrary vertex, mark it as visited, and push it onto the stack. Then, we explore adjacent vertices recursively until we return to the starting vertex, at which point we pop a vertex from the stack, indicating that we have finished exploring one face.

def find_faces(graph, start):
visited = set()
stack = [start]
faces = []

while stack:
current = stack[-1]
if current not in visited:
visited.add(current)
for neighbor in graph[current]:
if neighbor not in visited:
stack.append(neighbor)
break
else:

If we couldn't find an unvisited neighbor, the current vertex is part of a boundary

faces.append(list(visited))

stack.pop()

return faces[1:] # Remove the initial empty face created when starting DFS

Worked Example

Let's apply this algorithm to our example graph:

graph = {
'A': ['B', 'C'],
'B': ['A', 'D'],
'C': ['A', 'D', 'E'],
'D': ['B', 'C'],
'E': ['C']
}

Running the find_faces function will return:

[['A', 'B', 'D'], ['C', 'E']]

This result indicates that our planar graph has two faces: one with vertices A, B, and D, and another with vertex C and E.

Common Mistakes

  1. Not initializing the visited set: Make sure to initialize an empty set for visited vertices before starting DFS.
  2. Not breaking out of recursion when returning to the starting vertex: If we don't pop the starting vertex from the stack and remove it from the visited set, we will get incorrect results.
  3. Incorrect handling of boundary vertices: When visiting a boundary vertex (i.e., one with unvisited neighbors), make sure to create a new face instead of appending the current face to itself.
  4. Not properly initializing the faces list: The faces list should be initialized as an empty list before starting DFS.
  5. Not returning the correct number of faces: Remember to remove the initial empty face created when starting DFS by using return faces[1:].
  6. Not handling multiple connected components: If the graph has multiple connected components, you might need to call the find_faces function recursively for each component and combine the results.
  7. Not properly handling cycles in the graph: In case of cycles, you may end up with incorrect face counts or infinite loops. To avoid this, make sure your graph is acyclic before applying the DFS algorithm.
  8. Not considering self-loops: Self-loops (edges connecting a vertex to itself) should be handled carefully as they can affect the face count and the structure of the graph.
  9. Not handling multiple edges between vertices: If your graph has multiple edges between some vertices, you need to account for this during DFS traversal to ensure correct face counts.

Practice Questions

  1. Implement the find_faces function for a planar graph represented as an adjacency matrix.
  2. Given a planar graph, write a Python function that returns the number of edges on the boundary between two faces.
  3. Modify the find_faces function to handle graphs with multiple connected components.
  4. Write a Python function that checks if a given graph is planar using the planarity test algorithm (Hopcroft's or Kuratowski's theorem).
  5. Implement a function that finds the minimum number of edges needed to make a non-planar graph planar, known as the crossing number.
  6. Write a Python function that finds the Eulerian cycle in a connected planar graph with an even number of vertices.
  7. Given a planar graph, write a Python function that colors its vertices using 3 or fewer colors such that no two adjacent vertices have the same color (3-coloring).
  8. Write a Python function that finds the maximum number of edges in a planar graph with n vertices (Maximum Planar Subgraph problem).
  9. Implement a function that generates all non-isomorphic planar graphs with a given number of vertices.
  10. Write a Python function that finds the shortest Hamiltonian cycle in a connected planar graph.

FAQ

  1. Why do we need to keep track of the current face during DFS?: Keeping track of the current face allows us to determine when we have finished exploring one face and are ready to start exploring another.
  2. Can we use Breadth-First Search (BFS) instead of Depth-First Search (DFS) to find faces of a planar graph?: Yes, it is possible to use BFS, but DFS is generally more efficient for this problem because it allows us to explore the graph more deeply before backtracking.
  3. What if our graph is not planar?: If our graph is not planar, we cannot find faces using the algorithm described in this lesson. In such cases, we would need to use more advanced algorithms like Kuratowski's theorem or Hopcroft's algorithm to determine whether a graph is planar and to find its crossing edges if it is not.
  4. What are some applications of finding faces in a planar graph?: Finding faces in a planar graph has various applications, including map labeling, VLSI design, network routing, and more. It can help optimize the layout of electronic circuits to reduce interference between components or minimize the number of layers required for wiring in printed circuit boards. Additionally, it can be used in computer graphics to create 3D models that do not intersect each other.
Finding faces of a planar graph (Data Structures & Algorithms) | Data Structures & Algorithms | XQA Learn