As a Sheldor the conqueror, you're supposed to conquer the kingdom of Wonder-blunder-berg , which happens to be a collection of cities connected with roads.
This time you plan to destroy the bridges, which are roads when rendered unusable, splits the kingdom into parts that cannot communicate with each other.
For example,
In the above kingdom, the red roads are bridges.
This is the Adjacency List of the road network of Wonderblunderberg showing which city has a direct road to which other city.
How many such bridges exist in the kingdom?
Clarification: A component is a maximal set of cities such that there is a walk between any two cities of the set. A bridge is a road which when removed increases the number of components.
Try also: Can you attack cities and conquer ?
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
In the following analysis, we'll assume that the graph is connected. If it is not, we run the algorithm separately for each component.
We could explore our graph using Depth First Search . While doing so, we could think of a spanning tree called the DFS tree . In the DFS tree ,
The non-tree edges are classified into forward edges, backward edges and cross edge depending on the depths they connect.
Consider this graph and its corresponding DFS tree
We define a concept called the lowpoint of a vertex. For each vertex v , the lowest depth of neighbors of all descendants of v in the DFS tree is l o w p o i n t ( v ) . For example, the lowpoint of 2 in this graph is d = 1 , since one of its descendants have a backedge to a vertex at depth 1.
Notice that an edge ( u , v ) is never a bridge if it is not a part of the DFS tree. This is because the DFS tree spans all the vertices in the component, and if there is a way to span u and v without using ( u , v ) , then ( u , v ) is redundant.
An edge u → v on the DFS tree is a bridge if that is the only way to access u from v or vice-versa. This means that there are no back-edges from v to u or its ancestors. Or in other words, d e p t h [ u ] < l o w [ v ]
Let's implement that: