CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 4: Dynamic Programming, Trees

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
CPSC 322, Lecture 5Slide 1 Uninformed Search Computer Science cpsc322, Lecture 5 (Textbook Chpt 3.4) January, 14, 2009.
B+-Trees (PART 1) What is a B+ tree? Why B+ trees? Searching a B+ tree
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 7: Greedy Algorithms
CS2420: Lecture 13 Vladimir Kulyukin Computer Science Department Utah State University.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 5: Logs and Trees
B + -Trees (Part 1) Lecture 20 COMP171 Fall 2006.
CS 206 Introduction to Computer Science II 11 / 24 / 2008 Instructor: Michael Eckmann.
CS 261 – Winter 2010 Trees. Ubiquitous – they are everywhere in CS Probably ranks third among the most used data structure: 1.Vectors and Arrays 2.Lists.
CS 206 Introduction to Computer Science II 02 / 25 / 2009 Instructor: Michael Eckmann.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Introduction to trees.
Binary Trees Chapter 6.
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 1: Introduction.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 25 There.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 2: Orders of Growth
CS261 Data Structures Trees Introduction and Applications.
CSCE350 Algorithms and Data Structure Lecture 17 Jianjun Hu Department of Computer Science and Engineering University of South Carolina
David Evans CS200: Computer Science University of Virginia Computer Science Class 36: Public-Key Cryptography If you want.
Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are.
Topic 17 Introduction to Trees
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 23: Review.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
Dynamic Programming David Kauchak cs302 Spring 2013.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 3: Levels of Abstraction
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
Week 8 - Wednesday.  What did we talk about last time?  Level order traversal  BST delete  2-3 trees.
David Evans CS150: Computer Science University of Virginia Computer Science Class 38: Googling.
Heapsort. What is a “heap”? Definitions of heap: 1.A large area of memory from which the programmer can allocate blocks as needed, and deallocate them.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
Computer Sciences Department1.  Property 1: each node can have up to two successor nodes (children)  The predecessor node of a node is called its.
Dynamic Programming David Kauchak cs161 Summer 2009.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Binary Search Trees.  Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement binary.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 6: Ordered Data Abstractions
CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Symbol tables.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 8: Crash Course in Computational Complexity.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
CS Prelim Review – 10/15/09  First prelim is TOMORROW at 7:30 PM  Review session – Tonight 7:30 PM, Phillips 101  Things you should do:  Review every.
Amortized Analysis and Heaps Intro David Kauchak cs302 Spring 2013.
"Teachers open the door, but you must enter by yourself. "
Non Linear Data Structure
Data Structures and Design in Java © Rick Mercer
Week 7 - Friday CS221.
Multiway Search Trees Data may not fit into main memory
Heaps, Heap Sort and Priority Queues
DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
Introduction to Algorithms
Multiway search trees and the (2,4)-tree
FLIPPED CLASSROOM ACTIVITY CONSTRUCTOR – USING EXISTING CONTENT
CO4301 – Advanced Games Development Week 10 Red-Black Trees continued
Trees Lecture 12 CS2110 – Fall 2017.
Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb.
Data Structures and Database Applications Binary Trees in C#
CS216: Program and Data Representation
Tree Representation Heap.
Heaps 12/4/2018 5:27 AM Heaps /4/2018 5:27 AM Heaps.
"Teachers open the door, but you must enter by yourself. "
CSE 589 Applied Algorithms Spring 1999
CS 261 – Data Structures Trees.
Amortized Analysis and Heaps Intro
Dynamic Programming.
Cs212: Data Structures Lecture 7: Tree_Part1
Presentation transcript:

CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 4: Dynamic Programming, Trees

2 UVa CS216 Spring Lecture 4: Dynamic Programming, Trees Schedule This Week Sections today and Tuesday –Thornton D rooms Wednesday, Feb 1: –Go to Ron Rivest’s talk –10:30am-noon, Newcomb Hall South Meeting Room Thursday, Feb 2: –Portman Wills, “Saving the World with a Computer Science Degree” –3:30pm, MEC 205 (CS290/CS390 +)

3 UVa CS216 Spring Lecture 4: Dynamic Programming, Trees RSA (Rivest, Shamir, Adelman 1978) Public-Key Encryption –Allows parties who have not established a shared secret to communicate securely –Your web browser used it when you see Most important algorithm invented in past 100 years Link from notes

4 UVa CS216 Spring Lecture 4: Dynamic Programming, Trees Wednesday’s Class Ron Rivest, “Security of Voting Systems” Newcomb Hall South Meeting Room, 10:30am-noon This replaces CS216 class (so you don’t have to walk out at 11am)

5 UVa CS216 Spring Lecture 4: Dynamic Programming, Trees Sequence Alignment Brute force algorithm in PS1 bestAlignment (U, V) = base case if |U| == 0 or |V| == 0 otherwise f (bestAlignment (U[1:], V), bestAlignment (U, V[1:]), bestAlignment (U[1:], V[1:]) Compare to Fibonacci: fibonacci (n) = base case if n == 0 or n == 1 g (fibonacci (n-1), fibonacci (n-2)) Running time   (  n )  = 1.618…

6 UVa CS216 Spring Lecture 4: Dynamic Programming, Trees Sequence Alignment Input size = n = |U| + |V| bestAlignment (U, V) = base case if |U| == 0 or |V| == 0 otherwise f (bestAlignment (U[1:], V), size = n-1 bestAlignment (U, V[1:]), size = n-1 bestAlignment (U[1:], V[1:]) size = n-2 a(n) = a(n-1) + a(n-1) + a(n-2) > a(n-1) + a(n-2)   (  n ) Running time of bestAlignment   (  n )

7 UVa CS216 Spring Lecture 4: Dynamic Programming, Trees Growth of Best Alignment 3n3n 2n2n nn f(n) = 2f(n-1) + f(n-2)

8 UVa CS216 Spring Lecture 4: Dynamic Programming, Trees BLAST GCGTTGCTGGCGTTTTTCCATAGGCTCCGCCCCCCTGACGAGCATCACAAAAATCGACGCGGTGGCGAAACCCGACA GGACTATAAAGATACCAGGCGTTTCCCCCTGGAAGCTCCCTCGTGTTCCGACCCTGCCGCTTACCGGATACCTGTCCG CCTTTCTCCCTTCGGGAAGCGTGGCTGCTCACGCTGTACCTATCTCAGTTCGGTGTAGGTCGTTCGCTCCAAGCTGG GCTGTGTGCCGTTCAGCCCGACCGCTGCGCCTTATCCGGTAACTATCGTCTTGAGTCCAACCCGGTAAAGTAGGACA GGTGCCGGCAGCGCTCTGGGTCATTTTCGGCGAGGACCGCTTTCGCTGGAGATCGGCCTGTCGCTTGCGGTATTCG GAATCTTGCACGCCCTCGCTCAAGCCTTCGTCACTCCAAACGTTTCGGCGAGAAGCAGGCCATTATCGCCGGCATGG CGGCCGACGCGCTGGGCTGGCGTTCGCGACGCGAGGCTGGATGGCCTTCCCCATTATGATTCTTCTCGCTTCCGGCG GCCCGCGTTGCAGGCCATGCTGTCCAGGCAGGTAGATGACGACCATCAGGGACAGCTTCAACGGCTCTTACCAGCCT AACTTCGATCACTGGACCGCTGATCGTCACGGCGATTTATGCCGCACATGGACGCGTTGCTGGCGTTTTTCCATAGGC TCCGCCCCCCTGACGAGCATCACAAACAAGTCAGAGGTGGCGAAACCCGACAGGACTATAAAGATACCAGGCGTTTC CCCCTGGAAGCGCTCTCCTGTTCCGACCCTGCCGCTTACCGGATACCTGTCCGCCTTTCTCCCTTCGGGCTTTCTCAA TGCTCACGCTGTAGGTATCTCAGTTCGGTGTAGGTCGTTCGCTCCAAGCTGACGAACCCCCCGTTCAGCCCGACCGC TGCGCCTTATCCGGTAACTATCGTCTTGAGTCCAACACGACTTAACGGGTTGGCATGGATTGTAGGCGCCGCCCTATA CCTTGTCTGCCTCCCCGCGGTGCATGGAGCCGGGCCACCTCGACCTGAATGGAAGCCGGCGGCACCTCGCTAACGG CCAAGAATTGGAGCCAATCAATTCTTGCGGAGAACTGTGAATGCGCAAACCAACCCTTGGCCATCGCGTCCGCCATCT CCAGCAGCCGCACGCGGCGCATCTCGGGCAGCGTTGGGTCCT Dinosaur DNA from Jurassic Park (p. 103) Basic Local Alignment Search Tool

9 UVa CS216 Spring Lecture 4: Dynamic Programming, Trees

10 UVa CS216 Spring Lecture 4: Dynamic Programming, Trees |U| = 1200 |V| > 16.5 Billion

11 UVa CS216 Spring Lecture 4: Dynamic Programming, Trees Needleman-Wunsch Algorithm Avoid effort duplication by storing intermediate results Computing a matrix showing the best possible score up to each pair of positions Find the alignment by following a path in that matrix

12 UVa CS216 Spring Lecture 4: Dynamic Programming, Trees N-W Initialization -CATG -0 A T G G Sequence U Sequence V

13 UVa CS216 Spring Lecture 4: Dynamic Programming, Trees Filling Matrix -CATG -0 A T G G Sequence U Sequence V For each square consider:  Gap in U score = score[  ] - g  Gap in V score = score[  ] - g  No gap match: score = score[  ] + c no match: score = score[  ]

14 UVa CS216 Spring Lecture 4: Dynamic Programming, Trees Filling Matrix -CATG -0 A T G G  score = score[  ] - g  score = score[  ] - g  match: score = score[  ] + c no match: score = score[  ] CATG CATG -A--

15 UVa CS216 Spring Lecture 4: Dynamic Programming, Trees Finished Matrix -CATG A T G G

16 UVa CS216 Spring Lecture 4: Dynamic Programming, Trees Finding Alignment -CATG A T G G Start in bottom right corner; follow arrows:  gap V  gap U  no gap GGGG -G-G TTTT AAAA C-C-

17 UVa CS216 Spring Lecture 4: Dynamic Programming, Trees N-W Correctness Informal argument: –Fills each cell by picking the best possible choice –Finds the best possible path using the filled in matrix Guaranteed to find the best possible alignment since all possibilities are considered

18 UVa CS216 Spring Lecture 4: Dynamic Programming, Trees N-W Analysis What is the space usage? Need to store the matrix: = (|U| + 1) * (|V| + 1)

19 UVa CS216 Spring Lecture 4: Dynamic Programming, Trees N-W Running Time Time to fill matrix –Each square  O(1) –Assumes: Lookups are O(1) –Time scales with number of cells:  (|U|  |V|) Time to find alignment –One decision for each entry in answer  (|U| + |V|) Total running time   (|U|  |V|)  score = score[  ] - g  score = score[  ] - g  match: score = score[  ] + c no match: score = score[  ]

20 UVa CS216 Spring Lecture 4: Dynamic Programming, Trees |U| = 1200 |V| > 16.5 Billion Good enough?

21 UVa CS216 Spring Lecture 4: Dynamic Programming, Trees Heuristic Alignment BLAST needs to be faster No asymptotically faster algorithm is guaranteed to find best alignment –Only way to do better is reject some alignments without considering them BLAST uses heuristics: –Looks for short sequences (~3 proteins = 9 nucleotides) that match well without gaps –Extend those sequences into longer sequences

22 UVa CS216 Spring Lecture 4: Dynamic Programming, Trees PS2 Part 1 (1-3): List representations Part 2 (4-5): Dynamic programming implementation of sequence alignment Part 3 (6-10): Brute force implementation of phylogeny –Like alignment, brute force doesn’t scale beyond very small inputs –Unlike alignment, to do better we will need to use heuristics (give up correctness!) This will be part of PS3 PS2 is longer and harder than PS1. You have 10 days to complete it - Get started early!

23 UVa CS216 Spring Lecture 4: Dynamic Programming, Trees Data Structures If we have a good list implementation, do we need any other data structures? For computing: no –We can compute everything with just lists (actually even less). The underlying machine memory can be thought of as a list. For thinking: yes –Lists are a very limited way of thinking about problems.

24 UVa CS216 Spring Lecture 4: Dynamic Programming, Trees List Limitations L Node Info: Next: 1 Node Info: Next: 2 Info: Next: 3 Node In a list, every element has direct relationships with only two things: predecessor and successor

25 UVa CS216 Spring Lecture 4: Dynamic Programming, Trees Complex Relationships Bill Cheswick’s Map of the Internet

26 UVa CS216 Spring Lecture 4: Dynamic Programming, Trees List  Tree List: each element has relationships with up to 2 other elements: Binary Tree: each element has relationships with up to 3 other elements: Element PredecessorSuccessor Element Parent Right Child Left Child

27 UVa CS216 Spring Lecture 4: Dynamic Programming, Trees Language Phylogeny English German Anglo-Saxon Norwegian Czech Spanish Italian Romanian French From Lecture 1...

28 UVa CS216 Spring Lecture 4: Dynamic Programming, Trees Tree Terms English German Anglo-Saxon Norwegian Czech Spanish Italian Romanian French Leaf Root Note that CS trees are usually drawn upside down! Height = 3 Height = 0 Depth = 4

29 UVa CS216 Spring Lecture 4: Dynamic Programming, Trees Tree Terms Root: a node with no parent –There can only be one root Leaf: a node with no children Height of a Node: length of the longest path from that node to a leaf Depth of a Node: length of the path from the Root to that node Height of a Tree: maximum depth of a node in that tree = height of the root

30 UVa CS216 Spring Lecture 4: Dynamic Programming, Trees Perfect Binary Tree All leaves have the same depth How many leaves? 2h2h h How many nodes? 2 h+1 -1 # Nodes = h

31 UVa CS216 Spring Lecture 4: Dynamic Programming, Trees Tree Node Operations getLeftChild (Node) getRightChild (Node) getInfo (Node) def isLeaf (self): return self.getLeftChild () == None and self.getRightChild () == None

32 UVa CS216 Spring Lecture 4: Dynamic Programming, Trees Calculating Height def height (self): if self.isLeaf (): return 0 else: return 1 + max(self.getLeftChild().height(), self.getRightChild().height()) Height of a Node: length of the longest path from that node to a leaf

33 UVa CS216 Spring Lecture 4: Dynamic Programming, Trees Analyzing Height What is the asymptotic running time or our height procedure? def height (self): if self.isLeaf (): return 0 else: return 1 + max(self.getLeftChild().height(), self.getRightChild().height())

34 UVa CS216 Spring Lecture 4: Dynamic Programming, Trees Charge Today and tomorrow: –Sections in Thorton D classrooms Wednesday: –Instead of CS216, go to Ron Rivest’s talk –10:30am, Newcomb Hall South Meeting Room Get started on PS2!