java - Time complexity analysis of minimum set cover solution -
for question
there n persons , k different type of dishes. each person has preference each dish. either likes or not. need feed people. every person should atleast 1 dish of chioce. minimum number of different type of dishes can order?
one of solution is,
public class optimumdish { private set<integer> result = new hashset<integer>(); public void print(){ for(int r:result) system.out.print(r + " "); } // find optimum dish navigating available options public void find(int[][] m, int r, int c, int mr, int mc, stack<integer> dishes) { dishes.push(c); if (r == mr) { // reached last person. unique dishes set<integer> d = new hashset<>(dishes); if(result.size() == 0 || result.size() > d.size()) result = d; } else if (r < mr) { // check next person's preferred dish (int = 0; <= mc; i++) { if (m[r + 1][i] == 1) { find(m, r+1, i, mr, mc, dishes); break; } } } dishes.pop(); // current dish may not optimum. // check other dish same person (int = c + 1; <= mc; i++) { if (m[r][i] == 1) { find(m, r, i, mr, mc, dishes); } } } public static void main(string[] args) { int[][] m = { { 0, 1, 1, 0, 0, 0, 0 }, { 0, 1, 0, 1, 0, 0, 0 }, { 0, 1, 1, 0, 0, 1, 0 }, { 1, 0, 0, 1, 0, 0, 0 }, { 0, 0, 1, 0, 1, 0, 0 }, { 0, 0, 0, 1, 0, 0, 1 } }; int mr = m.length - 1; int mc = m[0].length - 1; int c = 0; (int = 0; <= mr; i++) { if (m[0][i] == 1) { c = i; break; } } optimumdish od = new optimumdish(); stack<integer> dishes = new stack<>(); od.find(m, 0, c, mr, mc, dishes); od.print(); } }
this problem of type 'minimum set cover'. since np-complete problem, can't solved in polynomial time. per solution can solved in polynomial time?
please let me know time complexity of solution? o(n^4)?. thanks.
Comments
Post a Comment