CSC 373 Lecture # 3 Instructor: Milad Eftekhar

Similar documents
CSC 373: Algorithm Design and Analysis Lecture 4

Algorithms for Data Science

Algorithms for Data Science

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

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 6: BFS and Dijkstra s

Breadth-First Search, 1. Slides for CIS 675 DPV Chapter 4. Breadth-First Search, 3. Breadth-First Search, 2

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 8: Negative Edges

Unit-5 Dynamic Programming 2016

Algorithms (VII) Yijia Chen Shanghai Jiaotong University

Algorithms Dr. Haim Levkowitz

Algorithms (VII) Yijia Chen Shanghai Jiaotong University

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

Analysis of Algorithms, I

CS473-Algorithms I. Lecture 11. Greedy Algorithms. Cevdet Aykanat - Bilkent University Computer Engineering Department

CS 161 Lecture 11 BFS, Dijkstra s algorithm Jessica Su (some parts copied from CLRS) 1 Review

Analysis of Algorithms - Greedy algorithms -

Practice Problems for the Final

Algorithms (V) Path in Graphs. Guoqiang Li. School of Software, Shanghai Jiao Tong University

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

Shortest Path Problem

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

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

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

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

Minimum Spanning Trees

Analysis of Algorithms Prof. Karen Daniels

Algorithms (I) Introduction. Guoqiang Li. School of Software, Shanghai Jiao Tong University

Scribe: Virginia Williams, Sam Kim (2016), Mary Wootters (2017) Date: May 22, 2017

CSC Intro to Intelligent Robotics, Spring Graphs

Name: Lirong TAN 1. (15 pts) (a) Define what is a shortest s-t path in a weighted, connected graph G.

CS 561, Lecture 10. Jared Saia University of New Mexico

Single Source Shortest Path (SSSP) Problem

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

CS Final - Review material

Design and Analysis of Algorithms (I)

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

Single Source Shortest Path

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Shortest Paths Date: 10/13/15

1 Dijkstra s Algorithm

6.1 Minimum Spanning Trees

16 Greedy Algorithms

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

CS161 - Final Exam Computer Science Department, Stanford University August 16, 2008

CS 561, Lecture 10. Jared Saia University of New Mexico

Greedy Algorithms CHAPTER 16

CS350: Data Structures Dijkstra s Shortest Path Alg.

1 Non greedy algorithms (which we should have covered

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

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I

Greedy Algorithms. Alexandra Stefan

(Refer Slide Time: 00:18)

CS 125 Section #6 Graph Traversal and Linear Programs 10/13/14

CS 4349 Lecture October 23rd, 2017

Shortest path problems

Week 12: Minimum Spanning trees and Shortest Paths

Description of The Algorithm

Dijkstra s algorithm for shortest paths when no edges have negative weight.

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

Title. Ferienakademie im Sarntal Course 2 Distance Problems: Theory and Praxis. Nesrine Damak. Fakultät für Informatik TU München. 20.

tree follows. Game Trees

Analyze the obvious algorithm, 5 points Here is the most obvious algorithm for this problem: (LastLargerElement[A[1..n]:

Lecture 11: Analysis of Algorithms (CS ) 1

Design and Analysis of Algorithms

CSci 231 Final Review

FINAL EXAM SOLUTIONS

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

CS420/520 Algorithm Analysis Spring 2009 Lecture 14

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

Algorithms and Data Structures

CS3383 Unit 2: Greedy

CMSC 451: Dynamic Programming

looking ahead to see the optimum

ESO207A: Data Structures and Algorithms End-semester exam

CSC 373: Algorithm Design and Analysis Lecture 8

1 Dynamic Programming

CS161 - Minimum Spanning Trees and Single Source Shortest Paths

8.1 Introduction. Background: Breadth First Search. CS125 Lecture 8 Fall 2014

February 24, :52 World Scientific Book - 9in x 6in soltys alg. Chapter 3. Greedy Algorithms

Analysis of Algorithms

MCS-375: Algorithms: Analysis and Design Handout #G2 San Skulrattanakulchai Gustavus Adolphus College Oct 21, Huffman Codes

Three Graph Algorithms

Three Graph Algorithms

CSE 373: Data Structures and Algorithms

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

Dijkstra s Algorithm and Priority Queue Implementations. CSE 101: Design and Analysis of Algorithms Lecture 5

CMSC 451: Lecture 10 Dynamic Programming: Weighted Interval Scheduling Tuesday, Oct 3, 2017

Dijkstra s Shortest Path Algorithm

Question Points Score TOTAL 50

Tutorial 6-7. Dynamic Programming and Greedy

ECE608 - Chapter 16 answers

Efficient Sequential Algorithms, Comp309. Motivation. Longest Common Subsequence. Part 3. String Algorithms

Efficient Algorithms and Intractable Problems Spring 2016 Alessandro Chiesa and Umesh Vazirani MT 1 Solution

Lecture 5 Using Data Structures to Improve Dijkstra s Algorithm. (AM&O Sections and Appendix A)

Practical Session 10 - Huffman code, Sort properties, QuickSort algorithm, Selection

Greedy algorithms is another useful way for solving optimization problems.

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

Thus, it is reasonable to compare binary search trees and binary heaps as is shown in Table 1.

Text Compression through Huffman Coding. Terminology

1 More on the Bellman-Ford Algorithm

Introduction to Optimization

Transcription:

Huffman encoding: Assume a context is available (a document, a signal, etc.). These contexts are formed by some symbols (words in a document, discrete samples from a signal, etc). Each symbols s i is occurred f i times in the context. We aim to encode each symbol s i as a binary string such that the size of the encoded context is minimized (zipped file). Clearly if a symbol say s occurs very often (resp. infrequently), we want to use a relatively short (resp. long) string to represent it. In order to simplify decoding, a nice property is that the encodings satisfy the prefix-free property that no codeword is the prefix of another code word. Such an encoding is equivalent to a full ordered binary tree T ; that is, a rooted binary tree where Every non leaf has exactly two children With the left edge say labeled 0 and the right edge labeled 1 With every leaf labeled by a symbol Then the labels along the path to a leaf define the string encoding the symbol at that leaf. The goal is to create such a tree T so as to minimize cost(t ) = f i (depth of s i in T ) i The intuitive idea is to greedily combine the two lowest frequency symbols s 1 and s 2 to create a new symbol with frequency f 1 + f 2. Example (the DPV textbook): Symbols {A, B, C, D} with frequencies 70, 3, 20, 37. Obvious choice: 2 bits per symbol (A : 00, B : 01, C : 10, D : 11). Total document size = (70 + 3 + 20 + 37) 2 = 260. Pseudo-code: Algorithm 1: Huffman algorithm Input: An array of frequencies f[1 n] such that f 1 f 2 f n Output: An encoding tree T with n leaves 1 Let H be a priority queue of integers, ordered by f 2 for i in 1 to n do 3 insert(h, i) 4 for k = n + 1 to 2n 1 do 5 i = deletemin(h) 6 j = deletemin(h) 7 create a node numbered k with children i and j 8 f[k] = f[i] + f[j] 9 insert(h, k) Figure 1: The best encoding tree for the aforementioned example (from DPV). Huffman tree: (Total document size = 70 1 + 3 3 + 20 3 + 37 2 = 213. Summer 2013 Page 1 of 6

Dynamic programming: The weighted interval scheduling problem (WISP): Interval I i starts at s i, finishes at f i, and has weight (or profit) w i. We want to identify a non-overlapping subset S (S {I 1,, I n }) with maximum sum of interval weights in the chosen subset. Does greedy approaches work? Greedy by finish time doesn t work: -1- -1- -------3------- Greedy by max profit doesn t work: ----------2---------- -1- -1-... -1- Greedy by max unit profit doesn t work: -1- -1- -------3------- The DP approach: Sort intervals by finish time, as before (f 1 f n ). Consider optimal schedule S. There are two possibilities: I n S or I n S. If I n S, rest of S must consist of optimal way to schedule intervals I 1,..., I k, where k is largest index of intervals that do not overlap with I n (i.e., I k+1,..., I n 1 all overlap with I n ). If I n S, S must consist of optimal way to schedule intervals I 1,..., I n 1. In other words, problem has recursive structure: optimal solutions for I 1,..., I n = optimal solution for I 1,..., I n 1 or optimal solution for I 1,..., I k together with I n, where k is largest index of interval that does not overlap with I n. Recursive solution: Consider the following semantic array : V [i] = max profit obtainable by a set of intervals which are a subset of the first i intervals {I 1,, I i } We can define V [0] = 0. Clearly V [n] is the optimal value. How to compute the entries in V? Define pred[i] = the largest index j such that f j s i (so I pred[i] does not overlap with I i but I pred[i]+1,..., I i 1 all overlap with I i ). Assume values of pred[] computed once and stored in an array. Computing V : V [0] = 0 V [i] = max(v [i 1], V [pred(i)] + w i ) Correctness: it is immediate from reasoning above: either interval I n or don t, and since we don t know which choice leads to best schedule, just try both. Runtime: Summer 2013 Page 2 of 6

Recursive: Suppose for all i = 1,..., n 1, interval I i overlaps I i+1 and no other I j for j > i + 1. Thus, the complexity would be T [n] = T [n 1] + T [n 2]. The solution is exponential to n (recall Fibonacci sequences). Iterative: There are only n + 1 values that should be computed: V [0],, V [n]. Exponential runtime of recursive algorithm is due to wasted time recomputing values. Idea: store values in an array and compute each only once, looking it up afterwards. Time: Θ(n log n) for sorting and computing pred[i] values + Θ(n) for computing V values. Thus Θ(n log n) in total. Computing optimal answer: 1 S = {} 2 i = n 3 while i > 0 do 4 if V [i] == V [i 1] // don t schedule interval I i 5 then 6 i = i 1 7 else // schedule interval I i 8 S = S {I i } 9 i = pred[i] 10 return S Dynamic Programming Paradigm: For optimization problems that satisfy the following properties: subproblem optimality: an optimal solution to the problem can always be obtained from optimal solutions to subproblems; simple subproblems: subproblems can be characterized precisely using a constant number of parameters (usually numerical indices); subproblem overlap: smaller subproblems are repeated many times as part of larger problems (for efficiency). Step 0: Describe recursive structure of problem: how problem can be decomposed into simple subproblems and how global optimal solution relates to optimal solutions to these subproblems. Step 1: Define an array ( semantic array ) indexed by the parameters that define subproblems, to store the optimal value for each subproblem (make sure one of the subproblems actually equals the whole problem). Step 2: Based on the recursive structure of the problem, describe a recurrence relation satisfied by the array values from step 1. Step 3: Write iterative algorithm to compute values in the array, in a bottom-up fashion, following recurrence from step 2. Step 4: Use computed array values to figure out actual solution that achieves best value (generally, describe how to modify algorithm from step 3 to be able to find answer; can require storing additional information about choices made while filling up array in Step 3). Summer 2013 Page 3 of 6

Single-Source Shortest Paths with non-negative weights (Greedy approach): Input: connected graph G = (V, E) with non-negative edge weights (costs) w(e) for all e E; Source s V. Output: a path from s to all v V with minimum total cost (shortest path). Special case: if w(e) = 1 for all edges e: BFS! In general: Dijkstra Algorithm (an adjusted BFS): use a priority queue instead of a queue to collect unvisited vertices; set priority = shortest distance so far. Pseudo-code: Algorithm 2: Dijkstra s shortest-path algorithm (similar to Figure 4.8 in DPV textbook) Result: dist(u): The distance from s to u // Initialization 1 forall the u in V do 2 dist(u) = // minimum distance from s to u so far 3 prev(u) = nil // predecessor of u on the shortest path s-u so far 4 dist(s) = 0 5 H = makequeue(v ) // using dist values as keys // Main loop 6 while H is not empty do 7 u = deletemin(h) 8 forall the edges (u, v) E do 9 if dist(v) > dist(u) + w(u, v) then 10 dist(v) = dist(u) + w(u, v) 11 prev(v) = u 12 decreasekey(h, v) Runtime: O(n) for initialization. n insert operations for makequeue n operations for deletemin (each iteration removes one vertex from the queue). Each iteration examines a subset of edges and updates priorities. Over all iterations, each edge generates at most one queue update. The time needed for queue operations depends on implementation. Total (using a binary heap): O((m + n) log n) = O(m log n). Correctness: Using the exchange technique with induction. Summer 2013 Page 4 of 6

Single-Source Shortest Paths with real weights (Dynamic programming): Bellman-Ford Algorithm. Input: connected graph G = (V, E) with edge weights w(e) for all e E (weights can be negative but there is no negative cycle); Vertex s V. Output: For each v V, a shortest path (i.e., minimum weight) from s to v. Two natural ways to characterize subproblems: restrict number of edges in a path, or restrict possible vertices allowed on a path. Consider restricting edges. Step 0: Consider a shortest path P from s to v. Since G contains no negative weight cycle, P must be simple (no cycles) so it contains at most n 1 edges. If P contains more than 1 edge, let u be the last vertex on P before v. Claim: The part of P from s to u must be a shortest path in G. Otherwise, there would be a shorter path from s to v. Step 1: Define an array, using one index to restrict number of edges. A[k, v] = smallest weight of paths from s to v with at most k edges where 0 k n 1, v V. Step 2: Write a recurrence. A[0, s] = 0 A[0, v] = for all v s (only node reachable from s with no edge is s itself) A[k, v] = min(a[k 1, v], A[k 1, u] + w(u, v) : (u, v) E), for k [0,..., n 1] and v V (shortest path with at most k edges either has at most k 1 edges or it consists of a shortest path with at most k 1 edges followed by one edge (u, v); examine all possible last edges (u, v) to find the best) Step 3: Compute values bottom-up. [will be explained next week] Create a two dimensional array. Each column corresponds to a vertex v V. Each row corresponds to a number from 0 to n 1. Calculate the values row by row. The last row contains the weight of the shortest path from s to any vertex v V. Step 4: Find the optimal answer. Work backwards from v. The amount of additional work required can be decreased by using a simple trick and storing additional information as the values are computed in Step 3. Use second array p[v] to store predecessor of v on shortest path. [Intuition: algorithm examines many possibilities to compute best value of A[k, v] remember the possibility that gave the best answer.] Modified algorithm: 1 forall the v in V do 2 A[0, v] := 3 p[v] := NIL 4 A[0, s] := 0 5 for k = 1 to n 1 do 6 forall the v in V do 7 A[k, v] := A[k 1, v] 8 forall the edges (u, v) in E do 9 if A[k 1, u] + w(u, v) < A[k, v] then 10 A[k, v] := A[k 1, u] + w(u, v) 11 p[v] := u Summer 2013 Page 5 of 6

To obtain the shortest s-v path (as list of edges): Function Path(s, v) 1 if v = s then 2 return [] 3 return [Path(s, p[v]), (p[v], v)] Summer 2013 Page 6 of 6