Simple DFS just a bit of Memoraisation of Dynamic Programming . Whenever an Adjacent vertex (child) of a parent vertex (node) is not visited we will send him in dfs function and if it is visited than we will store maximum result of the dpvalue of parent vertex and dpvalue of child vertex+1 in dpvalue of parent .