CPS 100, Fall 2009 11.1 Algorithms and Idioms l How do you avoid calculating the same thing over and over and over and over and …  Does it matter?  Recalculate.

Slides:



Advertisements
Similar presentations
Recursion CS 367 – Introduction to Data Structures.
Advertisements

1 Transportation Model. 2 Basic Problem The basic idea in a transportation problem is that there are sites or sources of product that need to be shipped.
(())()(()) ((())()(())()(()))
Dynamic Programming.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
CPS 100, Fall Backtracking by image search.
Data Structures and Algorithms Data Structures and Algorithms (CS210/ESO207/ESO211) Lecture 12 Application of Stack and Queues Application of Stack and.
CompSci 100e Program Design and Analysis II March 3, 2011 Prof. Rodger CompSci 100e, Spring
Algorithm Strategies Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CPSC 311, Fall 2009: Dynamic Programming 1 CPSC 311 Analysis of Algorithms Dynamic Programming Prof. Jennifer Welch Fall 2009.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
CPSC 411 Design and Analysis of Algorithms Set 5: Dynamic Programming Prof. Jennifer Welch Spring 2011 CPSC 411, Spring 2011: Set 5 1.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 08 / 2008 Instructor: Michael Eckmann.
Backtracking.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
ADA: 7. Dynamic Prog.1 Objective o introduce DP, its two hallmarks, and two major programming techniques o look at two examples: the fibonacci.
CS 5243: Algorithms Dynamic Programming Dynamic Programming is applicable when sub-problems are dependent! In the case of Divide and Conquer they are.
Scaling up Decision Trees. Decision tree learning.
“Planning is bringing the future into the present so that you can do something about it now.” – Alan Lakein Thought for the Day.
Honors Track: Competitive Programming & Problem Solving Optimization Problems Kevin Verbeek.
CPS 100, Fall GridGame APT How would you solve this problem using recursion?
Dynamic Programming Louis Siu What is Dynamic Programming (DP)? Not a single algorithm A technique for speeding up algorithms (making use of.
Topic 25 Dynamic Programming "Thus, I thought dynamic programming was a good name. It was something not even a Congressman could object to. So I used it.
CPS 100, Spring Tools: Solve Computational Problems l Algorithmic techniques  Brute-force/exhaustive, greedy algorithms, dynamic programming,
CPS Backtracking, Search, Heuristics l Many problems require an approach similar to solving a maze  Certain mazes can be solved using the “right-hand”
Efficient Algorithms for Some Variants of the Farthest String Problem Chih Huai Cheng, Ching Chiang Huang, Shu Yu Hu, Kun-Mao Chao.
CPS 100, Fall From Recursion to Self Reference public int calc(int n){ return n*calc(n-1); } l What is the Internet?  A network of networks.
CompSci 100e 6.1 Plan for the week l More recursion examples l Backtracking  Exhaustive incremental search  When we a potential solution is invalid,
Introduction to Algorithms Jiafen Liu Sept
Where’s the title? You gotta search for it!. PotW Solution String s = new Scanner(System.in).next(); int[] prev = new int[s.length() * 2 + 2]; Arrays.fill(prev,
Compsci 201 Recitation 10 Professor Peck Jimmy Wei 11/1/2013.
Game tree search Chapter 6 (6.1 to 6.3 and 6.6) cover games. 6.6 covers state of the art game players in particular. 6.5 covers games that involve uncertainty.
CompSci 100 Prog Design and Analysis II Oct 26, 2010 Prof. Rodger CPS 100, Fall
Dynamic Programming & Memoization. When to use? Problem has a recursive formulation Solutions are “ordered” –Earlier vs. later recursions.
Compsci 6/101, Spring PFTW: Functions, Control, Python/Tools l How do functions work and why do we use them?  Functions we call (APIs), Functions.
Probabilistic methods for phylogenetic tree reconstruction BMI/CS 576 Colin Dewey Fall 2015.
Fundamental Data Structures and Algorithms Ananda Guna March 18, 2003 Dynamic Programming Part 1.
Contents What is a trie? When to use tries
CPS 100, Spring Search, Backtracking,Heuristics l How do you find a needle in a haystack?  How does a computer play chess?  Why would you write.
1 Chapter 15-2: Dynamic Programming II. 2 Matrix Multiplication Let A be a matrix of dimension p x q and B be a matrix of dimension q x r Then, if we.
CompSci 100 Prog Design and Analysis II October 19, 2010 Prof. Rodger CompSci 100, Fall
TWTG What work is still to come APT, Huff, APT
CSC 421: Algorithm Design & Analysis
Hidden Markov Models BMI/CS 576
Game playing Types of games Deterministic vs. chance
Searching, Maps,Tries (hashing)
Lecture 12.
Word Ladder APT From->[words]->to
Programming Abstractions
13 Text Processing Hongfei Yan June 1, 2016.
CSC 421: Algorithm Design & Analysis
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Types of Algorithms.
Topic 25 Dynamic Programming
Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms
Dynamic Programming.
Sequence Alignment 11/24/2018.
CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act
CSE 589 Applied Algorithms Spring 1999
Chapter 15: Dynamic Programming II
Breadth First Search - A B C D E F G H I front FIFO Queue.
Dynamic Programming.
Trevor Brown DC 2338, Office hour M3-4pm
Dynamic Programming-- Longest Common Subsequence
Introduction to Algorithms: Dynamic Programming
Backtracking, Search, Heuristics
Backtracking, Search, Heuristics
Presentation transcript:

CPS 100, Fall Algorithms and Idioms l How do you avoid calculating the same thing over and over and over and over and …  Does it matter?  Recalculate v Retrieve: tradeoff of ?  Cache/Caching Memoize/Memoization l Numbers represent state, extend one step at a time  How many ways can we get to a corner in a grid? What subproblem helps answer the question  Sometimes related to memoizing, sometimes to dynamic programming

CPS 100, Fall Catalan number l How many trees are there with N nodes?  We can enumerate for a fixed N, e.g., 3 or 4 or …  If we know the number for 1,2,...,N-1, answer for N? l Try recursive solution dictated by observation  Why is it slow? How can we make it fast?  Recursion meets memoization!

CPS 100, Fall Digression: word ladders l How many ladders from cart to dire as shown?  Enqueue dare more than once?  Downside? Alternative? l We want to know number of ladders that end at W.  What do we know initially?  When we put something on the queue, what do we know?  How do we keep track? l Similar to tree-counting? cart care tart dart dare dirt dire

CPS 100, Fall Word Ladder: more details l # ladders that end at dare  At each word W l Ladder length to W  Calculable from?? l Two maps cart care tart dart dare dirt dire hire wire mire here were mere pere l Dequeue s  foreach W one-away  if not-seen ???  else ???

CPS 100, Fall Alan Kay l Turing award 2003  OO programming, Dynabook l “The best way to predict the future is to invent it” l “American’s have no past and no future, they live in an extended present.” I think the main thing about doing …any kind of programming work, is that there has to be some exquisite blend between beauty and practicality. There's no reason to sacrifice either one of those, and people who are willing to sacrifice either one of those, I don't think really get what computing is all about.

CPS 100, Fall Digression: Bogglescore APT l How many ways to find Aha?!  Each one scores points  Related to genomics problem l Number of ways that AHA ends at (2,1)?  What do we know initially?  When we extend search what do we know?  How do we keep track? l Why do we need to avoid revisits?  Caching, memoizing needed! A HAA A A H A H HAA A A H A

CPS 100, Fall Digression: Bogglescore APT l # times we can find: AAAAA…AAA  # 50-step paths in grid, why? Big? Small?  Calculate # 49 step paths at each spot Foreach cell, sum neighbors If recursive, what happens? l Memoize on row, column, word-length  For each word, clear cache/memo  For every row, column If [r][c] == first char, recurse l String shorter by 1  Toward length 1, stop and return 1  If seen [r][c][length] return value  When do we return zero? A AAA A A A A A AAA A A A A

CPS 100, Fall Bogglescore alternative l Last solution was lexicon-first with memoizing  Visit every cell, but don’t revisit with identical substring l What about board-first solution?  When we visit a cell, we’re working with word-so-far  Avoid revisiting cell with same word-so-far Memoize on word/row/col, make this Map-able Hash on word, row, and col Compare based on word, row, and col  Don’t recurse if not a prefix  Store score if word-so-far is a word, still recurse