Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
44 views

What Is Greedy Algorithm?

Greedy algorithms make locally optimal choices at each step to find a global optimal solution. They work by choosing the optimal choice for the current subproblem in a way that builds toward the optimal solution. Examples given include using a greedy approach to find minimum spanning trees via Kruskal's and Prim's algorithms, and to solve the traveling salesperson problem by always moving to the nearest unvisited city. Dijkstra's algorithm also provides a greedy approach for finding the shortest paths between nodes in a graph.

Uploaded by

Yuyun Joe
Copyright
© © All Rights Reserved
0% found this document useful (0 votes)
44 views

What Is Greedy Algorithm?

Greedy algorithms make locally optimal choices at each step to find a global optimal solution. They work by choosing the optimal choice for the current subproblem in a way that builds toward the optimal solution. Examples given include using a greedy approach to find minimum spanning trees via Kruskal's and Prim's algorithms, and to solve the traveling salesperson problem by always moving to the nearest unvisited city. Dijkstra's algorithm also provides a greedy approach for finding the shortest paths between nodes in a graph.

Uploaded by

Yuyun Joe
Copyright
© © All Rights Reserved
You are on page 1/ 4

Dwi Putri Wahyuningsih

2019390004

Assignment: Intro to Greedy Algorithm

Write your exploration in 30 minutes and upload your answer here in the repository

1. What is greedy Algorithm?


A greedy algorithm is a simple and intuitive algorithm used in optimization problems.
The algorithm makes optimal choices at every step while trying to find the overall
optimal way to solve the entire problem. A greedy algorithm takes all the data in a
specific problem, then sets rules for which elements to add to the solution at each
algorithm step. In the animation above, the data set is all the numbers in the graph, and
the rule is to choose the largest number available at each chart level. Algorithm-built
solutions are the sum of those choices.
2. How to find the optimal solution in greedy Algorithm?
 Greedy choice property: A global (overall) optimal solution can be reached by
choosing the optimal choice at each step.
 Optimal substructure: A problem has an optimal substructure if an optimal
solution to the entire problem contains the optimal solutions to the sub-problems.

In other words, greedy algorithms work on problems for which it is true that, at every
step, there is a choice that is optimal for the problem up to that step, and after the last
step, the algorithm produces the optimal solution of the complete problem.

3. What is Huffman encoding?


Huffman Encoding (also known as Huffman Coding) is an algorithm for performing data
compression and forms the basic idea behind file compression. This port talks about the
fixed length and variable length coding, unique decodable code, prefix rules and Huffman
Tree constructs. Each character is stored in a sequence of 0 and 1 using 8 bits. This is
called fixed length encoding because each character uses the same number of fixed bit
stores.
4. How to find minimum spanning tree?
There are two famous algorithms for finding the Minimum Spanning Tree:
 Kruskal’s Algorithm
Kruskal’s Algorithm builds the spanning tree by adding edges one by one into a
growing spanning tree. Kruskal's algorithm follows a greedy approach as in each
iteration it finds an edge which has the least weight and adds it to the growing
spanning tree.
Algorithm Steps:
o Sort the graph edges concerning their weights.
o Start adding edges to the MST from the edge with the smallest weight
until the edge of the largest weight.
o Only add edges which do not form a cycle, edges which connect only
disconnected components.
 Prim's Algorithm
Prim's algorithm also uses a Greedy approach to find the minimum spanning tree.
In Prim's Algorithm, we grow the spanning tree from the starting position. Unlike
the edges in Kruskal, we add corner points to the spanned trees that grow in Prim.
Algorithm Step:
o Maintain two intermittent sets of knots. One contains nodes that are in the
growing span tree and the other that is not in the growing span tree.
o Select the cheapest node connected to the growing span tree and not in the
growing span tree and add it to the growing span tree. This can be done
using Priority Queues. Insert the vertices, which are connected to the
growing spanning tree, into the Priority Queue.
o Check the cycle. To do that, mark the nodes that have been selected and
enter only those nodes in the unmarked Priority Queue.
5. By using a greedy algorithm, how to find the solution for TSP?
Here are the steps:
 Create two main data holders:
o The list that stores the index of cities in terms of the input matrix of distances
between cities.
o A result array which will have all the cities which can be displayed to the
console in any way.
 Traversal the specific neighborhood matrix tsp [][] for all cities and if the cost to
reach any city from the current city is less than the current cost updates the cost.
 Generate the minimum path cycle using the steps above and return to it the
minimum cost.
6. Explain the following greedy algorithm:
 Dijkstra’s algorithm for finding the shortest path in a graph
Dijkstra’s algorithm finds the shortest path tree from a single source node, by
building a set of nodes that have a minimum distance from the source.
The graph has the following:
o vertices, or nodes, denoted in the algorithm by v or u;
o weighted edges that connect two nodes: (u,v) denotes an edge, and w(u,v)
denotes its weight. In the diagram on the right, the weight for each edge is
written in grey.
 Kruskal’s algorithm for finding a minimum-cost spanning tree
Kruskal's algorithm for finding minimal-cost spanning trees uses a greedy
approach. This algorithm treats the graph as a forest and each node it has as an
individual tree. A tree is only connected to others and only if, it has the lowest
cost among all the available options and does not violate MST properties.
Steps:
o Remove all loops and Parallel Edges
o Arrange all edges in increasing order of weight
o Add the edge with the least weight
 Prim’s algorithm for finding a minimum-cost spanning tree
Prim's algorithm for finding minimum cost spanning trees (as Kruskal's
algorithm) uses a greedy approach. Prim's algorithm has a region with the shortest
path first algorithm. Prim's algorithm, in contrast to Kruskal's algorithm, treats
vertices as single trees and keeps adding new nodes to the spanning tree of the
given graph.
Steps:
o Delete all circles and parallel edges
o Select any node as the root node
o Check the edges out and choose one with a lower cost

You might also like