Search code examples
c++algorithmsearch-enginegraph-algorithmcode-search-engine

Algorithm for finding internally connected cluster of nodes within a graph from which no edge points outwards


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.


Solution

  • 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.