This document discusses data structures for priority queues and binomial heaps. It begins with an overview of priority queue structures like heaps and their common operations. It then discusses implementing a binary heap using an array, with operations like insert, delete, and change in O(log n) time. Binary heaps also enable heapsort in O(n log n) time. The document next covers binomial trees and binomial heaps, which support union in O(log n) time through merging trees of the same order. Overall, the document provides an in-depth overview of priority queue data structures and their applications.
The document discusses various data structures used to implement priority queues, including binary heaps and binomial heaps. It describes how each structure can be implemented using an array and the time complexities of common operations like insertion, deletion, finding the minimum element, etc. It also provides an example of how binary heaps can be used to implement Dijkstra's algorithm for finding the shortest paths from a single source vertex in a graph.
Here is a linear programming formulation of the election problem:
Let x = amount spent on urban ads
y = amount spent on suburban ads
z = amount spent on rural ads
Minimize: x + y + z
Subject to: x ≥ 100,000 (urban votes)
y ≥ 200,000 (suburban votes)
z ≥ 25,000 (rural votes)
x, y, z ≥ 0
The objective is to minimize total spending while satisfying the vote constraints.
The document discusses different types of heaps and heap algorithms. It describes binary min-heaps and max-heaps, including their properties and implementations using arrays. Basic heap operations like insert, delete, and build heap are explained along with their time complexities. Applications of heaps like priority queues and selection algorithms are covered. More advanced heap types like leftist heaps, skew heaps and binomial queues are also mentioned.
This document describes heap data structures and algorithms like heap sort. It defines a max heap and min heap. It explains the build heap, heapify, insertion and deletion algorithms. Build heap transforms an array into a max heap by applying heapify to each node from bottom to top. Heapify maintains the heap property when a node is added or removed. Heap sort works by building a max heap from the input array and then extracting elements from the root to sort the array in descending order.
The document discusses the heap sort algorithm which has two steps: 1) Build a max heap from the input data in O(n) time by transforming it into a complete binary tree that satisfies the heap property. 2) Perform n deleteMax operations to extract the maximum element from the heap and place it in the sorted output, doing this in O(log(n)) time per operation for an overall time of O(nlog(n)).
This document provides information about priority queues and binary heaps. It defines a binary heap as a nearly complete binary tree where the root node has the maximum/minimum value. It describes heap operations like insertion, deletion of max/min, and increasing/decreasing keys. The time complexity of these operations is O(log n). Heapsort, which uses a heap data structure, is also covered and has overall time complexity of O(n log n). Binary heaps are often used to implement priority queues and for algorithms like Dijkstra's and Prim's.
The document discusses different searching algorithms like linear search and binary search. It explains the time complexity of linear search is O(n) in the worst case while binary search has a time complexity of O(log n). The document also covers multidimensional arrays and matrix multiplication algorithms.
This document describes the heapsort algorithm. Heapsort works by first converting the input array into a max heap using the BuildHeap algorithm. BuildHeap converts subtrees of the array into heaps from bottom to top. Once the array is a max heap, the largest element is swapped with the last element and removed from the heap. This process is repeated to sort the entire array in O(n log n) time. The array can be viewed as a complete binary tree to help visualize heap operations like finding the children of a node.
The document discusses various data structures used to implement priority queues, including binary heaps and binomial heaps. It describes how each structure can be implemented using an array and the time complexities of common operations like insertion, deletion, finding the minimum element, etc. It also provides an example of how binary heaps can be used to implement Dijkstra's algorithm for finding the shortest paths from a single source vertex in a graph.
Here is a linear programming formulation of the election problem:
Let x = amount spent on urban ads
y = amount spent on suburban ads
z = amount spent on rural ads
Minimize: x + y + z
Subject to: x ≥ 100,000 (urban votes)
y ≥ 200,000 (suburban votes)
z ≥ 25,000 (rural votes)
x, y, z ≥ 0
The objective is to minimize total spending while satisfying the vote constraints.
The document discusses different types of heaps and heap algorithms. It describes binary min-heaps and max-heaps, including their properties and implementations using arrays. Basic heap operations like insert, delete, and build heap are explained along with their time complexities. Applications of heaps like priority queues and selection algorithms are covered. More advanced heap types like leftist heaps, skew heaps and binomial queues are also mentioned.
This document describes heap data structures and algorithms like heap sort. It defines a max heap and min heap. It explains the build heap, heapify, insertion and deletion algorithms. Build heap transforms an array into a max heap by applying heapify to each node from bottom to top. Heapify maintains the heap property when a node is added or removed. Heap sort works by building a max heap from the input array and then extracting elements from the root to sort the array in descending order.
The document discusses the heap sort algorithm which has two steps: 1) Build a max heap from the input data in O(n) time by transforming it into a complete binary tree that satisfies the heap property. 2) Perform n deleteMax operations to extract the maximum element from the heap and place it in the sorted output, doing this in O(log(n)) time per operation for an overall time of O(nlog(n)).
This document provides information about priority queues and binary heaps. It defines a binary heap as a nearly complete binary tree where the root node has the maximum/minimum value. It describes heap operations like insertion, deletion of max/min, and increasing/decreasing keys. The time complexity of these operations is O(log n). Heapsort, which uses a heap data structure, is also covered and has overall time complexity of O(n log n). Binary heaps are often used to implement priority queues and for algorithms like Dijkstra's and Prim's.
The document discusses different searching algorithms like linear search and binary search. It explains the time complexity of linear search is O(n) in the worst case while binary search has a time complexity of O(log n). The document also covers multidimensional arrays and matrix multiplication algorithms.
This document describes the heapsort algorithm. Heapsort works by first converting the input array into a max heap using the BuildHeap algorithm. BuildHeap converts subtrees of the array into heaps from bottom to top. Once the array is a max heap, the largest element is swapped with the last element and removed from the heap. This process is repeated to sort the entire array in O(n log n) time. The array can be viewed as a complete binary tree to help visualize heap operations like finding the children of a node.
The document discusses heap data structures and their use in priority queues and heapsort. It defines a heap as a complete binary tree stored in an array. Each node stores a value, with the heap property being that a node's value is greater than or equal to its children's values (for a max heap). Algorithms like Max-Heapify, Build-Max-Heap, Heap-Extract-Max, and Heap-Increase-Key are presented to maintain the heap property during operations. Priority queues use heaps to efficiently retrieve the maximum element, while heapsort sorts an array by building a max heap and repeatedly extracting elements.
The document discusses heap data structures and algorithms. A heap is a binary tree that satisfies the heap property of a parent being greater than or equal to its children. Common operations on heaps like building
The document discusses priority queues and binary heaps. It explains that priority queues store tasks based on priority level and ensure the highest priority task is at the head of the queue. Binary heaps are the underlying data structure used to implement priority queues. The key operations on a binary heap are insert and deleteMin. Insert involves adding an element and percolating it up the heap, while deleteMin removes the minimum element and percolates the replacement down. Both operations have O(log n) time complexity. The document provides examples and pseudocode for building a heap from a list of elements in O(n) time using a buildHeap method.
The heap data structure is a nearly complete binary tree implemented as an array. There are two types of heaps: max-heaps and min-heaps. The MAX-HEAPIFY algorithm maintains the heap property by allowing a value to "float down" the tree. BUILD-MAX-HEAP builds a max-heap by calling MAX-HEAPIFY on each node, and HEAPSORT sorts an array using the heap structure.
Below are the lists of available Webi 3.1 report functions and will describe each & every functions along with example. Webi 4.0 reports are also having some functions with some additional futures.
The document discusses heap sort, which is a sorting algorithm that uses a heap data structure. It works in two phases: first, it transforms the input array into a max heap using the insert heap procedure; second, it repeatedly extracts the maximum element from the heap and places it at the end of the sorted array, reheapifying the remaining elements. The key steps are building the heap, processing the heap by removing the root element and allowing the heap to reorder, and doing this repeatedly until the array is fully sorted.
Heap sort uses a heap data structure that maintains the max-heap or min-heap property. It involves two main steps: 1) building the heap from the input array using the BUILD-MAX-HEAP procedure in O(n) time, and 2) repeatedly extracting the maximum/minimum element from the heap and inserting it into the sorted portion using the DELHEAP procedure, running in O(n log n) time overall. The key operation is MAX-HEAPIFY, which maintains the max-heap property in O(log n) time during heap operations like insertion and deletion.
This document discusses heaps and priority queues. It defines a min-heap as a complete binary tree where the root value is the smallest and its subtrees are also heaps. This property allows a min-heap to efficiently implement a priority queue, where the smallest element can always be found at the root. Elements can be inserted in O(log n) time by adding to the end and percolating up, and removed in O(log n) time by removing the root and percolating down the replacement. This makes heap-based priority queues more efficient than other implementations like sorted lists. Heapsort is also discussed, which sorts in O(n log n) time by building a heap and repeatedly removing elements
The document describes a new method called SITAd for scalable similarity search of molecular descriptors in large databases. SITAd uses two techniques: 1) database partitioning to limit the search space, and 2) converting similarity search to inner product search. It builds a wavelet tree to efficiently solve the inner product search problem. Experiments on a database of 42 million compounds showed that SITAd was over 100 times faster than alternative inverted index methods while using less memory.
DSA (Data Structure and Algorithm) QuestionsRESHAN FARAZ
The document contains questions related to data structures and algorithms. Some questions ask to perform operations on data structures like stacks, queues, trees, graphs and hash tables. Other questions involve algorithms like topological sorting, shortest path algorithms, minimum spanning tree algorithms, and binary search tree operations. The document tests understanding of concepts taught in an algorithms course through examples and problem solving questions.
The document discusses heaps and heapsort. It defines max heaps and min heaps as complete binary trees where each node's key is greater than or less than its children's keys. It describes operations on heaps like insertion, deletion of the max/min element, and creation of an empty heap. Algorithms for insertion and deletion into max heaps are provided. Heapsort is described as building a max heap of the input array and then repeatedly extracting the max element to sort the array.
This document describes how to insert nodes into a balanced binary tree. It defines a balanced tree as one where the number of inner nodes in the left and right subtrees differ by at most 1. The algorithm uses an array to track the path during insertion and adjusts balance indicators up the tree if needed to maintain balance after insertion. It handles two cases for rebalancing - where the grandchild is balanced or imbalanced.
This document discusses algorithms complexity and data structures efficiency. It covers topics like time and memory complexity, asymptotic notation, fundamental data structures like arrays, lists, trees and hash tables, and choosing proper data structures. Computational complexity is important for algorithm design and efficient programming. The document provides examples of analyzing complexity for different algorithms.
The document discusses hashing and its components. [1] It describes how hashing works by mapping keys to locations in a hash table using a hash function. [2] Common collision resolution techniques are chaining, linear probing, quadratic probing, and double hashing. [3] Hashing provides fast average-case performance of O(1) by storing data in blocks based on the hash value and load factor.
Heapsort is a sorting algorithm that uses a heap data structure. It first transforms the input array into a max-heap, where the largest element is at the root, in O(n) time. It then repeatedly swaps the root element with the last element, reducing the size of the heap by 1, and sifts the new root element down to maintain the heap property. This process takes O(n log n) time overall, making heapsort an efficient sorting algorithm with O(n log n) time complexity in all cases.
Christoph Koch is a professor of Computer Science at EPFL, specializing in data management. Until 2010, he was an Associate Professor in the Department of Computer Science at Cornell University. Previously to this, from 2005 to 2007, he was an Associate Professor of Computer Science at Saarland University. Earlier, he obtained his PhD in Artificial Intelligence from TU Vienna and CERN (2001), was a postdoctoral researcher at TU Vienna and the University of Edinburgh (2001-2003), and an assistant professor at TU Vienna (2003-2005). He has won Best Paper Awards at PODS 2002, ICALP 2005, and SIGMOD 2011, an Outrageous Ideas and Vision Paper Award at CIDR 2013, a Google Research Award (in 2009), and an ERC Grant (in 2011). He is a PI of the FET Flagship Human Brain Project and of NCCR MARVEL, a new Swiss national research center for materials research. He (co-)chaired the program committees of DBPL 2005, WebDB 2008, ICDE 2011, VLDB 2013, and was PC vice-chair of ICDE 2008 and ICDE 2009. He has served on the editorial board of ACM Transactions on Internet Technology and as Editor-in-Chief of PVLDB.
This document outlines a talk on using category theory concepts in functional programming. It begins by introducing the definition of a category from category theory and using Scala examples to demonstrate how types and functions in Scala satisfy this definition. It then defines functors, natural transformations, and monads from category theory and provides examples showing how options, lists, and functions in Scala form a monad. It proves that the category theory definition of a monad is equivalent to the definition used in functional programming. The document suggests category theory concepts help formalize design patterns and make code more refactorable.
This document discusses various data structures used for priority queues, including binary heaps and binomial heaps. It provides details on implementing priority queue operations like insert, delete, and change key on these structures. Key points covered include:
- Binary heaps allow efficient priority queue operations like insert, delete, and change key in O(log n) time and are commonly used in programming competitions.
- Binomial heaps also support priority queue operations in O(log n) time but have more complex union operations that take O(log n) time.
- Dijkstra's algorithm can be implemented using a binary heap to efficiently find the shortest path between nodes.
Binomial heaps are a data structure that combines multiple binomial trees. Each binomial tree obeys the min-heap property and there is at most one tree per degree. Common operations on binomial heaps include inserting and deleting nodes, finding the minimum, and merging heaps. These operations make use of properties like each tree's degree and structure to perform the operation in O(log n) time.
The document discusses heap data structures and their use in priority queues and heapsort. It defines a heap as a complete binary tree stored in an array. Each node stores a value, with the heap property being that a node's value is greater than or equal to its children's values (for a max heap). Algorithms like Max-Heapify, Build-Max-Heap, Heap-Extract-Max, and Heap-Increase-Key are presented to maintain the heap property during operations. Priority queues use heaps to efficiently retrieve the maximum element, while heapsort sorts an array by building a max heap and repeatedly extracting elements.
The document discusses heap data structures and algorithms. A heap is a binary tree that satisfies the heap property of a parent being greater than or equal to its children. Common operations on heaps like building
The document discusses priority queues and binary heaps. It explains that priority queues store tasks based on priority level and ensure the highest priority task is at the head of the queue. Binary heaps are the underlying data structure used to implement priority queues. The key operations on a binary heap are insert and deleteMin. Insert involves adding an element and percolating it up the heap, while deleteMin removes the minimum element and percolates the replacement down. Both operations have O(log n) time complexity. The document provides examples and pseudocode for building a heap from a list of elements in O(n) time using a buildHeap method.
The heap data structure is a nearly complete binary tree implemented as an array. There are two types of heaps: max-heaps and min-heaps. The MAX-HEAPIFY algorithm maintains the heap property by allowing a value to "float down" the tree. BUILD-MAX-HEAP builds a max-heap by calling MAX-HEAPIFY on each node, and HEAPSORT sorts an array using the heap structure.
Below are the lists of available Webi 3.1 report functions and will describe each & every functions along with example. Webi 4.0 reports are also having some functions with some additional futures.
The document discusses heap sort, which is a sorting algorithm that uses a heap data structure. It works in two phases: first, it transforms the input array into a max heap using the insert heap procedure; second, it repeatedly extracts the maximum element from the heap and places it at the end of the sorted array, reheapifying the remaining elements. The key steps are building the heap, processing the heap by removing the root element and allowing the heap to reorder, and doing this repeatedly until the array is fully sorted.
Heap sort uses a heap data structure that maintains the max-heap or min-heap property. It involves two main steps: 1) building the heap from the input array using the BUILD-MAX-HEAP procedure in O(n) time, and 2) repeatedly extracting the maximum/minimum element from the heap and inserting it into the sorted portion using the DELHEAP procedure, running in O(n log n) time overall. The key operation is MAX-HEAPIFY, which maintains the max-heap property in O(log n) time during heap operations like insertion and deletion.
This document discusses heaps and priority queues. It defines a min-heap as a complete binary tree where the root value is the smallest and its subtrees are also heaps. This property allows a min-heap to efficiently implement a priority queue, where the smallest element can always be found at the root. Elements can be inserted in O(log n) time by adding to the end and percolating up, and removed in O(log n) time by removing the root and percolating down the replacement. This makes heap-based priority queues more efficient than other implementations like sorted lists. Heapsort is also discussed, which sorts in O(n log n) time by building a heap and repeatedly removing elements
The document describes a new method called SITAd for scalable similarity search of molecular descriptors in large databases. SITAd uses two techniques: 1) database partitioning to limit the search space, and 2) converting similarity search to inner product search. It builds a wavelet tree to efficiently solve the inner product search problem. Experiments on a database of 42 million compounds showed that SITAd was over 100 times faster than alternative inverted index methods while using less memory.
DSA (Data Structure and Algorithm) QuestionsRESHAN FARAZ
The document contains questions related to data structures and algorithms. Some questions ask to perform operations on data structures like stacks, queues, trees, graphs and hash tables. Other questions involve algorithms like topological sorting, shortest path algorithms, minimum spanning tree algorithms, and binary search tree operations. The document tests understanding of concepts taught in an algorithms course through examples and problem solving questions.
The document discusses heaps and heapsort. It defines max heaps and min heaps as complete binary trees where each node's key is greater than or less than its children's keys. It describes operations on heaps like insertion, deletion of the max/min element, and creation of an empty heap. Algorithms for insertion and deletion into max heaps are provided. Heapsort is described as building a max heap of the input array and then repeatedly extracting the max element to sort the array.
This document describes how to insert nodes into a balanced binary tree. It defines a balanced tree as one where the number of inner nodes in the left and right subtrees differ by at most 1. The algorithm uses an array to track the path during insertion and adjusts balance indicators up the tree if needed to maintain balance after insertion. It handles two cases for rebalancing - where the grandchild is balanced or imbalanced.
This document discusses algorithms complexity and data structures efficiency. It covers topics like time and memory complexity, asymptotic notation, fundamental data structures like arrays, lists, trees and hash tables, and choosing proper data structures. Computational complexity is important for algorithm design and efficient programming. The document provides examples of analyzing complexity for different algorithms.
The document discusses hashing and its components. [1] It describes how hashing works by mapping keys to locations in a hash table using a hash function. [2] Common collision resolution techniques are chaining, linear probing, quadratic probing, and double hashing. [3] Hashing provides fast average-case performance of O(1) by storing data in blocks based on the hash value and load factor.
Heapsort is a sorting algorithm that uses a heap data structure. It first transforms the input array into a max-heap, where the largest element is at the root, in O(n) time. It then repeatedly swaps the root element with the last element, reducing the size of the heap by 1, and sifts the new root element down to maintain the heap property. This process takes O(n log n) time overall, making heapsort an efficient sorting algorithm with O(n log n) time complexity in all cases.
Christoph Koch is a professor of Computer Science at EPFL, specializing in data management. Until 2010, he was an Associate Professor in the Department of Computer Science at Cornell University. Previously to this, from 2005 to 2007, he was an Associate Professor of Computer Science at Saarland University. Earlier, he obtained his PhD in Artificial Intelligence from TU Vienna and CERN (2001), was a postdoctoral researcher at TU Vienna and the University of Edinburgh (2001-2003), and an assistant professor at TU Vienna (2003-2005). He has won Best Paper Awards at PODS 2002, ICALP 2005, and SIGMOD 2011, an Outrageous Ideas and Vision Paper Award at CIDR 2013, a Google Research Award (in 2009), and an ERC Grant (in 2011). He is a PI of the FET Flagship Human Brain Project and of NCCR MARVEL, a new Swiss national research center for materials research. He (co-)chaired the program committees of DBPL 2005, WebDB 2008, ICDE 2011, VLDB 2013, and was PC vice-chair of ICDE 2008 and ICDE 2009. He has served on the editorial board of ACM Transactions on Internet Technology and as Editor-in-Chief of PVLDB.
This document outlines a talk on using category theory concepts in functional programming. It begins by introducing the definition of a category from category theory and using Scala examples to demonstrate how types and functions in Scala satisfy this definition. It then defines functors, natural transformations, and monads from category theory and provides examples showing how options, lists, and functions in Scala form a monad. It proves that the category theory definition of a monad is equivalent to the definition used in functional programming. The document suggests category theory concepts help formalize design patterns and make code more refactorable.
This document discusses various data structures used for priority queues, including binary heaps and binomial heaps. It provides details on implementing priority queue operations like insert, delete, and change key on these structures. Key points covered include:
- Binary heaps allow efficient priority queue operations like insert, delete, and change key in O(log n) time and are commonly used in programming competitions.
- Binomial heaps also support priority queue operations in O(log n) time but have more complex union operations that take O(log n) time.
- Dijkstra's algorithm can be implemented using a binary heap to efficiently find the shortest path between nodes.
Binomial heaps are a data structure that combines multiple binomial trees. Each binomial tree obeys the min-heap property and there is at most one tree per degree. Common operations on binomial heaps include inserting and deleting nodes, finding the minimum, and merging heaps. These operations make use of properties like each tree's degree and structure to perform the operation in O(log n) time.
The document discusses binary heaps and binomial heaps, which are specialized tree data structures used to implement priority queues. A binary heap is a complete binary tree that satisfies the heap property, where a parent node's key is greater than or equal to its children's keys. Operations on a binary heap like insertion and deletion take O(log n) time. A binomial heap is a collection of binomial trees, where each tree satisfies the heap property. Common operations on binomial heaps like finding the minimum, uniting two heaps, and deleting elements take O(log n) time.
Binomial heap (a concept of Data Structure)Saliha Bilal
A binomial heap is a data structure that implements a min heap using a collection of binomial trees. Each binomial tree has a specific shape and properties. A binomial heap supports operations like make-heap, find-minimum, extract-minimum, insert, delete, and decrease-key in O(log n) time by using a union operation to efficiently merge two binomial heaps. The union operation works by processing the binomial trees level-by-level from smallest to largest order. Binomial heaps are useful for applications that require fast priority queue operations and efficient merging of multiple heaps.
This document provides an overview of several common data structures: stacks, queues, heaps, priority queues, union-find structures, binary search trees, Fenwick trees, and lowest common ancestor trees. For each data structure, it describes their key properties and operations and provides examples of how to implement them to achieve efficient runtimes, often in O(log n) time. It also notes the related standard library implementations available in C++ and Java.
The document discusses binary search trees and their properties. It explains that a binary search tree is a binary tree where every node's left subtree contains values less than the node's value and the right subtree contains greater values. Operations like search, insert, delete can be done in O(h) time where h is the height of the tree. The height is O(log n) for balanced trees but can be O(n) for unbalanced trees. The document also provides examples of using a binary search tree to sort a set of numbers in O(n log n) time by building the BST and doing an inorder traversal.
I am Anne L. I am an Algorithms Design Homework Expert at programminghomeworkhelp.com. I hold a Ph.D. in Programming, Auburn University, USA. I have been helping students with their homework for the past 8 years. I solve homework related to Algorithms Design.
Visit programminghomeworkhelp.com or email support@programminghomeworkhelp.com. You can also call on +1 678 648 4277 for any assistance with the Algorithm Design Homework.
CS-102 BT_24_3_14 Binary Tree Lectures.pdfssuser034ce1
The document discusses operations on heaps and leftist trees. Key points include:
- Heaps can be used to implement priority queues, with operations like MAX-HEAPIFY, BUILD-MAX-HEAP, and HEAPSORT taking O(log n) time on average.
- Leftist trees are a type of self-balancing binary search tree that supports priority queue operations like insertion and deletion in O(log n) time.
- Leftist trees have properties like shortest root-to-leaf paths being O(log n) that allow them to efficiently support priority queue operations through melding of subtrees.
The document discusses red-black trees, which are binary search trees augmented with node colors to guarantee a height of O(log n). It describes the properties that red-black trees must satisfy, including that every node is red or black, leaves are black, and if a node is red its children are black. It then proves that these properties ensure the height is O(log n) by showing a subtree has at least 2^bh - 1 nodes, where bh is the black-height. Finally, it notes that common operations like search, insert and delete run in O(log n) time on red-black trees.
The document provides information about algorithms exam help from programmingexamhelp.com. It includes their contact information and discusses asymptotic analysis of functions and implementing operations on a doubly linked list in constant time. Specifically, it provides sample solutions for ordering functions by asymptotic growth, implementing reverse, move and read operations on a sequence data structure, maintaining a binder of pages with bookmarks, and describing insert and delete operations on a doubly linked list.
The document discusses heapsort, an efficient sorting algorithm that uses a heap data structure. Heapsort first builds a max heap from the input array in O(n) time using a heapify procedure. It then extracts elements from the heap into the sorted output array, maintaining the heap property, resulting in an overall time complexity of O(n log n).
Binomial queues allow merging of heaps in O(log N) time rather than the usual O(N) time for binary heaps. They use a forest of binomial trees where each tree is used 0 or 1 times. To merge two binomial queues, the corresponding binomial trees are combined level by level. Insertion takes O(log N) time by merging the new node as its own queue. Deletion of the minimum takes O(log N) time by removing the smallest tree and merging its children.
1. The document discusses algorithms for handling collisions in hashing, including open hashing which uses separate chaining with linked lists, and closed hashing which resolves collisions through techniques like linear probing.
2. It also discusses binary search trees and their operations, as well as balanced search trees like AVL and red-black trees which have logarithmic time performance.
3. Finally, it covers multiway search trees including 2-3 trees and how they allow for more than one key per node, providing improved time performance over standard binary search trees.
The document discusses algorithms for heap data structures and their applications. It begins with an introduction to heaps and their representation as complete binary trees or arrays. It then covers the heap operations of MaxHeapify, building a max heap, and heapsort. Heapsort runs in O(n log n) time by using a max heap to iteratively find and remove the maximum element. The document concludes by discussing how heaps can be used to implement priority queues, with common operations like insert, extract maximum, and increase key running in O(log n) time.
Hashing is a common technique for implementing dictionaries that provides constant-time operations by mapping keys to table positions using a hash function, though collisions require resolution strategies like separate chaining or open addressing. Popular hash functions include division and cyclic shift hashing to better distribute keys across buckets. Both open hashing using linked lists and closed hashing using linear probing can provide average constant-time performance for dictionary operations depending on load factor.
Under most circumstances you would use a “normal” binary heap
Except some algorithms that may use heaps might require a “Union” operation
How would you implement “Union” to merge two binary heaps?
The document discusses binary search trees (BSTs) and their use as a data structure for dynamic sets. It covers BST properties, operations like search, insert, delete and their running times. It also discusses using BSTs to sort an array in O(n lg n) time by inserting elements, similar to quicksort. Maintaining a height of O(lg n) is important for efficient operations.
This document describes the heap sort algorithm. It begins by explaining heaps and their properties like the heap property. It then describes key heap operations like Max-Heapify, Build-Max-Heap, finding the maximum element, extract max, increase key, and insert. These operations allow heaps to function as priority queues. Heap sort works by building a max heap from the input array using Build-Max-Heap, then repeatedly extracting the maximum element and putting it in its sorted position. This runs in O(n log n) time, combining advantages of insertion and merge sort by sorting in-place like insertion sort but with a faster running time of merge sort.
The document discusses algorithms including heapsort and quicksort. It provides explanations of heap data structures and how they can be used to implement priority queues. It then describes how priority queues can be used to simulate event-driven systems like collisions in a combat billiards simulation. Quicksort is also summarized, including the key partition step that divides the array into two subarrays.
This document discusses binary search trees (BSTs) and their use for dynamic sets and sorting. It covers BST operations like search, insert, find minimum/maximum, and delete. It explains that BST sorting runs in O(n log n) time like quicksort. Maintaining a height of O(log n) can lead to efficient implementations of priority queues and other dynamic set applications using BSTs.
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...Zilliz
Join us to introduce Milvus Lite, a vector database that can run on notebooks and laptops, share the same API with Milvus, and integrate with every popular GenAI framework. This webinar is perfect for developers seeking easy-to-use, well-integrated vector databases for their GenAI apps.
Building RAG with self-deployed Milvus vector database and Snowpark Container...Zilliz
This talk will give hands-on advice on building RAG applications with an open-source Milvus database deployed as a docker container. We will also introduce the integration of Milvus with Snowpark Container Services.
UiPath Test Automation using UiPath Test Suite series, part 6DianaGray10
Welcome to UiPath Test Automation using UiPath Test Suite series part 6. In this session, we will cover Test Automation with generative AI and Open AI.
UiPath Test Automation with generative AI and Open AI webinar offers an in-depth exploration of leveraging cutting-edge technologies for test automation within the UiPath platform. Attendees will delve into the integration of generative AI, a test automation solution, with Open AI advanced natural language processing capabilities.
Throughout the session, participants will discover how this synergy empowers testers to automate repetitive tasks, enhance testing accuracy, and expedite the software testing life cycle. Topics covered include the seamless integration process, practical use cases, and the benefits of harnessing AI-driven automation for UiPath testing initiatives. By attending this webinar, testers, and automation professionals can gain valuable insights into harnessing the power of AI to optimize their test automation workflows within the UiPath ecosystem, ultimately driving efficiency and quality in software development processes.
What will you get from this session?
1. Insights into integrating generative AI.
2. Understanding how this integration enhances test automation within the UiPath platform
3. Practical demonstrations
4. Exploration of real-world use cases illustrating the benefits of AI-driven test automation for UiPath
Topics covered:
What is generative AI
Test Automation with generative AI and Open AI.
UiPath integration with generative AI
Speaker:
Deepak Rai, Automation Practice Lead, Boundaryless Group and UiPath MVP
Threats to mobile devices are more prevalent and increasing in scope and complexity. Users of mobile devices desire to take full advantage of the features
available on those devices, but many of the features provide convenience and capability but sacrifice security. This best practices guide outlines steps the users can take to better protect personal devices and information.
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfMalak Abu Hammad
Discover how MongoDB Atlas and vector search technology can revolutionize your application's search capabilities. This comprehensive presentation covers:
* What is Vector Search?
* Importance and benefits of vector search
* Practical use cases across various industries
* Step-by-step implementation guide
* Live demos with code snippets
* Enhancing LLM capabilities with vector search
* Best practices and optimization strategies
Perfect for developers, AI enthusiasts, and tech leaders. Learn how to leverage MongoDB Atlas to deliver highly relevant, context-aware search results, transforming your data retrieval process. Stay ahead in tech innovation and maximize the potential of your applications.
#MongoDB #VectorSearch #AI #SemanticSearch #TechInnovation #DataScience #LLM #MachineLearning #SearchTechnology
Maruthi Prithivirajan, Head of ASEAN & IN Solution Architecture, Neo4j
Get an inside look at the latest Neo4j innovations that enable relationship-driven intelligence at scale. Learn more about the newest cloud integrations and product enhancements that make Neo4j an essential choice for developers building apps with interconnected data and generative AI.
UiPath Test Automation using UiPath Test Suite series, part 5DianaGray10
Welcome to UiPath Test Automation using UiPath Test Suite series part 5. In this session, we will cover CI/CD with devops.
Topics covered:
CI/CD with in UiPath
End-to-end overview of CI/CD pipeline with Azure devops
Speaker:
Lyndsey Byblow, Test Suite Sales Engineer @ UiPath, Inc.
Pushing the limits of ePRTC: 100ns holdover for 100 daysAdtran
At WSTS 2024, Alon Stern explored the topic of parametric holdover and explained how recent research findings can be implemented in real-world PNT networks to achieve 100 nanoseconds of accuracy for up to 100 days.
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AIVladimir Iglovikov, Ph.D.
Presented by Vladimir Iglovikov:
- https://www.linkedin.com/in/iglovikov/
- https://x.com/viglovikov
- https://www.instagram.com/ternaus/
This presentation delves into the journey of Albumentations.ai, a highly successful open-source library for data augmentation.
Created out of a necessity for superior performance in Kaggle competitions, Albumentations has grown to become a widely used tool among data scientists and machine learning practitioners.
This case study covers various aspects, including:
People: The contributors and community that have supported Albumentations.
Metrics: The success indicators such as downloads, daily active users, GitHub stars, and financial contributions.
Challenges: The hurdles in monetizing open-source projects and measuring user engagement.
Development Practices: Best practices for creating, maintaining, and scaling open-source libraries, including code hygiene, CI/CD, and fast iteration.
Community Building: Strategies for making adoption easy, iterating quickly, and fostering a vibrant, engaged community.
Marketing: Both online and offline marketing tactics, focusing on real, impactful interactions and collaborations.
Mental Health: Maintaining balance and not feeling pressured by user demands.
Key insights include the importance of automation, making the adoption process seamless, and leveraging offline interactions for marketing. The presentation also emphasizes the need for continuous small improvements and building a friendly, inclusive community that contributes to the project's growth.
Vladimir Iglovikov brings his extensive experience as a Kaggle Grandmaster, ex-Staff ML Engineer at Lyft, sharing valuable lessons and practical advice for anyone looking to enhance the adoption of their open-source projects.
Explore more about Albumentations and join the community at:
GitHub: https://github.com/albumentations-team/albumentations
Website: https://albumentations.ai/
LinkedIn: https://www.linkedin.com/company/100504475
Twitter: https://x.com/albumentations
“An Outlook of the Ongoing and Future Relationship between Blockchain Technologies and Process-aware Information Systems.” Invited talk at the joint workshop on Blockchain for Information Systems (BC4IS) and Blockchain for Trusted Data Sharing (B4TDS), co-located with with the 36th International Conference on Advanced Information Systems Engineering (CAiSE), 3 June 2024, Limassol, Cyprus.
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...SOFTTECHHUB
The choice of an operating system plays a pivotal role in shaping our computing experience. For decades, Microsoft's Windows has dominated the market, offering a familiar and widely adopted platform for personal and professional use. However, as technological advancements continue to push the boundaries of innovation, alternative operating systems have emerged, challenging the status quo and offering users a fresh perspective on computing.
One such alternative that has garnered significant attention and acclaim is Nitrux Linux 3.5.0, a sleek, powerful, and user-friendly Linux distribution that promises to redefine the way we interact with our devices. With its focus on performance, security, and customization, Nitrux Linux presents a compelling case for those seeking to break free from the constraints of proprietary software and embrace the freedom and flexibility of open-source computing.
Securing your Kubernetes cluster_ a step-by-step guide to success !KatiaHIMEUR1
Today, after several years of existence, an extremely active community and an ultra-dynamic ecosystem, Kubernetes has established itself as the de facto standard in container orchestration. Thanks to a wide range of managed services, it has never been so easy to set up a ready-to-use Kubernetes cluster.
However, this ease of use means that the subject of security in Kubernetes is often left for later, or even neglected. This exposes companies to significant risks.
In this talk, I'll show you step-by-step how to secure your Kubernetes cluster for greater peace of mind and reliability.
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...James Anderson
Effective Application Security in Software Delivery lifecycle using Deployment Firewall and DBOM
The modern software delivery process (or the CI/CD process) includes many tools, distributed teams, open-source code, and cloud platforms. Constant focus on speed to release software to market, along with the traditional slow and manual security checks has caused gaps in continuous security as an important piece in the software supply chain. Today organizations feel more susceptible to external and internal cyber threats due to the vast attack surface in their applications supply chain and the lack of end-to-end governance and risk management.
The software team must secure its software delivery process to avoid vulnerability and security breaches. This needs to be achieved with existing tool chains and without extensive rework of the delivery processes. This talk will present strategies and techniques for providing visibility into the true risk of the existing vulnerabilities, preventing the introduction of security issues in the software, resolving vulnerabilities in production environments quickly, and capturing the deployment bill of materials (DBOM).
Speakers:
Bob Boule
Robert Boule is a technology enthusiast with PASSION for technology and making things work along with a knack for helping others understand how things work. He comes with around 20 years of solution engineering experience in application security, software continuous delivery, and SaaS platforms. He is known for his dynamic presentations in CI/CD and application security integrated in software delivery lifecycle.
Gopinath Rebala
Gopinath Rebala is the CTO of OpsMx, where he has overall responsibility for the machine learning and data processing architectures for Secure Software Delivery. Gopi also has a strong connection with our customers, leading design and architecture for strategic implementations. Gopi is a frequent speaker and well-known leader in continuous delivery and integrating security into software delivery.
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Albert Hoitingh
In this session I delve into the encryption technology used in Microsoft 365 and Microsoft Purview. Including the concepts of Customer Key and Double Key Encryption.
How to Get CNIC Information System with Paksim Ga.pptxdanishmna97
Pakdata Cf is a groundbreaking system designed to streamline and facilitate access to CNIC information. This innovative platform leverages advanced technology to provide users with efficient and secure access to their CNIC details.
Communications Mining Series - Zero to Hero - Session 1DianaGray10
This session provides introduction to UiPath Communication Mining, importance and platform overview. You will acquire a good understand of the phases in Communication Mining as we go over the platform with you. Topics covered:
• Communication Mining Overview
• Why is it important?
• How can it help today’s business and the benefits
• Phases in Communication Mining
• Demo on Platform overview
• Q/A
Unlocking Productivity: Leveraging the Potential of Copilot in Microsoft 365, a presentation by Christoforos Vlachos, Senior Solutions Manager – Modern Workplace, Uni Systems
3. Priority Queue (PQ)
Structures
Stores elements in a list by comparing a
key field
Often has other satellite data
For example, when sorting pixels by their R
value, we consider the R as the key field
and GB as satellite data
Priority queues allow us to sort
elements by their key field.
Christopher Moh 2005
4. Common PQ operations
Create()
Find_Min()
Insert element x (with predefined key field)
Delete(x)
Returns the smallest element (by key field)
Insert(x)
Creates an empty priority queue
Delete position x from the queue
Change(x, k)
Change key field of position x to k
Christopher Moh 2005
5. Optional PQ operations
Union (a,b)
Combines two PQs a and b
Search (k)
Returns the position of the element in the
heap with key value k
Christopher Moh 2005
6. Considerations when
implementing a PQ in competition
How complicated is it?
How fast does it need to be?
Is the code likely to be buggy?
Does a constant factor also come into the
equation?
Do I need to store extra data to do a Search?
During the course of this presentation, we shall
assume that there exists existing extra data which
allows us to do a search in O(1) time. The
handling of this data structure will be assumed and
not covered.
Christopher Moh 2005
7. Linear Array
Unsorted Array
Create, Insert, Change in O(1) time
Find_min, Delete in O(n) time
Sorted Array
Create, Find_min in O(1) time
Insert, Delete, Change in O(n + log n) =
O(n) time
Christopher Moh 2005
8. Binary Heaps
Will be the most common structure that will
be implemented in competition setting
Efficient for most applications
Easy to implement
A heap is a structure where the value of a
node is less than the value of all of its
children
A binary heap is a heap where the maximum
number of children for each node is 2.
Christopher Moh 2005
9. Array implementation
Consider a heap of size nheap in an array
BHeap[1..nheap] (Define BHeap[nheap+1 ..
(nheap*2)+1] to be INFINITY for practical reasons)
The children of BHeap[x] are BHeap[x*2] and BHeap[x*2+1]
The parent of BHeap[x] are BHeap[x/2]
This allows a near uniform Binary Heap where we can
ensure that the number of levels in this heap is O(log n)
Some properties wrt Key values: BHeap[x] >= BHeap[x/2],
BHeap[x] <= BHeap[x*2], BHeap[x] <= BHeap[x*2+1],
BHeap[x*2] ?? BHeap[x*2+1]
Christopher Moh 2005
10. PQ Operations on a BHeap
We define BTree(x) to be the Binary Tree rooted at
BHeap[x]
We define Heapify(x) to be an operation that does the
following:
Assume: BTree(x*2) and BTree(x*2+1) are binary heaps but
BTree(x) is not necessarily a binary heap
Produce: BTree(x) binary heap
Details of Heapify in later slides – but for now, we assume
Heapify is O(log n)
For the rest of the presentation, we assume the
variable n refers to nheap
Christopher Moh 2005
11. Operations on a BHeap
Create is trivial – O(1) time
Find_min:
1.
Return BHeap[1]
O(1) time
Insert (element with key value x)
1.
2.
3.
4.
nheap++
BHeap[nheap] = x
T = nheap
While (T != 1 && Bheap[T] < BHeap[T/2])
1.
2.
Swap (Bheap[T], BHeap[T/2]
T=T/2
O(log n) time as the number of levels is O(log n)
Christopher Moh 2005
12. Operations on a BHeap
ChangeDown (position x, new key value k)
1.
2.
3.
Assume: k < existing BHeap[x]
BHeap[x] = k
T=x
While (T != 1 && BHeap[T] < BHeap[T/2])
1.
2.
Swap (BHeap[T], BHeap[T/2])
T = T/2
Complexity: O(log n)
This procedure is known as “bubbling up” the
heap
Christopher Moh 2005
13. Operations on a BHeap
ChangeUp (position x, new key value
k)
1.
2.
Assume: k > existing BHeap[x]
BHeap[x] = k
Heapify(x)
O(log n) as complexity of Heapify is O(log
n)
Christopher Moh 2005
14. Operations on a BHeap
Delete (position x on the heap)
1.
2.
3.
4.
5.
BHeap[x] = BHeap[nheap]
nheap—
Heapify(x)
T=x
While (T != 1 && BHeap[T] < BHeap[T/2])
1.
2.
Swap (BHeap[T], BHeap[T/2])
T=T/2
Complexity is O(log n)
Why must I do both Heapify and “bubble up”?
Christopher Moh 2005
15. Operations on a BHeap
Heapify (position x on the heap)
1.
2.
3.
4.
5.
T = min(BHeap[x], BHeap[x*2], BHeap[x*2+1])
If (T == BHeap[x]) return;
K = position where BHeap[K] = T
Swap(BHeap[x], BHeap[K])
Heapify(K)
O(log n) as the maximum number of levels in the
heap is O(log n) and Heapify only goes through
each level at most once
Christopher Moh 2005
16. BHeap Operations: Summary
Create, Find_min in O(1) time
Change (includes both ChangeUp and
ChangeDown), Insert, and Delete are
O(log n) time
Union operations are how long?
Insertion: O(n log n) union
Heapify: O(n) union
Christopher Moh 2005
17. Corollary: Heapsort
We can convert an unsorted array to a heap
using Heapify (why does this work?):
1.
For (i = n/2; i >= 1; i--)
1.
We can then return a sorted list (list initially
empty):
1.
For (i = 1; i <= n; i++)
1.
2.
Heapify(i)
Append the value of find_min to the list
Delete(1)
Complexity is O(n log n)
Christopher Moh 2005
18. Binomial Trees
Define Binomial Tree B(k) as follows:
B(0) is a single node
B(n), n != 0, is formed by merging two B(n-1) trees
in the following way:
The root of the B(n) tree is the root of one of the B(n-1)
trees, and the (new) leftmost child of this root is the root
of the other B(n-1) tree.
Within the tree, the heap property holds i.e. that
the key field of any node is greater than the key
field of all its children.
Christopher Moh 2005
19. Properties of Binomial Trees
The number of nodes in B(k) is exactly
2^k.
The height of B(k) is exactly (k + 1)
For any tree B(k)
The root of B(k) has exactly k children
If we take the children of B(k) from left to
right, they form the roots of a B(k-1), B(k2), …, B(0) tree in that order
Christopher Moh 2005
20. Binomial Heaps
Binomial Heaps are a forest of binomial trees with the
following properties:
All the binomial trees are of different sizes
The binomial trees are ordered (from left to right) by
increasing size
If we consider the fact that the size of B(k) is 2^k, the
binomial tree B(k) exists in a binomial heap of n
nodes iff the bit representing 2^k is “1” in the binary
representation of n
For example: 13 (decimal) = 1101 (binary), so the binomial
heap with 13 nodes consists of the binomial trees B(0), B(2),
and B(3).
Christopher Moh 2005
21. Binomial Heap
Implementation
Each node will store the following data:
Key field
Pointers (if non-existent, points to NIL) to
Parent
Next Sibling (ordered left to right; a sibling must have the same
parent); For roots of binomial trees, next sibling points to the
root of the next binomial tree
Leftmost child
Number of children in field degree
Any other data that might be useful for the program
The binomial heap is represented by a head pointer
that points to the root of the smallest binomial tree
(which is the leftmost binomial tree)
Christopher Moh 2005
22. Operations on Binomial Trees
Link (h1, h2)
1.
2.
3.
4.
Links two binomial trees with root h1 and h2 of
the same order k to form a new binomial tree of
order (k+1)
We assume h1->key < h2->key which implies
that h1 is the root of the new tree
T = h1->leftchild
h1->leftchild = h2
h2->parent = h1
H2->next_sibling= T
O(1) time
Christopher Moh 2005
23. Operations on binomial heaps
Create – Create a new binomial heap with one
node (key field set)
Set Parent, Leftchild, Next sibling to NIL
O(1) time
Find_min
1.
2.
X = head, min = INFINITY
While (X != nil)
1.
2.
3.
If (X->key < min) min = X->key
X = X->next_sibling
Return min
O(log n) time as there are at most log n binomial trees (log
n bits)
Christopher Moh 2005
24. More Operations
Merge (h1, h2, L)
Given binomial heaps with head pointers
h1 and h2, create a list L of all the
binomial trees of h1 U h2 arranged in
ascending order of size
For any order k, there may be zero, one,
or two binomial trees of order k in this list.
Christopher Moh 2005
25. More Operations
Merge (h1, h2, L)
1.
2.
Assume that NIL is a node of infinitely small
order
L = empty
While (h1 != NIL || h2 != NIL)
1.
If (h1->degree < h2->degree)
1.
2.
2.
Append the (binomial)tree with root h1 to L
h1 = h1->next_sibling
Else
1.
Apply above steps to h2 instead
Christopher Moh 2005
26. More Operations
Union (h1, h2)
The fundamental operation involving
binomial heaps
Takes two binomial heaps with head
pointers h1 and h2 and creates a new
binomial heap of the union of h1 and h2
Christopher Moh 2005
27. More Operations
Union (h1, h2)
1.
2.
3.
Start with empty binomial heap
Merge (h1, h2, L)
Go by increasing k in the list L until L is empty
1.
2.
If there is exactly one or exactly three (how can this
happen?) binomial trees of order k in L, append one
binomial tree of order k to the binomial heap and
remove that tree from L
If there are two trees of order k, remove both trees,
use Link to form a tree of order (k+1) and pre-pend this
tree to L
Union is O(log n)
Christopher Moh 2005
28. More Operations
Inserting a new node with key field set
Create a new binomial heap with that one node
Union (existing heap with head h, new heap)
O (log n) time
ChangeDown (node at position x, new value)
Decreasing the key value of a node
Same idea as binary heap: “Bubble” up the
binomial tree containing this node (exchange only
key fields and satellite data! What’s the
complexity if you physically change the node?)
O (log n) time
Christopher Moh 2005
29. More Operations
Delete (node at position x)
1.
2.
3.
4.
Deleting position x from the heap
ChangeDown(x, -INFINITY)
Now x is at the root of its binomial tree
Supposing that the binomial tree is of order k
Recall that the children of the root of the binomial tree,
from right to left, are binomial trees of order 0, 1, 2, 3, 4,
…, k-1
Form a new binomial heap with the children of the root of
this binomial tree the roots in the new binomial heap
Remove the original binomial tree from the original
binomial heap
Union (original heap, new heap)
O(log n) complexity
Christopher Moh 2005
30. More Operations
ChangeUp (node at position X, new
value)
1.
2.
Delete (X)
Insert (new value)
O (log n) time
Christopher Moh 2005
31. Summary – Binomial Heaps
Create in O(1) time
Union, Find_min, Delete, Insert, and Change
operations take O(log n) time
In general, because they are more
complicated, in competition it is far more
prudent (saves time coding and debugging) to
use a binary heap instead
Unless there are MANY Union operations
Christopher Moh 2005
32. Application of heaps: Dijkstra
1.
2.
3.
The following describes how Dijkstra’s
algorithm can be coded with a binary heap
Initializing phase:
Let n be the number of nodes
Create a heap of size n, all key fields
initialized to INFINITY
Change_val (s, 0) where s is the source
node
Christopher Moh 2005
33. Running of Dijkstra’s algorithm
1.
While (heap is not empty)
1.
2.
3.
X = node corresponding to find_min value
Delete (position of X in heap = 1)
For all nodes k that are adjacent to X
1.
If (cost[X] + distance[X][k] < cost[k])
1.
ChangeDown (position of k in heap, cost[X] +
distance[X][k])
Christopher Moh 2005
34. Analysis of running time
At most n nodes are deleted
Let m be the number of edges. Each edge is
relaxed at most once.
O(n log n)
O(m log n)
Total running time O([m+n] log n)
This is faster than using a basic array list
unless the graph is very dense, in which case
m is about O(n^2) which leads to a running
time of O(n^2 log n)
Christopher Moh 2005
35. Cumulative Sum on Intervals
Problem: We have a line that runs from x
coordinate 1 to x coordinate N. At x
coordinate X [X an integer between 0 and N],
there is g(X) gold. Given an interval [a,b],
how much gold is there between a and b?
How efficiently can this be done if we
dynamically change the amount of gold and
the interval [a,b] keeps changing?
Christopher Moh 2005
36. Cumulative Sum Array
Let us define C(0) = 0, and C(x) = C(x-1) + g(x)
where g(x) is the amount of gold at position x
C(x) then defines the total amount of gold from
position 1 to position x
The amount of gold in interval [a,b] is simply C(b) –
C(a-1)
For any change in a or b, we can perform the update in O(1)
time
However, if we change g(x), we will have to change
C(x), C(x+1), C(x+2), …, C(N)
Any change in gold results in an update in O(N) time
Christopher Moh 2005
37. Cumulative Sum Tree
We can use the binary representation of any number
to come up with a cumulative sum tree
For example, let say we take 13 (decimal) = 1101
(binary)
The cumulative sum of g(1) + g(2) + … g(13) can be
represented as the sum of:
g(1) + g(2) + … + g(8) [ 8 elements ]
g(9) + g(10) + … + g(12) [ 4 elements ]
g(13) [ 1 element ]
Notice that the number of elements in each case represents
a bit that is “1” in the binary representation of the number
Christopher Moh 2005
38. Cumulative Sum Tree
Another example: C(19)
19 (decimal) is 10011 (binary)
C(19) is the sum of the following:
g(1) + g(2) + … + g(16) [ 16 elements ]
g(17) + g(18) [ 2 elements ]
g(19) [ 1 element ]
Christopher Moh 2005
39. Cumulative Sum Tree
Let us define C2(x) to be the sum of g(x) +
g(x-1) + … + g(p + 1) where p is a number
with the same binary representation as x
except the least significant bit of x (the
rightmost bit of x that is “1”) is “0”
Examples of x and the corresponding p:
x = 6 [110], p = 4 [100]
x = 13 [1101], p = 12 [1100]
x = 16 [10000], p = 0 [00000]
Christopher Moh 2005
40. Cumulative Sum Tree
If we want to find the cumulative sum C(x) = g(1) +
g(2) + … + g(x), we can trace through the values of
C2 using the binary representation of x
Examples:
C(13) = C2(8) + C2(8+4) + C2(8+4+1)
C(16) = C2(16)
C(21) = C2(16) + C2(16+4) + C2(16+4+1)
C(99) = C2(64) + C2(64+32) + C2(64+32+2) +
C2(64+32+2+1)
This allows us to find C(x) in log x time
Hence the amount of gold in interval [a,b] = C(b) – C(a-1)
can be found in log N time, which implies updates of a and b
can be done in O(log N)
Christopher Moh 2005
41. Cumulative Sum Tree
What happens when we change g(x)?
If g(x) is changed, we only need to update C2(y)
where C2(y) covers g(x)
We can go through all necessary C2(y) in the
following way:
1.
While (x <= N)
1.
2.
Update C2(x)
Add the value of the least significant bit of x to x
This runs in O(log N) time
Hence updates to g can also be done in O(log n)
time, which is a great improvement over the
O(N) needed for an array.
Christopher Moh 2005
42. Cumulative Sum Tree
Examples [binary representation in brackets]
Change to g(5) [ 101 ] : Update C2(5), C2(6), C2(8),
C2(16) and all C2(power of 2 > 16)
Change to g(13) [ 1101 ]: Update C2(13), C2(14), C2(16),
and all C2(power of 2 > 16)
Change to g(35) [ 100011 ]: Update C2(35), C2(36),
C2(40), C2(48), C2(64), and all C2(power of 2 > 64)
We can implement a cumulative sum tree very
simply: By simply using a linear array to store the
values of C2.
Can we extend a cumulative sum tree to 2 or more
dimensions?
See IOI 2001 Day 1 Question 1
Christopher Moh 2005
43. Sum of Intervals Tree
Another way to solve the question is to use a “Sum of
Intervals” Binary Tree
Each node in the tree is represented by (L, R) and
the value of (L,R) is g(L) + g(L+1) + … + g(R)
The root of the tree has L = 1 and R = N
Every leaf has L = R
Every non-leaf has children (L, [L+R]/2) [left child]
and ([L+R]/2+1, R) [right child]
The number of nodes in the tree is O(2*N) [ why? ]
In an implementation, every node should have
pointers to its children and its parent
Christopher Moh 2005
44. Sum of Intervals Tree
How to find C(x) = g(1) + g(2) + … + g(x)?
1.
2.
We trace from the root downwards
L = 1, R = N, C = 0
While (L != R)
1.
2.
M = (L + R) / 2
If (M < x)
2.
C += value of (L,R)
Set L and R to the left child of the current node
1.
Set L and R to the right child of the current node
1.
3.
3.
Else
C += value at (L,R) [ or (L,L) or (R,R) as L = R ]
Time complexity: O(log n)
Christopher Moh 2005
45. Sum of Intervals Tree
What happens when g(x) is changed?
1.
2.
Trace from (x,x) upwards to the root
Let L = R = x
While (L,R) is not the root
1.
2.
3.
Update the value of (L,R)
Set (L,R) to the parent of (L,R)
Update the root
Complexity of O(log N)
Hence all updates of interval [a,b] and g(x)
can be done in O(log N) time
Christopher Moh 2005
46. Augmenting Data Structures
It is often useful to change the data structure
in some way, by adding additional data in
each node or changing what each node
represents.
This allows us to use the same data structure
to solve problems
For example, we can use so-called “interval
trees” to solve not just cumulative sum
problems
We can use properties of elements in the interval
(L,R) that are related to L and R.
Christopher Moh 2005
47. Other data structures
Balanced (and unbalanced) binary trees
Red-Black trees
2-3-4 trees
Splay trees
Suffix Trees
Fibonacci Heaps
Christopher Moh 2005