CSCE350 Algorithms and Data Structure Lecture 17 Jianjun Hu Department of Computer Science and Engineering University of South Carolina 2009.11.

Slides:



Advertisements
Similar presentations
Advanced Algorithm Design and Analysis (Lecture 7) SW5 fall 2004 Simonas Šaltenis E1-215b
Advertisements

 2004 SDU Lecture11- All-pairs shortest paths. Dynamic programming Comparing to divide-and-conquer 1.Both partition the problem into sub-problems 2.Divide-and-conquer.
RAIK 283: Data Structures & Algorithms
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Design and Analysis of Algorithms - Chapter 81 Dynamic Programming Warshall’s and Floyd’sAlgorithm Dr. Ying Lu RAIK 283: Data Structures.
B-Trees. Motivation for B-Trees Index structures for large datasets cannot be stored in main memory Storing it on disk requires different approach to.
Dynamic Programming Reading Material: Chapter 7..
Dynamic Programming Technique. D.P.2 The term Dynamic Programming comes from Control Theory, not computer science. Programming refers to the use of tables.
Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Other time considerations Source: Simon Garrett Modifications by Evan Korth.
Design and Analysis of Algorithms - Chapter 81 Dynamic Programming Dynamic Programming is a general algorithm design technique Dynamic Programming is a.
Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
1 Advanced Algorithms All-pairs SPs DP algorithm Floyd-Warshall alg.
UNC Chapel Hill Lin/Manocha/Foskey Optimization Problems In which a set of choices must be made in order to arrive at an optimal (min/max) solution, subject.
B-Trees. CSM B-Trees 2 Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so.
Design and Analysis of Algorithms - Chapter 81 Dynamic Programming Dynamic Programming is a general algorithm design techniqueDynamic Programming is a.
Dynamic Programming A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River,
Important Problem Types and Fundamental Data Structures
Dynamic Programming Introduction to Algorithms Dynamic Programming CSE 680 Prof. Roger Crawfis.
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
Indexing. Goals: Store large files Support multiple search keys Support efficient insert, delete, and range queries.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
1 B Trees - Motivation Recall our discussion on AVL-trees –The maximum height of an AVL-tree with n-nodes is log 2 (n) since the branching factor (degree,
Dynamic Programming. Well known algorithm design techniques:. –Divide-and-conquer algorithms Another strategy for designing algorithms is dynamic programming.
Dynamic Programming Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by or formulated.
B-Trees. CSM B-Trees 2 Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so.
1 B-Trees & (a,b)-Trees CS 6310: Advanced Data Structures Western Michigan University Presented by: Lawrence Kalisz.
Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
CSC401: Analysis of Algorithms CSC401 – Analysis of Algorithms Chapter Dynamic Programming Objectives: Present the Dynamic Programming paradigm.
B-Trees. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it.
B-Trees. CSM B-Trees 2 Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so.
MA/CSSE 473 Day 28 Dynamic Programming Binomial Coefficients Warshall's algorithm Student questions?
1 Tree Indexing (1) Linear index is poor for insertion/deletion. Tree index can efficiently support all desired operations: –Insert/delete –Multiple search.
The all-pairs shortest path problem (APSP) input: a directed graph G = (V, E) with edge weights goal: find a minimum weight (shortest) path between every.
Lecture 11COMPSCI.220.FS.T Balancing an AVLTree Two mirror-symmetric pairs of cases to rebalance the tree if after the insertion of a new key to.
Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
CSCE350 Algorithms and Data Structure Lecture 19 Jianjun Hu Department of Computer Science and Engineering University of South Carolina
Lecture 15 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
1 Ch20. Dynamic Programming. 2 BIRD’S-EYE VIEW Dynamic programming The most difficult one of the five design methods Has its foundation in the principle.
Optimization Problems In which a set of choices must be made in order to arrive at an optimal (min/max) solution, subject to some constraints. (There may.
All-Pairs Shortest Paths
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
Computer Sciences Department1.  Property 1: each node can have up to two successor nodes (children)  The predecessor node of a node is called its.
Chapter 7 Dynamic Programming 7.1 Introduction 7.2 The Longest Common Subsequence Problem 7.3 Matrix Chain Multiplication 7.4 The dynamic Programming Paradigm.
Lecture 14 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
MA/CSSE 473 Day 30 B Trees Dynamic Programming Binomial Coefficients Warshall's algorithm No in-class quiz today Student questions?
B-Trees Katherine Gurdziel 252a-ba. Outline What are b-trees? How does the algorithm work? –Insertion –Deletion Complexity What are b-trees used for?
B-Trees B-Trees.
B-Trees B-Trees.
Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Introduction to the Design and Analysis of Algorithms
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Chapter 8 Dynamic Programming
CSCE350 Algorithms and Data Structure
Warshall’s and Floyd’sAlgorithm
CSCE350 Algorithms and Data Structure
Analysis and design of algorithm
Chapter 8 Dynamic Programming
Other time considerations
Dynamic Programming 1/15/2019 8:22 PM Dynamic Programming.
Dynamic Programming.
CSIT 402 Data Structures II With thanks to TK Prasad
Dynamic Programming.
Dynamic Programming.
Dynamic Programming.
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Major Design Strategies
Major Design Strategies
COSC 3101A - Design and Analysis of Algorithms 12
Presentation transcript:

CSCE350 Algorithms and Data Structure Lecture 17 Jianjun Hu Department of Computer Science and Engineering University of South Carolina

Midterm2 Average 84 >90 12 students

B-Trees Organize data for fast queries Index for fast search For datasets of structured records, B-tree indexing is used

B-Trees4 Motivation (cont.) Assume that we use an AVL tree to store about 20 million records We end up with a very deep binary tree with lots of different disk accesses; log 2 20,000,000 is about 24, so this takes about 0.2 seconds We know we can’t improve on the log n lower bound on search for a binary tree But, the solution is to use more branches and thus reduce the height of the tree! As branching increases, depth decreases

B-Trees5 Constructing a B-tree

B-Trees6 Definition of a B-tree A B-tree of order m is an m-way tree (i.e., a tree where each node may have up to m children) in which: 1.the number of keys in each non-leaf node is one less than the number of its children and these keys partition the keys in the children in the fashion of a search tree 2.all leaves are on the same level 3.all non-leaf nodes except the root have at least  m / 2  children 4.the root is either a leaf node, or it has from two to m children 5.a leaf node contains no more than m – 1 keys The number m should always be odd

B-Trees7 Comparing Trees Binary trees Can become unbalanced and lose their good time complexity (big O) AVL trees are strict binary trees that overcome the balance problem Heaps remain balanced but only prioritise (not order) the keys Multi-way trees B-Trees can be m-way, they can have any (odd) number of children One B-Tree, the 2-3 (or 3-way) B-Tree, approximates a permanently balanced binary tree, exchanging the AVL tree’s balancing operations for insertion and (more complex) deletion operations

Chapter 8: Dynamic Programming Dynamic Programming is a general algorithm design technique Invented by American mathematician Richard Bellman in the 1950s to solve optimization problems “Programming” here means “planning” Main idea: solve several smaller (overlapping) subproblems record solutions in a table so that each subproblem is only solved once final state of the table will be (or contain) solution

Example: Fibonacci numbers Recall definition of Fibonacci numbers: f (0) = 0 f (1) = 1 f (n) = f (n-1) + f (n-2) Computing the n th Fibonacci number recursively (top-down): f(n) f(n-1) + f(n-2) f(n-2) + f(n-3) f(n-3) + f(n-4)...

Example: Fibonacci numbers Computing the n th Fibonacci number using bottom-up iteration: f(0) = 0 f(1) = 1 f(2) = 0+1 = 1 f(3) = 1+1 = 2 f(4) = 1+2 = 3 f(5) = 2+3 = 5 … f(n-2) = f(n-1) = f(n) = f(n-1) + f(n-2)

Examples of Dynamic Programming Algorithms Computing binomial coefficients Optimal chain matrix multiplication Constructing an optimal binary search tree Warshall’s algorithm for transitive closure Floyd’s algorithms for all-pairs shortest paths Some instances of difficult discrete optimization problems: travelling salesman knapsack

The problem: check if all pair have a valid path a cd b You can run depth first search from each vertex… But can u do better?

Warshall’s Algorithm: Transitive Closure Computes the transitive closure of a relation Computes the transitive closure of a relation (Alternatively: all paths in a directed graph) (Alternatively: all paths in a directed graph) Example of transitive closure: Example of transitive closure:

Warshall’s Algorithm Main idea: a path exists between two vertices i, j, iff there is an edge from i to j; or there is a path from i to j going through vertex 1; or there is a path from i to j going through vertex 1 and/or 2; or there is a path from i to j going through vertex 1, 2, and/or 3; or... there is a path from i to j going through any of the other vertices

Warshall’s Algorithm R R R R R

Warshall’s Algorithm In the k th stage determine if a path exists between two vertices i, j using just vertices among 1,…,k R (k-1) [i,j] (path using just 1,…,k-1) R (k) [i,j] = or (R (k-1) [i,k] and R (k-1) [k,j]) (path from i to k and from k to i using just 1,…,k-1) i j k k th stage {

Pseudocode of Warshall’s Algorithm

Example Time efficiency? a cd b

Intuitive understanding of Warshall algo Can the above Warshall algorithm find all possible paths? Does Warshall algorithm avoid duplicate updates? i=6  9  12  3  7  5=j 6  9 9   3 3  7 7  5=j

Floyd’s Algorithm: All pairs shortest paths In a weighted graph, find shortest paths between every pair of vertices Same idea: construct solution through series of matrices D (0), D (1), … using an initial subset of the vertices as intermediaries

Similar to Warshall’s Algorithm in is equal to the length of shortest path among all paths from the ith vertex to jth vertex with each intermediate vertex, if any, numbered not higher than k

Pseudocode of Floyd’s Algorithm The next matrix in sequence can be written over its predecessor

Example Time efficiency?

Principle of Optimality An optimal solution to any instance of a optimization problem is composed of optimal solution to its subinstance e.g. Shortest path between a and b… then any distance of two points S, T on the path from a to b must also be shortest…. Why? Otherwise, we can choose another path S’ and T’!

Key issues in DP algorithm design Derive a recurrence relationship that expresses a solution to an instance of the problem in terms of solutions to its smaller subinstances

Design Algo for Sequence Alignment Problem using DP ACTGGAGGCCA AGCTAGAGAAG Score= Sum(si) match: +3 Mismatch:-2 gap-X -1