4

I am representing my graph as a adjacency list. I want to know how can I find a cluster of nodes which are internally connected but no edge points outwards from them. Is there any well known algorithm out there which I can use?

for e.g.This is my graph.

1---->2
2---->1
2---->3
3---->1
3---->4
4---->5
5---->4

Here nodes 4 and 5 are internally connected. Yet no outside edge comes from this. This would be my answer. Similarly nodes 1,2,3 even though they form a cycle, do not fit the criteria as an outside edge emanates from node 3. So it is not same as finding a cycle in a adjacency list.

Optional read: (why I need this) I am working on a Ranking page (search engine) algorithm, nodes like 4 and 5 are called rank-sink.

Anon
  • 2,435
  • 5
  • 23
  • 36

1 Answers1

7

You could detect strongly connected components using Kosaraju, Tarjan or Cheriyan-Mehldorn/Gabow algorithm.

After finding these components, you compress each strongly connected components into one single node (i.e. you represent a whole component by a single node).

In the resulting graph, you look for nodes with no outgoing edges. These nodes represent the components you are interested in.

phimuemue
  • 30,699
  • 8
  • 74
  • 109
  • Thanks. I did not get the compressing each strongly connected components into one single node. How do I do that? – Anon Sep 14 '11 at 13:07
  • 1
    @Anon: by substituting all references to any of the nodes in the component by the component itself, i.e. if `1, 2, 3` belongs to `A` component you substitute `5 ----> 1` with `5 ----> A` and remove entries like `1 ----> 2` entirely. – Grozz Sep 14 '11 at 13:29