Segment Trees. Longest Non Decreasing Subsequence (Revisited) 1252863697 Length of LNDS ending at i th Loc 123 3 444566 This runs in O(n 2 ). Can we do.

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

Trees Types and Operations
BY Lecturer: Aisha Dawood. Heapsort  O(n log n) worst case like merge sort.  Sorts in place like insertion sort.  Combines the best of both algorithms.
Analysis of Algorithms
Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
B+-Trees (PART 1) What is a B+ tree? Why B+ trees? Searching a B+ tree
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
Self-Balancing Search Trees Chapter 11. Chapter 11: Self-Balancing Search Trees2 Chapter Objectives To understand the impact that balance has on the performance.
Fall 2007CS 2251 Self-Balancing Search Trees Chapter 9.
Self-Balancing Search Trees Chapter 11. Chapter Objectives  To understand the impact that balance has on the performance of binary search trees  To.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Week 10: Heap and Priority queue. Any feature here?
CHAPTER 12 Trees. 2 Tree Definition A tree is a non-linear structure, consisting of nodes and links Links: The links are represented by ordered pairs.
Heapsort CIS 606 Spring Overview Heapsort – O(n lg n) worst case—like merge sort. – Sorts in place—like insertion sort. – Combines the best of both.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 17: Binary Search Trees; Heaps.
Binary Tree. Binary Trees – An Informal Definition A binary tree is a tree in which no node can have more than two children Each node has 0, 1, or 2 children.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Trees By Charl du Plessis. Contents Basic Terminology Basic Terminology Binary Search Trees Binary Search Trees Interval Trees Interval Trees Binary Indexed.
Data Structure & Algorithm II.  Delete-min  Building a heap in O(n) time  Heap Sort.
Data Structures Week 8 Further Data Structures The story so far  Saw some fundamental operations as well as advanced operations on arrays, stacks, and.
CSC 211 Data Structures Lecture 13
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
© University of Auckland Trees – (cont.) CS 220 Data Structures & Algorithms Dr. Ian Watson.
Binary Search Trees (BST)
Tree Data Structures. Heaps for searching Search in a heap? Search in a heap? Would have to look at root Would have to look at root If search item smaller.
1 Heaps A heap is a binary tree. A heap is best implemented in sequential representation (using an array). Two important uses of heaps are: –(i) efficient.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
1 Heap Sort. A Heap is a Binary Tree Height of tree = longest path from root to leaf =  (lgn) A heap is a binary tree satisfying the heap condition:
Mergeable Heaps David Kauchak cs302 Spring Admin Homework 7?
1Computer Sciences. 2 HEAP SORT TUTORIAL 4 Objective O(n lg n) worst case like merge sort. Sorts in place like insertion sort. A heap can be stored as.
Binary Tree.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Symbol tables.
1 Algorithms CSCI 235, Fall 2015 Lecture 13 Heap Sort.
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Data Structures: Disjoint Sets, Segment Trees, Fenwick Trees
Multiway Search Trees Data may not fit into main memory
Recursive Objects (Part 4)
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Tree (BST)
B+ Trees What are B+ Trees used for What is a B Tree What is a B+ Tree
Binary Search Tree Chapter 10.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Section 8.1 Trees.
ITEC 2620M Introduction to Data Structures
Tree data structure.
Binary Trees, Binary Search Trees
Binary Search Trees.
7/23/2009 Many thanks to David Sun for some of the included slides!
Data Structures: Segment Trees, Fenwick Trees
Priority Queue & Heap CSCI 3110 Nan Chen.
Tree data structure.
CS200: Algorithm Analysis
AVL Trees: AVL Trees: Balanced binary search tree
Search Sorted Array: Binary Search Linked List: Linear Search
"Teachers open the door, but you must enter by yourself. "
Chapter 12: Binary Search Trees
Binary Search Trees.
2-3-4 Trees Red-Black Trees
Design and Analysis of Algorithms
Binary Trees, Binary Search Trees
Topic 5: Heap data structure heap sort Priority queue
Algorithms CSCI 235, Spring 2019 Lecture 14 Heap Sort Read: Ch. 6
Binary Trees, Binary Search Trees
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

Segment Trees

Longest Non Decreasing Subsequence (Revisited) Length of LNDS ending at i th Loc This runs in O(n 2 ). Can we do better? Challenge: Given a value of a[i] we need to find the maximum length possible among all subsequences ending at any value less than equal to a[i] quickly. Possible with small change in algorithm and usage of STL, refer to the link We will look at another way of doing it in a restricted setting when all elements in the initial array are not really large say bounded by 1e6.

Find the Max value among the first i elements of an array Another Approach: Maintain another array, each index storing the maximum value up to that index. Cost of operations: Finding maximum O(1) Updating an element in initial array O(n) This works well if the number of query operations are large and very few updates If the number of query and updates are equal then it is as good/bad as a naïve way of doing it. Naïve Approach: Just scan till the ith index and output the maximum. Cost of operations: Finding maximum O(1) Updating an element in initial array O(n) Can we perform both the operations in O(log n) time once given the array

A Basic Segment Tree Leaf Nodes are the elements in the array. Each internal node represents some merging of the leaf nodes Fill each node with the maximum value in the left sub array.

Querying To find the maximum value stored up to the i th index do the following Start at the i th leaf with max set to the value in the leaf, keep traversing till the root. If you had reached the parent from the left child don’t take any action otherwise if the max value in the left sub-tree is greater than max update max Start at leaf Max: 9 Coming from left No Action Max: 9 Coming from Right Val < Max Max: 9 Coming from left No Action Max: 9 Start at leaf Max: 1 Coming from Right Val > Max Max: 3 Coming from Right Val > Max Max: 13 Coming from Right Val < Max Max: 13

Updating an element in an array and precomputing – Just the reverse of querying. For each operation we need to travel till the root of the tree. Height of tree is bounded by O(log n) hence each operation can be done in O(log n) If the query range is from i th index to the end of the array just do the reverse of what we had done What if the query range is any segment in between the array? Update the i th leaf keep with value ‘v’ traversing till the root. If you had reached the parent from the right child don’t take any action otherwise if the value in the node is less than ‘v’ update it with ‘v’

Segment Tree Leaf Nodes are the elements in the array. Each internal node stores maximum value among all its children. Start Here Recursively query on both sub-trees they return Max value in the range l to r in them Returns 9 Returns 16 Compare both and hence maximum value is 16 Out of range Return 0 Within range Return max (9) in this sub-tree Within range Return max (13) in this sub-tree Returns 16 Returns 0 Returns max(16,0)

Querying Query(root,l,r) How to represent the tree How to check if the range is between left and right I think it will be pretty tough to code this It is very simple to code a segment tree Demo query(node,l,r){ if range of node is within l and r return value in node else return max(query(left-child,l,r),query(right-child,l,r)) } range of node is the [left most leaf,right most leaf] in the sub-tree rooted at node.

Representation of the tree Number each node in the tree level by level Observe that for each node the left child is 2*i and right child is 2*i+1 For each node the parent is i/2 Just use an array to represent the tree, operate on indices to access parents and children For ease of access ignore the 0-indexed element in the array

Updates as well as queries on intervals (On Board) Read t=url&hl=en&ie=UTF8&prev=_t&rurl=translate.google. com&sl=auto&tl=en&twu=1&u= maxx.ru/algo/segment_tree&usg=ALkJrhgDDakTy9AeC VpP7dGwJoPxR--qSw before attempting any of the problems t=url&hl=en&ie=UTF8&prev=_t&rurl=translate.google. com&sl=auto&tl=en&twu=1&u= maxx.ru/algo/segment_tree&usg=ALkJrhgDDakTy9AeC VpP7dGwJoPxR--qSw Read about Binary indexed trees =tutorials&d2=binaryIndexedTrees =tutorials&d2=binaryIndexedTrees

Practice Problems – kodefest-solutions/89-problem-g kodefest-solutions/89-problem-g N/ N/