0% found this document useful (0 votes)
15 views7 pages

Kruskal Analysi

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views7 pages

Kruskal Analysi

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Basics of Greedy Algorithms

Introduction

In an algorithm design there is no one 'silver bullet' that is a cure for all computation problems. Different problems

require the use of different kinds of techniques. A good programmer uses all these techniques based on the type of

problem. Some commonly-used techniques are:

1. Divide and conquer

2. Randomized algorithms

3. Greedy algorithms (This is not an algorithm, it is a technique.)

4. Dynamic

What is a 'Greedy algorithm'?

A greedy algorithm, as the name suggests, always makes the choice that seems to be the best at that moment.
This means that it makes a locally-optimal choice in the hope that this choice will lead to a globally-optimal
solution.
A Greedy algorithm is an approach to solving a problem that selects the most appropriate option

based on the current situation. This algorithm ignores the fact that the current best result may not

bring about the overall optimal result. Even if the initial decision was incorrect, the algorithm never

reverses it.

How do you decide which choice is optimal?

Assume that you have an objective function that needs to be optimized (either maximized or minimized) at a given

point. A Greedy algorithm makes greedy choices at each step to ensure that the objective function is optimized. The

Greedy algorithm has only one shot to compute the optimal solution so that it never goes back and reverses the

Decision programming.

Greedy algorithms have some advantages and disadvantages:

1. It is quite easy to come up with a greedy algorithm (or even multiple greedy algorithms) for a problem.

2. Analyzing the run time for greedy algorithms will generally be much easier than for other techniques (like

Divide and conquer). For the Divide and conquer technique, it is not clear whether the technique is fast or

slow. This is because at each level of recursion the size of gets smaller and the number of sub-problems

increases.

3. The difficult part is that for greedy algorithms you have to work much harder to understand correctness

issues. Even with the correct algorithm, it is hard to prove why it is correct. Proving that a greedy algorithm is
correct is more of an art than a science. It involves a lot of creativity.

How to create a Greedy Algorithm?

Steps for Creating a Greedy Algorithm

By following the steps given below, you will be able to formulate a greedy solution for the given

problem statement:

Step 1: In a given problem, find the best substructure or sub problem.

Step 2: Determine what the solution will include (e.g., largest sum, shortest path).

Step 3: Create an iterative process for going over all subproblems and creating an optimum

solution.

Being a very busy person, you have exactly T time to do some interesting things and you want to do maximum

such things.

You are given an array A of integers, where each element indicates the time a thing takes for completion. You
want to calculate the maximum number of things that you can do in the limited time that you have.

This is a simple Greedy-algorithm problem. In each iteration, you have to greedily select the things which will
take the minimum amount of time to complete while maintaining two variables currentTime and numberOfThings. To

complete the calculation, you must:

1. Sort the array A in a non-decreasing order.

2. Select each to-do item one-by-one.

3. Add the time that it will take to complete that to-do item into currentTime.

4. Add one to number Of Things.

Repeat this as long as the currentTime is less than or equal to T.

Let A = {5, 3, 4, 2, 1} and T = 6

After sorting, A = {1, 2, 3, 4, 5}

After the 1st iteration:

currentTime = 1

number Of Things = 1

After the 2nd iteration:

currentTime is 1 + 2 = 3

number Of Things = 2
After the 3rd iteration:

Current Time is 3 + 3 = 6

numberOfThings = 3

After the 4th iteration, currentTime is 6 + 4 = 10, which is greater than T. Therefore, the answer is 3.

Implementation

Applications of Greedy Algorithm

Following are few applications of the greedy algorithm:

Used for Constructing Minimum Spanning Trees: Prim’s and Kruskal’s Algorithms used to

construct minimum spanning trees are greedy algorithms.

Used to Implement Huffman Encoding: A greedy algorithm is utilized to build a Huffman tree that

compresses a given image, spreadsheet, or video into a lossless compressed ¦le.

Used to Solve Optimization Problems: Graph - Map Coloring, Graph - Vertex Cover, Knapsack
Problem, Job Scheduling Problem, and activity selection problem are classic optimization

problems solved using a greedy algorithmic paradigm

KRUSHKAL

You might also like