CS3383 Unit 2: Greedy

Similar documents
CSC 373: Algorithm Design and Analysis Lecture 4

CSE 100 Minimum Spanning Trees Prim s and Kruskal

UC Berkeley CS 170: Efficient Algorithms and Intractable Problems Handout 8 Lecturer: David Wagner February 20, Notes 8 for CS 170

CS 310 Advanced Data Structures and Algorithms

Algorithms (VI) Greedy Algorithms. Guoqiang Li. School of Software, Shanghai Jiao Tong University

Chapter 9. Greedy Technique. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

CSE331 Introduction to Algorithms Lecture 15 Minimum Spanning Trees

looking ahead to see the optimum

CS 6783 (Applied Algorithms) Lecture 5

TU/e Algorithms (2IL15) Lecture 2. Algorithms (2IL15) Lecture 2 THE GREEDY METHOD

CSC 373 Lecture # 3 Instructor: Milad Eftekhar

CSC 1700 Analysis of Algorithms: Minimum Spanning Tree

tree follows. Game Trees

2.1 Greedy Algorithms. 2.2 Minimum Spanning Trees. CS125 Lecture 2 Fall 2016

Analysis of Algorithms Prof. Karen Daniels

CS 161: Design and Analysis of Algorithms

Design and Analysis of Algorithms

G205 Fundamentals of Computer Engineering. CLASS 21, Mon. Nov Stefano Basagni Fall 2004 M-W, 1:30pm-3:10pm

Introduction to Algorithms

Three Graph Algorithms

Three Graph Algorithms

CSE 431/531: Algorithm Analysis and Design (Spring 2018) Greedy Algorithms. Lecturer: Shi Li

Introduction to Algorithms

CS200: Graphs. Rosen Ch , 9.6, Walls and Mirrors Ch. 14

Outline. Computer Science 331. Analysis of Prim's Algorithm

Minimum Spanning Trees

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

Greedy Algorithms CSE 780

Minimum Spanning Trees

Greedy Algorithms CSE 6331

CSC 8301 Design & Analysis of Algorithms: Warshall s, Floyd s, and Prim s algorithms

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

Design and Analysis of Algorithms

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck

Decreasing a key FIB-HEAP-DECREASE-KEY(,, ) 3.. NIL. 2. error new key is greater than current key 6. CASCADING-CUT(, )

Chapter 16: Greedy Algorithm

Greedy Algorithms. Textbook reading. Chapter 4 Chapter 5. CSci 3110 Greedy Algorithms 1/63

Week 11: Minimum Spanning trees

COMP 182: Algorithmic Thinking Prim and Dijkstra: Efficiency and Correctness

6.1 Minimum Spanning Trees

Algorithms Dr. Haim Levkowitz

Minimum-Spanning-Tree problem. Minimum Spanning Trees (Forests) Minimum-Spanning-Tree problem

Review of course COMP-251B winter 2010

We ve done. Introduction to the greedy method Activity selection problem How to prove that a greedy algorithm works Fractional Knapsack Huffman coding

Minimum Spanning Trees

Huffman Coding. Version of October 13, Version of October 13, 2014 Huffman Coding 1 / 27

Priority queue ADT part 1

Minimum Spanning Trees. Minimum Spanning Trees. Minimum Spanning Trees. Minimum Spanning Trees

CS521 \ Notes for the Final Exam

COP 4531 Complexity & Analysis of Data Structures & Algorithms

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

22 Elementary Graph Algorithms. There are two standard ways to represent a

managing an evolving set of connected components implementing a Union-Find data structure implementing Kruskal s algorithm

22 Elementary Graph Algorithms. There are two standard ways to represent a

Introduction to Algorithms

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018

Single Source Shortest Path

MST & Shortest Path -Prim s -Djikstra s

Text Compression through Huffman Coding. Terminology

Greedy Algorithms. CLRS Chapters Introduction to greedy algorithms. Design of data-compression (Huffman) codes

Chapter 23. Minimum Spanning Trees

Lecture 13. Reading: Weiss, Ch. 9, Ch 8 CSE 100, UCSD: LEC 13. Page 1 of 29

Minimum Spanning Trees. CSE 373 Data Structures

Kruskal s MST Algorithm

Announcements Problem Set 5 is out (today)!

CSE373: Data Structures & Algorithms Lecture 17: Minimum Spanning Trees. Dan Grossman Fall 2013

CS Algorithms and Complexity

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 20. Example. Shortest Paths Definitions

Algorithm Analysis Graph algorithm. Chung-Ang University, Jaesung Lee

Algorithms for Minimum Spanning Trees

Greedy Algorithms and Data Compression. Curs Fall 2017

( ) + n. ( ) = n "1) + n. ( ) = T n 2. ( ) = 2T n 2. ( ) = T( n 2 ) +1

Notes 4 : Approximating Maximum Parsimony

Single Source, Shortest Path Problem

Solutions to relevant spring 2000 exam problems

Minimum Spanning Trees Shortest Paths

CSE 431/531: Analysis of Algorithms. Greedy Algorithms. Lecturer: Shi Li. Department of Computer Science and Engineering University at Buffalo

CS 170 Second Midterm ANSWERS 7 April NAME (1 pt): SID (1 pt): TA (1 pt): Name of Neighbor to your left (1 pt):

) $ f ( n) " %( g( n)

CS302 - Data Structures using C++

Greedy Algorithms Part Three

CSE 421 Greedy: Huffman Codes

Algorithm Design and Analysis

Minimum Spanning Trees

Minimum Spanning Trees and Prim s Algorithm

CSC 421: Algorithm Design & Analysis. Spring 2015

Lecture 6 Basic Graph Algorithms

Theorem 2.9: nearest addition algorithm

Recitation 13. Minimum Spanning Trees Announcements. SegmentLab has been released, and is due Friday, November 17. It s worth 135 points.

Final Exam Solutions

Greedy Algorithms. At each step in the algorithm, one of several choices can be made.

CIS 121 Data Structures and Algorithms Midterm 3 Review Solution Sketches Fall 2018

Analysis of Algorithms - Greedy algorithms -

Problem Set 2 Solutions

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

Dijkstra s Algorithm. Dijkstra s algorithm is a natural, greedy approach towards

Advanced algorithms. topological ordering, minimum spanning tree, Union-Find problem. Jiří Vyskočil, Radek Mařík 2012

Algorithms and Theory of Computation. Lecture 7: Priority Queue

CHAPTER 23. Minimum Spanning Trees

Transcription:

CS3383 Unit 2: Greedy David Bremner January 31, 2018

Contents Greedy Huffman Coding MST

Huffman Coding DPV 5.2 http://jeffe.cs.illinois.edu/teaching/algorithms/ notes/07-greedy.pdf Huffman Coding is covered in 7.4

Prefix codes Char Freq Symbol A 70 0 B 3 001 C 20 01 D 37 11 variable length symbols

Prefix codes Char Freq Symbol A 70 0 B 3 001 C 20 01 D 37 11 variable length symbols avoiding ambiguous bitstreams: what is 001?

Prefix codes Char Freq Symbol A 70 0 B 3 001 C 20 01 D 37 11 variable length symbols avoiding ambiguous bitstreams: what is 001? no symbol should be a prefix of another.

Prefix codes Char Freq Symbol A 70 0 B 3 001 C 20 01 D 37 11 variable length symbols avoiding ambiguous bitstreams: what is 001? no symbol should be a prefix of another. if A is 0, what is D?

Huffman coding Figure 5_10 in DPV cost(t ) = f i depth i n i=1 (Avg cost)

Lightest leaves are deepest Figure 5_10b in DPV proof by swapping

Huffman Algorithm Huffman ( f [ 1.. n ] ) H = p r i o r i t y queue o f i n d., by f r e q. f o r i = 1 to n do H. i n s e r t ( i ) f o r k = n+1 to 2n 1 do i = H. d e l e t e m i n ( ) j = H. d e l e t e m i n ( ) f [ k ] = f [ i ] + f [ j ] H. i n s e r t ( k ) c h i l d r e n [ k ] = ( i, j ) Correctness (Board) Huffman yields an optimal prefix code (induction)

Contents Greedy Huffman Coding MST

Minimum spanning tree Definition (Minimum Spanning Tree) Given G = (V, E), w E R, a minimum spanning tree T is a spanning tree (i.e. connecting all vertices) that minimizes cost(t ) = e T w(e)

Minimum Spanning trees Figure 5_0 in DPV Is this solution unique?

Minimum Spanning trees Figure 5_0 in DPV Is this solution unique? Figure 5_1 in DPV How about this one?

Cut Property Figure 5_2 in DPV Lemma (Board) Let T be a minimum spanning tree, X T s.t. X does not connect (S, V S). Let e be the lightest edge from S to V S. X e is part of some MST.

Generic MST X {} while X < V 1 do Choose S s.t. X does not connect (S, V S) Add the lightest crossing edge to X end while Figure 5_8a in DPV

Greedy Algorithms in General Discrete Optimization Problems solution defined by a sequence of choices solutions are ranked from best to worst

Greedy Algorithms in General Discrete Optimization Problems solution defined by a sequence of choices solutions are ranked from best to worst Greedy Design Strategy Each choice leaves one smaller subproblem Prove that an optimal solution that makes the greedy choice Show that the greedy choice, combined with an optimal solution to the subproblem, yields an optimal solution to the original problem.

Prim s Algorithm Figure 5_8 in DPV S = nodes reached so far

Example of Prim s algorithm A V A 6 12 5 14 7 8 00 3 10 9

Example of Prim s algorithm A V A 6 12 5 14 7 8 00 3 10 9

Example of Prim s algorithm A V A 6 12 5 77 14 7 8 00 3 10 10 9

Example of Prim s algorithm A V A 6 12 5 77 14 7 8 00 3 10 10 9

Example of Prim s algorithm A V A 12 6 12 5 55 77 14 7 8 00 3 10 10 9 99

Example of Prim s algorithm A V A 12 6 12 5 55 77 14 7 8 00 3 10 10 9 99

Example of Prim s algorithm A V A 66 6 12 5 55 77 14 7 8 14 00 3 88 10 9 99

Example of Prim s algorithm A V A 66 6 12 5 55 77 14 7 8 14 00 3 88 10 9 99

Example of Prim s algorithm A V A 66 6 12 5 55 77 14 7 8 14 00 3 88 10 9 99

Example of Prim s algorithm A V A 66 6 12 5 55 77 14 7 8 33 00 3 88 10 9 99

Example of Prim s algorithm A V A 66 6 12 5 55 77 14 7 8 33 00 3 88 10 9 99

Example of Prim s algorithm A V A 66 6 12 5 55 77 14 7 8 33 00 3 88 10 9 99

Prim s Algorithm u 0 = arbitrary vertex cost(u 0 ) = 0; cost(v) =, v u 0 for v V: enq(h, v) while H is not empty do v = deletemin(h) for e = {v, z}, e E, z H do if cost(z) > w(v, z) then cost(z) = w(v, z) prev(z) = v decreasekey(h,z) end if end for end while X S V S

Analysis of Prim s Algorithm (board) Correctness follows from the cut property, induction Closely connected with the Djikstra s Shortest path algorithm; only two lines change Tree can be read back from prev Cost is dominated by priority queue operations