Lecture 14 Jianjun Hu Department of Computer Science and Engineering University of South Carolina 2009.10. CSCE350 Algorithms and Data Structure.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

Lecture 9 : Balanced Search Trees Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
COP 3502: Computer Science I (Note Set #21) Page 1 © Mark Llewellyn COP 3502: Computer Science I Spring 2004 – Note Set 21 – Balancing Binary Trees School.
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.
Chapter 4: Trees Part II - AVL Tree
Solution of Assignment 3 and Midterm CSC2100B. AVL Tree A binary search tree is a binary tree in which every node has larger key than the nodes in its.
Transform and Conquer Chapter 6. Transform and Conquer Solve problem by transforming into: a more convenient instance of the same problem (instance simplification)
Anany Levitin ACM SIGCSE 1999SIG. Outline Introduction Four General Design Techniques A Test of Generality Further Refinements Conclusion.
BY Lecturer: Aisha Dawood. Heapsort  O(n log n) worst case like merge sort.  Sorts in place like insertion sort.  Combines the best of both algorithms.
Sorting Comparison-based algorithm review –You should know most of the algorithms –We will concentrate on their analyses –Special emphasis: Heapsort Lower.
Time Complexity of Basic BST Operations Search, Insert, Delete – These operations visit the nodes along a root-to- leaf path – The number of nodes encountered.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Transform & Conquer Replacing One Problem With Another Saadiq Moolla.
Chapter 6: Transform and Conquer
Design and Analysis of Algorithms - Chapter 61 Transform and Conquer Solve problem by transforming into: b a more convenient instance of the same problem.
Lecture 10 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Transform & Conquer Lecture 08 ITS033 – Programming & Algorithms
COSC 3100 Transform and Conquer
Spring 2015 Lecture 5: QuickSort & Selection
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
MA/CSSE 473 Day 25 Transform and Conquer Student questions?
Lecture 8 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Design and Analysis of Algorithms – Chapter 61 Transform and Conquer Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
Analysis of Algorithms CS 477/677
TCSS 343, version 1.1 Algorithms, Design and Analysis Transform and Conquer Algorithms Presorting HeapSort.
2 -1 Analysis of algorithms Best case: easiest Worst case Average case: hardest.
Course Review COMP171 Spring Hashing / Slide 2 Elementary Data Structures * Linked lists n Types: singular, doubly, circular n Operations: insert,
B-Trees Disk Storage What is a multiway tree? What is a B-tree?
Design and Analysis of Algorithms - Chapter 61 Transform and Conquer Solve problem by transforming into: b a more convenient instance of the same problem.
1 B-Trees Disk Storage What is a multiway tree? What is a B-tree? Why B-trees? Comparing B-trees and AVL-trees Searching a B-tree Insertion in a B-tree.
Chapter 6 Transform-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
B-Trees and B+-Trees Disk Storage What is a multiway tree?
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.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
Important Problem Types and Fundamental Data Structures
Theory of Algorithms: Transform and Conquer James Gain and Edwin Blake {jgain | Department of Computer Science University of Cape.
CSCE350 Algorithms and Data Structure Lecture 17 Jianjun Hu Department of Computer Science and Engineering University of South Carolina
CSC401: Analysis of Algorithms CSC401 – Analysis of Algorithms Chapter 5 – 2. Divide-and-Conquer Objectives: Introduce the Divide-and-conquer paradigm.
Chapter 6 Transform-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
1 CSC 421: Algorithm Design Analysis Spring 2014 Transform & conquer  transform-and-conquer approach  presorting  balanced search trees, heaps  Horner's.
CS Data Structures Chapter 10 Search Structures.
September 29, Algorithms and Data Structures Lecture V Simonas Šaltenis Aalborg University
Computer Sciences Department1. Sorting algorithm 3 Chapter 6 3Computer Sciences Department Sorting algorithm 1  insertion sort Sorting algorithm 2.
Transform and Conquer This group of techniques solves a problem by a transformation to a simpler/more convenient instance of the same problem (instance.
Sorting. Pseudocode of Insertion Sort Insertion Sort To sort array A[0..n-1], sort A[0..n-2] recursively and then insert A[n-1] in its proper place among.
CSC 211 Data Structures Lecture 13
MA/CSSE 473 Day 21 AVL Tree Maximum height 2-3 Trees Student questions?
CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.
CS 206 Introduction to Computer Science II 04 / 22 / 2009 Instructor: Michael Eckmann.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Lecture 15 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Internal and External Sorting External Searching
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Chapter 4, Part II Sorting Algorithms. 2 Heap Details A heap is a tree structure where for each subtree the value stored at the root is larger than all.
1 CSC 421: Algorithm Design Analysis Spring 2013 Transform & conquer  transform-and-conquer approach  presorting  balanced search trees, heaps  Horner's.
Chapter 6 Transform-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 6 Transform-and-Conquer
AVL DEFINITION An AVL tree is a binary search tree in which the balance factor of every node, which is defined as the difference between the heights of.
Analysis & Design of Algorithms (CSCE 321)
Chapter 6 Transform and Conquer.
Transform and Conquer This group of techniques solves a problem by a transformation to a simpler/more convenient instance [eg sorted] of the same problem.
Transform and Conquer This group of techniques solves a problem by a transformation to a simpler/more convenient instance of the same problem (instance.
Lecture 3 / 4 Algorithm Analysis
Chapter 6: Transform and Conquer
Transform and Conquer This group of techniques solves a problem by a transformation to a simpler/more convenient instance of the same problem (instance.
Transform and Conquer Transform and Conquer Transform and Conquer.
Transform and Conquer Transform and Conquer Transform and Conquer.
CSC 380: Design and Analysis of Algorithms
Presentation transcript:

Lecture 14 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure

Announcements Midterm 2 – Thursday Oct 29 in class As in Midterm 1, you will be allowed to bring in one single-side letter-size cheat sheet. For the good cheat sheets, we will give you bonus points – 5 points

Transform and Conquer Solve problem by transforming into: a more convenient instance of the same problem (instance simplification) presorting Gaussian elimination a different representation of the same instance (representation change) balanced search trees heaps and heapsort polynomial evaluation by Horner’s rule Fast Fourier Transform a different problem altogether (problem reduction) reductions to graph problems linear programming

Instance Simplification - Presorting Solve instance of problem by transforming into another simpler/easier instance of the same problem Presorting: Many problems involving lists are easier when list is sorted. searching computing the median (selection problem) computing the mode finding repeated elements

Selection Problem Find the k th smallest element in A[1],…A[n]. Special cases: minimum: k = 1 maximum: k = n median: k = n/2 Presorting-based algorithm sort list return A[k] Partition-based algorithm (Variable decrease & conquer): pivot/split at A[s] using partitioning algorithm from quicksort if s=k return A[s] else if s<k repeat with sublist A[s+1],…A[n]. else if s>k repeat with sublist A[1],…A[s-1].

Notes on Selection Problem Presorting-based algorithm: Ω(nlgn) + Θ(1) = Ω(nlgn) Partition-based algorithm (Variable decrease & conquer): worst case: T(n) =T(n-1) + (n+1)  Θ(n 2 ) best case: Θ(n) average case: T(n) =T(n/2) + (n+1)  Θ(n) Bonus: also identifies the k smallest elements (not just the k th ) Special cases of max, min: better, simpler linear algorithm (brute force) Conclusion: Presorting does not help in this case.

Finding Repeated Elements Presorting-based algorithm: use mergesort (optimal): Θ(nlgn) scan array to find repeated adjacent elements: Θ(n) Brute force algorithm: Θ(n 2 ) Conclusion: Presorting yields significant improvement Θ(nlgn)

Computing A Mode A mode is a value that occurs most often in a given list of numbers For example: the mode of [5, 1, 5, 7, 6, 5, 7] is 5 Brute-force technique: construct a list to record the frequency of each distinct element In each iteration, the i-th element is compared to the stored distinct elements. If a matching is found, its frequency is incremented by 1. Otherwise, current element is added to the list as a distinct element Θ(n 2 ), when all the given n elements are distinctWorst case complexity Θ(n 2 ), when all the given n elements are distinct

Computing A Mode With Presorting Algorithm

Complexity of PresortMode() The dominating part is on the sorting Θ(nlgn) The complexity of the outer while loop is linear since each element will be visited once for comparison Therefore, the complexity of presortMode is Θ(nlgn) Θ(n 2 ) This is much more efficient than the brute-force algorithm that needs Θ(n 2 )

Searching Problem Brute-force search: worst case Θ(n). Presorted search T(n)=T sort (n)+ T search (n) = Θ(n log n)+ Θ(log n) = Θ(n log n) Therefore, the presorted search is inferior to the brute-force search However, if we are going to repeat the searching in the same list many times, presorted search may be more efficient because the sorting need not be repeated Binary search

Taxonomy of Searching Algorithms Elementary searching algorithms sequential search binary search binary tree search Balanced tree searching AVL trees red-black trees multiway balanced trees (2-3 trees, trees, B trees) Hashing separate chaining open addressing

Balanced Trees: AVL trees For every node, difference in height between left and right subtree is at most 1 An AVL treeNot an AVL tree The number shown above the node is its balance factor, the difference between the heights of the node’s left and right subtrees.

Maintain the Balance of An AVL Tree Insert a new node to a AVL binary search tree may make it unbalanced. The new node is always inserted as a leaf We transform it into a balanced one by rotation operations A rotation in an AVL tree is a local transformation of its subtree rooted at a node whose balance factor has become either +2 or -2 because of the insertion of a new node If there are several such nodes, we rotate the tree rooted at the unbalanced node that is closest to the newly inserted leaf There are four types of rotations and two of them are mirror images of the other two

Four Types of Rotations for Three-Node AVL Trees

General Case: Single R-rotation

Double LR-rotation

Notes on Rotations Rotations can be done in constant time (steps) Rotations not only guarantee that the resulting tree is balanced, but also should preserve the basic property of a binary search tree, i.e., all the keys in the left subtree should be smaller than the root, the root should be smaller than all the keys in the right subtree. The height (h) of an AVL tree with n nodes is bounded by lg n ≤ h ≤ lg (n + 2) average: 1.01 lg n for large n So the operations of searching and insertion in an AVL tree take Θ(logn) in the worst case

Example: Construct an AVL Tree for the List [5, 6, 8, 3, 2, 4, 7]

Continued

Other Trees Balance tree: Red-black trees (height of subtrees is allowed to differ by up to a factor of 2) and splay trees Allow more than one element in a node of a search tree: 2-3 trees, trees and B-trees. Read Section 6.3 for more details.