CS Lecture 5. Vidroha debroy. Material adapted courtesy of Prof. Xiangnan Kong and Prof. Carolina Ruiz at Worcester Polytechnic Institute

Similar documents
SIDDHARTH INSTITUTE OF ENGINEERING & TECHNOLOGY :: PUTTUR (AUTONOMOUS) Siddharth Nagar, Narayanavanam Road QUESTION BANK (DESCRIPTIVE)

II. NORMAL BUBBLE SORT (WORST CASE)

Heap Sort. Lecture 35. Robb T. Koether. Hampden-Sydney College. Mon, Apr 25, 2016

Algorithms and Data Structures

Problem Solving Agents

CS 4649/7649 Robot Intelligence: Planning

Better Search Improved Uninformed Search CIS 32

CT PET-2018 Part - B Phd COMPUTER APPLICATION Sample Question Paper

Decision Trees. Nicholas Ruozzi University of Texas at Dallas. Based on the slides of Vibhav Gogate and David Sontag

Supplementary Material for Bayes Merging of Multiple Vocabularies for Scalable Image Retrieval

Uninformed search methods

Princess Nora University Faculty of Computer & Information Systems ARTIFICIAL INTELLIGENCE (CS 370D) Computer Science Department

DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2017)

Performance Visualization of Bubble Sort in Worst Case Using R Programming in Personal Computer

Uninformed search methods II.

Uninformed Search (Ch )

Uninformed Search (Ch )

COMP219: Artificial Intelligence. Lecture 8: Combining Search Strategies and Speeding Up

Overview. Depth Limited Search. Depth Limited Search. COMP219: Artificial Intelligence. Lecture 8: Combining Search Strategies and Speeding Up

Search I. Tuomas Sandholm Carnegie Mellon University Computer Science Department. [Read Russell & Norvig Chapter 3]

The system design must obey these constraints. The system is to have the minimum cost (capital plus operating) while meeting the constraints.

4. Please Do Break the Crystal

CS 4649/7649 Robot Intelligence: Planning

Uninformed search methods II.

Chapter 4 (sort of): How Universal Are Turing Machines? CS105: Great Insights in Computer Science

Uninformed search strategies

Introduction to Algorithms 6.046J/18.401J/SMA5503

SEARCH TREE. Generating the children of a node

PREAMBLE. Why the Informatics Olympiad in Team were born

SEARCH SEARCH TREE. Node: State in state tree. Root node: Top of state tree

CS 221 PROJECT FINAL

In my left hand I hold 15 Argentine pesos. In my right, I hold 100 Chilean

Real World Search Problems. CS 331: Artificial Intelligence Uninformed Search. Simpler Search Problems. Example: Oregon. Search Problem Formulation

A Coach s Guide to Using USA Swimming s OME (Online Meet Entry) for North Carolina Swimming (NCS) Championship Meet Entry

PREDICTING the outcomes of sporting events

Uninformed search methods

Dynamic Programming: The Matrix Chain Algorithm

CS 351 Design of Large Programs Zombie House

Chapter 23 Traffic Simulation of Kobe-City

Bulgarian Olympiad in Informatics: Excellence over a Long Period of Time

CENG 466 Artificial Intelligence. Lecture 4 Solving Problems by Searching (II)

Agent Based Urban Models The Notting Hill Carnival Model

COMP 406 Lecture 05. Artificial. Fiona Yan Liu Department of Computing The Hong Kong Polytechnic University

Highway 111 Corridor Study

Dobbin Day - User Guide

Automating Injection Molding Simulation using Autonomous Optimization

Artificial Intelligence. Uninformed Search Strategies

Diameter in cm. Bubble Number. Bubble Number Diameter in cm

Optimizing positional scoring rules for rank aggregation

Uninformed Search Strategies

Fun with M&M s. By: Cassandra Gucciardo. Sorting

HAP e-help. Obtaining Consistent Results Using HAP and the ASHRAE 62MZ Ventilation Rate Procedure Spreadsheet. Introduction

Williams, Justin à Player name (last name, first name) CAR

ECE 697B (667) Spring 2003

The only high-speed filling system for LPG cylinders in the world

Shot-by-shot directional source deghosting and directional designature using near-gun measurements

The next criteria will apply to partial tournaments. Consider the following example:

STICTION: THE HIDDEN MENACE

Introduction. AI and Searching. Simple Example. Simple Example. Now a Bit Harder. From Hammersmith to King s Cross

A new Decomposition Algorithm for Multistage Stochastic Programs with Endogenous Uncertainties

Revision 2013 Vacuum Technology 1-3 day Good Vacuum Practice 1 Day Course Outline

Policy Gradient RL to learn fast walk

Systems Theoretic Process Analysis (STPA)

Supplementary Figures

Repetition Structures

Application of Dijkstra s Algorithm in the Evacuation System Utilizing Exit Signs

CSE 3401: Intro to AI & LP Uninformed Search II

Front derailleur. Dealer's Manual DURA-ACE FD-R9100 ULTEGRA FD-R FD ROAD MTB Trekking. City Touring/ Comfort Bike DM-RAFD001-03

INTRODUCTION AND METHODS OF CMAP SOFTWARE BY IPC-ENG

The Incremental Evolution of Gaits for Hexapod Robots

CS472 Foundations of Artificial Intelligence. Final Exam December 19, :30pm

Algorithm for Line Follower Robots to Follow Critical Paths with Minimum Number of Sensors

ERCBH2S Frequently Asked Questions.

Three New Methods to Find Initial Basic Feasible. Solution of Transportation Problems

A IMPROVED VOGEL S APPROXIMATIO METHOD FOR THE TRA SPORTATIO PROBLEM. Serdar Korukoğlu 1 and Serkan Ballı 2.

Rollover Warning/Control for Sports Utility Vehicles

Aryeh Rappaport Avinoam Meir. Schedule automation

Agile project management with scrum

Analysis of Professional Cycling Results as a Predictor for Future Success

CSC242: Intro to AI. Lecture 21

CSM Pre-Test. 3) Who is responsible for achieving a Sprint Goal? A) ScrumMaster B) Product Owner C) Project Manager D) Scrum Development Team

Optimizing Cyclist Parking in a Closed System

Seismic waves. Seismic waves, like all waves, transfer energy from one place to another without moving material. Seismic Waves 1 Author Paul Denton

6/16/2010 DAG Execu>on Model, Work and Depth 1 DAG EXECUTION MODEL, WORK AND DEPTH

Artificial Intelligence for the EChO Mission Scheduler

Team assignment strategies in youth sports

FREQUENTLY ASKED QUESTIONS ANSWERED! MOUNTING TO MY BIKE

Iteration: while, for, do while, Reading Input with Sentinels and User-defined Functions

LOCOMOTION CONTROL CYCLES ADAPTED FOR DISABILITIES IN HEXAPOD ROBOTS

Online Companion to Using Simulation to Help Manage the Pace of Play in Golf

FIRE PROTECTION. In fact, hydraulic modeling allows for infinite what if scenarios including:

2600T Series Pressure Transmitters Plugged Impulse Line Detection Diagnostic. Pressure Measurement Engineered solutions for all applications

Wind Flow Validation Summary

Calculating Forces in the Pulley Mechanical Advantage Systems Used in Rescue Work By Ralphie G. Schwartz, Esq

Estimating balanced detailed SUT using benchmark SUT

Density of Individual Airborne Wind Energy Systems in AWES Farms

Pressure Vessel Calculation for Yavin Thruster

1 GENERAL PROCEDURES THAT APPLY TO ANY RACE

STAT 155 Introductory Statistics. Lecture 2: Displaying Distributions with Graphs

Provably Secure Camouflaging Strategy for IC Protection

Transcription:

CS 3353 Lecture 5 Vidroha debroy Material adapted courtesy of Prof. Xiangnan Kong and Prof. Carolina Ruiz at Worcester Polytechnic Institute Searching for a number Find a specific number 91-3 26 54 73 3 68 103 131 88 91 19 1

Searching for a number Find a specific number 91 - what about now? 3 54 87 17 73 61 21 103 35 68 41 94 11 131 49 8 39 66 76 99 73 26 13 71 85 383 102 88 91 97 114 22 57 12 19 63 111 Why Sorting? Practical Application People by last name Countries by population Search engine results by relevance Fundamental to other algorithms Different algorithms have different asymtotic and constantfactor trade-offs no single best sort for all scenarios knowing one way to sort just isn't enough Keeping data in order allows it to be searched more efficiently 2

Sorting Sorting: Rearranging the values in an array or collection into a specific order (usually into their "natural ordering"). one of the fundamental problems in computer science it is estimated that 25~50% of all computing power is used for sorting activities. Popular Sorting algorithms: bubble sort: swap adjacent pairs that are out of order selection sort: look for the smallest element, move to front insertion sort: build an increasingly large sorted front portion merge sort: recursively divide the array in half and sort it heap sort: place the values into a sorted tree structure quick sort: recursively partition array based on a middle value Sorting Example Bubble Sort 3

Problem Definition Sorting takes an unordered collection and makes it an ordered one. 1 2 3 4 5 6 Input: 77 42 35 12 101 5 Output: 1 2 3 4 5 6 5 12 35 42 77 101 One Idea: Bubble Water 3 3 3 2 2 3 1 Air 1 2 1 2 1 4

"Bubbling Up" the Largest Element Traverse a collection of elements Move from the front to the end Bubble the largest value to the end using pairwise comparisons and swapping 1 2 3 4 5 6 77 42 35 12 101 5 "Bubbling Up" the Largest Element Traverse a collection of elements Move from the front to the end Bubble the largest value to the end using pairwise comparisons and swapping 1 2 3 4 5 6 77 42 42 77 35 12 101 5 5

"Bubbling Up" the Largest Element Traverse a collection of elements Move from the front to the end Bubble the largest value to the end using pairwise comparisons and swapping 1 2 3 4 5 6 42 77 35 35 77 12 101 5 "Bubbling Up" the Largest Element Traverse a collection of elements Move from the front to the end Bubble the largest value to the end using pairwise comparisons and swapping 1 2 3 4 5 6 42 35 77 12 12 77 101 5 6

"Bubbling Up" the Largest Element Traverse a collection of elements Move from the front to the end Bubble the largest value to the end using pairwise comparisons and swapping 1 2 3 4 5 6 42 35 12 77 101 5 No need to swap "Bubbling Up" the Largest Element Traverse a collection of elements Move from the front to the end Bubble the largest value to the end using pairwise comparisons and swapping 1 2 3 4 5 6 42 35 12 77 101 5 101 5 7

"Bubbling Up" the Largest Element Traverse a collection of elements Move from the front to the end Bubble the largest value to the end using pairwise comparisons and swapping 1 2 3 4 5 6 42 35 12 77 5 101 Largest value correctly placed The Bubble Up Algorithm Bubble-Sort-step1( A ): for k = 1 (N 1): if A[k] > A[k+1]: ( A, k, k+1 ) ( A, x, y ): tmp = A[x] A[x] = A[y] A[y] = tmp 8

Need More Iterations Notice that only the largest value is correctly placed All other values are still out of order So we need to repeat this process 1 2 3 4 5 6 42 35 12 77 5 101 Largest value correctly placed Repeat Bubble Up How Many Times? If we have N elements And if each time we bubble an element, we place it in its correct location Then we repeat the bubble up process N 1 times. This guarantees we ll correctly place all N elements. 9

N - 1 2/1/2016 Bubbling All the Elements 1 2 3 4 5 6 42 35 12 77 5 101 1 2 3 4 5 6 35 12 42 5 77 101 1 2 3 4 5 6 12 35 5 42 77 101 1 2 3 4 5 6 12 5 35 42 77 101 1 2 3 4 5 6 5 12 35 42 77 101 The Bubble Up Algorithm Bubble-Sort( A ): for k = 1 (N 1): if A[k] > A[k+1]: ( A, k, k+1 ) 10

Inner loop Outer loop 2/1/2016 The Bubble Up Algorithm Bubble-Sort( A ): for i = 1 (N 1): for k = 1 (N 1): if A[k] > A[k+1]: ( A, k, k+1 ) The Bubble Up Algorithm Bubble-Sort( A ): for i = 1 (N 1): To do N-1 iterations To bubble a value for k = 1 (N 1): if A[k] > A[k+1]: ( A, k, k+1 ) 11

Reducing the Number of Comparisons 1 2 3 4 5 6 77 42 35 12 101 5 1 2 3 4 5 6 42 35 12 77 5 101 1 2 3 4 5 6 35 12 42 5 77 101 1 2 3 4 5 6 12 35 5 42 77 101 1 2 3 4 5 6 12 5 35 42 77 101 Reducing the Number of Comparisons Assume the array size N On the i th iteration, we only need to do N-i comparisons. For example: N = 6 i = 4 (4 th iteration) Thus, we have 2 comparisons to do 1 2 3 4 5 6 12 35 5 42 77 101 12

The Bubble Up Algorithm Bubble-Sort( A ): for i = 1 (N 1): for k = 1 (N 1): if A[k] > A[k+1]: ( A, k, k+1 ) The Bubble Up Algorithm Bubble-Sort( A ): for i = 1 (N 1): for k = 1 (N i): if A[k] > A[k+1]: ( A, k, k+1 ) 13

Code Demo An Animated Example i 1 k 98 23 45 14 6 67 33 42 14

An Animated Example i 1 k 1 98 23 45 14 6 67 33 42 An Animated Example i 1 k 1 98 23 45 14 6 67 33 42 15

An Animated Example i 1 k 1 23 98 45 14 6 67 33 42 An Animated Example i 1 k 2 23 98 45 14 6 67 33 42 16

An Animated Example i 1 k 2 23 98 45 14 6 67 33 42 An Animated Example i 1 k 2 23 45 98 14 6 67 33 42 17

An Animated Example i 1 k 3 23 45 98 14 6 67 33 42 An Animated Example i 1 k 3 23 45 98 14 6 67 33 42 18

An Animated Example i 1 k 3 23 45 14 98 6 67 33 42 An Animated Example i 1 k 4 23 45 14 98 6 67 33 42 19

An Animated Example i 1 k 4 23 45 14 98 6 67 33 42 An Animated Example i 1 k 4 23 45 14 6 98 67 33 42 20

An Animated Example i 1 k 5 23 45 14 6 98 67 33 42 An Animated Example i 1 k 5 23 45 14 6 98 67 33 42 21

An Animated Example i 1 k 5 23 45 14 6 67 98 33 42 An Animated Example i 1 k 6 23 45 14 6 67 98 33 42 22

An Animated Example i 1 k 6 23 45 14 6 67 98 33 42 An Animated Example i 1 k 6 23 45 14 6 67 33 98 42 23

An Animated Example i 1 k 7 23 45 14 6 67 33 98 42 An Animated Example i 1 k 7 23 45 14 6 67 33 98 42 24

An Animated Example i 1 k 7 23 45 14 6 67 33 42 98 After First Pass of Outer Loop i 1 k 8 Finished first Bubble Up 23 45 14 6 67 33 42 98 25

The Second Bubble Up i 2 k 1 23 45 14 6 67 33 42 98 The Second Bubble Up i 2 k 1 No 23 45 14 6 67 33 42 98 26

The Second Bubble Up i 2 k 2 23 45 14 6 67 33 42 98 The Second Bubble Up i 2 k 2 23 45 14 6 67 33 42 98 27

The Second Bubble Up i 2 k 2 23 14 45 6 67 33 42 98 The Second Bubble Up i 2 k 3 23 14 45 6 67 33 42 98 28

The Second Bubble Up i 2 k 3 23 14 45 6 67 33 42 98 The Second Bubble Up i 2 k 3 23 14 6 45 67 33 42 98 29

The Second Bubble Up i 2 k 4 23 14 6 45 67 33 42 98 The Second Bubble Up i 2 k 4 No 23 14 6 45 67 33 42 98 30

The Second Bubble Up i 2 k 5 23 14 6 45 67 33 42 98 The Second Bubble Up i 2 k 5 23 14 6 45 67 33 42 98 31

The Second Bubble Up i 2 k 5 23 14 6 45 33 67 42 98 The Second Bubble Up i 2 k 6 23 14 6 45 33 67 42 98 32

The Second Bubble Up i 2 k 6 23 14 6 45 33 67 42 98 The Second Bubble Up i 2 k 6 23 14 6 45 33 42 67 98 33

After Second Pass of Outer Loop i 2 k 7 Finished second Bubble Up 23 14 6 45 33 42 67 98 The Third Bubble Up i 3 k 1 23 14 6 45 33 42 67 98 34

The Third Bubble Up i 3 k 1 23 14 6 45 33 42 67 98 The Third Bubble Up i 3 k 1 14 23 6 45 33 42 67 98 35

The Third Bubble Up i 3 k 2 14 23 6 45 33 42 67 98 The Third Bubble Up i 3 k 2 14 23 6 45 33 42 67 98 36

The Third Bubble Up i 3 k 2 14 6 23 45 33 42 67 98 The Third Bubble Up i 3 k 3 14 6 23 45 33 42 67 98 37

The Third Bubble Up i 3 k 3 No 14 6 23 45 33 42 67 98 The Third Bubble Up i 3 k 4 14 6 23 45 33 42 67 98 38

The Third Bubble Up i 3 k 4 14 6 23 45 33 42 67 98 The Third Bubble Up i 3 k 4 14 6 23 33 45 42 67 98 39

The Third Bubble Up i 3 k 5 14 6 23 33 45 42 67 98 The Third Bubble Up i 3 k 5 14 6 23 33 45 42 67 98 40

The Third Bubble Up i 3 k 5 14 6 23 33 42 45 67 98 After Third Pass of Outer Loop i 3 k 6 Finished third Bubble Up 14 6 23 33 42 45 67 98 41

The Fourth Bubble Up i 4 k 1 14 6 23 33 42 45 67 98 The Fourth Bubble Up i 4 k 1 14 6 23 33 42 45 67 98 42

The Fourth Bubble Up i 4 k 1 6 14 23 33 42 45 67 98 The Fourth Bubble Up i 4 k 2 6 14 23 33 42 45 67 98 43

The Fourth Bubble Up i 4 k 2 No 6 14 23 33 42 45 67 98 The Fourth Bubble Up i 4 k 3 6 14 23 33 42 45 67 98 44

The Fourth Bubble Up i 4 k 3 No 6 14 23 33 42 45 67 98 The Fourth Bubble Up i 4 k 4 6 14 23 33 42 45 67 98 45

The Fourth Bubble Up i 4 k 4 No 6 14 23 33 42 45 67 98 After Fourth Pass of Outer Loop i 4 k 5 Finished fourth Bubble Up 6 14 23 33 42 45 67 98 46

The Fifth Bubble Up i 5 k 1 6 14 23 33 42 45 67 98 The Fifth Bubble Up i 5 k 1 No 6 14 23 33 42 45 67 98 47

The Fifth Bubble Up i 5 k 2 6 14 23 33 42 45 67 98 The Fifth Bubble Up i 5 k 2 No 6 14 23 33 42 45 67 98 48

The Fifth Bubble Up i 5 k 3 6 14 23 33 42 45 67 98 The Fifth Bubble Up i 5 k 3 No 6 14 23 33 42 45 67 98 49

After Fifth Pass of Outer Loop i 5 k 4 Finished fifth Bubble Up 6 14 23 33 42 45 67 98 i 5 k 4 6 14 23 33 42 45 67 98 50

Inner loop Outer loop 2/1/2016 Analysis of Bubble Sort and Loop Invariant Bubble Sort Algorithm Bubble-Sort( A ): for i = 1 (N 1): To do N-1 iterations To bubble a value for k = 1 (N 1): if A[k] > A[k+1]: ( A, k, k+1 ) 51

Inner loop Inner loop Outer loop Outer loop 2/1/2016 Loop Invariant It is a condition or property that is guaranteed to be correct with each iteration in the loop Usually used to prove the correctness of the algorithm for i = 1 (N 1): To do N-1 iterations To bubble a value for k = 1 (N 1): if A[k] > A[k+1]: ( A, k, k+1 ) Loop Invariant for Bubble Sort for i = 1 (N 1): To do N-1 iterations To bubble a value for k = 1 (N 1): if A[k] > A[k+1]: ( A, k, k+1 ) By the end of iteration i the right-most i items (largest) are sorted and in place 52

N - 1 2/1/2016 N-1 Iterations 1 2 3 4 5 6 42 35 12 77 5 101 1 2 3 4 5 6 35 12 42 5 77 101 1 2 3 4 5 6 12 35 5 42 77 101 1 2 3 4 5 6 12 5 35 42 77 101 1 2 3 4 5 6 5 12 35 42 77 101 1 st 2 nd 3 rd 4 th 5 th Correctness of Bubble Sort (using Loop Invariant) Bubble sort has N-1 Iterations Invariant: By the end of iteration i the right-most i items (largest) are sorted and in place Then: After the N-1 iterations The rightmost N-1 items are sorted This implies that all the N items are sorted 53

Bubble Sort Time Complexity Best-Case Time Complexity The scenario under which the algorithm will do the least amount of work (finish the fastest) What if we had a pass and nothing was swapped? What does that tell us? Worst-Case Time Complexity The scenario under which the algorithm will do the largest amount of work (finish the slowest) Summary Bubble Up algorithm will move largest value to its correct location (to the right) Repeat Bubble Up until all elements are correctly placed: Maximum of N-1 times Can finish early if no swapping occurs We reduce the number of elements we compare each time one is correctly placed Not the most efficient algorithm around but is easy to understand 54