Lecture 3 Nearest Neighbor Algorithms Shang-Hua Teng.

Slides:



Advertisements
Similar presentations
Divide and Conquer (Merge Sort)
Advertisements

A simple example finding the maximum of a set S of n numbers.
Lecture 4 Divide and Conquer for Nearest Neighbor Problem
Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions.
September 12, Algorithms and Data Structures Lecture III Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Divide-and-Conquer Recursive in structure –Divide the problem into several smaller sub-problems that are similar to the original but smaller in size –Conquer.
CS223 Advanced Data Structures and Algorithms 1 Divide and Conquer Neil Tang 4/15/2010.
A Basic Study on the Algorithm Analysis Chapter 2. Getting Started 한양대학교 정보보호 및 알고리즘 연구실 이재준 담당교수님 : 박희진 교수님 1.
11 Computer Algorithms Lecture 6 Recurrence Ch. 4 (till Master Theorem) Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
2. Getting started Hsu, Lih-Hsing. Computer Theory Lab. Chapter 2P Insertion sort Example: Sorting problem Input: A sequence of n numbers Output:
ALGORITHMS Introduction. Definition Algorithm: Any well-defined computational procedure that takes some value or set of values as input and produces some.
Divide and Conquer Chapter 6. Divide and Conquer Paradigm Divide the problem into sub-problems of smaller sizes Conquer by recursively doing the same.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 5.
Analysis of Algorithms CS 477/677 Sorting – Part B Instructor: George Bebis (Chapter 7)
Spring 2015 Lecture 5: QuickSort & Selection
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
CS 253: Algorithms Chapter 7 Mergesort Quicksort Credit: Dr. George Bebis.
Sorting. Input: A sequence of n numbers a 1, …, a n Output: A reordering a 1 ’, …, a n ’, such that a 1 ’ < … < a n ’
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Lecture 2: Divide and Conquer I: Merge-Sort and Master Theorem Shang-Hua Teng.
Analysis of Algorithms CS 477/677 Midterm Exam Review Instructor: George Bebis.
Lecture 4: Divide and Conquer III: Other Applications and Examples Shang-Hua Teng.
CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 4. Recurrences - 1 Recurrences.
Lecture 6 Divide and Conquer for Nearest Neighbor Problem Shang-Hua Teng.
CS Main Questions Given that the computer is the Great Symbol Manipulator, there are three main questions in the field of computer science: What kinds.
Unit 1. Sorting and Divide and Conquer. Lecture 1 Introduction to Algorithm and Sorting.
Introduction to Algorithm design and analysis
October 1, Algorithms and Data Structures Lecture III Simonas Šaltenis Nykredit Center for Database Research Aalborg University
HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
Project 2 due … Project 2 due … Project 2 Project 2.
10/14/ Algorithms1 Algorithms - Ch2 - Sorting.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
1 Designing algorithms There are many ways to design an algorithm. Insertion sort uses an incremental approach: having sorted the sub-array A[1…j - 1],
CMPT 438 Algorithms. Why Study Algorithms? Necessary in any computer programming problem ▫Improve algorithm efficiency: run faster, process more data,
September 17, 2001 Algorithms and Data Structures Lecture II Simonas Šaltenis Nykredit Center for Database Research Aalborg University
COSC 3101A - Design and Analysis of Algorithms 2 Asymptotic Notations Continued Proof of Correctness: Loop Invariant Designing Algorithms: Divide and Conquer.
Algorithm Analysis Part of slides are borrowed from UST.
Divide and Conquer Andreas Klappenecker [based on slides by Prof. Welch]
Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.
Midterm Review 1. Midterm Exam Thursday, October 15 in classroom 75 minutes Exam structure: –TRUE/FALSE questions –short questions on the topics discussed.
Introduction to Algorithms (2 nd edition) by Cormen, Leiserson, Rivest & Stein Chapter 2: Getting Started.
Algorithms A well-defined computational procedure that takes some value as input and produces some value as output. (Also, a sequence of computational.
2IS80 Fundamentals of Informatics Fall 2015 Lecture 6: Sorting and Searching.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
 Design and Analysis of Algorithms تصميم وتحليل الخوارزميات (311 عال) Chapter 2 Sorting (insertion Sort, Merge Sort)
Lecture # 6 1 Advance Analysis of Algorithms. Divide-and-Conquer Divide the problem into a number of subproblems Similar sub-problems of smaller size.
Sorting. 2 The Sorting Problem Input: A sequence of n numbers a 1, a 2,..., a n Output: A permutation (reordering) a 1 ’, a 2 ’,..., a n ’ of the input.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture3.
Lecture 2 Algorithm Analysis
Analysis of Algorithms CS 477/677
UNIT- I Problem solving and Algorithmic Analysis
Unit 1. Sorting and Divide and Conquer
Lecture 4 Divide-and-Conquer
CSCE 411 Design and Analysis of Algorithms
CS 3343: Analysis of Algorithms
Divide and Conquer (Merge Sort)
Algorithms and Data Structures Lecture III
Lecture No 6 Advance Analysis of Institute of Southern Punjab Multan
CS200: Algorithms Analysis
Divide and Conquer Algorithms Part I
Divide and Conquer (Merge Sort)
Divide-and-Conquer 7 2  9 4   2   4   7
Divide & Conquer Algorithms
Introduction To Algorithms
Algorithms and Data Structures Lecture III
Algorithms and Data Structures Lecture II
Presentation transcript:

Lecture 3 Nearest Neighbor Algorithms Shang-Hua Teng

What is Algorithm? A computable set of steps to achieve a desired result from a given input Example: –Input: An array A of n numbers –Desired result Pseudo-code of Algorithm SUM

Complexity: Input Size n Number of steps: n-1 additions

Example 2: Integer Multiplication c = a b When do we need to multiply two very large numbers? –In Cryptography and Network Security message as numbers encryption and decryption need to multiply numbers

How to multiply 2 n-bit numbers ************ ************************

Asymptotic Notation of Complexity as input size grows, how fast does the running time grow. –T 1 (n) = 100 n –T 2 (n) = n 2 Which algorithm is better? When n < 100 is small then T 2 is smaller As n becomes larger, T 2 grows much faster To solve large-scale problem, algorithm1 is preferred.

Asymptotic Notation (Removing the constant factor) The  Notation  (g(n)) = { f(n): there exist positive c 1 and c 2 and n 0 such that for all n > n 0 } For example T(n) = 4nlog n + n =  (nlog n) For example n – 1 =  (n)

Asymptotic Notation (Removing the constant factor) The  Big  Notation O(g(n)) = { f(n): there exist positive c and n 0 such that for all n > n 0 } For example T(n) = 4nlog n + n =  (nlog n) But also T(n) = 4nlog n + n =  (n 2 )

Nearest Neighbor Problem: General Formulation

Nearest Neighbor Problem

Applications Points could be web-page, closest neighbor is the most similar web-page Points could be people, closest neighbor could be the best friend Points could be biological spices, the closest neighbor could be the closest spice …

An O(dn 2 ) time Algorithm Why O(dn 2 ) time?

Can We do better? Yes, Handout #4, by Jon Louis Bentley

One-Dimensional Geometry If we can order points from small to large, then we just need to look at the left neighbor and right neighbor of each point to find its nearest neighbor

Reduce to Sorting Input: Array A[1...n], of elements in an arbitrary order; array size n Output: Array A[1...n] of the same elements, but in the non-decreasing order

Divide and Conquer Divide the problem into a number of sub-problems (similar to the original problem but smaller); Conquer the sub-problems by solving them recursively (if a sub-problem is small enough, just solve it in a straightforward manner. Combine the solutions to the sub-problems into the solution for the original problem

Algorithm Design Paradigm I Solve smaller problems, and use solutions to the smaller problems to solve larger ones –Divide and Conquer Correctness: mathematical induction

Merge Sort Divide the n-element sequence to be sorted into two subsequences of n/2 element each Conquer: Sort the two subsequences recursively using merge sort Combine: merge the two sorted subsequences to produce the sorted answer Note: during the recursion, if the subsequence has only one element, then do nothing.

Merge-Sort(A,p,r) A procedure sorts the elements in the sub-array A[p..r] using divide and conquer Merge-Sort(A,p,r) –if p >= r, do nothing –if p< r then Merge-Sort(A,p,q) Merge-Sort(A,q+1,r) Merge(A,p,q,r) Starting by calling Merge-Sort(A,1,n)

A = MergeArray(L,R) Assume L[1:s] and R[1:t] are two sorted arrays of elements: Merge-Array(L,R) forms a single sorted array A[1:s+t] of all elements in L and R. A = MergeArray(L,R) – –for k 1 to s + t do if –then –else

Complexity of MergeArray At each iteration, we perform 1 comparison, 1 assignment (copy one element to A) and 2 increments (to k and i or j ) So number of operations per iteration is 4. Thus, Merge-Array takes at most 4(s+t) time. Linear in the size of the input.

Merge (A,p,q,r) Assume A[p..q] and A[q+1..r] are two sorted Merge(A,p,q,r) forms a single sorted array A[p..r]. Merge (A,p,q,r) –

Merge-Sort(A,p,r) A procedure sorts the elements in the sub-array A[p..r] using divide and conquer Merge-Sort(A,p,r) –if p >= r, do nothing –if p< r then Merge-Sort(A,p,q) Merge-Sort(A,q+1,r) Merge(A,p,q,r)

Running Time of Merge-Sort Running time as a function of the input size, that is the number of elements in the array A. The Divide-and-Conquer scheme yields a clean recurrences. Assume T(n) be the running time of merge- sort for sorting an array of n elements. For simplicity assume n is a power of 2, that is, there exists k such that n = 2 k.

Recurrence of T(n) T(1) = 1 for n > 1, we have if n = 1 if n > 1

Solution of Recurrence of T(n) T(n) = 4 nlog n + n = O(nlog n) Picture Proof by Recursion Tree