-3

Suppose an array of integers is given:

{1,3,2,4,6,5,2} 
- MAX: 6

Using Brute force, finding maximum element in this is to observe every element (each element is a candidate for the solution), thus searching entire search space.

Considering Greedy approach, we will modify the maximum element at each element of the array if necessary. Thus a local optimal choice will eventually lead to global optimal choice.

Are brute force and greedy approach similar here? So what exacty the difference between two algos in general?

Approach:

Brute force - Starting from first element - 1 taking it as max. Now considering each and every next element in array - entire search space. Choosing maximum at each step if necessary. This is brute force.

Greedy Algorithm - starting from nothing, taking first element - taking it max as 1. Then considering second element - 3, making local optimal choice between 1 and 3- taking 3 as maximum. And so on for other elements. At last, we will have 6 as maximum element- the global optimal choice.

How will you exactly tell the difference between the two algos in general?

copo
  • 41
  • 10
  • 1
    You haven't shown an approach, you've described them both. Your question is unclear – reggaeguitar Jun 25 '15 at 16:30
  • Brute force - Starting from first element - 1 taking it as max. Now considering each and every next element in array - entire search space. Choosing maximum at each step if necessary. This brute force. Greedy Algorithm - starting from nothing, taking first element - taking it max as 1. Then considering second element - 3, making local optimal choice between 1 and 3- taking 3 as maximum. And so on for other elements. At last, we will have 6 as maximum element- the global optimal choice. – copo Jun 25 '15 at 16:41
  • I don't know what your goals are, but this problem is a bad one to compare and contrast brute force vs. greedy strategies. Your greedy algorithm could also be the description for divide & conquer strategies. – Hyo Byun Jun 25 '15 at 20:59
  • I was confused as to what I should call the above problem - a brute force or greedy. It raised some doubts in me. May be the above should be categorized as both brute force and greedy. – copo Jun 26 '15 at 06:05
  • If you will see this question -[link](http://codeforces.com/contest/463/problem/B) (http://codeforces.com/contest/463/problem/B). Again there is a problem between deciding if it belongs to greedy or brute force paradigms. – copo Jun 26 '15 at 06:34

2 Answers2

0

Finding the maximum element in an unsorted array will require the brute-force approach.

There are ways of minimizing the complexity of this operation by using data-structures such as heaps, which sort the data as it is being added to the structure.

0

A greedy algorithm might look something like this:

pick random starting point
if any of the two neighbors have higher value:
    move to the neighbor
else:  
    return value at current point

I would not call the linear scan of an array a greedy algorithm.

James
  • 1,198
  • 8
  • 14
  • Array: {1,3,2,4,6,5,2} So if you start at the second element - '3'. Then going by your approach, the two neighbors of 3 - 1 and 2, will not have larger value than 3 and so answer will be 3 but maximum is 6. – copo Jun 25 '15 at 16:46
  • Right, the greedy approach gives a local maximum, which may be different from the global maximum. I'm not claiming that this algorithm solves your problem. – James Jun 25 '15 at 17:17
  • But the greedy approach that I have mentioned in the question is correct. Isn't it ? A linear scan if we are making locally optimal choices may be considered as greedy. It's just that I am unable to differentiate Brute Force and Greedy algorithms clearly in the above problem. – copo Jun 25 '15 at 17:31