Question

First: I admit that its a part of programming contest(where nothings on a stake to win or something)

I've come to the following conclusion after reading the problem & tried following algorithm.

Given Undirected Connected Graph of n vertices,

count = 0
For i=1 to n:
  remove(ith vertex)
  check for connectivity of graph with remaining vertices
  if connected 
    then increment count
  attach the removed vertex back
print count

I've implemented this by two ways: (1) DFS (2) Disjoint-Set Union but none of the algorithms are efficient enough to get AC. Does there exist even better ways to do so? I don't need detailed explanations, few words will do, rest I'll figure out or die trying :p. Thank You!

Was it helpful?

Solution 2

Depending on your input graph, there're several options, each with its own advantages. I personally wouldn't simulate the failure of every node and then check for connectivity in the whole graph, that takes too much time.

A more clever approach could be to construct a spanning tree. Every leaf on that tree can be removed without disconnecting the rest of the graph as the rest of the tree still connects everything else. Building that one tree allows you to skip the leaves from individual examination. You can even construct more trees to try and get some of the other nodes (non-leaves) to be leaves (which can then be skipped). BFS from several nodes could get you started.

Also, when you simulate the removal of 1 node, you only have to check if its neighbours are still connected to each other. If the neighbours are connected, then the rest of the graph is also connected. If the graph is dense, this should limit the work per node.

OTHER TIPS

A vertex whose removal makes a graph disconnected is called an articulation point. All articulation points in a graph can be found in linear time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top