Greedy algorithm

1.What is greedy algorithm?

You could get basic introduction from Chrome,Edge,or any other brower.It’s definition is ‘A greedy algorithm is any algorithm that follows the problem-solving heuristic of making the locally optimal choice at each stage.’ You may feel puzzled about ‘locally optimal choice’, The basic Strategy is expand a node that you think is closest to a goal state. This will be introduced in the following passage.

2. Essence and principle about greedy algorithm

[photo]

In the whole system of searching, before talking about the greedy algorithm, we have to talk about Depth First Search Algorithm (DFS). In fact, Greedy algorithm optimize from DFS, though sometimes Greedy algorithm performs worse than DFS.
As for DFS, boundary branches are selected to traverse first without traceback. In the DFS algorithm, to get the optimal solution to the problem, a branch of searching tree is totally traversed without breadth extend(from beginning to root). After boundary branches finish traversing, the node which is first discovered to be the node that its branches are all traversed,will be traced back to initial node. Repeat this action until all the nodes are traversed. This algorithm is easy to understand and operate, but if DFS the most optimal algorithm? Absolutely not! As far as you concerned, DFS is a type of blind search. This could cause low effect and bad feedback. As usual, direct DFS algorithm has few opportunity to use. Similarly, Breadth First search Algorithm(BFS) is a type of blind search as well. Compared with DFS, BFS traverse nodes layer by layer instead of traverse depth first. And this will introduce greedy algorithm.The greedy expand a node that you think is cloest to a goal state. What’s more, it just consider the goal proximity. Starting with the initial node, there will must be a step which need to decide which one to choose from several nodes. The most optimal node will be selected(this also includes many condition,such as least steps,shotest theoretical distance). Repeat this circulation until the root note.There is no doubt that at least one step of traverse is a part of whole optimal solution.In other words, greedy algorithm solution must be part of whole optimal solution. However, in most circumstance, greedy algorithm takes the node which consistent the feature of conditionally optimal choice. It essence is to resolve the whole optimal problem into several conditional optimal problem. Theoretically, if and only if one sub problem is left and this sub problem has optimal sub structure, global optimal solution could be found. Instead of traverse all the nodes in searching system, greedy algorithm is a type of intelligent search. This is a basic introduction of greedy algorithm.

3.Code example

1. You are given an array prices where prices[i] is the price of a given stock on the ith day.Find the maximum profit you can achieve. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
链接:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii

public solution{
    public int maxProfit(int[] prices){
        int sum = 0;
        for (int i = 0,i< prices.length-1;i++){
            if (prices[i]<prices[i + 1]{
                sum += prices[ i + 1]-prices[i];
            }    
        }
        return sum;
    }    
}    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值