1/9
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Greedy Algorithms
Solve your problem in stages
In each stage, choose the locally optimal choice
Fast! But for many problems, can be incorrect, or give non-optimal solutionsโฆ
Maybe surprisingly, it turns out that they:
Can approximate (for some problems) the optimal solution (and fast),
Solve some very well-known problem
Greedy Algorithm for the Euclidian Travelling Salesman Problem
Starting at node 1:
Repeatedly visit nearest node (to current)
When you have visited all nodes, go back to origin city
Does not output the shortest route
Greedy Algorithm for Shortest Paths
Start at the source node, and visit in each stage, the (yet) unvisited node which is closest to the source
This is Dijkstraโs algorithm
But importantly, it gives an optimal solution to the shortest path computation problem
Greedy Algorithm for Vertex-3 Colouring
Greedy Algorithm for Vertex-2 Colouring
Greedy Algorithm for Maximal Independent Set
Initialize ๐ผ = โ
For ๐ = 1, โฆ , ๐ :
Check if node ๐ฃ๐ has any neighbours in ๐ผ
If not, then add ๐ฃ๐ to ๐ผ, or also: ๐ผ = ๐ผ โช {๐ฃ๐}
Outputs a correct solution (i.e., an MIS)
Greedy Algorithm for Maximum Independent Set
Initialize ๐ผ = โ
For ๐ = 1, โฆ , |๐|:
Check if node ๐ฃ๐ has any neighbours in ๐ผ
If not, then add ๐ฃ๐ to ๐ผ, or also: ๐ผ = ๐ผ โช {๐ฃ๐}
Will not output a correct solution (MaxIS) but can output an approximation if all nodes have few neighbours
Greedy Algorithm for Minimum-Weight Spanning Trees (MST)
Kruskalโs Algorithm
Primโs Algorithm
Kruskalโs Algorithm
Start with tree ๐ = โ ,
Sort edges from lowest to highest weight,
For ๐ = 1, โฆ , ๐ธ :
If edge ๐๐ does not form a cycle when added to ๐, then add ๐๐ to ๐, or also: ๐ = ๐ โช {๐๐}
Takes ๐(|๐ธ|log|๐|) worst-case time
๐(|๐ธ|log|๐|) time for sorting edges
Checking for all |๐ธ| edges whether they create a cycle is a bit harder to bound
Output is a spanning tree is trivial to show (spanning + no cycles)
Minimum-weight can be shown by proof of induction
Induction step is a bit tricky
Primโs Algorithm
Start with ๐๐ = ๐ฃ1 and ๐ธ๐ = โ
While ๐๐ โ ๐:
Find the minimum weight edge {๐ข, ๐ค} going from a node in ๐ to a node outside ๐,
Add {๐ข, ๐ค} to ๐ธ๐,
Without loss of generality, ๐ข โ ๐. Then add ๐ค to ๐๐
Best implementation gives ๐(|๐ธ|+|๐| log|๐|) worst-case time
Easier implementations give ๐( ๐2) or ๐(|๐ธ|log|๐|) worst-case time
Output is a spanning tree is trivial to show (spanning + no cycles)
Minimum-weight also a bit tricky to show (just as for Kruskalโs algorithm)