How to find a triangle inside a graph?

前端 未结 4 749
刺人心
刺人心 2021-01-31 18:05

Here is an exercise in the Algorithm Design Manual.

Consider the problem of determining whether a given undirected graph G = (V, E) contains a triangle

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-31 18:20

    Here is what I think :

    The origianl BFS solution is incorrect as pointed above. But we can modify the DFS. Assign numbers to the nodes visited as we visit each vertex in the DFS. Now, if we reach a vertex( in the question I saw cross edges, there are none in an undirected graph), we check its adjacency list and suppose one vertex is discovered(but not processed, cannot happen), then we check its number. If the difference is 2 then there is a cycle of length 3.

提交回复
热议问题