Graph Theory37 sections · 1633 units
Open in Course

Assign Cookies - Matching Solution

(Bipartite matching approach)

You can also model this as bipartite matching: child ii connects to cookie jj if and only if size[j]greed[i]\text{size}[j] \geq \text{greed}[i]. Run Kuhn's algorithm to find the maximum matching between children and cookies.

This approach is O(nmm)O(nm \cdot m) worst case, which is slower than the greedy solution.

But it demonstrates that matching can solve problems where greedy also works. For this specific problem, use greedy. Know the matching alternative exists.

Space complexity is O(V)O(V) for the data structures used.