Principle of Greedy algorithm

Greedy algorithm is used to solve a problem with a heuristic way, In many problems, a greedy strategy does not usually produce an optimal solution, but nonetheless a greedy heuristic may yield locally optimal solutions that approximate a globally optimal solution in a reasonable amount of time.

But sometimes it could yield a optimal solution when the problem could be fracted into some Subitem. For example Kruskal’s Minimum Spanning Tree Algorithm use the greedy

Kruskal Algorithm

the process of Kruskal Algorithm

1. Sort all the edges in non-decreasing order of their weight.
2. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If cycle is not formed, include this edge. Else, discard it.
3. Repeat step#2 until there are (V-1) edges in the spanning tree.

1
[Not supported by viewer]
0
[Not supported by viewer]
7
[Not supported by viewer]
8
[Not supported by viewer]
2
[Not supported by viewer]
6
[Not supported by viewer]
5
[Not supported by viewer]
3
[Not supported by viewer]
4
[Not supported by viewer]
4
[Not supported by viewer]
8
[Not supported by viewer]
11
[Not supported by viewer]
8
[Not supported by viewer]
7
[Not supported by viewer]
2
[Not supported by viewer]
6
[Not supported by viewer]
7
[Not supported by viewer]
1
[Not supported by viewer]
2
[Not supported by viewer]
4
[Not supported by viewer]
14
[Not supported by viewer]
9
[Not supported by viewer]
10
[Not supported by viewer]

Another example. solution to the problem of Jump ,This problem also have a feature that it could be separated into sub problems;

The most important in greedy algorithm is Status updating.

In general, greedy algorithms have five components:

  1. A candidate set, from which a solution is created
  2. A selection function, which chooses the best candidate to be added to the solution
  3. A feasibility function, that is used to determine if a candidate can be used to contribute to a solution
  4. An objective function, which assigns a value to a solution, or a partial solution, and
  5. A solution function, which will indicate when we have discovered a complete solution

We can solve the problem with follow code:

class Solution {
public:
    int jump(vector<int>& nums) {
        int res = 0, n = nums.size(), i = 0, cur = 0;
        while (cur < n - 1) {
            ++res;
            int pre = cur;
            for (; i <= pre; ++i) {
                cur = max(cur, i + nums[i]);   // cur状态的更新
            }
            if (pre == cur) return -1; // May not need this
        }
        return res;
    }
};

Next we illustrate the code with above five components:

  1. **candidate set: ** for (; i <= pre; ++i) enumerate(列举,枚举) all the positions that from now status.
  2. selection function: cur = max(cur, i + nums[i]); choose the best candidate of all possible candidates.
  3. feasibility function: this may not need, some problem must have, if (pre == cur) return -1;
  4. In this problem, we can get a complete solution because of the subitems of the big problem.
posted @ 2019-12-30 09:38  虾野百鹤  阅读(153)  评论(1编辑  收藏  举报