Greedy algorithms Flashcards
What is Greedy?
Make a local optimal choice in hope that it will ultimately lead to global optimal solution.
Greedy specifics
- Control-function
- Selection-function
- Feasibility function
- Objective function
- Solution function
Greedy requirements!!!
- Feasible
- local optimal
- irrevocable
Dijkstra’s algorithm
Choose starting node. Continue to keep track of final score and last visited node. Begin by looking at all edges from starting node and value which one is the best etc. Proceed to the nodes that are directly connected to the starting node and repeat process. Remember not to compare against the node that you came from, however compare against nodes that you have visited before but from a different position. If new path has smaller value then replace it.
Minimum spanning tree
a way to connect all nodes with smallest final weight (value).
Prim’s algorithm
Begin with selecting any vertex. Look at all the edges that the vertex has and choose the one with smallest weight (value). Now you have 2 vertices in your solution. Look at all the edges available from those vertices and choose the one with smallest value. Repeat until all vertices are in solution.
Kruskal’s algorithm
Find and start with edge with smallest weight. Find the second smallest edge that doesn’t create a cycle with previously selected edges. Repeat until all vertices are in solution.
Difference between Prim and Kruskal?
Prim: vertex approach
Kruskal: smallest weight edge approach