Algorithms + Data Structures Programs

Similar documents
Lecture 2: Getting Started

Scientific Computing. Algorithm Analysis

Sorting & Growth of Functions

The divide-and-conquer paradigm involves three steps at each level of the recursion: Divide the problem into a number of subproblems.

Algorithms - Ch2 Sorting

Divide and Conquer. Design and Analysis of Algorithms Andrei Bulatov

Lecture 1. Introduction / Insertion Sort / Merge Sort

6/12/2013. Introduction to Algorithms (2 nd edition) Overview. The Sorting Problem. Chapter 2: Getting Started. by Cormen, Leiserson, Rivest & Stein

Recurrences and Divide-and-Conquer

ECS122A Lecture Notes on Algorithm Design and Analysis

Lecture Notes for Chapter 2: Getting Started

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1

Introduction to Algorithms

Chapter 4. Divide-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Chapter 1 Divide and Conquer Algorithm Theory WS 2015/16 Fabian Kuhn

Chapter 1 Divide and Conquer Algorithm Theory WS 2014/15 Fabian Kuhn

CS583 Lecture 01. Jana Kosecka. some materials here are based on Profs. E. Demaine, D. Luebke A.Shehu, J-M. Lien and Prof. Wang s past lecture notes

Algorithms. Algorithms 1.4 ANALYSIS OF ALGORITHMS

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

Divide and Conquer 4-0

Chapter 1 Divide and Conquer Algorithm Theory WS 2013/14 Fabian Kuhn

CS S-11 Sorting in Θ(nlgn) 1. Base Case: A list of length 1 or length 0 is already sorted. Recursive Case:

CIS 121 Data Structures and Algorithms with Java Spring Code Snippets and Recurrences Monday, January 29/Tuesday, January 30

Quicksort. Repeat the process recursively for the left- and rightsub-blocks.

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 5. The Towers of Hanoi. Divide and Conquer

Divide and Conquer. Algorithm D-and-C(n: input size)

CPSC 320 Midterm 2 Thursday March 13th, 2014

CSE 421 Closest Pair of Points, Master Theorem, Integer Multiplication

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: September 28, 2016 Edited by Ofir Geri

Lecture 5: Sorting Part A

The divide and conquer strategy has three basic parts. For a given problem of size n,

DIVIDE & CONQUER. Problem of size n. Solution to sub problem 1

EECS 2011M: Fundamentals of Data Structures

COMP Analysis of Algorithms & Data Structures

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: April 1, 2015

CSE 373: Data Structures and Algorithms

Intro to Algorithms. Professor Kevin Gold

Chapter 1 Divide and Conquer Algorithm Theory WS 2013/14 Fabian Kuhn

PRAM Divide and Conquer Algorithms

Divide-and-Conquer. Dr. Yingwu Zhu

Lecture 5 Sorting Arrays

ESc101 : Fundamental of Computing

Sorting. Weiss chapter , 8.6

5. DIVIDE AND CONQUER I

Back to Sorting More efficient sorting algorithms

Plan for Today. Finish recurrences. Inversion Counting. Closest Pair of Points

Randomized Algorithms, Quicksort and Randomized Selection

CSC Design and Analysis of Algorithms

Divide-and-Conquer. Divide-and conquer is a general algorithm design paradigm:

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

Divide-and-Conquer. The most-well known algorithm design strategy: smaller instances. combining these solutions

CS302 Topic: Algorithm Analysis. Thursday, Sept. 22, 2005

Data Structures Lecture 8

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Divide and Conquer

Theory and Algorithms Introduction: insertion sort, merge sort

LECTURE 9 Data Structures: A systematic way of organizing and accessing data. --No single data structure works well for ALL purposes.

CS4311 Design and Analysis of Algorithms. Lecture 1: Getting Started

CSci 231 Final Review

Advanced Algorithms and Data Structures

Section 7.3 Divide-and-Conquer Algorithms and Recurrence Relations. 0,a m. - Division into thirds gives m = 3

O(n): printing a list of n items to the screen, looking at each item once.

Outline. Computer Science 331. Three Classical Algorithms. The Sorting Problem. Classical Sorting Algorithms. Mike Jacobson. Description Analysis

Programming II (CS300)

CS 206 Introduction to Computer Science II

Advanced Algorithms and Data Structures

COT 5407: Introduction to Algorithms. Giri Narasimhan. ECS 254A; Phone: x3748

Algorithm Efficiency & Sorting. Algorithm efficiency Big-O notation Searching algorithms Sorting algorithms

Merge Sort. Run time typically depends on: Insertion sort can be faster than merge sort. Fast, able to handle any data

CS2351 Data Structures. Lecture 1: Getting Started

CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting. Aaron Bauer Winter 2014

CS302 Topic: Algorithm Analysis #2. Thursday, Sept. 21, 2006

Data Structures and Algorithms Key to Homework 1

Data Structures and Algorithms CSE 465

Copyright 2009, Artur Czumaj 1

Chapter 7 Sorting. Terminology. Selection Sort

Solutions to Exam Data structures (X and NV)

Divide and Conquer Algorithms

CS 206 Introduction to Computer Science II

Data Structures and Algorithms Week 4

Problem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n.

Fundamental problem in computing science. putting a collection of items in order. Often used as part of another algorithm

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

CS-A1140 Data structures and algorithms

Lecture 13: Divide and Conquer (1997) Steven Skiena. skiena

CPSC W2 Midterm #2 Sample Solutions

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?

Lecture Notes on Sorting

Module 2: Classical Algorithm Design Techniques

CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014

Analyze the obvious algorithm, 5 points Here is the most obvious algorithm for this problem: (LastLargerElement[A[1..n]:

4. Sorting and Order-Statistics

CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK

Data Structures and Algorithms Running time, Divide and Conquer January 25, 2018

Chapter 3:- Divide and Conquer. Compiled By:- Sanjay Patel Assistant Professor, SVBIT.

Introduction. e.g., the item could be an entire block of information about a student, while the search key might be only the student's name

CLASS: II YEAR / IV SEMESTER CSE CS 6402-DESIGN AND ANALYSIS OF ALGORITHM UNIT I INTRODUCTION

Analysis of Sorting Algorithms. Imagine you have a few thousand dollars in your safe in all different denominations. The

Introduction to Algorithms 6.046J/18.401J

Problem Set 6 Due: 11:59 Sunday, April 29

Divide-and-Conquer CSE 680

Transcription:

Introduction Algorithms Method for solving problems suitable for computer implementation Generally independent of computer hardware characteristics Possibly suitable for many different programming languages Input and output for algorithms Problem must be well-specified Old adage Garbage In Garbage Out (GIGO) Programs always do a specific and well-defined task Problem instance Algorithm correctness correct solution for every instance of input Incorrect algorithm may not halt Pseudocode No attention paid to data structures Data structures Methods of organizing data used in the computations Algorithms + Data Structures Programs Why study algorithms? Know the existing ways of solving problems Proper choice of algorithms, depending on efficiency and other constraints Time and space as a function of amount of input data YK problem Division of larger problems into smaller subproblems that can be easily solved Gain an understanding of the fundamental ways of problem-solving in computers, regardless of available hardware and software Similar to the study of basic number manipulation techniques (addition of numbers) when a calculator can do the job faster Correct amount of optimization of code Is it worth it to save three milliseconds on a program and spend five hours into the optimization? Connectivity Problem Problem to determine, given a set of pairs of connected nodes, whether there are redundant pair of nodes which may be connected through other nodes (for example, forming a cycle)

Consider the following example: 3-4 3-4 4-9 4-9 8-0 8-0 -3-3 5-6 5-6 -9-3-4-9 5-9 5-9 7-3 7-3 4-8 4-8 5-6 5-6 0-0-8-4-3-6-1 6-1 The pair of nodes in second column are uniquely connected; the ones missed can be connected to each other through other nodes as shown in third column Other variations Find out a way of connecting all points in the network to minimize the amount of connecting material used Find out a way to connect all points in a network such that you can go from one point to another in minimum amount of time How can you tell inside of a computer program whether two given points are connected? Problem specification point: Find out whether a pair of points is connected, and not all possible ways to connect the pair Heuristic: A connectivity algorithm will never give you more than n 1 connected pairs for a graph with n nodes Implication: You can stop looking for new pairs once you have found n 1 connected pairs, as the new pairs would invariably be connected Input to this algorithm is a graph and the output is a spanning tree for that graph Union-find algorithms Purpose: In the above connectivity problem, we tried to find if a given pair is already connected Each time we get a new pair, we have to find if it is a new connection This information is added (union) to a set of connection for future reference First write a simple prototype to check that we understand the problem and have a solution First idea: Save all pairs Write a function to pass through them to discover which ones are connected Quick-find algorithm Use an array of nodes to save the connectivity Two nodes are connected if and only if their corresponding entries in the array are equal We spend time in building up the array but the find part is just the lookup, hence the name quick-find

Algorithm for quick-find algorithm quick-find /* Initialize the array of variables */ 1 for i 0 to N - 1 do id[i] = i 3 for each pair p, q 4 if ( id[p] == id[q] ) continue 5 tmp = id[p] 6 for i 1 to N do 7 if ( id[i] == tmp ) 8 id[i] = id[q] p q 0 1 3 4 5 6 7 8 9 3 4 0 1 4 4 5 6 7 8 9 4 9 0 1 9 9 5 6 7 8 9 8 0 0 1 9 9 5 6 7 0 9 3 0 1 9 9 9 5 6 7 0 9 5 6 0 1 9 9 9 6 6 7 0 9 9 0 1 9 9 9 6 6 7 0 9 5 9 0 1 9 9 9 9 9 7 0 9 7 3 0 1 9 9 9 9 9 9 0 9 4 8 0 1 0 0 0 0 0 0 0 0 5 6 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 6 1 1 1 1 1 1 1 1 1 1 1 Property 1 The quick-find algorithm executes at least mn instructions to solve a connectivity problem with m pairs of n objects. Quick-union algorithm Complementary to quick-find Based on array just like quick-find but with a different interpretation Each object in the set points to another object in the same set, and membership is determined by following the pointers Objects are in the same set if and only if this process of pointing ends up at the same object; if they are not in the same set, we end up at different objects that point to themselves Union is formed by linking the two sets if they are different (quick-union) p q 0 1 3 4 5 6 7 8 9 3 4 0 1 4 4 5 6 7 8 9 4 9 0 1 4 9 5 6 7 8 9 8 0 0 1 4 9 5 6 7 0 9 3 0 1 9 4 9 5 6 7 0 9 5 6 0 1 9 4 9 6 6 7 0 9 9 0 1 9 4 9 6 6 7 0 9 5 9 0 1 9 4 9 6 9 7 0 9 7 3 0 1 9 4 9 6 9 9 0 9 4 8 0 1 9 4 9 6 9 9 0 0 5 6 0 1 9 4 9 6 9 9 0 0 0 0 1 9 4 9 6 9 9 0 0 6 1 1 1 9 4 9 6 9 9 0 0 5 8 1 1 9 4 9 6 9 9 0 0 Quick-union is an improvement over quick-find because it removes quick-find s main liability that the program requires at least NM instructions to process M union operations in N objects 3

Property For m > n, the quick-union algorithm could take more than mn instructions to solve a connectivity problem with m pairs of n objects. Weighted quick-union algorithm Selection sort What if input pairs come as 1, 3, 3 4,... Find operation for nth object requires following n 1 pointers Average number of pointers for the first n objects is (0 + 1 + + + (n 1)) n Problem solved by weighted quick-union algorithm = n 1 Keep track of number of nodes in the tree and always connect the smaller to the larger Algorithm Property 3 The weighted quick-union algorithm follows at most lg n pointers to determine whether two of n objects are connected. algorithm selection sort (a) 1 for i 1 to n-1 do min i 3 for j i+1 to n do 4 if a[j] < a[min] then 5 min j 6 exchange(a[i],a[min]) Pseudocode convention Indentation for block structure while, for, repeat, if, then, else same as Pascal Comment indicated by /*... */ i j e No global variables without explicit declaration Array accessed by index to array name Parameters passed by value Algorithm analysis Predict the resources required by algorithm Space requirements Time requirements Model of the machine RAM More data more time Input size Number of items 4

Number of bits Number of pairs (vertices and edges in graph) Running time Number of primitive operations or steps executed Constant amount of time for each line of pseudocode Analysis of selection sort Statement Cost Times algorithm selection sort (a) 1 for i 1 to n-1 do c 1 n 1 min i c n 1 3 for j i+1 to n do c 3 (n i) 4 if a[j] < a[min] then c 4 (n i) 5 min j c 5 (n i) 6 exchange(a[i],a[min]) c 6 n 1 T (n) = c 1 (n 1) + c (n 1) + c 3 (n i) + c 4 (n i) + c 5 (n i) + c 6 (n 1) Quadratic function of n Worst case analysis = (c 1 + c + c 6 )(n 1) + (c 3 + c 4 + c 5 ) (n i) n(n 1) = (c 1 + c + c 6 )(n 1) + (c 3 + c 4 + c 5 ) = (c 3 + c 4 + c 5 ) n + (c 1 + c c 3 c 4 c 5 + c 6)n (c 1 + c + c 6 ) Upper bound on running time for any input of size n Average case analysis All inputs equally likely Often roughly as bad as the worst case Order of growth Actual cost of statement is ignored Constants are ignored as they represent the cost of statement Consider only the leading term of expression Worst case running time of selection sort Θ(n ) Lower order of growth more efficient algorithm Significant only for large number of inputs Designing Algorithms Selection sort based on incremental approach 5

Select the smallest element in the array and exchange it with the first element Repeat with the remainder of the array Divide-and-conquer Divide the problem into a number of smaller subproblems Conquer the subproblems by solving them recursively Combine the solutions to the subproblems into the solution for the original problem Mergesort algorithm Divide the n-element sequence into two n/-element subsequences Conquer by recursively mergesorting the two subsequences Combine by merging the two sorted subsequences to produce the sorted sequence algorithm mergesort(a,p,r) 1 if p < r then q trunc((p+r)/) 3 mergesort(a,p,q) 4 mergesort(a,q+1,r) 5 merge(a,p,q,r) Analyzing divide-and-conquer algorithms Running time described by a recurrence equation Based on three steps Problem size n Run time T (n) Small problem size n c straightforward solution in time Θ(1) Divide the problem into a subproblems; each being 1/b of the original D(n) Time to divide the problem into subproblems C(n) Time to combine the solution to original problem Recurrence Analysis of mergesort T (n) = { Θ(1) if n c at (n/b) + D(n) + C(n) otherwise 6