2019/4/10 chapter25.

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia Binary Search Trees
Advertisements

Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 11.
© 2004 Goodrich, Tamassia Binary Search Trees   
CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties and maintenance.
Chapter 5 Trees PROPERTIES OF TREES 3 4.
Binary Search Trees1 Part-F1 Binary Search Trees   
Binary Search Trees   . 2 Ordered Dictionaries Keys are assumed to come from a total order. New operations: closestKeyBefore(k) closestElemBefore(k)
Binary Search Trees1 ADT for Map: Map stores elements (entries) so that they can be located quickly using keys. Each element (entry) is a key-value pair.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
CompSci 100e Program Design and Analysis II March 29, 2011 Prof. Rodger CompSci 100e, Spring20111.
Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying.
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
© 2004 Goodrich, Tamassia Binary Search Trees1 CSC 212 Lecture 18: Binary and AVL Trees.
Binary Search Trees   . 2 Binary Search Tree Properties A binary search tree is a binary tree storing keys (or key-element pairs) at its.
Chapter 10: Search Trees Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Acknowledgement: These slides are adapted from slides provided with Data.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called.
Binary Search Trees1 Chapter 3, Sections 1 and 2: Binary Search Trees AVL Trees   
DAST Tirgul 6. What is a Binary Search Tree? The keys in a binary search tree (BST) are always stored in such a way as to satisfy the search property:
© 2004 Goodrich, Tamassia BINARY SEARCH TREES Binary Search Trees   
Algorithms Design Fall 2016 Week 6 Hash Collusion Algorithms and Binary Search Trees.
Part-D1 Binary Search Trees
Binary Search Trees < > = © 2010 Goodrich, Tamassia
Binary Search Trees < > =
Analysis of Algorithms
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery
Dynamic Programming Sequence of decisions. Problem state.
Search Trees.
Binary Search Trees < > =
Analysis of Algorithms
Heaps, Heap Sort and Priority Queues
Weighted Interval Scheduling
Binary Search Trees (10.1) CSE 2011 Winter August 2018.
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
Priority Queues © 2010 Goodrich, Tamassia Priority Queues 1
Data Structures Review Session 2
Binary Search Tree Chapter 10.
Chapter 10.1 Binary Search Trees
Binary Search Trees < > = Binary Search Trees
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
Heaps 9/13/2018 3:17 PM Heaps Heaps.
Binary Search Trees.
Chapter 2: Basic Data Structures
Binary Search Trees (10.1) CSE 2011 Winter November 2018.
Priority Queues and Heaps
Binary Search Trees < > = © 2010 Goodrich, Tamassia
Copyright © Aiman Hanna All rights reserved
Binary Search Trees < > =
Prepared by Chen & Po-Chuan 2016/03/29
Binary Search Trees < > = Binary Search Trees
Dictionaries < > = /3/2018 8:58 AM Dictionaries
© 2013 Goodrich, Tamassia, Goldwasser
Dictionaries < > = /9/2018 3:06 AM Dictionaries
Richard Anderson Lecture 16a Dynamic Programming
Dictionaries < > = /17/2019 4:20 PM Dictionaries
Binary Search Trees < > =
Building Java Programs
Binary Search Trees < > =
Binary Search Trees < > = Binary Search Trees
Dynamic Programming II DP over Intervals
Dynamic Programming Sequence of decisions. Problem state.
Tree traversals BST properties Search Insertion
1 Lecture 13 CS2013.
Dynamic Programming.
Dynamic Programming Sequence of decisions. Problem state.
Binary Search Trees < > = Dictionaries
Analysis of Algorithms
Dictionaries 二○一九年九月二十四日 ADT for Map:
Heaps 9/29/2019 5:43 PM Heaps Heaps.
Presentation transcript:

2019/4/10 chapter25

Backtracking and time complexity Backtracking is used to get the schedule. Time complexity O(n) if the jobs are sorted. Total time: O(n log n) including sorting. 2019/4/10 chapter25

Weighted Interval Scheduling: Bottom-Up Input: n, s1, s2, …, sn, f1, f2, …, fn, v1, v2, …, vn Sort jobs by finish times so that f1f2 … fn. Compute p(1), p(2) , …, p(n) M[0]=0; for j = 1 to n do M[j] = max { vj+m[p(j)], m[j-1]} if (M[j] == m[j-1]) then B[j]=j-1 else B[j]=p(j) /*for backtracking m=n; while ( m ≠0) { print “job m”; m=B[m]; } 2019/4/10 chapter25

Weighted Interval Scheduling: Bottom-Up Input: n, s1, s2, …, sn, f1, f2, …, fn, v1, v2, …, vn Sort jobs by finish times so that f1f2 … fn. Compute p(1), p(2) , …, p(n) M[0]=0; for j = 1 to n do M[j] = max { vj+m[p(j)], m[j-1]} if (M[j] == m[j-1]) then B[j]=j-1 else B[j]=p(j) /*for backtracking m=n; while ( m ≠0) { print “job m”; m=B[m]; } Solving recurrence equation is not required. 2019/4/10 chapter25

Interval with Maximum Sum.   2019/4/10 chapter25

2019/4/10 chapter25

Algorithm: O(n) 2019/4/10 chapter25

Int maxSum (int A[], int i, int j){ if (i==j){ maxSum(int A[], int i, int j) finds the interval [p, q] in array A[i…j] with max sum and return the interval [p, q]. Int maxSum (int A[], int i, int j){ if (i==j){ return [i, i] as the interval and A[i] as the sum } m=(i+j)/2; ([p1,q1], s1) = maxSum(A[], i, m); ([p2, q2], s2) = maxSum(A[], m+1, j); ([p3,q3], s3) = sumBothSides(A[], i, j, m) ([p, q], s) = the max of the three return [p, q} and s. 2019/4/10 chapter25

f(n)=2f(n/2)+O(n). So, f(n)=O(nlog n). Running time: f(n)=2f(n/2)+O(n). So, f(n)=O(nlog n). Open Problem: Can we design a faster algorithm? (come back later after learning other techniques) 2019/4/10

Part-D1 Binary Search Trees Dictionaries 二○一九年四月十日 Part-D1 Binary Search Trees < 6 2 > 9 1 4 = 8 Binary Search Trees

Binary Search Trees (§ 10.1) A binary search tree is a binary tree storing keys (or key-value entries) at its internal nodes and satisfying the following property: Let u, v, and w be three nodes such that u is in the left subtree of v and w is in the right subtree of v. We have key(u)  key(v)  key(w) External nodes do not store items An inorder traversal of a binary search trees visits the keys in increasing order 6 9 2 4 1 8 Binary Search Trees

Cost of the tree: f6*1+f2*2+f9*2+f1*3+f4*3+f8*3   < 6 2 > 9 1 4 = 8 Cost of the tree: f6*1+f2*2+f9*2+f1*3+f4*3+f8*3 2019/4/10

  2019/4/10

  2019/4/10

Assume that e1<e2<…en Algorithm: Assume that e1<e2<…en Calculate the sum of fi 2019/4/10

Running time: A bit disappointing??? Using a technique called memoization, we can have a polynomial time algorithm. 2019/4/10