Tuesday 11 March 2014

Greedy Algorithms


A greedy algorithm repeatedly executes a procedure which tries to maximize the return based on examining local conditions, with the hope that the outcome will lead to a desired outcome for the global problem. In some cases such a strategy is guaranteed to offer optimal solutions, and in some other cases it may provide a compromise that produces acceptable approximations.
Typically, the greedy algorithms employ simple strategies that are simple to implement and require minimal amount of resources.

Prim’s Minimal Spanning Tree Algorithm

Grows a tree by adding in each step a branch with minimal cost. Despite this “short sight” approach, the outcome is optimal.

Kruskal’s Minimal Spanning Tree Algorithm

At each stage, the edge with the least cost is processed.

 Dijkstra’s Single-Source Shortest Paths Algorithm

Establish the shortest path between a single source node and all of the other nodes in a graph.
Greedy algorithm: Add an edge that connects a new node to those already selected. The path from the source to the new node should be the shortest among all the paths to nodes that have not been selected yet.
 a0OO ---1--- bOO ---7---- oo OcO
 |  ----     oo      ---- |
7|    2----  |3 ---6    |9
 dOO -------- OeO -------  OfO
  oo  ---8     oo     4     oo 
3|    ----   |5         |8
 g    5  ---
  oo OO---8--- h oo OO ---1---  oo OiO  a0OO ---1--- b1O,aO ---7---- oo OcO
 |  ----     |    ----- |
7|    2 ---- |3----6    |9
 dOO -------- OeO -------  OfO
  oo  ---8     oo     4     oo 
3|    5----  |5         |8
 gOO --------hOO -------  OiO
  oo     8     oo     1     oo   a     1          7     c
 0OO ------- b1O,aO -------- oo OO
 |   ----    |    ----  |
7|    2 ---- |3----6    |9
 dOO ------- 2Oe,aO -------  OfO
  oo  ---8     |    4     oo 
3|    5----  |5         |8
 gOO --------hOO -------  OiO
  oo     8     oo     1     oo   aOO ---1--- bOO ---7---  OcO
 0  --      1,a       -- oo 
7|   -----   |3  ----   |9
      2  ---   --- 6
 d oo OO ------- 2Oe,aO ------- 6Of,Oe
 |  ---8-    |    44     |
3|    5 ---- |5         |8
 gOO ------- hOO -------  OiO
  oo     8     oo     1     oo   aOO ---1--- bOO ---7---  OcO
 0  ---     1,a      --- oo 
7|    ----   |3  ----   |9
 d    2  --- e --  6    f
7,OaO ---8--- 2O,aO ---44--- 6O,Oe
 |   ----    |          |
3|    5 ---- |5         | 8
 g oo OO ------- hOO -------  oo OiO
       8     oo     1  aOO ---1--- bOO ---7---  OcO
 0  ---     1,a     ---- oo 
7|    2----  |3 ---6    |9
 dOO -------- OeO -------  OfO
7,a -- 8    2,a    44    6,e
3|    ----   |5         | 8
      5  ---
 g oo OO ------- h7O,eO -------  oo OiO
       8          1  aOO ---1--- bOO ---7---  OcO
 0  ----    1,a     ---- oo 
7|    2----  |3 ---6    |9
 dOO -------- OeO -------  OfO
7,a -- 8    2,a    44    6,e
3|    ----   |5         | 8
      5  ---
 g oo OO ---8--- h7O,eO ---1--- 8Oi,Oh  a0OO ---1--- b1O,aO ---7----8Oc,Ob
 |  ----     |     ---- |
7|    2----- |3----6    |9
 dOO -------- OeO -------  OfO
7,a ---8    2,a    44    6,e
3|    ----   |5         | 8
 g  --5-----h  -------  i
  oo OO   8    7O,eO    1    8O,Oh        1          7     c
 a0OO ------- b1O,aO --------8O,Ob
 |   ----    |    ----  |
7|    2 ---- |3----6    |9
 dOO ------- 2Oe,aO -------  OfO
7,a ---8     |    44    6,e
3|    5----  |5         | 8
 gOO --------hOO -------  OiO
10,d    8    7,e    1    8,h
The algorithm has time complexity O(||2). (Note the similarity and difference relatively to Prim’s algorithm.)

Coin Change

The problem asks to provide a change for a specified amount, using a minimal number of coins. A greedy algorithm may at each stage employ the criteria of providing the largest available coin which is not greater than the amount still owed.

Egyptian Fractions

Each fraction can be expressed as a sum of different fractions with unit numerators. Such representations have been used by the ancient Egyptians.
 87   1   1   1
110 = 2 + 5 + 11
A given fraction might have more than one Egyptian representation. A greedy algorithm, for finding such a representation, can at each stage add to the sum the largest unit fraction which does not cause the sum to exceed the fraction. Fibonacci proved that this greedy algorithm is guaranteed to halt.

Map Coloring

The map coloring problem asks to assign colors to the different regions, where adjacent regions are not allowed to have the same color. There is no general algorithm to assign minimal number of colors, yet each map has an assignment which uses no more than four colors.
A greedy approach repeatedly choose a new color, and assign it to as many regions as possible.

Voting Districts

Given an integer number N, and a map in which each region is assigned an integer number (= population size), the problem asks to create district with minimal deviation of population. The regions in each district must be connected.
A greedy algorithm can start by choosing the biggest regions as cores of the different districts. Then, in each iteration, the largest unassigned region, among those that are adjacent to assigned regions, is assigned to the district with the smallest population.

Vertex Cover

A vertex cover of a graph = (V, E) is a subset of nodes, where the nodes of the subset are attached all the edges of the graph. The problem of finding a vertex cover of minimum size is NP-complete, implying the non-existence of efficient algorithms for solving the problem accurately.
A greedy algorithm may provide approximated solutions, by selecting in each stage a vertex which covers the most edges that have not been covered yet.

0/1 Knapsack

Given a set of item (vi, wi), and a container of capacity C, find a subset of the items that maximizes the value  sum  vi while satisfying the weight constraints  sum  wi < C. This problem is a NP-hard problem, requiring an exhaustive search over the 2N possible combinations of items, for determining an exact solution.
A greedy algorithm may consider the items in order of decreasing value-per-unit weight vi/wi. Such an approach guarantees a solution with value no worst than 1/2 the optimal solution.

No comments:

Post a Comment

 

FACEBOOK PAGE

SKETCHES & PAINTINGS