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

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

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

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

Design and Analysis of Algorithms - - Assessment

Greedy algorithms. Given a problem, how do we design an algorithm that solves the problem? There are several strategies:

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

Solutions for the Exam 6 January 2014

Notes on Minimum Spanning Trees. Red Rule: Given a cycle containing no red edges, select a maximum uncolored edge on the cycle, and color it red.

Kruskal s MST Algorithm

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

Graphs and Network Flows ISE 411. Lecture 7. Dr. Ted Ralphs

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

CSE101: Design and Analysis of Algorithms. Ragesh Jaiswal, CSE, UCSD

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

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

CSC 373 Lecture # 3 Instructor: Milad Eftekhar

Minimum spanning trees

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

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

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

Algorithms for Minimum Spanning Trees

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

CS 6783 (Applied Algorithms) Lecture 5

Algorithm Design (8) Graph Algorithms 1/2

Theorem 2.9: nearest addition algorithm

1 Format. 2 Topics Covered. 2.1 Minimal Spanning Trees. 2.2 Union Find. 2.3 Greedy. CS 124 Quiz 2 Review 3/25/18

Greedy Algorithms CHAPTER 16

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

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

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

FINAL EXAM SOLUTIONS

Minimum-Cost Spanning Tree. Example

Example. Minimum-Cost Spanning Tree. Edge Selection Greedy Strategies. Edge Selection Greedy Strategies

Algorithms for Data Science

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

Design and Analysis of Algorithms

Exam 3 Practice Problems

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

UNIT 3. Greedy Method. Design and Analysis of Algorithms GENERAL METHOD

Definition For vertices u, v V (G), the distance from u to v, denoted d(u, v), in G is the length of a shortest u, v-path. 1

Design and Analysis of Algorithms

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

Minimum Spanning Trees

looking ahead to see the optimum

CSE 421 Greedy Alg: Union Find/Dijkstra s Alg

Greedy Algorithms. Previous Examples: Huffman coding, Minimum Spanning Tree Algorithms

Week 12: Minimum Spanning trees and Shortest Paths

CS 310 Advanced Data Structures and Algorithms

CSE331 Introduction to Algorithms Lecture 15 Minimum Spanning Trees

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

11.9 Connectivity Connected Components. mcs 2015/5/18 1:43 page 419 #427

Greedy Algorithms 1. For large values of d, brute force search is not feasible because there are 2 d

Dr. Amotz Bar-Noy s Compendium of Algorithms Problems. Problems, Hints, and Solutions

4.8 Huffman Codes. These lecture slides are supplied by Mathijs de Weerd

Homework Assignment 3

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

CMPSCI 311: Introduction to Algorithms Practice Final Exam

6.1 Minimum Spanning Trees

Algorithm Design and Analysis

COMP3121/3821/9101/ s1 Assignment 1

Practice Problems for the Final

Greedy algorithms is another useful way for solving optimization problems.

Questions Total Points Score

Homework Assignment #3. 1 (5 pts) Demonstrate how mergesort works when sorting the following list of numbers:

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1

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

Lecture 11: Analysis of Algorithms (CS ) 1

2. CONNECTIVITY Connectivity

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

U.C. Berkeley CS170 : Algorithms, Fall 2013 Midterm 1 Professor: Satish Rao October 10, Midterm 1 Solutions

COMPSCI 311: Introduction to Algorithms First Midterm Exam, October 3, 2018

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

CSC 373: Algorithm Design and Analysis Lecture 4

Lecture 3, Review of Algorithms. What is Algorithm?

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

tree follows. Game Trees

Subset sum problem and dynamic programming

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Problem set 2. Problem 1. Problem 2. Problem 3. CS261, Winter Instructor: Ashish Goel.

This is a set of practice questions for the final for CS16. The actual exam will consist of problems that are quite similar to those you have

Algorithm Design and Analysis

Minimum Spanning Trees Ch 23 Traversing graphs

CS521 \ Notes for the Final Exam

Lecture 6 Basic Graph Algorithms

Instructions. Definitions. Name: CMSC 341 Fall Question Points I. /12 II. /30 III. /10 IV. /12 V. /12 VI. /12 VII.

Discrete mathematics

Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology. Assignment

Algorithm Design and Analysis

Greedy Algorithms 1 {K(S) K(S) C} For large values of d, brute force search is not feasible because there are 2 d {1,..., d}.

The Shortest Path Problem. The Shortest Path Problem. Mathematical Model. Integer Programming Formulation

DO NOT RE-DISTRIBUTE THIS SOLUTION FILE

We have already seen the transportation problem and the assignment problem. Let us take the transportation problem, first.

Algorithms Dr. Haim Levkowitz

CSE 417 Branch & Bound (pt 4) Branch & Bound

Shortest path problems

Trees, Trees and More Trees

Minimum Spanning Trees

Reference Sheet for CO142.2 Discrete Mathematics II

In this chapter, we consider some of the interesting problems for which the greedy technique works, but we start with few simple examples.

Transcription:

1. (15 pts) (a) Define what is a shortest s-t path in a weighted, connected graph G. A shortest s-t path is a path from vertex to vertex, whose sum of edge weights is minimized. (b) Give the pseudocode for Dijkstra's algorithm for solving the shortest s-t path. Analyze its time complexity. Implemented with Fibonacci heap. Maintain a set ( ) of explored nodes, for which the shortest path has been determined, and a set ( ) of unexplored nodes. Initialize =, =, =0, =, for all other vertex Insert all vertices into While (IsEmpty( )=False) do =ExtractMin( ) add to for in.neighbors() changekey( ) return Therefore, the total time is: +

(c) Prove that Dijkstra's algorithm is correct. Claim: =min, : is the shortest path from to We prove this by induction on. 1, is obviously the shortest path from to, if is selected to be added to next. 1, let be the next node to be added to. And we need to prove is still the shortest path from to. There are two possibilities for the path from to : 1, the path only contains nodes in S. In this situation, is the shortest path since we update every time we add a new node to 2, the path contains nodes in both and, as shown in the figure below. Suppose there is a node on path. Let be the first edge in that leaves, and let be the subpath to. Due to the nonnegative property of weights, we ignore the weights from to, thus,

Due to the hypothesis that is the shortest path from to for all in, +, From the definition of, we have +, +, is the next node to be added to, so +, +, To sum up, any path from to will be at least as expensive as 2. (15 pts) For the following questions you may assume that all edge weights are unique. (a) Define what is a minimum cost spanning tree of a weighted, connected graph G. =,. A minimum cost spanning tree is a subset of edges such that is a spanning tree whose sum of edge weights is minimized. (b) Give the pseudocode for Kruskal's Algorithm. Analyze its time complexity. (Implemented with union-find data structure) =(, ), Kruskal(G,C) =, = % is the set of weight costs Sort edge weights so that ( ) ( ) ( ) = for each ( ) make a set containing singleton for =1 to (, )= (1) ( ) ( ) ( )

If ( and are in different sets) the sets containing and return T 1) Time complexity: For union-find data structure: requires (1) time, the label of a node also takes (1) time For ( 1, 2) operation, each node undergoes at most logn label changes. Since, we have: ( ) ( ) Therefore, the total time complexity is: (c) Prove that Kruskal's algorithm is correct. The correctness of Kruskal s algorithm is obvious according to the two properties of MST. Cut property. Let be any subset of nodes, and let be the cost edge with exactly one endpoint in. Then the MST contains. Cycle property. Let be any cycle, and let be the cost edge belonging to. Then the MST does not contain. Kruskal s algorithm considers edges in ascending order of weight. For each edge, ), if and are in the same component, adding to creates a cycle, thus we discard according to cycle property; otherwise, insert into according to cut property. Since we consider edges in ascending order of weight, the sum of edge weights is minimized.

3. (15 pts) Give a linear time O(n+m) algorithm to test whether or not a particular edge e (of a graph G with n nodes and m edges) belongs to a minimum spanning tree. Idea: My algorithm is based on the cycle property of MST. Claim: If edge has the max cost in a cycle that includes it, edge does not belong to a minimum spanning tree. Otherwise, it belongs to a MST. Proof: The first part of the claim in obvious according to the cycle property. We prove the second part by contradiction. Hypothesis: Edge is not the most expensive edge in any cycle that includes it, but it does not belong to a MST. If we add edge to the MST, there will be a cycle. Since edge is not the most expensive one, we can replace the most expensive one with edge. Thus, the sum of edge weights for the new MST will be lower than the original one, which contradicts with the definition of MST. Thus, we refuse the hypothesis. Algorithm: For implementation, we first traverse all the edges once and delete the edges whose weights are larger than edge. And we suppose that all edge weights are unique. This step takes ( ) time. For the new graph, all the edges are cheaper than edge. If there exists a cycle including edge in the new graph, edge does not belong to a MST. Otherwise, it does. Then, we traverse all the nodes once. Suppose the nodes linked by edge are and. We start from node and find all the nodes reachable from node. Each time we visit a node, we determine whether this node is. If it is, a cycle detected. Furthermore, we mark the node as visited for the first time we visit it, which will avoid duplicated visiting to the same node. This step takes ( ) time. Therefore, the total time complexity is ( + )

=(, ),, ) belongto(, ) For each If ( > ) delete from =,.()= While ( ) do =. () for each vertex adjacent to If (.()= ) continue else If ( = ) print edge does not belong to a MST break else.()= Add to

4. (15 pts)consider the Huffman Coding algorithm (a) Trace the action of the algorithm for the frequency table [(a,10); (b,7); (c, 3); (d, 5); (e,9); (f,2); (g, 3); (h, 2)] Sort the symbols according to frequency: [(f,2);(h,2);(c,3);(g,3);(d,5);(b,7);(e,9);(a,10)] (b) Using the code from above, encode the word 'decade'. 'decade'= 0100010001101000

(c) True or False. Every 2-tree (i.e., binary tree where each internal node has 2 children) is a Huffman tree for some set of frequencies. Justify your answer. True. For an arbitrary 2-tree, whose depth is D. Suppose there are n leaf nodes in this 2- tree. For node, the depth value is, the frequency is. Let =2. From the property of frequency, we have =1. Thus, we can obtain the value of, and consequently the frequency for each leaf node. The frequency value for an internal node is the sum of its two children. This set of frequencies (,,, ) makes the original 2-tree a Huffman tree. 5. (15 pts) Recall the "Selecting Breakpoints" problem from slide-25 in Lecture 3 notes. Suppose instead of optimizing the number of stops, we wanted to minimize the total cost in fuel for the entire trip. Assume that there is a parameter C representing the fuel capacity of the car. Also, at each input breakpoint b, we know the price per gallon p b of fuel at that stop. (a) Design and verify a greedy algorithm to find the minimum cost trip. Idea: For each stop, add fuel to the amount of, which is the exact amount of fuel that is needed to reach the next stop whose price is lower than the current stop. Due to the limitation of fuel capacity, we set = if it requires more than to reach the next lower price stop. Algorithm: stops:,,, prices:,,, the amount of fuel added at each stop:,,, the amount of fuel remained at each stop:,,, the amount of fuel needed from any two stops 1, =( +1)

for ( =1;1; ) %for each stop determine the amount of fuel to be added % is the amount of fuel needed from stop (i-1) to i % 0, 0 for (1;1; ) if ( ) % find the next lower-price stop If ( ) % is the amount of fuel else needed from stop to stop break If no lower-price stop found, Add fuel with the amount of, Verification: Claim: greedy algorithm is optimal. We prove it by contradiction

Suppose the greedy algorithm and the optimal solution are the same for the largest value of. Greedy algorithm selects the next stop and OPT selects stop. Stop can only be prior to stop, since at stop we add oil to, which is the exact amount that is required from stop to stop and we can not go any farther than stop. Thus, >. Let the amount of fuel added at stop is. Why do not we add gallon at instead of at stop? This change to the OPT will achieve a lower cost than OPT, which contradicts with the definition of OPT. This process continues. And we will find that greedy algorithm is optimal. (b) Analyze the complexity of your algorithm. First, we need to know the amount of fuel needed from stop to stop, which takes ( ) time. Furthermore, there is a nested loop. For the worst case, the time would be: = ( ) Thus, the total time complexity is: ( ). 6. (15 pts) Suppose you are given the closing prices of ABC stock over the past n days, and you want to compute the maximum difference in prices over any pair of days such that if you bought low and sold high you could maximize your profit. In other words, you want the maximum difference in which the smaller price precedes the larger. Clearly, this can be done by considering all the differences over each of the O(n2) (or, n choose 2) pairs of days. You are to design a divide-and-conquer algorithm that runs in time O(n log n) for this problem. Hint: consider using the solution to the closest-pair problem as a model. Algorithm: Recursively divide the original problem into two halves until there is only one element in each sub-problem, which can be solved in constant time.

For merging part, we find the lowest price ( ) in the first part, and highest price ( ) in the second part. Compare = with the largest profit for each part ( ), and the profit for the merged problem=max,, ) Original problem: =,, ) 1: 2: : : Divide: 2, = 2 1: : : : Time Complexity: =max(,, ) min(,, ) Let the time complexity of the original problem is With the divide-and-conquer technique, we split the original problem into two halves, and solve each half in time 2 or 2. To merge the two halves, we only need to find the lowest price in the first half and the highest price in the second half. To find the smallest or largest element in a list takes, where is the size of the list. Thus, merging two halves takes time. Thus, we have 2 2 ( )=2 2 + ( )=2 2 4 + /2 + =4 4 +2 ( )= + log Since 1)= (1), the total time complexity ( )= ( )

7. (10 pts) This final question asks you to write a reasoned paragraph giving your feedback of the class so far. It is open ended, so you can share praise or criticism. You can suggest refinements to the lectures or the lecture notes. You can share your frustrations, or share any insights you have gained long the way. Overall, excellent course and excellent instructor! In this course, Professor Annexstein has introduced the classic algorithms in various domains, and techniques for designing efficient algorithms. He is passionate, and his lecture is clear and easy to follow. I have learnt a lot from this course. In addition, I also have a couple of suggestions for this course. First of all, I would prefer to split one class into two shorter classes. It is hard to keep concentrated for such a long time. Too many materials in one class also make the digestion after class more difficult. Therefore, I strongly recommend two classes per week instead of one. Second, I d better the instructor to talk a little bit about data structures, like B-tree, binary heap and so on. Data structures are the basis of the implementation of algorithms and I think they should be briefly covered in this course. Another minor point is that I think the assignment questions are not conveyed clearly enough sometime. Take the second assignment for example. We are not clear about whether the subsequence is consecutive or not. An explanation of that would be preferred.