SFU CMPT Lecture: Week 3

Size: px
Start display at page:

Download "SFU CMPT Lecture: Week 3"

Transcription

1 SFU CMPT Lecture: Week 3 SFU CMPT Lecture: Week 3 Ján Maňuch jmanuch@sfu.ca Lecture on May 20, 2008, 5.30pm-8.20pm

2 SFU CMPT Lecture: Week 3 Important basic procedures for max-heaps 1. Max-Heapify, runs in time Ç ÐÓ Òµ time, a key procedure that maintains the max-heap property (if we change a value in the root of a heap, this procedure will correct the heap, so that it s again a heap) 2. Build-Max-Heap, runs in time Ç Òµ, produces a max-heap from unsorted data 3. Heap-Sort, runs in time Ç Ò ÐÓ Òµ, sorts array in place (uses above) Also Max-Heap-Insert, Heap-Extract-Max, Heap-Increase-Key (run in time Ç ÐÓ Òµ), Heap-Maximum, (run in time Ç ½µ), used when the heap is used to implement a priority queue

3 SFU CMPT Lecture: Week 3 Max-Heapify Maintains the max-heap property Inputs are an array and an index Assumption: sub-trees rooted in LEFT µ and RIGHT µ are proper max-heaps, but may be smaller than its children

4 SFU CMPT Lecture: Week 3 Task of Max-Heapify is to let float down in the max-heap below it so that heap rooted in becomes proper max-heap

5 SFU CMPT Lecture: Week 3 µ Max-Heapify LEFT µ 1: 2: Ö RIGHT µ 3: if heap-size µ and then 4: largest 5: else 6: largest 7: end if 8: if Ö heap-size µ and Ö largest then 9: Ö largest 10: end if 11: if largest then 12: exchange largest 13: Max-Heapify largestµ 14: end if

6 SFU CMPT Lecture: Week 3 Idea: Lines 1 2 are just for convenience Lines 3 10 find the largest of elements,, and Ö Lines first check if there s anything to be done at all, 2. and if yes, (a) move the misplaced element one level down, (b) and make a recursive call one level deeper on this element We know that after the exchange we have the largest of,, and Ö in position, so among these three, everything is OK. However, further down may still be problems. Also note that we check heap-size µ and Ö heap-size µ, so that we don t go checking outside of the heap the recursion will end if these tests fail

7 SFU CMPT Lecture: Week

8 SFU CMPT Lecture: Week 3 Running time of Max-Heapify on subtree of size Ò rooted at is ½µ for finding largest and possibly swapping plus time to run Max-Heapify on sub-tree rooted in one of the children of Size of s sub-tree in consideration is about Ò ½µ ¾ if complete trees We allow for nearly complete trees, but here the size of any sub-tree is at most ¾Ò This worst-case occurs if last level is exactly half full ½½: ¾ ½½ ¾¾ Ò

9 Ì Òµ Ì ¾Ò µ ½µ SFU CMPT Lecture: Week 3 This gives Case 2 of Master Theorem gives Ì Òµ ÐÓ Òµ Alternative proof: running time of Max-Heapify on node of height (counted from bottom!) Ç µ is Ç ÐÓ Òµ and by Assignment Problem 2.5.

10 SFU CMPT Lecture: Week 3 Building a heap Easy using Max-Heapify Suppose we have given an unordered array Ò with Ò length µ ½ One can show that the elements ½ Ò ¾ ¾ Ò Ò ¾ are the leaves of a heap.. Thus, it s OK to initially consider them as ½-element heaps, and run Max-Heapify on top of them, once for each non-leaf element (½-element heaps are always proper heaps!). Build-Max-Heap µ heap-size µ length µ 1: 2: for length µ ¾ downto ½ do 3: Max-Heapify µ 4: end for

11 SFU CMPT Lecture: Week Input array A Binary tree representing A Result

12 SFU CMPT Lecture: Week 3 Build-Max-Heap Correctness Loop invariant: At the start of each iteration, each node ½ ¾ Ò is a root of a proper max-heap Initialization: Initially, Ò ¾. Nodes Ò ¾ ½ Ò ¾ ¾ Ò are leaves. Leaves are always roots of trivial max-heaps. Maintenance: Observe, children nodes of are numbered higher than. By invariant, both are roots of max-heaps. Thus, we can call Max-Heapify µ. Now is a root of max-heap. Max-Heapify does not destroy the max-heap property of ½ ¾ Ò, thus they re still roots of max-heaps. Decrementing reestablishes invariant for next iteration. Termination: Now ¼, thus ½ ¾ Ò are roots of max-heaps (in particular, ½ is).

13 SFU CMPT Lecture: Week 3 Build-Max-Heap Running time Simple bound: calls to Max-Heapify cost Ç ÐÓ Òµ, there are Ç Òµ of them, thus Ç Ò ÐÓ Òµ. Correct, but not tight (asymptotically). Truth is Òµ. First observation: time for Max-Heapify depends on height of node (clearly). Second observation: Ò-element heap has height ÐÓ Ò (Problem 2.5). Third observation: it has at most Ò ¾ ½ nodes of any height (Problem 3.1).

14 SFU CMPT Lecture: Week 3 Assignment Problem 3.1. (deadline: May 27, 5:30pm) Prove by mathematical induction on that there are at most Ò ¾ ½ nodes of height in any Ò-element heap. Hint: For the induction step, your induction hypothesis is For any Ò, Ò-element heap has at most Ò ¾ nodes of height ½. and you want to prove that For any Ò, Ò-element heap has at most Ò ¾ ½ nodes of height. Hence, if you are proving the claim for a heap À with Ò elements, then you can apply the induction hypothesis on any heap (for instance on a heap which contains only a part of À).

15 Ì Òµ Ò ½ Ñ Ð Ç Ò Ç µ ¾ ½ Ü ¾ Ü Üµ ¾ ½ ¾ ¾ SFU CMPT Lecture: Week 3 We know: Time required by Max-Heapify on node of height is Ç µ. Thus, running time of Build-Max-Heap is upper-bounded by Ò ÐÓ Ò ÐÓ ¼ ¼ By formula and substituting Ü ½ ¾ we obtain ¼ and thus ½ ¼ ½ ¾ ½ ½ ¾µ ¾ ¾ ¾ Ò ÐÓ ½ Ç Ò Òµ Ì Ç Òµ Ò Ç ¼ ¼ Hence, Build-Max-Heap runs in linear time.

16 SFU CMPT Lecture: Week 3 Heapsort Now it s very easy to write down the algorithm for Heapsort. Idea as follows: 1. Given unsorted array, build heap on, using Build-Max-Heap 2. Extract largest element (is in ½ ), and move it to the end of array (swap ½ and Ò ) 3. Decrease the size of the heap by 1 4. The root might not satisfy the heap property (that s where the element formerly in Ò now is), hence, using Max-Heapify, correct the heap 5. Extract the 2nd-largest element (again, in ½ ), and so on...

17 SFU CMPT Lecture: Week 3 Heapsort µ Build-Max-Heap µ 1: 2: for length µ downto ¾ do 3: exchange ½ 4: heap-size µ heap-size µ ½ 5: Max-Heapify ½µ 6: end for Running time: Ç Ò ÐÓ Òµ (Build-Max takes Ç Òµ, and then Ç Òµ rounds with Ç ÐÓ Òµ each). Correctness Loop invariant: At the start of each iteration of the for loop of lines 2 5, the subarray ½ is a max-heap containing the smallest elements of ½ Ò, and the subarray ½ Ò contains the Ò largest elements in sorted order.

18 SFU CMPT Lecture: Week 3 Initialization: After running Build-Max-Heap, the elements ½ Ò form a max-heap. Since, innitially Ò, the first subarray is ½ Ò (a max-heap), while the second is empty, and so satisfies all conditions trivially. Maintenance: Since ½ contains the maximal element of ½, but is smaller than any of the elements in ½ Ò, after swapping ½ and : Ò is sorted (since ½ Ò was sorted) elements in ½ ½ are smaller than elements in Ò ½ ½ is a max-heap except for the root Lines 4-5 correct the max-heap property in the root, hence, in the end of the ½ ½ iteration is a proper max-heap. Termination: in the end, we have that the loop invariant is satisfied for ½: elements ¾ Ò are sorted and greater than ½... done

19 SFU CMPT Lecture: Week 3 (Simple) Queues dequeue (remove) What is a queue? a b c enqueue (insert) We insert new elements at the tail, and remove elements from the head. The standard queue is a First-In-First-Out (FIFO) buffer. Many applications, for instance CD burning: reader process reads files from a hard disk and inserts data into a queue writer process reads from the queue and writes onto CD

20 SFU CMPT Lecture: Week 3 Priority queues They are different: there s no real FIFO rule anymore. A priority queue maintains set Ë of elements, each with a key (priority). Two kinds: max-priority queues and min-priority queues, usually implemented by max-heaps and min-heaps. Max-priority queue Operations: Insert Ë Üµ inserts element Ü into set Ë Maximum ˵ returns element of Ë with largest key Extract-Max ˵ removes and returns element of Ë with largest key Increase-Key Ë Ü µ increases Ü s key to new value, assuming is at least as large as Ü s old key Min-priority queue is used via operations: Insert, Minimum, Extract-Min, and Decrease-Key.

21 SFU CMPT Lecture: Week 3 Example for Max-priority queue: job scheduling on a shared computer What are they good for? jobs with priorities, can be stored in a max-priority queue each time a new job is to be scheduled, it s got to be one of highest priority (Extract-Max operation) new jobs can be inserted using Insert operation in order to avoid starvation, priorities can be increased (Increase-Key operation) Example for Min-priority queue: event-driven simulator items in the queue are events to be simulated, and the key is the time of the event when it should occur (happen) events are simulated in time order, hence we always extract the event with the smallest time from the queue and simulate it new events are procuded, so they have to be inserted in the queue

22 SFU CMPT Lecture: Week 3 Heaps are very convenient here: using max-heaps, we know that the largest element is in ½ : we Ç ½µ have access to largest element removing/inserting elements and increasing keys means that we (basically) can call Max-Heapify or a similar procedure (fixing the heap from bottom up) at the right place (relatively efficient operation Òµ) Ç ÐÓ Min-priority queues analogous. Implementation We re considering max-priority queues, implemented using max-heaps Let s start with a simple operation. Heap-Maximum µ 1: ½ return implements Maximum operation in Ç ½µ time.

23 SFU CMPT Lecture: Week 3 Extract max Now, how to we remove the largest element from the queue/heap so that we will still have a proper max-heap? Heap-Extract-Max µ 1: heap-size µ ½ if then 2: error heap underflow 3: end if 4: max ½ 5: ½ heap-size µ 6: heap-size µ heap-size µ ½ Max-Heapify ½µ 7: 8: return max implements Extract-Max Running time is Ç ÐÓ Òµ

24 SFU CMPT Lecture: Week 3 Increase key Suppose that the element which key is to be increased is identified by index We first update the key Clearly, this can destroy the max-heap property, thus we need to find a new place for this element The idea is to move the updated element as far up toward the root as necessary

25 SFU CMPT Lecture: Week 3 keyµ Heap-Increase-Key 1: key if then 2: error new key is smaller than current key 3: end if 4: key 5: while ½ and Parent µ do 6: exchange Parent µ Parent µ 7: 8: end while Running time is Ç ÐÓ Òµ (height of tree)

26 SFU CMPT Lecture: Week 3 Example: ½ µ Heap-Increase-Key The node with index : update the key from ½ to

27 SFU CMPT Lecture: Week 3 Assignment Problem 3.2. (deadline: May 27, 5:30pm) Argue the correctness of Heap-Increase-Key using the following loop invariant At the start of each iteration of the while loop of lines 5 8, the array ½ heap-size µ satisfies the max-heap property with possible one exception: may be larger than Parent µ.

28 SFU CMPT Lecture: Week 3 Insert Inserting is now easy: 1. add a new element to the end of heap 2. set its key to desired value keyµ Heap-Insert heap-size µ heap-size µ ½ 1: 2: heap-size µ ½ 3: Heap-Increase-Key heap-size µ keyµ Running time is Ç ÐÓ Òµ Conclusion: all considered operations can be implemented to run in time Ç ÐÓ Òµ, Maximum µ even in Ç ½µ Note: there are other, more efficient implementations around!

29 SFU CMPT Lecture: Week 3 Exercise 3.1. Consider an implementation of priority queue using a sorted array instead of a max-heap. What are the running time of the four operations needed for priority queue?

30 SFU CMPT Lecture: Week 3 A lower bound for comparison-based sorting We ve seen a few sorting algorithms: Selection-Sort: in place, upper bound Ç Ò ¾ µ Merge-Sort: upper bound Ç Ò ÐÓ Òµ Heap-Sort: in place, upper bound Ç Ò ÐÓ Òµ Can we get any better algorithm? Or is Ò ÐÓ Òµ an inherent barrier? a comparison sort information about the elements is collected only via comparing the elements We prove that any comparison sort must make Å Ò ÐÓ Òµ comparisons in the worst case to sort Ò elements.

31 SFU CMPT Lecture: Week 3 input sequence ½ ¾ Ò for and we can perform any of the tests,,, to determine their relative order we are not interested in actual values of the elements for simplicity let s us assume that for all, and hence we can assume that all comparisons have the form Decision tree a full binary tree (every node has either zero or two children) represents comparisons between elements performed by particular algorithm run on all possible inputs only comparisons are relevant, everything else is ignored

32 SFU CMPT Lecture: Week 3 internal nodes are labelled for ½ Ò, meaning elements and are compared edges are labelled or, depending on the outcome of the comparisons ¾µ Òµ ½µ i.e., at this point representing the output of the algorithm, leaves are labelled with a permutation ½µ ¾µ Òµ a branch from the root to a leaf describes a sequence of comparisons (nodes and edges) resulting in a permutation (a leaf) a reachable node a node into which we can get on an input; especially we are interested in reachable leaves

33 SFU CMPT Lecture: Week 3 Example: SELECTION-SORT Ò ½ ½ Ò 1: for to do 2: /* 3 10: find min in */ 3: smallest element ½ Ò 4: smallest position 5: for to do 6: if smallest element then 7: smallest element 8: smallest position 9: end if 10: end for 11: swap and smallest position 12: end for

34 ½ ½µ, ¾ ¾µ,..., Ò Òµ SFU CMPT Lecture: Week 3 Note: distinguish between the input sequence ½ ¾ Ò and the array ½ ¾ Ò used to store the sequence. in the beginning we have ½, ¾ ¾,..., Ò Ò ½ but in the end we want

35 SFU CMPT Lecture: Week 3 Decision tree for Selection-Sort on -element inputs < 1:2 > < 1:3 2:3 > < > 2:3 1:2 1:3 1:2 < > < > < > < > 1,2,3 1,3,2 3,1,2 3,1,2 2,1,3 2,3,1 3,2,1 3,2,1 SELECTION-SORT Ò ½ ½ Ò 1: for to do 2: /* 3 10: find min in */ 3: smallest element ½ Ò 4: smallest position 5: for to do 6: if smallest element then 7: smallest element 8: smallest position 9: end if 10: end for 11: swap and smallest position 12: end for

36 SFU CMPT Lecture: Week 3 More efficient decision tree < 1:2 > < 1:3 2:3 > < > < 2:3 > 3,1,2 1:3 3,2,1 < > 1,2,3 1,3,2 2,1,3 2,3,1

37 SFU CMPT Lecture: Week 3 Assignment Problem 3.3. (deadline: May 27, 5:30pm) Draw an efficient decision tree for inputs with elements. Hint: The height of the tree should be.

38 SFU CMPT Lecture: Week 3 Any correct sorting algorithm must be able to produce each permutation of its input Thus, a necessary condition is that each of the Ò permutations must appear in a reachable leaf of the decision tree Lower bound for worst case Length of longest path from root of decision tree to any leaf represents worst case number of comparisons Lower bound on heights of all decision trees where each permutation appears as leaf is thus lower bound on running time of any comparison based sort algorithm

39 Binary tree of height has at most ¾ leaves, thus Ò ¾ SFU CMPT Lecture: Week 3 Theorem. Any comparison sort algorithm requires Å Ò ÐÓ Òµ comparisons in the worst case. Proof. Sufficient to determine the height of a decision tree in which each permutation appears as a leaf Consider decision tree of height with leaves corresponding to a comparison sort on Ò elements Each of the Ò permutations of the input appears as a leaf, therefore Ò Take logs: ÐÓ Ò µ Å Ò ÐÓ Òµ Exercise 3.2. Show that there is no comparison sort whose running time is linear for at least half of the Ò inputs of length Ò. What about a fraction of ½ Ò of the inputs of length Ò? What about a fraction ½ ¾ Ò?

40 SFU CMPT Lecture: Week 3 Quicksort Quicksort is based on Divide&Conquer paradigm its worst-case running time is Ò ¾ µ so why should we consider it? efficient in average: the expected running time is Ò ÐÓ Òµ with a small constant (hidden in Theta-notation)! in place in practice, we don t care that it performs very bad for some few inputs, if it performs very good on the most of them µ the best practical choice for sorting

41 SFU CMPT Lecture: Week 3 Input: array ½ Ò how to sort a subarray Ô Ö : Divide: Compute (in some way) some index Õ, and partition Ô Ö into two (possibly empty) subarrays Ô Õ ½ and Õ ½ Ö s.t. each element of Ô Õ ½ is less than or equal to Õ, and each element of Õ ½ Ö is greater than Õ element Õ is called a pivot Conquer: Sort the two subarrays Ô Õ ½ and Õ ½ Ö recursively Combine: Subarrays are sorted in-place, nothing is needed here; the entire Ô Ö is sorted the performance of Quicksort depends extremely on how the pivots are selected!

42 SFU CMPT Lecture: Week 3 Ô Öµ Quicksort 1: Ô Ö if then 2: Õ Partition Ô Öµ 3: Quicksort Ô Õ ½µ 4: Quicksort Õ ½ Öµ 5: end if To sort the entire array, call Quicksort ½ length µµ Note: this is a very naive implementation because it recurses down to the very end. Efficient implementations do special things at the bottom of recursion (calls optimal procedures to sort sub-arrays of lengths up to, say, )

43 SFU CMPT Lecture: Week 3 So, a more practical solution would look something like this: Ô Öµ Quicksort 1: Ö Ô ¾ if then 2: Sort2 Ôµ 3: else if Ö Ô then 4: Sort3 Ôµ 5: else if Ö Ô then 6: Sort4 Ôµ 7: else if Ö Ô then 8: Sort5 Ôµ 9: else if Ö Ô then 10: Sort6 Ôµ 11: else if Ö Ô then 12: Sort7 Ôµ 13: else if Ô Ö then 14: Õ Partition Ô Öµ 15: Quicksort Ô Õ ½µ 16: Quicksort Õ ½ Öµ 17: end if

44 SFU CMPT Lecture: Week 3 Partition The most important part of Quicksort. Ô Öµ Partition Ü Ö 1: /* choose a Ü pivot */ 2: Ô ½ 3: for Ô to Ö ½ do 4: if Ü then 5: ½ 6: exchange 7: end if 8: end for 9: exchange ½ Ö 10: return ½

45 SFU CMPT Lecture: Week 3 Ô Öµ Partition Ü Ö 1: /* choose a Ü pivot */ 2: Ô ½ 3: for Ô to Ö ½ do 4: if Ü then 5: ½ 6: exchange 7: end if 8: end for 9: exchange ½ Ö 10: return ½ the loop invariant: at the beginning of each iteration of the loop each element Ô of is smaller than or equal to Ü pivot 2. each element ½ ½ of is greater Ü than 3. Ö ½ elements are so-far arbitrary 4. element Ö Ü

46 SFU CMPT Lecture: Week 3 i j p r i j p r i j p r i j p r i j p r i j p r i j p r i p r

47 SFU CMPT Lecture: Week 3 Correctness: enough to prove first 2 conditions (condition 4 satisfied by line 1) Initialization: before 1st iteration, Ô ½ and Ô; first two conditions are trivial Maintenance: 2 cases: Ü only is increased in the loop; enough to check condition 2 for element ½ Ü is incremented, then and are swapped, then is incremented; enough to check condition 1 for (ok, because of swapping) and condition 2 ½ for (ok, by the loop invariant) Termination: Ö, hence there are no elements in part 3, the others are divided as required finally, to get correct order of these 3 parts, we swap the first element of the second part Ö with running time Òµ, where Ò Ö Ô ½

48 SFU CMPT Lecture: Week 3 This particular implementation of Partition is not good because it always uses the rightmost element as pivot. It s easy to come up with examples that force this implementation to be very bad (meaning overall running time Å Ò ¾ µ). What we want is an even partition of Ô Ö into two sub-arrays of (about) the same size. That s the second very important feature of efficient implementations of Quicksort. There are several not-too-bad ways: look at, say, fixed array elements and pick the median pick a randomly chosen element look at, say randomly chosen elements and pick the median many more In practice, the random variant usually works best (unless you happen to know something about your inputs)

Converting a Number from Decimal to Binary

Converting a Number from Decimal to Binary Converting a Number from Decimal to Binary Convert nonnegative integer in decimal format (base 10) into equivalent binary number (base 2) Rightmost bit of x Remainder of x after division by two Recursive

More information

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * * Binary Heaps A binary heap is another data structure. It implements a priority queue. Priority Queue has the following operations: isempty add (with priority) remove (highest priority) peek (at highest

More information

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1. Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology 6.046J/18.410J Professors Erik Demaine and Shafi Goldwasser Quiz 1 Quiz 1 Do not open this quiz booklet until you are directed

More information

Data Structures Fibonacci Heaps, Amortized Analysis

Data Structures Fibonacci Heaps, Amortized Analysis Chapter 4 Data Structures Fibonacci Heaps, Amortized Analysis Algorithm Theory WS 2012/13 Fabian Kuhn Fibonacci Heaps Lacy merge variant of binomial heaps: Do not merge trees as long as possible Structure:

More information

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2) Sorting revisited How did we use a binary search tree to sort an array of elements? Tree Sort Algorithm Given: An array of elements to sort 1. Build a binary search tree out of the elements 2. Traverse

More information

Full and Complete Binary Trees

Full and Complete Binary Trees Full and Complete Binary Trees Binary Tree Theorems 1 Here are two important types of binary trees. Note that the definitions, while similar, are logically independent. Definition: a binary tree T is full

More information

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++ Answer the following 1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++ 2) Which data structure is needed to convert infix notations to postfix notations? Stack 3) The

More information

A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heap-order property

A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heap-order property CmSc 250 Intro to Algorithms Chapter 6. Transform and Conquer Binary Heaps 1. Definition A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called

More information

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) 3 4 4 7 5 9 6 16 7 8 8 4 9 8 10 4 Total 92.

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) 3 4 4 7 5 9 6 16 7 8 8 4 9 8 10 4 Total 92. Name: Email ID: CSE 326, Data Structures Section: Sample Final Exam Instructions: The exam is closed book, closed notes. Unless otherwise stated, N denotes the number of elements in the data structure

More information

1/1 7/4 2/2 12/7 10/30 12/25

1/1 7/4 2/2 12/7 10/30 12/25 Binary Heaps A binary heap is dened to be a binary tree with a key in each node such that: 1. All leaves are on, at most, two adjacent levels. 2. All leaves on the lowest level occur to the left, and all

More information

Algorithms and Data Structures

Algorithms and Data Structures Algorithms and Data Structures CMPSC 465 LECTURES 20-21 Priority Queues and Binary Heaps Adam Smith S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 1 Trees Rooted Tree: collection

More information

Section IV.1: Recursive Algorithms and Recursion Trees

Section IV.1: Recursive Algorithms and Recursion Trees Section IV.1: Recursive Algorithms and Recursion Trees Definition IV.1.1: A recursive algorithm is an algorithm that solves a problem by (1) reducing it to an instance of the same problem with smaller

More information

CS473 - Algorithms I

CS473 - Algorithms I CS473 - Algorithms I Lecture 9 Sorting in Linear Time View in slide-show mode 1 How Fast Can We Sort? The algorithms we have seen so far: Based on comparison of elements We only care about the relative

More information

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R Binary Search Trees A Generic Tree Nodes in a binary search tree ( B-S-T) are of the form P parent Key A Satellite data L R B C D E F G H I J The B-S-T has a root node which is the only node whose parent

More information

Algorithms and Data Structures

Algorithms and Data Structures Algorithms and Data Structures Part 2: Data Structures PD Dr. rer. nat. habil. Ralf-Peter Mundani Computation in Engineering (CiE) Summer Term 2016 Overview general linked lists stacks queues trees 2 2

More information

6 March 2007 1. Array Implementation of Binary Trees

6 March 2007 1. Array Implementation of Binary Trees Heaps CSE 0 Winter 00 March 00 1 Array Implementation of Binary Trees Each node v is stored at index i defined as follows: If v is the root, i = 1 The left child of v is in position i The right child of

More information

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team Lecture Summary In this lecture, we learned about the ADT Priority Queue. A

More information

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child Binary Search Trees Data in each node Larger than the data in its left child Smaller than the data in its right child FIGURE 11-6 Arbitrary binary tree FIGURE 11-7 Binary search tree Data Structures Using

More information

Data Structures and Algorithms Written Examination

Data Structures and Algorithms Written Examination Data Structures and Algorithms Written Examination 22 February 2013 FIRST NAME STUDENT NUMBER LAST NAME SIGNATURE Instructions for students: Write First Name, Last Name, Student Number and Signature where

More information

Binary Search Trees CMPSC 122

Binary Search Trees CMPSC 122 Binary Search Trees CMPSC 122 Note: This notes packet has significant overlap with the first set of trees notes I do in CMPSC 360, but goes into much greater depth on turning BSTs into pseudocode than

More information

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Questions 1 through 25 are worth 2 points each. Choose one best answer for each. Questions 1 through 25 are worth 2 points each. Choose one best answer for each. 1. For the singly linked list implementation of the queue, where are the enqueues and dequeues performed? c a. Enqueue in

More information

Data Structure [Question Bank]

Data Structure [Question Bank] Unit I (Analysis of Algorithms) 1. What are algorithms and how they are useful? 2. Describe the factor on best algorithms depends on? 3. Differentiate: Correct & Incorrect Algorithms? 4. Write short note:

More information

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb Binary Search Trees Lecturer: Georgy Gimel farb COMPSCI 220 Algorithms and Data Structures 1 / 27 1 Properties of Binary Search Trees 2 Basic BST operations The worst-case time complexity of BST operations

More information

Cpt S 223. School of EECS, WSU

Cpt S 223. School of EECS, WSU Priority Queues (Heaps) 1 Motivation Queues are a standard mechanism for ordering tasks on a first-come, first-served basis However, some tasks may be more important or timely than others (higher priority)

More information

CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting

CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting CSC148 Lecture 8 Algorithm Analysis Binary Search Sorting Algorithm Analysis Recall definition of Big Oh: We say a function f(n) is O(g(n)) if there exists positive constants c,b such that f(n)

More information

Algorithms. Margaret M. Fleck. 18 October 2010

Algorithms. Margaret M. Fleck. 18 October 2010 Algorithms Margaret M. Fleck 18 October 2010 These notes cover how to analyze the running time of algorithms (sections 3.1, 3.3, 4.4, and 7.1 of Rosen). 1 Introduction The main reason for studying big-o

More information

APP INVENTOR. Test Review

APP INVENTOR. Test Review APP INVENTOR Test Review Main Concepts App Inventor Lists Creating Random Numbers Variables Searching and Sorting Data Linear Search Binary Search Selection Sort Quick Sort Abstraction Modulus Division

More information

Data Structures and Algorithm Analysis (CSC317) Intro/Review of Data Structures Focus on dynamic sets

Data Structures and Algorithm Analysis (CSC317) Intro/Review of Data Structures Focus on dynamic sets Data Structures and Algorithm Analysis (CSC317) Intro/Review of Data Structures Focus on dynamic sets We ve been talking a lot about efficiency in computing and run time. But thus far mostly ignoring data

More information

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction Class Overview CSE 326: Data Structures Introduction Introduction to many of the basic data structures used in computer software Understand the data structures Analyze the algorithms that use them Know

More information

Sequential Data Structures

Sequential Data Structures Sequential Data Structures In this lecture we introduce the basic data structures for storing sequences of objects. These data structures are based on arrays and linked lists, which you met in first year

More information

Chapter 13: Query Processing. Basic Steps in Query Processing

Chapter 13: Query Processing. Basic Steps in Query Processing Chapter 13: Query Processing! Overview! Measures of Query Cost! Selection Operation! Sorting! Join Operation! Other Operations! Evaluation of Expressions 13.1 Basic Steps in Query Processing 1. Parsing

More information

CS711008Z Algorithm Design and Analysis

CS711008Z Algorithm Design and Analysis CS711008Z Algorithm Design and Analysis Lecture 7 Binary heap, binomial heap, and Fibonacci heap 1 Dongbo Bu Institute of Computing Technology Chinese Academy of Sciences, Beijing, China 1 The slides were

More information

Analysis of Algorithms I: Binary Search Trees

Analysis of Algorithms I: Binary Search Trees Analysis of Algorithms I: Binary Search Trees Xi Chen Columbia University Hash table: A data structure that maintains a subset of keys from a universe set U = {0, 1,..., p 1} and supports all three dictionary

More information

From Last Time: Remove (Delete) Operation

From Last Time: Remove (Delete) Operation CSE 32 Lecture : More on Search Trees Today s Topics: Lazy Operations Run Time Analysis of Binary Search Tree Operations Balanced Search Trees AVL Trees and Rotations Covered in Chapter of the text From

More information

GRAPH THEORY LECTURE 4: TREES

GRAPH THEORY LECTURE 4: TREES GRAPH THEORY LECTURE 4: TREES Abstract. 3.1 presents some standard characterizations and properties of trees. 3.2 presents several different types of trees. 3.7 develops a counting method based on a bijection

More information

External Sorting. Chapter 13. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

External Sorting. Chapter 13. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 External Sorting Chapter 13 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Why Sort? A classic problem in computer science! Data requested in sorted order e.g., find students in increasing

More information

Load Balancing. Load Balancing 1 / 24

Load Balancing. Load Balancing 1 / 24 Load Balancing Backtracking, branch & bound and alpha-beta pruning: how to assign work to idle processes without much communication? Additionally for alpha-beta pruning: implementing the young-brothers-wait

More information

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees B-Trees Algorithms and data structures for external memory as opposed to the main memory B-Trees Previous Lectures Height balanced binary search trees: AVL trees, red-black trees. Multiway search trees:

More information

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles B-Trees Algorithms and data structures for external memory as opposed to the main memory B-Trees Previous Lectures Height balanced binary search trees: AVL trees, red-black trees. Multiway search trees:

More information

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge, The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge, cheapest first, we had to determine whether its two endpoints

More information

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C Tutorial#1 Q 1:- Explain the terms data, elementary item, entity, primary key, domain, attribute and information? Also give examples in support of your answer? Q 2:- What is a Data Type? Differentiate

More information

Big Data and Scripting. Part 4: Memory Hierarchies

Big Data and Scripting. Part 4: Memory Hierarchies 1, Big Data and Scripting Part 4: Memory Hierarchies 2, Model and Definitions memory size: M machine words total storage (on disk) of N elements (N is very large) disk size unlimited (for our considerations)

More information

Lecture Notes on Linear Search

Lecture Notes on Linear Search Lecture Notes on Linear Search 15-122: Principles of Imperative Computation Frank Pfenning Lecture 5 January 29, 2013 1 Introduction One of the fundamental and recurring problems in computer science is

More information

Lecture 1: Course overview, circuits, and formulas

Lecture 1: Course overview, circuits, and formulas Lecture 1: Course overview, circuits, and formulas Topics in Complexity Theory and Pseudorandomness (Spring 2013) Rutgers University Swastik Kopparty Scribes: John Kim, Ben Lund 1 Course Information Swastik

More information

Ordered Lists and Binary Trees

Ordered Lists and Binary Trees Data Structures and Algorithms Ordered Lists and Binary Trees Chris Brooks Department of Computer Science University of San Francisco Department of Computer Science University of San Francisco p.1/62 6-0:

More information

Symbol Tables. Introduction

Symbol Tables. Introduction Symbol Tables Introduction A compiler needs to collect and use information about the names appearing in the source program. This information is entered into a data structure called a symbol table. The

More information

Sorting Algorithms. Nelson Padua-Perez Bill Pugh. Department of Computer Science University of Maryland, College Park

Sorting Algorithms. Nelson Padua-Perez Bill Pugh. Department of Computer Science University of Maryland, College Park Sorting Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park Overview Comparison sort Bubble sort Selection sort Tree sort Heap sort Quick sort Merge

More information

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)! The Tower of Hanoi Recursion Solution recursion recursion recursion Recursive Thinking: ignore everything but the bottom disk. 1 2 Recursive Function Time Complexity Hanoi (n, src, dest, temp): If (n >

More information

Lecture Notes on Binary Search Trees

Lecture Notes on Binary Search Trees Lecture Notes on Binary Search Trees 15-122: Principles of Imperative Computation Frank Pfenning André Platzer Lecture 17 October 23, 2014 1 Introduction In this lecture, we will continue considering associative

More information

International Journal of Advanced Research in Computer Science and Software Engineering

International Journal of Advanced Research in Computer Science and Software Engineering Volume 3, Issue 7, July 23 ISSN: 2277 28X International Journal of Advanced Research in Computer Science and Software Engineering Research Paper Available online at: www.ijarcsse.com Greedy Algorithm:

More information

Heaps & Priority Queues in the C++ STL 2-3 Trees

Heaps & Priority Queues in the C++ STL 2-3 Trees Heaps & Priority Queues in the C++ STL 2-3 Trees CS 3 Data Structures and Algorithms Lecture Slides Friday, April 7, 2009 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks

More information

Binary Heap Algorithms

Binary Heap Algorithms CS Data Structures and Algorithms Lecture Slides Wednesday, April 5, 2009 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks CHAPPELLG@member.ams.org 2005 2009 Glenn G. Chappell

More information

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and: Binary Search Trees 1 The general binary tree shown in the previous chapter is not terribly useful in practice. The chief use of binary trees is for providing rapid access to data (indexing, if you will)

More information

Why Use Binary Trees?

Why Use Binary Trees? Binary Search Trees Why Use Binary Trees? Searches are an important application. What other searches have we considered? brute force search (with array or linked list) O(N) binarysearch with a pre-sorted

More information

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D. 1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D. base address 2. The memory address of fifth element of an array can be calculated

More information

Mathematical Induction. Lecture 10-11

Mathematical Induction. Lecture 10-11 Mathematical Induction Lecture 10-11 Menu Mathematical Induction Strong Induction Recursive Definitions Structural Induction Climbing an Infinite Ladder Suppose we have an infinite ladder: 1. We can reach

More information

Introduction to Data Structures and Algorithms

Introduction to Data Structures and Algorithms Introduction to Data Structures and Algorithms Chapter: Elementary Data Structures(1) Lehrstuhl Informatik 7 (Prof. Dr.-Ing. Reinhard German) Martensstraße 3, 91058 Erlangen Overview on simple data structures

More information

Row Echelon Form and Reduced Row Echelon Form

Row Echelon Form and Reduced Row Echelon Form These notes closely follow the presentation of the material given in David C Lay s textbook Linear Algebra and its Applications (3rd edition) These notes are intended primarily for in-class presentation

More information

Analysis of Algorithms, I

Analysis of Algorithms, I Analysis of Algorithms, I CSOR W4231.002 Eleni Drinea Computer Science Department Columbia University Thursday, February 26, 2015 Outline 1 Recap 2 Representing graphs 3 Breadth-first search (BFS) 4 Applications

More information

Lecture Notes on Binary Search Trees

Lecture Notes on Binary Search Trees Lecture Notes on Binary Search Trees 15-122: Principles of Imperative Computation Frank Pfenning Lecture 17 March 17, 2010 1 Introduction In the previous two lectures we have seen how to exploit the structure

More information

Scheduling Shop Scheduling. Tim Nieberg

Scheduling Shop Scheduling. Tim Nieberg Scheduling Shop Scheduling Tim Nieberg Shop models: General Introduction Remark: Consider non preemptive problems with regular objectives Notation Shop Problems: m machines, n jobs 1,..., n operations

More information

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes. 1. The advantage of.. is that they solve the problem if sequential storage representation. But disadvantage in that is they are sequential lists. [A] Lists [B] Linked Lists [A] Trees [A] Queues 2. The

More information

External Sorting. Why Sort? 2-Way Sort: Requires 3 Buffers. Chapter 13

External Sorting. Why Sort? 2-Way Sort: Requires 3 Buffers. Chapter 13 External Sorting Chapter 13 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Why Sort? A classic problem in computer science! Data requested in sorted order e.g., find students in increasing

More information

The Goldberg Rao Algorithm for the Maximum Flow Problem

The Goldberg Rao Algorithm for the Maximum Flow Problem The Goldberg Rao Algorithm for the Maximum Flow Problem COS 528 class notes October 18, 2006 Scribe: Dávid Papp Main idea: use of the blocking flow paradigm to achieve essentially O(min{m 2/3, n 1/2 }

More information

CS473 - Algorithms I

CS473 - Algorithms I CS473 - Algorithms I Lecture 4 The Divide-and-Conquer Design Paradigm View in slide-show mode 1 Reminder: Merge Sort Input array A sort this half sort this half Divide Conquer merge two sorted halves Combine

More information

Data Structures and Data Manipulation

Data Structures and Data Manipulation Data Structures and Data Manipulation What the Specification Says: Explain how static data structures may be used to implement dynamic data structures; Describe algorithms for the insertion, retrieval

More information

Tables so far. set() get() delete() BST Average O(lg n) O(lg n) O(lg n) Worst O(n) O(n) O(n) RB Tree Average O(lg n) O(lg n) O(lg n)

Tables so far. set() get() delete() BST Average O(lg n) O(lg n) O(lg n) Worst O(n) O(n) O(n) RB Tree Average O(lg n) O(lg n) O(lg n) Hash Tables Tables so far set() get() delete() BST Average O(lg n) O(lg n) O(lg n) Worst O(n) O(n) O(n) RB Tree Average O(lg n) O(lg n) O(lg n) Worst O(lg n) O(lg n) O(lg n) Table naïve array implementation

More information

Algorithms Chapter 12 Binary Search Trees

Algorithms Chapter 12 Binary Search Trees Algorithms Chapter 1 Binary Search Trees Outline Assistant Professor: Ching Chi Lin 林 清 池 助 理 教 授 chingchi.lin@gmail.com Department of Computer Science and Engineering National Taiwan Ocean University

More information

CMPSCI611: Approximating MAX-CUT Lecture 20

CMPSCI611: Approximating MAX-CUT Lecture 20 CMPSCI611: Approximating MAX-CUT Lecture 20 For the next two lectures we ll be seeing examples of approximation algorithms for interesting NP-hard problems. Today we consider MAX-CUT, which we proved to

More information

Data Structures and Algorithms

Data Structures and Algorithms Data Structures and Algorithms CS245-2016S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco 06-0: Ordered List ADT Operations: Insert an element in the list

More information

Binary Heaps. CSE 373 Data Structures

Binary Heaps. CSE 373 Data Structures Binary Heaps CSE Data Structures Readings Chapter Section. Binary Heaps BST implementation of a Priority Queue Worst case (degenerate tree) FindMin, DeleteMin and Insert (k) are all O(n) Best case (completely

More information

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015 Data Structures Jaehyun Park CS 97SI Stanford University June 29, 2015 Typical Quarter at Stanford void quarter() { while(true) { // no break :( task x = GetNextTask(tasks); process(x); // new tasks may

More information

Exam study sheet for CS2711. List of topics

Exam study sheet for CS2711. List of topics Exam study sheet for CS2711 Here is the list of topics you need to know for the final exam. For each data structure listed below, make sure you can do the following: 1. Give an example of this data structure

More information

CSE 326: Data Structures B-Trees and B+ Trees

CSE 326: Data Structures B-Trees and B+ Trees Announcements (4//08) CSE 26: Data Structures B-Trees and B+ Trees Brian Curless Spring 2008 Midterm on Friday Special office hour: 4:-5: Thursday in Jaech Gallery (6 th floor of CSE building) This is

More information

3. Mathematical Induction

3. Mathematical Induction 3. MATHEMATICAL INDUCTION 83 3. Mathematical Induction 3.1. First Principle of Mathematical Induction. Let P (n) be a predicate with domain of discourse (over) the natural numbers N = {0, 1,,...}. If (1)

More information

root node level: internal node edge leaf node CS@VT Data Structures & Algorithms 2000-2009 McQuain

root node level: internal node edge leaf node CS@VT Data Structures & Algorithms 2000-2009 McQuain inary Trees 1 A binary tree is either empty, or it consists of a node called the root together with two binary trees called the left subtree and the right subtree of the root, which are disjoint from each

More information

recursion, O(n), linked lists 6/14

recursion, O(n), linked lists 6/14 recursion, O(n), linked lists 6/14 recursion reducing the amount of data to process and processing a smaller amount of data example: process one item in a list, recursively process the rest of the list

More information

A binary search tree is a binary tree with a special property called the BST-property, which is given as follows:

A binary search tree is a binary tree with a special property called the BST-property, which is given as follows: Chapter 12: Binary Search Trees A binary search tree is a binary tree with a special property called the BST-property, which is given as follows: For all nodes x and y, if y belongs to the left subtree

More information

Lecture 4 Online and streaming algorithms for clustering

Lecture 4 Online and streaming algorithms for clustering CSE 291: Geometric algorithms Spring 2013 Lecture 4 Online and streaming algorithms for clustering 4.1 On-line k-clustering To the extent that clustering takes place in the brain, it happens in an on-line

More information

Lecture 3: Finding integer solutions to systems of linear equations

Lecture 3: Finding integer solutions to systems of linear equations Lecture 3: Finding integer solutions to systems of linear equations Algorithmic Number Theory (Fall 2014) Rutgers University Swastik Kopparty Scribe: Abhishek Bhrushundi 1 Overview The goal of this lecture

More information

Regular Expressions and Automata using Haskell

Regular Expressions and Automata using Haskell Regular Expressions and Automata using Haskell Simon Thompson Computing Laboratory University of Kent at Canterbury January 2000 Contents 1 Introduction 2 2 Regular Expressions 2 3 Matching regular expressions

More information

Closest Pair Problem

Closest Pair Problem Closest Pair Problem Given n points in d-dimensions, find two whose mutual distance is smallest. Fundamental problem in many applications as well as a key step in many algorithms. p q A naive algorithm

More information

Class Notes CS 3137. 1 Creating and Using a Huffman Code. Ref: Weiss, page 433

Class Notes CS 3137. 1 Creating and Using a Huffman Code. Ref: Weiss, page 433 Class Notes CS 3137 1 Creating and Using a Huffman Code. Ref: Weiss, page 433 1. FIXED LENGTH CODES: Codes are used to transmit characters over data links. You are probably aware of the ASCII code, a fixed-length

More information

Reminder: Complexity (1) Parallel Complexity Theory. Reminder: Complexity (2) Complexity-new

Reminder: Complexity (1) Parallel Complexity Theory. Reminder: Complexity (2) Complexity-new Reminder: Complexity (1) Parallel Complexity Theory Lecture 6 Number of steps or memory units required to compute some result In terms of input size Using a single processor O(1) says that regardless of

More information

Reminder: Complexity (1) Parallel Complexity Theory. Reminder: Complexity (2) Complexity-new GAP (2) Graph Accessibility Problem (GAP) (1)

Reminder: Complexity (1) Parallel Complexity Theory. Reminder: Complexity (2) Complexity-new GAP (2) Graph Accessibility Problem (GAP) (1) Reminder: Complexity (1) Parallel Complexity Theory Lecture 6 Number of steps or memory units required to compute some result In terms of input size Using a single processor O(1) says that regardless of

More information

Module 2 Stacks and Queues: Abstract Data Types

Module 2 Stacks and Queues: Abstract Data Types Module 2 Stacks and Queues: Abstract Data Types A stack is one of the most important and useful non-primitive linear data structure in computer science. It is an ordered collection of items into which

More information

6.3 Conditional Probability and Independence

6.3 Conditional Probability and Independence 222 CHAPTER 6. PROBABILITY 6.3 Conditional Probability and Independence Conditional Probability Two cubical dice each have a triangle painted on one side, a circle painted on two sides and a square painted

More information

Mathematical Induction

Mathematical Induction Mathematical Induction In logic, we often want to prove that every member of an infinite set has some feature. E.g., we would like to show: N 1 : is a number 1 : has the feature Φ ( x)(n 1 x! 1 x) How

More information

COMP 250 Fall 2012 lecture 2 binary representations Sept. 11, 2012

COMP 250 Fall 2012 lecture 2 binary representations Sept. 11, 2012 Binary numbers The reason humans represent numbers using decimal (the ten digits from 0,1,... 9) is that we have ten fingers. There is no other reason than that. There is nothing special otherwise about

More information

Midterm Practice Problems

Midterm Practice Problems 6.042/8.062J Mathematics for Computer Science October 2, 200 Tom Leighton, Marten van Dijk, and Brooke Cowan Midterm Practice Problems Problem. [0 points] In problem set you showed that the nand operator

More information

Algorithm Analysis [2]: if-else statements, recursive algorithms. COSC 2011, Winter 2004, Section N Instructor: N. Vlajic

Algorithm Analysis [2]: if-else statements, recursive algorithms. COSC 2011, Winter 2004, Section N Instructor: N. Vlajic 1 Algorithm Analysis []: if-else statements, recursive algorithms COSC 011, Winter 004, Section N Instructor: N. Vlajic Algorithm Analysis for-loop Running Time The running time of a simple loop for (int

More information

Guessing Game: NP-Complete?

Guessing Game: NP-Complete? Guessing Game: NP-Complete? 1. LONGEST-PATH: Given a graph G = (V, E), does there exists a simple path of length at least k edges? YES 2. SHORTEST-PATH: Given a graph G = (V, E), does there exists a simple

More information

14.1 Rent-or-buy problem

14.1 Rent-or-buy problem CS787: Advanced Algorithms Lecture 14: Online algorithms We now shift focus to a different kind of algorithmic problem where we need to perform some optimization without knowing the input in advance. Algorithms

More information

6. Standard Algorithms

6. Standard Algorithms 6. Standard Algorithms The algorithms we will examine perform Searching and Sorting. 6.1 Searching Algorithms Two algorithms will be studied. These are: 6.1.1. inear Search The inear Search The Binary

More information

Efficient Recovery of Secrets

Efficient Recovery of Secrets Efficient Recovery of Secrets Marcel Fernandez Miguel Soriano, IEEE Senior Member Department of Telematics Engineering. Universitat Politècnica de Catalunya. C/ Jordi Girona 1 i 3. Campus Nord, Mod C3,

More information

To My Parents -Laxmi and Modaiah. To My Family Members. To My Friends. To IIT Bombay. To All Hard Workers

To My Parents -Laxmi and Modaiah. To My Family Members. To My Friends. To IIT Bombay. To All Hard Workers To My Parents -Laxmi and Modaiah To My Family Members To My Friends To IIT Bombay To All Hard Workers Copyright 2010 by CareerMonk.com All rights reserved. Designed by Narasimha Karumanchi Printed in

More information

6.852: Distributed Algorithms Fall, 2009. Class 2

6.852: Distributed Algorithms Fall, 2009. Class 2 .8: Distributed Algorithms Fall, 009 Class Today s plan Leader election in a synchronous ring: Lower bound for comparison-based algorithms. Basic computation in general synchronous networks: Leader election

More information

1 Solving LPs: The Simplex Algorithm of George Dantzig

1 Solving LPs: The Simplex Algorithm of George Dantzig Solving LPs: The Simplex Algorithm of George Dantzig. Simplex Pivoting: Dictionary Format We illustrate a general solution procedure, called the simplex algorithm, by implementing it on a very simple example.

More information

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2). CHAPTER 5 The Tree Data Model There are many situations in which information has a hierarchical or nested structure like that found in family trees or organization charts. The abstraction that models hierarchical

More information

Lecture 22: November 10

Lecture 22: November 10 CS271 Randomness & Computation Fall 2011 Lecture 22: November 10 Lecturer: Alistair Sinclair Based on scribe notes by Rafael Frongillo Disclaimer: These notes have not been subjected to the usual scrutiny

More information