Algorithms 2005 Ramesh Hariharan. Divide and Conquer+Recursion Compact and Precise Algorithm Description.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

CSC 421: Algorithm Design & Analysis
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
Transform and Conquer Chapter 6. Transform and Conquer Solve problem by transforming into: a more convenient instance of the same problem (instance simplification)
More Graphs COL 106 Slides from Naveen. Some Terminology for Graph Search A vertex is white if it is undiscovered A vertex is gray if it has been discovered.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Introduction to Algorithms
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Quicksort CSE 331 Section 2 James Daly. Review: Merge Sort Basic idea: split the list into two parts, sort both parts, then merge the two lists
Data Structures Data Structures Topic #13. Today’s Agenda Sorting Algorithms: Recursive –mergesort –quicksort As we learn about each sorting algorithm,
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Course Review COMP171 Spring Hashing / Slide 2 Elementary Data Structures * Linked lists n Types: singular, doubly, circular n Operations: insert,
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
David Luebke 1 7/2/2015 Medians and Order Statistics Structures for Dynamic Sets.
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
Balanced Search Trees CS 3110 Fall Some Search Structures Sorted Arrays –Advantages Search in O(log n) time (binary search) –Disadvantages Need.
Important Problem Types and Fundamental Data Structures
Binary Trees Chapter 6.
Sorting and Asymptotic Complexity
Algorithms 2005 Ramesh Hariharan. Amortization in Dynamic Algorithms A single insertion/deletion might take say O(log n) time Does a sequence of n insertions.
Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
INTRODUCTION TO MULTIWAY TREES P INTRO - Binary Trees are useful for quick retrieval of items stored in the tree (using linked list) - often,
IT 60101: Lecture #151 Foundation of Computing Systems Lecture 15 Searching Algorithms.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Computer Algorithms Submitted by: Rishi Jethwa Suvarna Angal.
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.
Starting at Binary Trees
Prepared by- Jatinder Paul, Shraddha Rumade
Data Structures Types of Data Structure Data Structure Operations Examples Choosing Data Structures Data Structures in Alice.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 7.
+ David Kauchak cs312 Review. + Midterm Will be posted online this afternoon You will have 2 hours to take it watch your time! if you get stuck on a problem,
HEAPS Amihood Amir Bar Ilan University Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
Lecture 9COMPSCI.220.FS.T Lower Bound for Sorting Complexity Each algorithm that sorts by comparing only pairs of elements must use at least 
Lecture 14 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Sorting Fundamental Data Structures and Algorithms Aleks Nanevski February 17, 2004.
1 Ch.19 Divide and Conquer. 2 BIRD’S-EYE VIEW Divide and conquer algorithms Decompose a problem instance into several smaller independent instances May.
CS 367 Introduction to Data Structures Lecture 11.
SORTING AND ASYMPTOTIC COMPLEXITY Lecture 13 CS2110 – Fall 2009.
Polygon Triangulation
Advanced Sorting 7 2  9 4   2   4   7
COMP261 Lecture 23 B Trees.
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
Top 50 Data Structures Interview Questions
CSC 421: Algorithm Design & Analysis
CSC 421: Algorithm Design & Analysis
Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008
CSC 421: Algorithm Design & Analysis
Week 11 - Friday CS221.
Chapter 6 Transform and Conquer.
Wednesday, April 18, 2018 Announcements… For Today…
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
CSC 421: Algorithm Design & Analysis
Sorting And Searching CSE116A,B 4/7/2019 B.Ramamurthy.
Important Problem Types and Fundamental Data Structures
CSC 421: Algorithm Design & Analysis
Data Structures Using C++ 2E
Presentation transcript:

Algorithms 2005 Ramesh Hariharan

Divide and Conquer+Recursion Compact and Precise Algorithm Description

Min Finding Recursively find Minimum on the first n-1 items Compare the above min with the nth item; return the smaller of the two T(n)=T(n-1)+1 T(1)=1

Sorting Recursively sort first n-1 items Insert the last item into the above order Use sequential search with item shifting to insert Use binary search? T(n)=T(n-1)+O(n) T(1)=1

Sorting Recursively sort first n/2 and last n/2 items Merge the two sets. T(n)=2*T(n/2)+O(n) T(1)=1

Merging Find the smaller of the A[0] and B[0]; wlog, it is in A[0]. Return A[0] followed by the recursively merge of A[1..n-1] and B[0..m] T(n,m)=max(T(n-1,m),T(n,m-1))+O(1) T(n,0)=n,T[0,m]=m

Parallel Merging Recursively Merge odds in A with odds in B For each item A[i], i even, binary search between the merged locations of A[i-1] and A[i+1]; do likewise for B[i], i even. T(n,m)=T(n/2,m/2)+O(logn) T(n,0)=1,T[0,m]=1

Parallel Merging Two Steps Compute final index j in the merged array for each A[i] and likewise for B[i] j=rank in A + rank in B find rank in B using the algo on the previous slide Move A[i] to C[j]; likewise for B[i].

Balanced Tree Construction N Sorted Items Make the middle item the root Recursively construct subtrees on the left and right halves; make these children of the root. T(n)=2*T(n/2)+O(1) T(1)=O(1)

Weighted Balanced Tree Construction N Sorted Weighted Items Identify the “middle item” such that sum of weights either side are as balanced as possible, using a binary search. Make the middle item the root Recursively construct subtrees on the left and right halves; make these children of the root. T(n)=max x (T(x)+T(n-x)+O(log n)) T(1)=O(1)

Weighted Balanced Tree Construction N Sorted Weighted Items Identify the “middle item” such that sum of weights either side are as balanced as possible, using doubling search from both ends T(n)=max x<n/2 (T(x)+T(n-x)+O(log x)) T(1)=O(1) How much is this?

Median Finding Generalize to finding a rank r item. Partition based on a pivot; suppose this results in a x n-x split. If r>x then recursively find the r-x th item in the right half else recursively find the r th item in the left half. T(n)=max y (T(y))+n O(n) is y is a constant fraction of n.

Median Finding How do we find a more balanced split pivot fast. Split into groups, find median in each group and find the median of these medians

Median Finding Time taken for groups of size k What should k be. T(n) = T(n/k) + T(3n/4) + O(n)

Randomized Median Finding Choose a random pivot Size sequence n X 1 X 2 X 3 X 4…….. Time = O(n+ X 1 + X 2 + X 3 + X 4.. ) E(n+ X 1 + X 2 + X 3 + X 4.. )= E(n)+ E(X 1 ) + E(X 2 ) + E(X 3 ) + E(X 4 )… ….

Crossing Lists and Arrays Arrays support binary search but insertions are expensive. Lists support quick insertions but searches are expensive. How can one cross the two? Fast search and insertion. AVL trees, Red-Black Trees: elaborate balancing conditions and cases

The Hybrid Sampling and Fractional Cascading Assign levels to items Give all items level 0 Increment level of odd items; then recurse on these. Each item A[i] at level j has pointers to the highest below-level-j items to the left and right. Height O(log n)

Searching the Hybrid Start at the top and search downwards. Suppose the search has been narrowed to between A[i] (level x) and A[j] (level y<x) There is exactly one item A[k] between A[i] and A[j] at level y-1. Given A[j], A[k] can be identified in constant time One comparison can narrow the search down to the range A[i]..A[k] or A[j]..A[k] Level reduces by 1, Recurse; Timing O(log n) A[i] A[j] A[k]

Inserting into the Hybrid Search and insert at the appropriate place at level 0. Invariant Fails: There are more than one A[k]s between A[i] and A[j] at level y-1.

Inserting into the Hybrid Relax the invariant: Allow 1 or 2 such A[k]s Search and insert at the appropriate place at level 0. If this creates more than 2 (i.e., 3) consecutive red nodes at a particular level, increase the level number of the middle node, and recurse upwards. Timing O(log n) Data struturally, what links are maintained?? Deletion??

DFS in Graphs Aim: To visit each node and walk on each edge at least once, starting at node s. Visit s Visit a neighbour t of s and recurse from t: Incomplete Coverage For each neighbour t of s, visit t and then recurse from t. Infinite Loop Mark a vertex as visited when it is visited for the first time; recurse only at unmarked (i.e., newly visited) vertices. Mark s For each neighbour t of s {If t is visited for the first time { Mark t Recurse from t }

BFS in Graphs Aim: To visit each node and walk on each edge at least once, starting at node s. Nodes must be visited in increasing order of distance from s. Inductive Invariant: Nodes with distances at most i from s have been visited. X i : nodes at distance i from s. Neighbourhood(X i ): neighbours of nodes in X i X i+1: Neighbourhood(X i ) – already visited nodes Given X 1..X i, Compute X i+1. and recurse on X 1..X i+1.

BFS Implementation Use a queue. Initialize queue to {s} Mark s as visited while queue not empty { x=dequeue Enqueue and mark each unmarked neighbour of x }

Closest Pair Given n points on a plane, find the closest pair Choose an arbitrary direction. Sort points along that direction. Split into halves and find closest pair in each half. Find closest pair straddling halves. How is this done in linear time?

Fractional Cascading Question Given downward monotonic chains comprising n points in all, show how to set up a data struture which will perform point location (which two chains does it lie between?) fast.

Thank You