CPS 100 7.1 From sets, toward maps, via big-Oh l Consider the MemberCheck weekly problem  Find elements in common to two vectors  Alphabetize/sort resulting.

Slides:



Advertisements
Similar presentations
A simple example finding the maximum of a set S of n numbers.
Advertisements

Analysis of Algorithms
Theory of Computing Lecture 3 MAS 714 Hartmut Klauck.
1 Merge Sort Review of Sorting Merge Sort. 2 Sorting Algorithms Selection Sort uses a priority queue P implemented with an unsorted sequence: –Phase 1:
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Quicksort Quicksort     29  9.
Sorting Algorithms and Average Case Time Complexity
© 2006 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 Algorithm Efficiency and Sorting CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
Sorting Algorithms and Analysis Robert Duncan. Refresher on Big-O  O(2^N)Exponential  O(N^2)Quadratic  O(N log N)Linear/Log  O(N)Linear  O(log N)Log.
CPS 100, Spring From data to information to knowledge l Data that’s organized can be processed  Is this a requirement?  What does “organized”
COMP s1 Computing 2 Complexity
SIGCSE Tradeoffs, intuition analysis, understanding big-Oh aka O-notation Owen Astrachan
Chapter 19 - basic definitions - order statistics ( findkth( ) ) - balanced binary search trees - Java implementations Binary Search Trees 1CSCI 3333 Data.
CPS Wordcounting, sets, and the web l How does Google store all web pages that include a reference to “peanut butter with mustard”  What efficiency.
Chapter 16: Searching, Sorting, and the vector Type.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
CPS 100, Spring Trees: no data structure lovelier?
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Chapter.
Midterm Review CSE 2011 Winter October 2015.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 9: Algorithm Efficiency and Sorting Data Abstraction &
Quicksort, Mergesort, and Heapsort. Quicksort Fastest known sorting algorithm in practice  Caveats: not stable  Vulnerable to certain attacks Average.
CompSci 100e Program Design and Analysis II March 29, 2011 Prof. Rodger CompSci 100e, Spring20111.
Review for Final Andy Wang Data Structures, Algorithms, and Generic Programming.
LECTURE 40: SELECTION CSC 212 – Data Structures.  Sequence of Comparable elements available  Only care implementation has O(1) access time  Elements.
1 Sorting Algorithms Sections 7.1 to Comparison-Based Sorting Input – 2,3,1,15,11,23,1 Output – 1,1,2,3,11,15,23 Class ‘Animals’ – Sort Objects.
CS 61B Data Structures and Programming Methodology July 21, 2008 David Sun.
Divide and Conquer Applications Sanghyun Park Fall 2002 CSE, POSTECH.
1 CSE 373 Sorting 3: Merge Sort, Quick Sort reading: Weiss Ch. 7 slides created by Marty Stepp
Searching and Sorting Recursion, Merge-sort, Divide & Conquer, Bucket sort, Radix sort Lecture 5.
Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director
CPS Scoreboard l What else might we want to do with a data structure? l What are maps’ limitations? AlgorithmInsertionDeletionSearch Unsorted.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
CompSci 100E 15.1 What is a Binary Search?  Magic!  Has been used a the basis for “magical” tricks  Find telephone number ( without computer ) in seconds.
Lecture 9COMPSCI.220.FS.T Lower Bound for Sorting Complexity Each algorithm that sorts by comparing only pairs of elements must use at least 
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
Searching Topics Sequential Search Binary Search.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
Chapter 9 Recursion © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CSE 250 – Data Structures. Today’s Goals  First review the easy, simple sorting algorithms  Compare while inserting value into place in the vector 
Chapter 16: Searching, Sorting, and the vector Type.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
CPS Searching, Maps, Tables (hashing) l Searching is a fundamentally important operation  We want to search quickly, very very quickly  Consider.
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Analysis: Algorithms and Data Structures
CS 162 Intro to Programming II
Chapter 4 Divide-and-Conquer
COSC160: Data Structures Linked Lists
Divide and Conquer.
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Data Structures and Analysis (COMP 410)
Merge Sort Merge sort is a recursive algorithm for sorting that decomposes the large problem.
Chapter 4: Divide and Conquer
MSIS 655 Advanced Business Applications Programming
Data Structures Review Session
8/04/2009 Many thanks to David Sun for some of the included slides!
common code “abstracted out” same code written several times: don’t!
ITEC 2620M Introduction to Data Structures
CSE 2010: Algorithms and Data Structures
Ch. 2: Getting Started.
Searching/Sorting/Searching
Alexandra Stefan Last updated: 4/16/2019
Կարգավորում Insertion Sort, Merge Sort
Generic Set Algorithms
Divide-and-Conquer 7 2  9 4   2   4   7
Quick Sort & Merge Sort Technique
Presentation transcript:

CPS From sets, toward maps, via big-Oh l Consider the MemberCheck weekly problem  Find elements in common to two vectors  Alphabetize/sort resulting vector, no duplicates l Write/code an algorithm, use vectors/algorithms, but no sets  Goal: easy to write and verify as correct  Goal: efficient (what does this mean?) l How do we measure efficiency of code and data structures? How do we measure "easy to write and verify"?

CPS Reasoning about tradeoffs l Consider intersection implementations in membercheck.cppmembercheck.cpp  Which was easier to write?  Which is easier to verify as correct?  Which is more efficient? tset, simple templated set implementation  Uses an unbalanced search tree map internally  O (log n) operations on average, but O(n) worst case Insert, search, delete  Object of study, not meant as industrial strength

CPS Tradeoffs in listunique.cpplistunique.cpp l Represent a set as an unsorted vector  Complexity of insert? Complexity of search? l Represent a set as an unsorted linked list  Complexity of insert? Complexity of search? l Represent a set as a vector of 256 linked lists  Complexity of insert? Complexity of search?

CPS C++ coding issues in class Unique l Why are all methods but add-a-vector pure virtual?  What does pure virtual mean? l How is helper function in ListUnique used in subclass?  Probably no reason to implement originally  Refactoring when we see duplicated code  Opportunity for getAll refactoring? l Why is node class protected, why is helper class after it?  What does public need to know? Scope.

CPS solving recurrence: findkth T(n) = T(n/100) + n T(XXX) = T(XXX/100) + XXX T(1) = 1 (note: n/100/100 = n/10 4 ) T(n) = [T(n/10 4 ) + n/100] + n = = [T(n/10 6 ) + n/10 4 ] + n/100 + n =... eureka! = T(1) + sum of geometric series = n + n/100 + n/100*100 + … = n(1 – 1/100 m )/(1/100) = O(n)

CPS merge sort linked lists: linksort.cpplinksort.cpp l Given a linked list, we want to sort it  Divide the list into two equal halves  Sort the halves  Merge the sorted halves together l What’s complexity of dividing an n-node in half?  How do we do this? l What’s complexity of merging (zipping) two sorted lists?  How do we do this? T(n) = time to sort n-node list = 2 T(n/2) + O(n) why?

CPS sidebar: solving recurrence T(n) = 2T(n/2) + n T(XXX) = 2T(XXX/2) + XXX T(1) = 1 (note: n/2/2 = n/4) T(n) = 2[2T(n/4) + n/2] + n = 4 T(n/4) + n + n = 4[2T(n/8) + n/4] + 2n = 8T(n/8) + 3n =... eureka! = 2 k T(n/2 k ) + kn let 2 k = n k = log n, this yields 2 log n T(n/2 log n ) + n(log n) n T(1) + n(log n) O(n log n)

CPS Nancy Leveson: Software Safety Founded the field l Mathematical and engineering aspects  Air traffic control  Microsoft word "C++ is not state-of-the-art, it's only state-of-the-practice, which in recent years has been going backwards" l Software and steam engines: once extremely dangerous? l l THERAC 25: Radiation machine that killed many people l