Chapter 5 · Simulation

Bipartiteness, Cycles and Bridges

← Chapter 5

Bipartiteness, Cycles and Bridges

The same undirected graph is examined from three structural angles.

How to use

  • Pick a graph and press one of the three analysis buttons; Clear removes the highlights.
  • Bipartiteness: the nodes are colored with two colors (blue / red). If the graph is not bipartite, the conflicting edge whose endpoints got the same color is highlighted.
  • Find Cycle: the first cycle found by DFS is highlighted.
  • Find Bridges: all bridges are highlighted.

Bipartiteness test (2-coloring with BFS)

Color a start node, give every neighbor the opposite color, and repeat with BFS. A graph is bipartite if and only if it has no odd-length cycle, that is, if no edge in the coloring joins two nodes of the same color. Time O(V + E).

Cycle detection (DFS)

In an undirected graph, DFS finds a cycle when it meets an already visited node that is not the parent of the current node (a back edge). Following the parent links from the current node up to that node gives the cycle. Time O(V + E).

Bridges (Tarjan)

A bridge is an edge whose removal disconnects the graph. DFS records for each node the discovery time disc[u] and low[u], the earliest discovery time reachable from the subtree of u using at most one back edge. A tree edge (u, v) is a bridge if and only if low[v] > disc[u]. Time O(V + E).