CS 583 Analysis of Algorithms

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Jan Binary Search Trees What is a search binary tree? Inorder search of a binary search tree Find Min & Max Predecessor and successor BST insertion.
Analysis of Algorithms CS 477/677 Binary Search Trees Instructor: George Bebis (Appendix B5.2, Chapter 12)
Binary Search Trees Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Introduction to Algorithms Jiafen Liu Sept
CS 332: Algorithms Binary Search Trees. Review: Dynamic Sets ● Next few lectures will focus on data structures rather than straight algorithms ● In particular,
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture six Dr. Hamdy M. Mousa.
Chapter 12 Binary search trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from.
David Luebke 1 5/4/2015 Binary Search Trees. David Luebke 2 5/4/2015 Dynamic Sets ● Want a data structure for dynamic sets ■ Elements have a key and satellite.
Binary Search Trees Comp 550.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 11.
Sorting. How fast can we sort? All the sorting algorithms we have seen so far are comparison sorts: only use comparisons to determine the relative order.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
10.Elementary data structures Hsu, Lih-Hsing. Computer Theory Lab. Chapter 11P Stacks and queues Stacks and queues are dynamic set in which element.
1.1 Data Structure and Algorithm Lecture 12 Binary Search Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Binary Search Trees.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE Binary search trees Motivation Operations on binary search trees: –Search –Minimum,
David Luebke 1 7/2/2015 ITCS 6114 Binary Search Trees.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Design & Analysis of Algorithms Unit 2 ADVANCED DATA STRUCTURE.
David Luebke 1 9/18/2015 CS 332: Algorithms Red-Black Trees.
Chapter 12. Binary Search Trees. Search Trees Data structures that support many dynamic-set operations. Can be used both as a dictionary and as a priority.
Elementary Data Structures Data Structures and Algorithms A. G. Malamos.
2IL50 Data Structures Fall 2015 Lecture 7: Binary Search Trees.
Binary SearchTrees [CLRS] – Chap 12. What is a binary tree ? A binary tree is a linked data structure in which each node is an object that contains following.
CS 206 Introduction to Computer Science II 10 / 05 / 2009 Instructor: Michael Eckmann.
Binary Search Tree Qamar Abbas.
CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann.
October 3, Algorithms and Data Structures Lecture VII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Lecture 9 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
1 Algorithms CSCI 235, Fall 2015 Lecture 22 Binary Search Trees.
Red-Black Trees. Review: Binary Search Trees ● Binary Search Trees (BSTs) are an important data structure for dynamic sets ● In addition to satellite.
Binary Search Trees (BST)
The Linked List (LL) Each node x in a linked list contains: Key(x) head key[x]- The value stored at x. next[x]- Pointer to left child of x. The while list.
Lecture 19. Binary Search Tree 1. Recap Tree is a non linear data structure to present data in hierarchical form. It is also called acyclic data structure.
Lecture 91 Data Structures, Algorithms & Complexity Insertion and Deletion in BST GRIFFITH COLLEGE DUBLIN.
CSE 2331/5331 Topic 8: Binary Search Tree Data structure Operations.
Chapter 10 Elementary data structures Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials.
CSC317 1 Binary Search Trees (binary because branches either to left or to right) Operations: search min max predecessor successor. Costs? Time O(h) with.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
Binary Search Trees What is a binary search tree?
DAST Tirgul 7.
Binary Search Trees.
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
CPSC 311 Section 502 Analysis of Algorithm
Data Structures Review Session 2
CS200: Algorithms Analysis
Lecture 7 Algorithm Analysis
Ch. 12: Binary Search Trees Ming-Te Chi
Elementary Data Structures
Algorithms Part III. Data Structures
Ch. 12: Binary Search Trees Ming-Te Chi
Ch. 12: Binary Search Trees
Binary Search Trees (13.1/12.1)
Lecture 7 Algorithm Analysis
Algorithms and Data Structures Lecture VII
Chapter 12: Binary Search Trees
CS 583 Analysis of Algorithms
CS6045: Advanced Algorithms
CS 583 Analysis of Algorithms
Lecture 7 Algorithm Analysis
Topic 6: Binary Search Tree Data structure Operations
Binary SearchTrees [CLRS] – Chap 12.
Design and Analysis of Algorithms
Analysis of Algorithms CS 477/677
Binary Search Trees Comp 122, Spring 2004.
Chapter 12&13: Binary Search Trees (BSTs)
Algorithms CSCI 235, Spring 2019 Lecture 21 Binary Search Trees
Presentation transcript:

CS 583 Analysis of Algorithms Basic Data Structures CS 583 Analysis of Algorithms 9/19/2018 CS583 Fall'06: Basic Data Structures

CS583 Fall'06: Basic Data Structures Outline HW1 analysis Basic Data Structures Stacks Queues Linked lists Rooted trees Binary Search Trees Tree walk Querying 9/19/2018 CS583 Fall'06: Basic Data Structures

CS583 Fall'06: Basic Data Structures HW1 HW1 answers are posted on the web site Check results by e-mail to the TA, or with the instructor in the class The grade can be changed within 1 week, by October 9. Submitting HW in the future: Attach the submission in a separate file, and double-check that it’s not empty. Prefix the file name with the student’s last name, e.g. JonesHw1.doc. Indent a pseudocode properly. 9/19/2018 CS583 Fall'06: Basic Data Structures

CS583 Fall'06: Basic Data Structures Stacks Stack is a dynamic data set in which the element deleted from the set is the latest inserted: last-in-first-out, or LIFO policy. The insert operation on stack is often called PUSH The delete operation is often called POP. A stack can be implemented by an array S[1..n] with an attribute top[S] that indexes the most recently inserted element. When top[S] = 0, the stack is empty. If an empty stack is popped, we say the stack underflows. When top[S] > n, the stack overflows. 9/19/2018 CS583 Fall'06: Basic Data Structures

CS583 Fall'06: Basic Data Structures Stack Algorithms Stack-Empty(S) 1 if top[S] = 0 2 return TRUE 3 else 4 return FALSE   Push(S, x) 1 top[S]++ 2 S[top[S]] = x Pop(S) 1 if Stack-Empty(S) 2 throw "underflow" 4 top[S]— 5 return S[top[S]+1] Each of above algorithms runs in O(1) time. 9/19/2018 CS583 Fall'06: Basic Data Structures

CS583 Fall'06: Basic Data Structures Queues The queue implements a first-in-first-out, or FIFO policy. The insert operation on a queue is called ENQUEUE The delete operation is called DEQUEUE. A queue of at most (n-1) elements can be implemented using an array Q[1..n]. The queue has an attribute head[Q] that points to the index of its head. The attribute tail[Q] indexes the next location at which the new element will be inserted. The order is circular, i.e. the location 1 immediately follows the location n. 9/19/2018 CS583 Fall'06: Basic Data Structures

CS583 Fall'06: Basic Data Structures Queues: Example 1 2 3 4 5 13 11 12 Q[1..5] tail head 11 = Dequeue(Q): 13 12 Q[1..5] tail head Enqueue(Q,14): 13 14 12 Q[1..5] tail head 9/19/2018 CS583 Fall'06: Basic Data Structures

CS583 Fall'06: Basic Data Structures Queue Algorithms Enqueue(Q,x) 1 /* head and tail cannot point to the same location,hence only (n-1) elements can be stored */ 2 if tail[Q] + 1 = head[Q] 3 throw "overflow" 4 Q[tail[Q]] = x 5 tail[Q] = tail[Q] % n + 1 // wrap around 9/19/2018 CS583 Fall'06: Basic Data Structures

Queue Algorithms (cont.) Dequeue(Q) 1 if head[Q]=tail[Q] 2 throw "underflow" 3 x = Q[head[Q]] 4 head[Q] = head[Q] % n + 1 // wrap around 5 return x   Both operations run in O(1) time. 9/19/2018 CS583 Fall'06: Basic Data Structures

CS583 Fall'06: Basic Data Structures Linked Lists A linked list is a data structure in which objects are arranged in linear order. Each object points to the location of the next object. In a doubly linked list each object also points to the location of the previous object. More formally, for a doubly linked list L: head[L] points to the first element of the list. Given an element x from L: prev[x] points to its predecessor. next[x] points to its successor. If next[x] = NIL, x is the last element in L. If prev[x] = NIL, x is the first element in L. 9/19/2018 CS583 Fall'06: Basic Data Structures

Linked Lists Algorithms The following algorithm finds the first element with key k in the list L:   List-Search(L,k) 1 x = head[L] 2 while x <> NIL and key[x] <> k 3 x = next[x] 4 return x The above algorithm runs in (n) time in the worst case, since it may have to search the entire list. 9/19/2018 CS583 Fall'06: Basic Data Structures

Linked Lists Algorithms (cont.) The following algorithm inserts an element x with a key set to the front of the list.   List-Insert(L,x) 1 next[x] = head[L] 2 if head[L] <> NIL 3 prev[head[L]] = x 4 head[L] = x 5 prev[x] = NIL The running time of the insert algorithm is O(1). 9/19/2018 CS583 Fall'06: Basic Data Structures

Linked Lists Algorithms (cont.) The following algorithm deletes an element x from the linked list. (It assumes the pointer to x is known, otherwise use List-Search to find x according to some key).   List-Delete(L,x) 1 if prev[x] <> NIL 2 next[prev[x]] = next[x] 3 else 4 head[L] = next[x] 5 if next[x] <> NIL 6 prev[next[x]] = prev[x] This algorithm runs in O(1), however it would require (n) time if it needs to search for an element. 9/19/2018 CS583 Fall'06: Basic Data Structures

CS583 Fall'06: Basic Data Structures Rooted Trees Trees can be represented using linked data structures. For example, in case of binary trees we use fields p, left, and right to store pointers to the parent, left child, and right child of each node of the binary tree T. The root of the tree is root[T] (x: p[x] = NIL).  To represent trees with unbounded branching, we use binary tree representation. In this scheme, each node still contains a parent pointer, and root[T] points to the root of T. Instead of having a pointer for each child of a node, each node x has only two pointers:  left-child[x] points to the leftmost child of node x. right-sibling[x] points to the sibling of x immediately to the right. If node x has no children, then left-child[x] = NIL. If node x is the rightmost child of its parent, then right-sibling[x] = NIL. 9/19/2018 CS583 Fall'06: Basic Data Structures

CS583 Fall'06: Basic Data Structures Binary Search Tree Binary search tree is a binary tree that stores keys and satellite data. The keys are stored in such a way to satisfy the following property:  Let x be a node in a BST. If y is a node in left subtree of x, then key[y] <= key[x]. If y is a node in the right subtree of x, then key[x] <= key[y]. The above property allows us to print out all keys in a BST in sorted order by an inorder tree walk algorithm (left, root, right). Similar algorithms include preorder tree walk (root, left, right), and postorder tree walk (left, right, root). 9/19/2018 CS583 Fall'06: Basic Data Structures

CS583 Fall'06: Basic Data Structures Inorder Tree Walk Inorder-Tree-Walk (x) 1 if x <> NIL 2 Inorder-Tree-Walk(left[x]) 3 print key[x] 4 Inorder-Tree-Walk(right[x]) It takes (n) to walk an n-node BST, since the procedure is called exactly twice for each node of the tree (left and right child) + printing the node. We prove it formally as follows. Theorem 12.1   If x is the root of an n-node subtree, then Inorder-Tree-Walk(x) takes (n) time. 9/19/2018 CS583 Fall'06: Basic Data Structures

Inorder Tree Walk: Performance Proof. On an empty subtree Inorder-Tree-Walk(x) takes a constant time (to compare to NIL), so T(0) = c for some positive constant c.   For n>0, suppose the procedure is called on a node x, whose left subtree has k nodes, and the right subtree has (n-k-1) nodes. The time to perform the algorithm is: T(n) = T(k) + T(n-k-1) + d, where d is time to print x. 9/19/2018 CS583 Fall'06: Basic Data Structures

Inorder Tree Walk: Performance (cont.) We use the substitution method to show that T(n) = (c+d)n + c. For n=0: T(0) = c. For n>0 we have: T(n) = T(k) + T(n-k-1) + d =   ((c+d)k + c) + ((c+d)(n-k-1) + c) + d = ck + dk + c +cn –ck –c + dn –dk –d + c + d = cn + dn + c = (c+d)n + c  9/19/2018 CS583 Fall'06: Basic Data Structures

CS583 Fall'06: Basic Data Structures BST: Querying We use the following procedure to search for a node in BST:   Tree-Search(x,k) Input: root x and key k Output: pointer to a node with key k, or NIL if not found 1 if x = NIL or k=key[x] 2 return x 3 if k < key[x] 4 return Tree-Search(left[x],k) 5 else 6 return Tree-Search(right[x],k) The nodes encountered form a path downward from the root of the tree, hence the running time of search is O(h), where h is the height of the tree. 9/19/2018 CS583 Fall'06: Basic Data Structures

BST: Minimum and Maximum Minimum and maximum elements in BST can be found by following the left, or the right subtree respectively:   Tree-Minimum(x) 1 while left[x] <> NIL 2 x = left[x] 3 return x Tree-Maximum(x) 1 while right[x] <> NIL 2 x = right[x] 9/19/2018 CS583 Fall'06: Basic Data Structures

BST: Finding Successor If all keys are distinct, the successor of the node x is the smallest key greater than key[x]. Tree-Successor(x) 1 if right[x] <> NIL 2 return Tree-Minimum(right[x]) 3 y = parent[x] 4 while y <> NIL and x = right[y] 5 x = y 6 y = parent[y] 7 return y   When the right subtree is empty, the successor of x is the lowest ancestor of x, whose left child is also ancestor of x (lines 3-7). To find such ancestor, we find the first one that is not on the "right subtree" path to x. The running time of the above algorithm is O(h) since we either follow a path down or up from the node. 9/19/2018 CS583 Fall'06: Basic Data Structures