CSE 4/531 Solution 3

Similar documents
Single Source Shortest Path (SSSP) Problem

Algorithms and Data Structures 2014 Exercises and Solutions Week 9

Solutions to Assignment# 4

Searching in Graphs (cut points)

CS 441 Discrete Mathematics for CS Lecture 26. Graphs. CS 441 Discrete mathematics for CS. Final exam

Dynamic Programming Algorithms

Shortest path problems

EXAM ELEMENTARY MATH FOR GMT: ALGORITHM ANALYSIS NOVEMBER 7, 2013, 13:15-16:15, RUPPERT D

Dynamic programming II - The Recursion Strikes Back

Efficient Sequential Algorithms, Comp309. Problems. Part 1: Algorithmic Paradigms

12 Dynamic Programming (2) Matrix-chain Multiplication Segmented Least Squares

Lecture 4: Dynamic programming I

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

Lecture 9 Graph Traversal

Chapter 3 Dynamic programming

Graph: representation and traversal

Algorithms IV. Dynamic Programming. Guoqiang Li. School of Software, Shanghai Jiao Tong University

Recursively Defined Functions

Minimum Spanning Trees Ch 23 Traversing graphs

Algorithm Design and Analysis

from notes written mostly by Dr. Carla Savage: All Rights Reserved

Mathematics and Statistics, Part A: Graph Theory Problem Sheet 1, lectures 1-4

Exercise set 2 Solutions

21# 33# 90# 91# 34# # 39# # # 31# 98# 0# 1# 2# 3# 4# 5# 6# 7# 8# 9# 10# #

Description of The Algorithm

MC 302 GRAPH THEORY 10/1/13 Solutions to HW #2 50 points + 6 XC points

CSE 417 Dynamic Programming (pt 4) Sub-problems on Trees

v V Question: How many edges are there in a graph with 10 vertices each of degree 6?

Recitation 12. Dynamic Programming Announcements. SegmentLab has been released and is due Apr 14.

Figure 1: A directed graph.

Graphs. CSE 2320 Algorithms and Data Structures Alexandra Stefan and Vassilis Athitsos University of Texas at Arlington

Dynamic Programming II

CSC Intro to Intelligent Robotics, Spring Graphs

Week 12: Minimum Spanning trees and Shortest Paths

CS/COE 1501 cs.pitt.edu/~bill/1501/ Graphs

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

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

UNIT III TREES. A tree is a non-linear data structure that is used to represents hierarchical relationships between individual data items.

1. Find f(1), f(2), f(3), and f(4) if f(n) is defined recursively by f(0) = 1 and for n = 0, 1, 2,

Graph Algorithms Using Depth First Search

Lecture 11: Analysis of Algorithms (CS ) 1

CS 231: Algorithmic Problem Solving

Introduction to Algorithms I

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING QUESTION BANK UNIT-III. SUB NAME: DESIGN AND ANALYSIS OF ALGORITHMS SEM/YEAR: III/ II PART A (2 Marks)

CSE 101, Winter Design and Analysis of Algorithms. Lecture 11: Dynamic Programming, Part 2

Week 11: Minimum Spanning trees

Lecture 5: Dynamic Programming II

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

Optimization II: Dynamic Programming

CS490: Problem Solving in Computer Science Lecture 6: Introductory Graph Theory

Solutions to Problem Set 3

ECE608 - Chapter 15 answers

Minimum Spanning Trees

Algorithms Assignment 3 Solutions

Unit 4: Dynamic Programming

Dynamic Programming (Part #2)

Steven Skiena. skiena

/463 Algorithms - Fall 2013 Solution to Assignment 3

Math 485, Graph Theory: Homework #3

Dynamic Programming Homework Problems

F(0)=0 F(1)=1 F(n)=F(n-1)+F(n-2)

Analysis of Algorithms. Unit 4 - Analysis of well known Algorithms

1 Dynamic Programming

Trees. 3. (Minimally Connected) G is connected and deleting any of its edges gives rise to a disconnected graph.

Sample Solutions to Homework #4

Chain Matrix Multiplication

Dynamic Programming Homework Problems

Source. Sink. Chapter 10: Iterative Programming Maximum Flow Problem. CmSc250 Intro to Algorithms

Graphs. Graph G = (V, E) Types of graphs E = O( V 2 ) V = set of vertices E = set of edges (V V)

DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2017)

Graph Representations and Traversal

We ve done. Now. Next

Chapter 6. Dynamic Programming

CSE 417 Dynamic Programming (pt 5) Multiple Inputs

Dynamic Programming CPE 349. Theresa Migler-VonDollen

Trees Rooted Trees Spanning trees and Shortest Paths. 12. Graphs and Trees 2. Aaron Tan November 2017

Unit-5 Dynamic Programming 2016

Recursion defining an object (or function, algorithm, etc.) in terms of itself. Recursion can be used to define sequences

Graph Algorithms. Definition

Practical Session No. 12 Graphs, BFS, DFS, Topological sort

******** Chapter-4 Dynamic programming

Lecture I: Shortest Path Algorithms

Definition Example Solution

Graph Algorithms. Chromatic Polynomials. Graph Algorithms

All Pairs Shortest Paths

DFS & STRONGLY CONNECTED COMPONENTS

These notes present some properties of chordal graphs, a set of undirected graphs that are important for undirected graphical models.

CSC Design and Analysis of Algorithms. Lecture 4 Brute Force, Exhaustive Search, Graph Traversal Algorithms. Brute-Force Approach

CS473-Algorithms I. Lecture 10. Dynamic Programming. Cevdet Aykanat - Bilkent University Computer Engineering Department

Section Summary. Introduction to Trees Rooted Trees Trees as Models Properties of Trees

Lecture 13: Chain Matrix Multiplication

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

Basic Combinatorics. Math 40210, Section 01 Fall Homework 4 Solutions

UNIT 5 GRAPH. Application of Graph Structure in real world:- Graph Terminologies:

Introduction to Graph Theory

CSE 241 Class 17. Jeremy Buhler. October 28, Ordered collections supported both, plus total ordering operations (pred and succ)

CS24 Week 8 Lecture 1

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

ifn >-- 2 is even, ifn is odd, ifn =0,

1 Introduction 2. 2 A Simple Algorithm 2. 3 A Fast Algorithm 2

Transcription:

CSE 4/531 Solution 3 Edited by Le Fang November 7, 2017 Problem 1 M is a given n n matrix and we want to find a longest sequence S from elements of M such that the indexes of elements in M increase and the values decrease. Without loss generality, we consider sequence S that the last number m ik,j k of S is M[i, j] for 1 i, j n. It means that i k = i, j k = j and m ik,j k = M[i, j]. Because of decreasing property for indexes (i r, j r ), 1 r k, we have i k 1 i 1 and j k 1 j 1. It also implies that (m i1,j 1, m i2,j 2,, m ik 1,j k 1 ) is a longest decreasing sequence of a sub-matrix M[1 i, 1 j ] for some 1 i < i and 1 j < j. From the above discussion, we immediately have the following recursive formula: Let L[i, j] denote the length of a longest decreasing sequence S = (m i1,j 1, m i2,j 2,, m ik,j k ) with m ik,j k = M[i, j], i k = i and j k = j in the matrix M[1 i, 1 j], L[i, j ] in the upper left corner of L[i, j], we have L[i, j] = max i <i;j <j and M[i,j ]>M[i,j] 1 + L[i, j ] (1) Next, how to complete the table L? The following two pseudocodes are approaches to find the length of a longest sequence of M and a longest sequence S. Also, we provide the time complexity analysis for this question. 1

Since we have n 2 entries in L and each entry L[i, j], 1 i, j n takes (i 1) (j 1) time to find the best i and j, the total time complexity is Σ 1 i;j n (i 1) (j 1) = O(n 4 ). Problem 2 This question is similar to Minimum Weight of Matrix Multiplication Problem. Hence we have a similar recursive formula: Assume the polygon P = (1, 2,, n). Then, let T [i, j] be the weight of a minimum triangulation for the polygon (i, i + 1,, j 1, j) and w(a, b) be the Euclidean distance between a and b. We can observe that given a minimum weighted triangulation for P with cost T (1, n), the edge (1, n) will company with some point k to form the triangle 1,k,n and T [1, n] = T [1, k] + w( 1,k,n ) + T [k, n] where w( i,k,j ) is w(i, j) + w(j, k) + w(i, k). Then we have { min i<k<j {T [i, k] + T [k, j] + w( i,k,j )} if i < j T [i, j] = (2) 0 otherwise Next, how to complete the table T? The following two pseudocodes are approaches to find the weight of a minimum triangulation of P and a minimum triangulation of P. Also, we provide the time complexity analysis for this question. Each entry T [i, j] has to take (j i 1) time to find the best k and we have O(n 2 ) entries for T. So, the total time complexity is Σ 1 i<j n (j i 1) = O(n 3 ). 2

Problem 3 Suppose the given starting point is v s, the ending point is v e. If there is an edge e(u, v) which is an edge from u to v, let d(u, v) = d(e), w(u, v) = w(e). Here suppose we use Adjacency List representation of graph. Let C be a V k matrix. Each C[v, j] represents the minimum cost from v s to v with delay j. Let M be a V k matrix. Each M[v, j] records the last element before v in the path from v s to v with the minimum cost C[v, j]. Then we have 0 if v = v s C[v, j] = min u:v Adj[u] {C[u, j d(u, v)] + w(u, v)} if u, such that d(u, v) < j if u, d(u, v) j So we use BFS to traverse the vertices of the graph, each time we come up with a vertex u, we update matrix C for all the vertices it directs into. The problem can be solved follow these steps: Initialization. C[v s, j] = 0,M[v s, j] = v s, j = 1,..., k C[v, j] =, v v s, j = 1,..., k BFS travel the vertices of the graph. Each time we come up with a vertex u, v Adj[u], If u = v s, d(u, v) k, then C[v, j] = w(u, v), M[v, j] = v s, j = d(u, v),..., k. If u = v s, d(u, v) > k, then do nothing. If u = v s, d(u, v) < k, C[u, j d(u, v)] + w(u, v) < C[v, j], then C[v, j] = C[u, j d(u, v)] + w(u, v), M[v, j] = u, j = d(u, v) + 1,..., k. If u = v s, d(u, v) k, then do nothing. The pseudocode of the algorithm is shown in Algorithm 5. The initialization precess need O(k V ) time. BFS takes O( V + E ) time to visit each vertex. But when we visit each vertex, we compute up to k entries of C and k entries of M. So the total running time would be O(k( V + E )). The extra space needed is for storing C and M. They are V k matrices, so the space need is O(k V ). 3

4

Problem 4 Let (1): r be the root of T, (2): T v denote the subtree rooted at v, (3): NT 1(v) denote the children of v in T and (4): N T 2 (v) denote the descendants of v which is two edges away from v in T. Observation 1: Given a maximum independent set S containing r, S can be partitioned into S v v N 2 T (r) and each S v is also a maximum independent set of T v. Observation 2: Given a maximum independent set S not containing r, S can be partitioned into S v v N 1 T (r) and each S v is also a maximum independent set of T v. From the above two observations, we immediately have the following recursive formula: { } M(T r ) = max w(r) + S v N 2 T (r)m(t v ), S v N 1 T (r)m(t v ) Next, how to complete the table M? The following two pseudocodes are approaches to find the weight of a maximum independent set of T and a maximum independent set of T. First, let l T (v) be the level of v in T, we can recursively dene l T (v) as follows: 1. for the root r of T, l T (r) = 1 and 2. { l T (v 2 ) = l T (v 1 ) + 1 v 2 NT 1(v 1) }. 5

Because we have O(n) rooted trees in this dynamic programming formula and each tree rooted at v takes O( N T 1(v) + N T 2(v) ) time to execute summations in the recursive formula. Also, we have v T N 1 T (v) = O(n) and Σ v T N 2 T (v) = O(n). So, the total time complexity is v T N 1 T (v) + N 2 T (v) = O(n). Problem 5 Let C be a (m+1) (n+1) matrix recording the minimum cost of converting A[1..i] to B[1..j]. Let M be a (m+1) (n+1) matrix recording the last operation. Then If A is empty, then to convert A to B, at least we need to insert every element in B into A with cost 5 B.length. And if we want to get the minimum cost, that is enough. So C[0, j] = 5j. If B is empty, at least we need to delete every element in A with cost 3 A.length. And if we want to get the minimum cost, that is enough. So C[i, 0] = 3i. If a i = b j, all we need to do is to convert A[1..i 1] to B[1..j 1]. So, C[i, j] = C[i 1, j 1]. If a i b j, there are three choices: (a) delete A[i], convert A[1..i 1] to B[1..j] with total cost C[i 1, j] + 3; (b) convert A[1..i] to B[1..j 1], then insert B[j] with total cost C[i, j 1]+5; (c) convert A[1..i 1] to B[1..j 1], then replace A[i] with B[j] with total cost C[i 1, j 1] + 7. We ll choose the one with minimum cost. 5j if i = 0 3i if j = 0 C[i, j] = C[i 1, j 1] if i > 0, j > 0, a i = b j min {C[i 1, j] + 3, C[i, j 1] + 5, C[i 1, j 1] + 7} if i > 0, j > 0, a i b j To compute C[i, j] we need C[i 1, j], C[i, j 1] and C[i 1, j 1]. So the algorithm will compute C from left to right, form up to bottom. To output the solution, use (m+1) (n+1) matrix to record the last operation. If A[i] = B[j], we do nothing in this step, so M[i, j] = N. In case of A[i] B[j], if the delete 6

operation leads to the minimum cost, let M[i, j] = D; if the insert operation leads to the minimum cost, let M[i, j] = I; if the replace operation leads to the minimum cost, let M[i, j] = R. The pseudocode of the algorithm is show in Algorithm 8. In the algorithm, we compute each entry of matrix C and M. It takes constant time to compute each entry. So the total running time would be Θ(mn). 7