Greedy Algorithms

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/9

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

10 Terms

1
New cards

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

2
New cards

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

<p>Starting at node 1:</p><ul><li><p>Repeatedly visit nearest node (to current)</p></li><li><p>When you have visited all nodes, go back to origin city</p></li></ul><p>Does not output the shortest route</p>
3
New cards

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

4
New cards

Greedy Algorithm for Vertex-3 Colouring

knowt flashcard image
5
New cards

Greedy Algorithm for Vertex-2 Colouring

knowt flashcard image
6
New cards

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)

<p>Initialize <span style="font-family: &quot;Cambria Math&quot;">๐ผ</span> = <span style="font-family: &quot;Cambria Math&quot;">โˆ…</span></p><p>For <span style="font-family: &quot;Cambria Math&quot;">๐‘–</span> = 1, โ€ฆ , <span style="font-family: &quot;Cambria Math&quot;">๐‘‰</span> :</p><ul><li><p>Check if node <span style="font-family: &quot;Cambria Math&quot;">๐‘ฃ๐‘–</span> has any neighbours in <span style="font-family: &quot;Cambria Math&quot;">๐ผ</span></p></li><li><p>If not, then add <span style="font-family: &quot;Cambria Math&quot;">๐‘ฃ๐‘–</span> to <span style="font-family: &quot;Cambria Math&quot;">๐ผ</span>, or also: <span style="font-family: &quot;Cambria Math&quot;">๐ผ</span> = <span style="font-family: &quot;Cambria Math&quot;">๐ผ โˆช</span> {<span style="font-family: &quot;Cambria Math&quot;">๐‘ฃ๐‘–</span>}</p></li></ul><p>Outputs a correct solution (i.e., an MIS)</p>
7
New cards

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

8
New cards

Greedy Algorithm for Minimum-Weight Spanning Trees (MST)

  • Kruskalโ€™s Algorithm

  • Primโ€™s Algorithm

<ul><li><p>Kruskalโ€™s Algorithm</p></li><li><p>Primโ€™s Algorithm</p></li></ul><p></p>
9
New cards

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

10
New cards

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)