Data Structures and Algorithms I Day 1, 8/30/11

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
© 2004 Goodrich, Tamassia QuickSort1 Quick-Sort     29  9.
Updated QuickSort Problem From a given set of n integers, find the missing integer from 0 to n using O(n) queries of type: “what is bit[j]
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 11: Sorting and Searching  Searching Linear Binary  Sorting.
© 2004 Goodrich, Tamassia Selection1. © 2004 Goodrich, Tamassia Selection2 The Selection Problem Given an integer k and n elements x 1, x 2, …, x n, taken.
Hash Tables1 Part E Hash Tables  
Hash Tables1 Part E Hash Tables  
Selection1. 2 The Selection Problem Given an integer k and n elements x 1, x 2, …, x n, taken from a total order, find the k-th smallest element in this.
Introduction to Computer Science Recursive Array Programming Recursive Sorting Algorithms Unit 16.
CS 2430 Day 28. Announcements We will have class in ULR 111 on Monday Exam 2 next Friday (sample exam will be distributed next week)
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Searching. The process used to find the location of a target among a list of objects Searching an array finds the index of first element in an array containing.
COMP Recursion, Searching, and Selection Yi Hong June 12, 2015.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Quicksort CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
1 Algorithms CSCI 235, Fall 2015 Lecture 19 Order Statistics II.
CMP 338 Data Structures and Algorithms I Day 1, 8/30/11.
Hash Tables 1/28/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Quick-Sort 2/18/2018 3:56 AM Selection Selection.
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Data Structures and Algorithms I Day 9, 9/22/11 Heap Sort
Order Statistics.
Order Statistics Comp 122, Spring 2004.
Introduction to Search Algorithms
CPSC 311 Section 502 Analysis of Algorithm
Randomized Algorithms
Programming in Java: lecture 10
COSC160: Data Structures Linked Lists
Data Structures and Algorithms I
Algorithms CSCI 235, Fall 2017 Lecture 16 Quick Sort Read Ch. 7
Dictionaries 9/14/ :35 AM Hash Tables   4
Hash Tables 3/25/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Teach A level Computing: Algorithms and Data Structures
Searching.
Data Structures and Algorithms
Big-Oh and Execution Time: A Review
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Divide and Conquer Mergesort Quicksort Binary Search Selection
CSC212 Data Structure - Section RS
CO 303 Algorithm Analysis And Design Quicksort
Unit-2 Divide and Conquer
Randomized Algorithms
searching Concept: Linear search Binary search
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
8/04/2009 Many thanks to David Sun for some of the included slides!
Topic: Divide and Conquer
Searching: linear & binary
Order Statistics Comp 550, Spring 2015.
Dictionaries 1/17/2019 7:55 AM Hash Tables   4
ITEC 2620M Introduction to Data Structures
Data Structures and Algorithms I Day 2, 9/1/11
Searching, Sorting, and Asymptotic Complexity
COMPUTER 2430 Object Oriented Programming and Data Structures I
Sorting And Searching CSE116A,B 2/23/2019 B.Ramamurthy.
Sorting And Searching CSE116A,B 4/7/2019 B.Ramamurthy.
Chapter 9: Medians and Order Statistics
CSC212 Data Structure - Section KL
Algorithms CSCI 235, Spring 2019 Lecture 20 Order Statistics II
Algorithms CSCI 235, Spring 2019 Lecture 16 Quick Sort Read Ch. 7
Data Structures & Algorithms
Order Statistics Comp 122, Spring 2004.
Algorithmic complexity
David Kauchak cs161 Summer 2009
Quick-Sort 5/7/2019 6:43 PM Selection Selection.
Quick-Sort 5/25/2019 6:16 PM Selection Selection.
Core Assessments Core #1: This Friday (5/4) Core #2: Tuesday, 5/8.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2017
Dictionaries and Hash Tables
Presentation transcript:

Data Structures and Algorithms I Day 1, 8/30/11 CMP 338 Data Structures and Algorithms I Day 1, 8/30/11

The Course Instructor: Bowen Alpern Texts: Email: alpern@acm.org Office hour: GI 137-I Tuesday 4-5pm (and by appointment) (poor speller, nominal aphasia, limited teaching experience) Texts: Algorithms, fourth edition, Sedgewick and Wayne Booksite: algs4.cs.princeton.edu Format: T, Th 6-7:40pm, room 333 Gillet Lecture (100 min) Grade policy (tentative) Program assignments 40% Exams 40% (4 in class, 5% each, final 20%) Homework 20%

CMP 338 “Algorithms + Data Structures = Programs” - Niklaus Wirth Algorithm – a method for solving a problem Data Structure – a method for storing information Why study algorithms (and data structures)?

I'm thinking of a number ...

I'm thinking of a number ... 0 ≤ x < 1024 = 210

I'm thinking of a number ... 0 ≤ x < 1024 = 210 Queries: is x ≤ y ? (for any integer y)

I'm thinking of a number ... 0 ≤ x < 1024 = 210 Queries: is x ≤ y ? (for any integer y) Algorithm: lo = 0; hi =1023 while (lo < hi) if (x ≤ lo + (lo+hi)/2) then hi = lo + (lo+hi)/2 else lo = lo + (lo+hi)/2 + 1 return lo // or hi

Analysis If 0 ≤ x < N = 2n (n = log2 N ≡ lg N) x has n bits Each query resolves 1 bit Why? Each query consists of a single comparison Therefore: Guessing one of N consecutive integers can be done with ~ lg N comparisons

So What?

So What? Same algorithm tests membership in a sorted list

So What? Same algorithm tests membership in a sorted list boolean member(Item i, List<Item> l) lo = 0; hi = l.length -1 while (lo < hi) int m = lo + (lo+hi)/2 if (0 ≤ i.compareTo(l.get(m))) then hi = m else lo = m + 1 return i.equals(l.get(lo))

Search Binary search Work to look up 1 of N keys ~ lg N comparisons Can we do better? Kabbalah Assign a numeric value to each letter of an alphabet Tries Work to look up key ~ ||key|| Hashing Work to look up key ~ c (average case)

Polynomials Polynomials over x with integer coefficients Operations: +, -, ●, /, %, evaluate, … First implementation: array of ints p[i] – coefficient of xi Good for dense polynomials Second implementation: linked list of nodes int coefficient Nomial link Good for sparse polynomials

Matrices Implementation: 2-D array of double Multiply-add: for (int i=0; i<M; i++) for (int j=0; j<N; j++) for (int k=0; k<L, k++) c[i][j] += a[i][k]*b[k][j] Analysis: ~ MNL double multiply-adds ~ N3 for square matrices Data structures for sparse matrices

Selection Select the kth smallest entry in a collection Application: sort the √N smallest entries Select the √Nth smallest Partition on k = √N Sort the partition with smallest entries Analysis: Select ??? Partition ~ N (next slide) Sort ~ c √N log N << N Can this be done in linear time?

Partitioning int partition(Item[] a, Item p, int lo, int hi) int i = lo-1, j = hi while (true) while (less(a[++i],p) if (i == hi) break; while (less(p, a[--j]) if (j == lo) break; swap(a, i, j) swap(a, lo, j) return j; // a[lo..j] < p & p < a[j+1..hi]

Analysis Partitioning N Items ~ 2N compares Average case: Two partitions are approximately equal in size Worst case: One partition is empty (or has only 1 Item)

Selection Algorithm Item select(Item[] a, int k, int lo, int hi) if (k == 0 && lo == hi) return a[lo] Item p = choosePivot(a, lo, hi) // a[lo+(lo+hi)/2] int j =partition(a, p, lo, hi) if (k <= j) return select(a, k, lo, j) else return select(a, k-(j+1), j+1, hi)

Selection Analysis Average case: T(N) < 2N + T(N/2 + c√N) ~ c' N Worst case: T(N) < 2N + T(N-1) ~ c N2 ~ c k N How can worst case performance be improved? Answer: choose a better pivot!

Choosing a Good Pivot Item pivot(Item[] a, int lo, int hi) Item[] b = new Item[N/5] for (int j=0; j<N/5; j++) b[j] = median5(a, j*5) Item p = select(b, N/10, 0, (N/5)-1) return p Item median5(Item[a], int lo) sort(a, lo, lo+4) return a[lo+2]

Analysis Computing b ~ c N Pivot : Tp(N) < c' N + Ts(N/5) Select: Ts(N) < c'' N + Ts(N/5) + Ts(7N/10) ~ c''' N Why Ts(7N/10)? Largest partition size <= 70% N Pivot is less than half of b Each element of b is less than 2 elements of a Pivot is less than 30% of a Similarly, pivot is greater than 30% of a

Homework 1) Create a Matrix library Constructor create a random N x M matrix void mutiplyAdd does c += a*b for conformant matrices a, b, and c 2) Code partition() Get the details right 3) Code select() 4) Code pivot()