News for hackers. Stuff that matters.
Re:Google Interview Questions Deconstructed: The Knight’s Di... by zinclozenge on Wednesday October 07, 2020 @03:24PM (#24711705)
For anybody wondering, this is a disguised 'count number of walks of length k on a graph' problem.

On a more general note, anytime a problem admits a dynamic programming solution, there almost always is a graph based approach.

Re:Google Interview Questions Deconstructed: The Knight’s Di... by tyrust on Wednesday October 07, 2020 @04:33PM (#24712400)
>On a more general note, anytime a problem admits a dynamic programming solution, there almost always is a graph based approach.

This concept was introduced to me back in my algorithms class and is pretty useful. For anyone looking for a longer explanation, page 167 of the textbook [0] has the nugget and some examples:

>Every dynamic program has an underlying dag (directed acyclic graph) structure: think of each node as representing a subproblem, and each edge as a precedence constraint on the order in which the subproblems can be tackled.

[0] - PDF: http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-...

Re:Google Interview Questions Deconstructed: The Knight’s Di... by avinassh on Thursday October 08, 2020 @03:55AM (#24716912)
> On a more general note, anytime a problem admits a dynamic programming solution, there almost always is a graph based approach.

Whoa, this is something new for me. For e.g. how would you transform calculating nth fibonacci number into a graph problem?

Re:Google Interview Questions Deconstructed: The Knight’s Di... by anandoza on Thursday October 08, 2020 @12:16PM (#24720551)
Draw a graph with vertices labeled 0 through N, then add edges i->(i+1) and i->(i+2). The answer is the number of paths from 0 to N.

This is still pretty specific to counting paths in the same way the original knight problem is, though.

The other comment is talking about how you represent each state for the recursive function as a vertex, then connect it to its dependencies (basically taking the recursion tree, but merging identical calls).

Re:Google Interview Questions Deconstructed: The Knight’s Di... by boyobo on Thursday October 08, 2020 @03:06PM (#24722577)
https://ibb.co/BGj5nDn

Consider the following graph. The nth fibonnaci number is the number of possible walks from n to 1.

(I made a mistake in the picture, there should be an edge from 2 to 1).