algorithm - Minimum-size vertex cover of tree: Dynamic Programming Formulation -
i'm trying understand how formulate problem of finding minimum-size vertex cover of tree dynamic programming problem , having trouble. me non-dynamic programming formulation involving depth-first search makes intuitive sense. involves doing dfs leaf nodes, including parent nodes in minimum size vertex cover, , repeating root. pseudocode this:
// dfs based solution find_minimum_vertex_cover_dfs(root) { // leaf nodes aren't in minimum size vertex cover if (root == null || isleafnode(root)) { return; } (each child node of root) { find_minimum_vertex_cover_dfs(child node); // child isn't in minimum size vertex cover need cover edge // current root child including current root if (!isinmincover(child node)) { include root in minimum vertex cover; } } }
the dynamic programming formulation got here follows:
dynamicvc(root): each child c: best[c][0], best[c][1] = dynamicvc(c) withoutroot = sum on c of best[c][1] withroot = 1 + sum on c of min(best[c][0], best[c][1]) return (withoutroot, withroot)
i think understand idea of subproblems being computing minimum size vertex cover subtree rooted @ each vertex including vertex in cover , excluding vertex cover. have 2 questions:
- it seems natural me exploit fact leaf nodes never in minimum vertex cover , hence use dfs-based solution. why 1 use dynamic programming solution problem?
- i'm used building dynamic programming matrix bottom iteratively without using recursion. in case, however, since need start computing solutions leaf nodes traversing tree using recursion build dynamic programming matrix leaf nodes seems intuitive me. seems odd me because dynamic programming problems i've worked on point have avoided need this. missing here?
edit: thought more maybe confusing me in case doing dfs on tree recursively more familiar me. i've been doing bunch of dynamic programming problems first 1 involving tree/graph traversal, , in other problems can use loops compute larger , larger subproblems. suppose make dynamic programming version more familiar me using explicit stack , doing tree traversal way opposed via recursion. make sense?
1: there no reason why. works why not use it. me dp solution you've shown more intuitive recursive one.
2: dynamic programming subproblems memoization of recursive solution. coming dp algorithm involves defining recursion first , adding memoization it. recursive solution can automatically transformed dp: create global hash table of type (subproblem id -> result)
, @ beginning of recursive call check if hashmap contains result given subproblem, if return right away otherwise compute , put hashmap. such approach fast bottom-up approach mentioned.
Comments
Post a Comment