CSE 5311 Advanced Algorithms

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

CSC 421: Algorithm Design & Analysis
Trees Types and Operations
Median Finding Algorithm
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
1 Sorting Problem: Given a sequence of elements, find a permutation such that the resulting sequence is sorted in some order. We have already seen: –Insertion.
Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances.
Balanced Search Trees CS 3110 Fall Some Search Structures Sorted Arrays –Advantages Search in O(log n) time (binary search) –Disadvantages Need.
Lecture 8 Sorting. Sorting (Chapter 7) We have a list of real numbers. Need to sort the real numbers in increasing order (smallest first). Important points.
Order Statistics. Order statistics Given an input of n values and an integer i, we wish to find the i’th largest value. There are i-1 elements smaller.
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
September 29, Algorithms and Data Structures Lecture V Simonas Šaltenis Aalborg University
Binary Search Tree vs. Balanced Search Tree. Why care about advanced implementations? Same entries, different insertion sequence: 10,20,30,40,50,60,70,
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Binary Search Trees (BST)
Tree Data Structures. Heaps for searching Search in a heap? Search in a heap? Would have to look at root Would have to look at root If search item smaller.
Chapter 6 (cont’) 1 AVL Tree. Search Trees 2 Two standard search trees: Binary Search Trees (non-balanced) All items in left sub-tree are less than root.
Problem Definition Given a set of "n" unordered numbers we want to find the "k th " smallest number. (k is an integer between 1 and n).
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Divide and Conquer Sorting
Advanced Algorithms Analysis and Design
CSCE 3110 Data Structures & Algorithm Analysis
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Order Statistics.
CSC 421: Algorithm Design & Analysis
CSC 421: Algorithm Design & Analysis
Median Finding Algorithm
COSC160: Data Structures Binary Trees
Sections 8.7 – 8.8 Balancing a Binary Search Tree.
CSC 421: Algorithm Design & Analysis
Randomized Algorithms
COSC160: Data Structures Linked Lists
Divide-And-Conquer-And-Combine
Quick-Sort 9/13/2018 1:15 AM Quick-Sort     2
ITEC 2620M Introduction to Data Structures
Divide and Conquer.
Quicksort 1.
CS 3343: Analysis of Algorithms
Chapter 4: Divide and Conquer
Searching.
Quick Sort (11.2) CSE 2011 Winter November 2018.
Search Sorted Array: Binary Search Linked List: Linear Search
Randomized Algorithms
B-Tree Insertions, Intro to Heaps
Data Structures Review Session
Lecture 3 / 4 Algorithm Analysis
Topic: Divide and Conquer
Searching CLRS, Sections 9.1 – 9.3.
Data Structures Lecture 30 Sohail Aslam.
EE 312 Software Design and Implementation I
Quicksort.
Order Statistics Def: Let A be an ordered set containing n elements. The i-th order statistic is the i-th smallest element. Minimum: 1st order statistic.
CSC 421: Algorithm Design & Analysis
Topic 6: Binary Search Tree Data structure Operations
CSC 143 Binary Search Trees.
Quick-Sort 4/25/2019 8:10 AM Quick-Sort     2
Topic: Divide and Conquer
Algorithms CSCI 235, Spring 2019 Lecture 20 Order Statistics II
Quicksort.
AVL Tree Chapter 6 (cont’).
Search Sorted Array: Binary Search Linked List: Linear Search
The Selection Problem.
Design and Analysis of Algorithms
CSC 421: Algorithm Design & Analysis
Analysis of Algorithms CS 477/677
Divide and Conquer Merge sort and quick sort Binary search
Quicksort.
Medians and Order Statistics
Divide-and-Conquer 7 2  9 4   2   4   7
Presentation transcript:

CSE 5311 Advanced Algorithms Instructor: Dr. Gautam Das Week 3 Class Notes Submitted By: Abhishek Hemrajani & Pinky Dewani

CSE 5311 Advanced Algorithms Topics Covered in Week 3: Median Finding Algorithm (Description and Analysis) Binary Search Tree

CSE 5311 Advanced Algorithms Given a set of “n” numbers we can say that, Mean: Average of the “n” numbers Median: Having sorted the “n” numbers, the value which lies in the middle of the list such that half the numbers are higher than it and half the numbers are lower than it. Thus the problem of finding the median can be generalized to finding the kth largest number where k = n/2.

CSE 5311 Advanced Algorithms kth largest number can be found using: Scan Approach with a time complexity T(n) = kn Sort Approach with a time complexity T(n) = nlogn

CSE 5311 Advanced Algorithms Linear Time Median Finding Algorithm Input: S = {a1,a2,…….,an} k such that 1 ≤ k ≤ n Output: kth largest number Algorithm: kth_largest (S, k)

CSE 5311 Advanced Algorithms Steps: Group the numbers into sets of 5 Sort individual groups and find the median of each group Let “M” be set of medians and find median of “M” using MedianOfMedian (MOM) = kth_largest(M,M/2) Partition original data around the MOM such that values less than it are in set “L” and values greater than it are in set “R”

CSE 5311 Advanced Algorithms Steps Continued: 5) If |L| = k-1, then return MOM else If |L| > k-1, then return kth_largest(L,k) else return kth_largest(R,k-|L|)

CSE 5311 Advanced Algorithms Example: (……..2,5,9,19,24,54,5,87,9,10,44,32,21,13,24,18,26,16,19,25,39,47,56,71,91,61,44,28………) is a set of “n” numbers

CSE 5311 Advanced Algorithms Step1: Group numbers in sets of 5 (Vertically) 2 2 54 54 44 44 4 4 25 25 ……………….. ……………….. ……………….. ……………….. ……………….. ……………….. 5 5 32 32 18 18 39 39 ……………….. 5 5 ……………….. ……………….. 47 47 21 21 26 26 9 9 87 87 ……………….. 13 13 16 16 56 56 19 19 9 9 ……………….. ……………….. ……………….. ……………….. 2 2 19 19 71 71 ……………….. ……………….. 24 24 10 10 ……………….. ………………..

CSE 5311 Advanced Algorithms Step2: Find Median of each group 2 5 2 4 25 ……………….. ……………….. ……………….. 5 13 16 39 ……………….. 9 ……………….. 9 10 18 47 21 ……………….. 32 19 56 19 54 ……………….. ……………….. 44 26 71 ……………….. 24 87 ……………….. Median of each group

CSE 5311 Advanced Algorithms Step3: Find the MedianOfMedians 2 5 2 4 25 ……………….. ……………….. 3.n/10 ……………….. 5 13 16 39 ……………….. 9 ……………….. 47 18 9 10 21 ……………….. 32 19 56 19 54 ……………….. ……………….. 44 26 71 ……………….. 24 87 ……………….. Find m ,the median of medians

CSE 5311 Advanced Algorithms Step4: Partition original data around the MOM M L R 3n/10<L<7n/10 3n/10<R<7n/10 Step5: If |L| = k-1, then return MOM else If |L| > k-1, then return kth_largest(L,k) else return kth_largest(R,k-|L|)

CSE 5311 Advanced Algorithms Time Analysis: Step Task Complexity 1 Group into sets of 5 O (n) 2 Find Median of each group 3 Find MOM T (n/5) 4 Partition around MOM 5 Condition T (7n/10) {Worst Case}

CSE 5311 Advanced Algorithms Time Complexity of Algorithm: T(n) = O (n) + T (n/5) + T (7n/10) T(1) = 1 Assume T (n) ≤ Cn (For it to be linear time) L.H.S = Cn R.H.S = C1n + Cn/5 + 7Cn/10 = (C1 + 9/10C)n Hence, L.H.S = R.H.S if C = 10C1 Thus it is a Linear Time Algorithm

CSE 5311 Advanced Algorithms Why sets of 5? Assuming sets of 3, T(n) = O (n) + T (n/3) + T (2n/3) T(1) = 1 Assume T (n) ≤ Cn (For it to be linear time) L.H.S = Cn R.H.S = C1n + Cn/3 + 2Cn/3 = (C1 + C) n Hence, L.H.S = R.H.S if C1 = 0 Thus this is invalid assumption. Hence we do not use groups of 3!

CSE 5311 Advanced Algorithms Assuming sets of 7, T(n) = O (n) + T (n/7) + T (5n/7) T(1) = 1 Assume T (n) ≤ Cn (For it to be linear time) L.H.S = Cn R.H.S = C1n + Cn/7 + 5Cn/7 = (C1 + 6C/7) n Hence, L.H.S = R.H.S if C = 7C1 However, constant factor of O (n) term increases to a sub-optimal value as size of set increases!

CSE 5311 Advanced Algorithms Points to remember: This algorithm uses Divide and Conquer strategy The technique is Recursion Most realistic approaches randomly select the MOM

CSE 5311 Advanced Algorithms Maintenance of Dynamic Sets: Given a Set “S” and a value “X”, we need to perform the following three operations: Insert (X, S) Delete (X,S) Find (X, S) Example: Customer Database, Yellow Pages etc.

CSE 5311 Advanced Algorithms Data Structures used for Dynamic Sets: Operation Unordered Array Sorted Array Unordered List Goal Insert (X, S) O (1) O (n) O (logn) Delete (X,S) Find (X, S)

CSE 5311 Advanced Algorithms Binary Search Tree: A Binary Search Tree is a data structure such that at any node, the values in the left subtree are smaller than the node and the values in the right subtree are greater than the node. Example: Given S = {3, 2, 5, 4, 7, 6, 1}, the Binary Search Tree is: 3 2 5 1 7 4 6

CSE 5311 Advanced Algorithms All operations performed on the Binary Search Tree are of the order O (path from root to leaf) which can be O (logn) in a favorable case or O (n) in the worst case (Sorted Input).

CSE 5311 Advanced Algorithms Sorting using In-Order Traversal: Binary Search Tree can be sorted using In-Order Traversal as follows: Sort (T) { Sort (LeftChild (T)) Write (T) Sort (RightChild (T)) } Here, T (n) = T (m) + T (n-m-1) + O (1)

CSE 5311 Advanced Algorithms For a Balanced Binary Search Tree: T(n) = T (L.H.S) + T (R.H.S) + O (1) = 2 T (n/2) + 1 = O (n)

CSE 5311 Advanced Algorithms Points to remember: Pointers do not give advantage of Random Access but make the data structure dynamic. B Tree and B+ Tree are optimizations of Binary Search Tree to give guaranteed O (logn) performance.