# Algorithm to Group Vertices of Graph

Given is the following graph which is logically divided into layers (with Dijkstra's shortest paths algorithm):

 Vertices   Layer

Root      0
/   \
A     B     1
/ \    |
C   D   E     2
\  |  /
\ | /
F         3


Now I'm looking for an algorithm which groups vertices when they have a (single) common ancestor in the previous layer, e.g. for the graph in the example the groups would be:

0: A, B
1: C, D
2: E
3: F


I know that this is doable by visiting vertices and comparing ancestors but I was wondering whether there is a well known algorithm for it.

Update: My question is really only related to find groups. I'm aware of the fact, that I can traverse vertices and test for incoming edges and group those vertices. Furthermore, the graph is fully constructed.

One (now deleted) answer mentioned DFS, which creates a search forest (as BFS creates a search tree which I basically used for levels, though I mentioned Dijkstra). So, I assume that combining BFS and DFS could give me the desired result.

• I've just found Lowest Common Ancestor, e.g. here. But this seems to work only on pairs. – Sebastian Dressler Dec 1 '13 at 18:10
• How is this different from simply going through vertices from root to "leaves" and grouping their children? – arsaKasra Dec 1 '13 at 19:23
• There is no difference. I just asked for the name of an algorithm, if there is any common known. – Sebastian Dressler Dec 1 '13 at 20:58
• So, you are talking about latices? – Jens Piegsa Dec 7 '13 at 12:16
• @JensPiegsa I'm unsure whether my stated problem always fulfills the criterions of a lattice, maybe I should prove this somehow? – Sebastian Dressler Dec 7 '13 at 14:39

this seems to be known as the "Lowest Common Ancestor" problem of graphs. see eg

Maybe you can use A* algorithm as a better alternative to dijkstra and use the metric function ( heuristic ) of a* to group or flag your vertices in an appropriate way.

Change due to assessment.

I think there is no appropriate algorithm doin that. Because you need to span the whole graph before knowing which connections are established. With the metric or heuristic approach you can predict some kind of grouping during the evolvement of the graph by defining rules for grouping within your metric function.

Best regards.

• I used Dijkstra only to determine the layers. This is separated from the problem of grouping. – Sebastian Dressler Dec 7 '13 at 14:44
• Yeah i know. I thought you wanted to realize grouping during the evolvement of the graph. If not i don't understand your question because after the creation of the graph you have all information you need to group by simply traverse the every node of every layer. Please concrete your question. – Christian Schack Dec 7 '13 at 17:08
• Thanks for clarification, I updated the question to get things clearer a bit. Also thanks for mentioning A* which is of great interest for this problem w/r/t a varied scenario, i.e. a not fully constructed graph. – Sebastian Dressler Dec 7 '13 at 20:41

I think Breath First Search would work, (by searching for a node not in the graph so, I guess check for a NIL node) and just print out the adjacent edges, when marking them as searched. It will search all the nodes until there are no nodes left to search and NIL is found.