CSE101: Algorithm Design and Analysis. Ragesh Jaiswal, CSE, UCSD

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

CSL 356: Analysis and Design of Algorithms. Ragesh Jaiswal CSE, IIT Delhi

Lectures: Feb 27 + Mar 1 + Mar 3, 2017

What is counting? (how many ways of doing things) how many possible ways to choose 4 people from 10?

arxiv: v1 [math.co] 30 Nov 2017

Maximum Contiguous Subarray Sum Problems

MA/CSSE 473 Day 14. Permutations wrap-up. Subset generation. (Horner s method) Permutations wrap up Generating subsets of a set

Algorithms and Data Structures CS 372. The Sorting Problem. Insertion Sort - Summary. Merge Sort. Input: Output:

Lecture5: Lossless Compression Techniques

Problem A. Vera and Outfits

Final Practice Problems: Dynamic Programming and Max Flow Problems (I) Dynamic Programming Practice Problems

An Optimal Algorithm for a Strategy Game

Southeastern European Regional Programming Contest Bucharest, Romania Vinnytsya, Ukraine October 21, Problem A Concerts

SMS Dictionary. Solution hint. Input format. Output format. (Indian National Olympiad in Informatics, INOI, 2007)

Playing Games. Henry Z. Lo. June 23, We consider writing AI to play games with the following properties:

: Principles of Automated Reasoning and Decision Making Midterm

LECTURE VI: LOSSLESS COMPRESSION ALGORITHMS DR. OUIEM BCHIR

Problem A Rearranging a Sequence

Problem A. Worst Locations

UMBC 671 Midterm Exam 19 October 2009

Mixing Business Cards in a Box

On the Capacity Regions of Two-Way Diamond. Channels

Problem A: Complex intersecting line segments

SGU 149. Computer Network. time limit per test: 0.50 sec. memory limit per test: 4096 KB input: standard input output: standard output

UNIVERSITY of PENNSYLVANIA CIS 391/521: Fundamentals of AI Midterm 1, Spring 2010

2 Textual Input Language. 1.1 Notation. Project #2 2

Directed Towers of Hanoi

Algorithmique appliquée Projet UNO

Permutations and Combinations

Stack permutations and an order relation for binary trees

In how many ways can we paint 6 rooms, choosing from 15 available colors? What if we want all rooms painted with different colors?

NANYANG TECHNOLOGICAL UNIVERSITY SEMESTER II EXAMINATION MH1301 DISCRETE MATHEMATICS. Time Allowed: 2 hours

An Algorithm for Longest Common Subsequence (LCS) Problem in Multiple Sequences

Merge Sort. Note that the recursion bottoms out when the subarray has just one element, so that it is trivially sorted.

ProCo 2017 Advanced Division Round 1

Divide & conquer. Which works better for multi-cores: insertion sort or merge sort? Why?

Sec 5.1 The Basics of Counting

6.006 Introduction to Algorithms. Lecture 20: Dynamic Programming III Prof. Erik Demaine

Sorting with Pop Stacks

Slides credited from Hsueh-I Lu, Hsu-Chun Hsiao, & Michael Tsai

CSE548, AMS542: Analysis of Algorithms, Fall 2016 Date: Sep 25. Homework #1. ( Due: Oct 10 ) Figure 1: The laser game.

1. Completing Sequences

EXPLAINING THE SHAPE OF RSK

CS3334 Data Structures Lecture 4: Bubble Sort & Insertion Sort. Chee Wei Tan

#A13 INTEGERS 15 (2015) THE LOCATION OF THE FIRST ASCENT IN A 123-AVOIDING PERMUTATION

Permutation Editing and Matching via Embeddings

mywbut.com Two agent games : alpha beta pruning

I.M.O. Winter Training Camp 2008: Invariants and Monovariants

Permutation Tableaux and the Dashed Permutation Pattern 32 1

Game Theory and Economics of Contracts Lecture 4 Basics in Game Theory (2)

ACM Collegiate Programming Contest 2016 (Hong Kong)

Enumeration of Two Particular Sets of Minimal Permutations

AIMA 3.5. Smarter Search. David Cline

CS 787: Advanced Algorithms Homework 1

Midterm for Name: Good luck! Midterm page 1 of 9

FIBONACCI KOLAMS -- AN OVERVIEW

ENGR170 Assignment Problem Solving with Recursion Dr Michael M. Marefat

Second Annual University of Oregon Programming Contest, 1998

Greedy Flipping of Pancakes and Burnt Pancakes

Problem F. Chessboard Coloring

Some algorithmic and combinatorial problems on permutation classes

Minimax Trees: Utility Evaluation, Tree Evaluation, Pruning

Chapter 5 Backtracking. The Backtracking Technique The n-queens Problem The Sum-of-Subsets Problem Graph Coloring The 0-1 Knapsack Problem

Unique Sequences Containing No k-term Arithmetic Progressions

Transportation Timetabling

Information Theory and Communication Optimal Codes

Activity Sheet #1 Presentation #617, Annin/Aguayo,

Eleventh Annual Ohio Wesleyan University Programming Contest April 1, 2017 Rules: 1. There are six questions to be completed in four hours. 2.

Spring 06 Assignment 2: Constraint Satisfaction Problems

COCI 2008/2009 Contest #3, 13 th December 2008 TASK PET KEMIJA CROSS MATRICA BST NAJKRACI

Chapter 1 INTRODUCTION TO SOURCE CODING AND CHANNEL CODING. Whether a source is analog or digital, a digital communication

COUNTING AND PROBABILITY

CMPUT 396 Tic-Tac-Toe Game

CS 2710 Foundations of AI. Lecture 9. Adversarial search. CS 2710 Foundations of AI. Game search

Design and Implementation of Hybrid Parallel Prefix Adder

ENUMERATION SCHEMES FOR PATTERN-AVOIDING WORDS AND PERMUTATIONS

2015 ACM ICPC Southeast USA Regional Programming Contest. Division 1

SCRABBLE ARTIFICIAL INTELLIGENCE GAME. CS 297 Report. Presented to. Dr. Chris Pollett. Department of Computer Science. San Jose State University

A time shift delay or advances the signal in time by a time interval +t 0 or t 0, without changing its shape.

The 2013 British Informatics Olympiad

Scheduling. Radek Mařík. April 28, 2015 FEE CTU, K Radek Mařík Scheduling April 28, / 48

Discrete Mathematics: Logic. Discrete Mathematics: Lecture 15: Counting

17. Symmetries. Thus, the example above corresponds to the matrix: We shall now look at how permutations relate to trees.

Problem A: Ordering supermarket queues

Outline. Content The basics of counting The pigeonhole principle Reading Chapter 5 IRIS H.-R. JIANG

Animation Demos. Shows time complexities on best, worst and average case.

Permutation Generation on Vector Processors

TWO-WAY communication between two nodes was first

CSE465, Spring 2009 March 16 1

game tree complete all possible moves

Module 3 Greedy Strategy

Computing Explanations for the Unary Resource Constraint

CSE : Python Programming

MITOCW watch?v=tw1k46ywn6e

Restricted Permutations Related to Fibonacci Numbers and k-generalized Fibonacci Numbers

Past questions from the last 6 years of exams for programming 101 with answers.

The Harassed Waitress Problem

2359 (i.e. 11:59:00 pm) on 4/16/18 via Blackboard

AN ALTERNATIVE METHOD FOR ASSOCIATION RULES

CS 1571 Introduction to AI Lecture 12. Adversarial search. CS 1571 Intro to AI. Announcements

Transcription:

Longest increasing subsequence Problem Longest increasing subsequence: You are given a sequence of integers A[1], A[2],..., A[n] and you are asked to find a longest increasing subsequence of integers.

Longest increasing subsequence Problem Longest increasing subsequence: You are given a sequence of integers A[1], A[2],..., A[n] and you are asked to find a longest increasing subsequence of integers. Let L(i) denote the length of the longest increasing subsequence that ends with the number A[i] What is L(1)? L(1) = 1 What is the value of L(i) in terms of L(1),...L(i 1)? L(i) = 1 + max {L(j)} j<i and A[j]<A[j] Note that if the set {j : j < i and A[j] < A[i]} is empty, then the second term on the RHS is 0.

Longest increasing subsequence Algorithm Length-LIS-recursive(A, n) - If (n = 1) return(1) - max 1 - For j = (n 1) to 1 - If (A[j] < A[n]) - s Length-LIS-recursive(A, j) - If (max < s + 1) max s + 1 - return(max) What is the running time of this algorithm? T (n) = T (n 1) + T (n 2) +... + T (1) + cn; T (1) c T (n) = O(n 2 n ) Lot of nodes in the recursion tree are repeated.

Longest increasing subsequence Algorithm Length-LIS(A, n) - For i = 1 to n - max 1 - For j = 1 to (i 1) - If (A[j] < A[i]) - If (max < L[j] + 1) max L[j] + 1 - L[i] max - return the maximum of L[i] s What is the running time of this algorithm?

Longest increasing subsequence Algorithm Length-LIS(A, n) - For i = 1 to n - max 1 - For j = 1 to (i 1) - If (A[j] < A[i]) - If (max < L[j] + 1) max L[j] + 1 - L[i] max - return the maximum of L[i] s What is the running time of this algorithm? O(n 2 ) But the problem was to find a longest increasing subsequence and not the length!

Longest increasing subsequence Algorithm LIS(A, n) - For i = 1 to n - max 1 - P[i] i - For j = 1 to (i 1) - If (A[j] < A[i]) - If (max < L[j] + 1) - max L[j] + 1 - P[i] j - L[i] max -... // Use P to output a longest increasing subsequence But the problem was to find a longest increasing subsequence and not the length! For each number, we just note down the index of the number preceding this number in a longest increasing subsequence.

Longest increasing subsequence Algorithm LIS(A, n) - For i = 1 to n - max 1 - P[i] i - For j = 1 to (i 1) - If (A[j] < A[i]) - If (max < L[j] + 1) - max L[j] + 1 - P[i] j - L[i] max - OutputSequence(A, L, P, n)

Longest increasing subsequence Algorithm OutputSequence(A, L, P, n) - Let j be the index such that L[j] is maximized - i 1 - While (P[j] j) - B[i] A[j] - j P[j] - i i + 1 - B[i] A[j] - Print B in reverse order So, one of the longest increasing subsequence is (7, 8, 9, 10).

Longest common subsequence Problem Let S and T be strings of characters. S is of length n and T is of length m. Find a longest common subsequence in S and T. This is a longest sequence of characters (not necessarily contiguous) that appear in both S and T. Example S = XYXZPQ, T = YXQYXP

Longest common subsequence Problem Let S and T be strings of characters. S is of length n and T is of length m. Find a longest common subsequence in S and T. This is a longest sequence of characters (not necessarily contiguous) that appear in both S and T. Example S = XYXZPQ, T = YXQYXP The longest common subsequence is XYXP S = XYXZPQ, T = YXQYXP How do we define the subproblems?

Longest common subsequence Problem Let S and T be strings of characters. S is of length n and T is of length m. Find a longest common subsequence in S and T. This is a longest sequence of characters (not necessarily contiguous) that appear in both S and T. Example S = XYXZPQ, T = YXQYXP The longest common subsequence is XYXP S = XYXZPQ, T = YXQYXP Let L(i, j) denote the length of the longest common subsequence in strings S[1],..., S[i] and T [1],..., T [j]. What is L(1, j) for 1 < j m?

Longest common subsequence Problem Let S and T be strings of characters. S is of length n and T is of length m. Find a longest common subsequence in S and T. This is a longest sequence of characters (not necessarily contiguous) that appear in both S and T. Example S = XYXZPQ, T = YXQYXP The longest common subsequence is XYXP S = XYXZPQ, T = YXQYXP Let L(i, j) denote the length of the longest common subsequence in strings S[1],..., S[i] and T [1],..., T [j]. What is L(1, j) for 1 < j m? 1 if S[1] is present in the string T [1],..., T [j], 0 otherwise.

Quiz

Quiz Quiz 06 1 Solve the following recurrence relations and give the exact value of T (n). (a) T (n) = T (n 1) + n; T (1) = 1 (b) T (n) = 2 T (n 1) + 2 n ; T (1) = 2

End