Answer:
Greedy is an algorithmic paradigm that builds up a solution piece by piece, always choosing the next piece that offers the most obvious and immediate benefit. Greedy algorithms are used for optimization problems. An optimization problem can be solved using Greedy if the problem has the following property: At every step, we can make a choice that looks best at the moment, and we get the optimal solution of the complete problem.
If a Greedy Algorithm can solve a problem, then it generally becomes the best method to solve that problem as the Greedy algorithms are in general more efficient than other techniques like Dynamic Programming. But Greedy algorithms cannot always be applied. For example, the Fractional Knapsack problem (See this) can be solved using Greedy, but 0-1 Knapsack cannot be solved using Greedy.
The following are some standard algorithms that are Greedy algorithms.
1) Kruskal’s Minimum Spanning Tree (MST): In Kruskal’s algorithm, we create an MST by picking edges one by one. The Greedy Choice is to pick the smallest weight edge that doesn’t cause a cycle in the MST constructed so far.
2) Prim’s Minimum Spanning Tree: In Prim’s algorithm also, we create an MST by picking edges one by one. We maintain two sets: a set of the vertices already included in MST and the set of the vertices not yet included. The Greedy Choice is to pick the smallest weight edge that connects the two sets.
3) Dijkstra’s Shortest Path: Dijkstra’s algorithm is very similar to Prim’s algorithm. The shortest-path tree is built up, edge by edge. We maintain two sets: a set of the vertices already included in the tree and the set of the vertices not yet included. The Greedy Choice is to pick the edge that connects the two sets and is on the smallest weight path from source to the set that contains not yet included vertices.
4) Huffman Coding: Huffman Coding is a loss-less compression technique. It assigns variable-length bit codes to different characters. The Greedy Choice is to assign the least bit length code to the most frequent character. The greedy algorithms are sometimes also used to get an approximation for Hard optimization problems. For example, the Traveling Salesman Problem is an NP-Hard problem. A Greedy choice for this problem is to pick the nearest unvisited city from the current city at every step. These solutions don’t always produce the best optimal solution but can be used to get an approximately optimal solution.
You have to be carefull no bulling cuiberbulling people hurt there self for stuff like that
Answer:
Programming and Software Development
Explanation:
Answer:
The solution code is written in Python 3:
- num = 11
-
- while(num <=88):
- firstDigit = num // 10
- secondDigit = num % 10
-
- if(firstDigit == secondDigit):
- print(num)
-
- num += 1
Explanation:
Firstly, create a variable, num, to hold the starting value, 11 (Line 1)
Create a while loop and set the loop condition to continue the loop while num smaller or equal to 88 (Line 3).
Within the loop, we can use // to perform the floor division and get the first digit of the num. We use modulo operator % to get the second digit of the num. (Line 4 - 5)
If the firstDigit is equal to secondDigit, print the number (Line 7 -8). Lastly, increment the num by one and proceed to next iteration (Line 10).
This question is incomplete. The complete question, answer & explanation for this question is given in the attachment below.