Design of Algorithms using Brute Force Approach. Primality Testing (given number is n binary digits)

Slides:



Advertisements
Similar presentations
Discrete Mathematics Lecture 3
Advertisements

MCS 312: NP Completeness and Approximation algorithms Instructor Neelima Gupta
22C:19 Discrete Structures Induction and Recursion Fall 2014 Sukumar Ghosh.
Knapsack Problem Section 7.6. Problem Suppose we have n items U={u 1,..u n }, that we would like to insert into a knapsack of size C. Each item u i has.
Analysis of Algorithms
Greed is good. (Some of the time)
Having Proofs for Incorrectness
1 Today’s Material Medians & Order Statistics – Ch. 9.
Divide and Conquer. Recall Complexity Analysis – Comparison of algorithm – Big O Simplification From source code – Recursive.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 7: Greedy Algorithms
Cs333/cutler Greedy1 Introduction to Greedy Algorithms The greedy technique Problems explored –The coin changing problem –Activity selection.
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
The number of edge-disjoint transitive triples in a tournament.
Greedy vs Dynamic Programming Approach
Lecture 5: Linear Time Sorting Shang-Hua Teng. Sorting Input: Array A[1...n], of elements in arbitrary order; array size n Output: Array A[1...n] of the.
Advanced Topics in Algorithms and Data Structures 1 Lecture 4 : Accelerated Cascading and Parallel List Ranking We will first discuss a technique called.
CSE115/ENGR160 Discrete Mathematics 03/03/11 Ming-Hsuan Yang UC Merced 1.
Accelerated Cascading Advanced Algorithms & Data Structures Lecture Theme 16 Prof. Dr. Th. Ottmann Summer Semester 2006.
Algorithm Design Techniques: Induction Chapter 5 (Except Section 5.6)
Combinations We should use permutation where order matters
Week 2: Greedy Algorithms
Dave Risch. Project Specifications There is a “knapsack” that you want to fill with the most valuable items that are available to you. Each item has a.
Properties of the Integers: Mathematical Induction
Copyright © 2007 Pearson Education, Inc. Slide 8-1.
Induction and recursion
1.7 Arrays academy.zariba.com 1. Lecture Content 1.Basic Operations with Arrays 2.Console Input & Output of Arrays 3.Iterating Over Arrays 4.List 5.Cloning.
CSE 321 Discrete Structures Winter 2008 Lecture 10 Number Theory: Primality.
- ABHRA DASGUPTA Solving Adhoc and Math related problems.
Ch. 8 & 9 – Linear Sorting and Order Statistics What do you trade for speed?
Approaches to Problem Solving greedy algorithms dynamic programming backtracking divide-and-conquer.
Merkle-Hellman Knapsack Cryptosystem Merkle offered $100 award for breaking singly - iterated knapsack Singly-iterated Merkle - Hellman KC was broken by.
Reading and Writing Mathematical Proofs
Mathematical Induction. F(1) = 1; F(n+1) = F(n) + (2n+1) for n≥ F(n) n F(n) =n 2 for all n ≥ 1 Prove it!
Chapter 14 Randomized algorithms Introduction Las Vegas and Monte Carlo algorithms Randomized Quicksort Randomized selection Testing String Equality Pattern.
Advanced Counting Techniques CSC-2259 Discrete Structures Konstantin Busch - LSU1.
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
Lecture 6 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Advanced Algorithms Analysis and Design By Syed Hasnain Haider.
Merkle-Hellman Knapsack Cryptosystem
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 8. Greedy Algorithms.
5-1-1 CSC401 – Analysis of Algorithms Chapter 5--1 The Greedy Method Objectives Introduce the Brute Force method and the Greedy Method Compare the solutions.
Honors Track: Competitive Programming & Problem Solving Optimization Problems Kevin Verbeek.
Lecture 4,5 Mathematical Induction and Fibonacci Sequences.
Advanced Counting Techniques CSC-2259 Discrete Structures Konstantin Busch - LSU1.
NP-Complete Problems. Running Time v.s. Input Size Concern with problems whose complexity may be described by exponential functions. Tractable problems.
COMPSCI 102 Discrete Mathematics for Computer Science.
Dynamic Programming.  Decomposes a problem into a series of sub- problems  Builds up correct solutions to larger and larger sub- problems  Examples.
COMP108 Algorithmic Foundations Polynomial & Exponential Algorithms
MA/CSSE 473 Day 09 Modular Division Revisited Fermat's Little Theorem Primality Testing.
Public Key Cryptosystem In Symmetric or Private Key cryptosystems the encryption and decryption keys are either the same or can be easily found from each.
Ch3 /Lecture #4 Brute Force and Exhaustive Search 1.
Induction and Recursion CSC-2259 Discrete Structures Konstantin Busch - LSU1.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar Dr Nazir A. Zafar Advanced Algorithms Analysis and Design.
Exhaustive search Exhaustive search is simply a brute- force approach to combinatorial problems. It suggests generating each and every element of the problem.
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar.
Introduction to Algorithms: Brute-Force Algorithms.
Chapter 5 1. Chapter Summary  Mathematical Induction  Strong Induction  Recursive Definitions  Structural Induction  Recursive Algorithms.
Hubert Chan (Chapters 1.6, 1.7, 4.1)
COMP108 Algorithmic Foundations Polynomial & Exponential Algorithms
Advanced Algorithms Analysis and Design
Advanced Algorithms Analysis and Design
CS2210:0001Discrete Structures Induction and Recursion
Hubert Chan (Chapters 1.6, 1.7, 4.1)
CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett
Advanced Algorithms Analysis and Design
Greedy Algorithms.
Advanced Analysis of Algorithms
Chapter 11: Further Topics in Algebra
Discrete Mathematics CS 2610
Presentation transcript:

Design of Algorithms using Brute Force Approach

Primality Testing (given number is n binary digits)

Brute Force Approach Prime (n) for i  2 to n-1 if n  0 mod i then “number is composite” else “number is prime” The computational cost is  (n) First Algorithm for Testing Primality

Prime (n) for i  2 to n/2 if n  0 mod i then “number is composite” else “number is prime” The computational cost is  (n/2) Refined Algorithm for Testing Primality

Statement If n  N, n > 1 is not prime then n is divisible by some prime number p ≤ square root of n. Proof Since n is not prime hence it can be factored as n = x.ywhere 1 <x ≤ y <n x or y is a prime, if not it can be further factored out. Also suppose without loss of generality that x ≤ y Now our claim is that x ≤ sq(n) This is because otherwise x.y > n, a contradiction We only require to check till sqr(n) for primality test. Lemma

Prime (n) for i  2 to sqr(n) if n  0 mod i then “number is composite” else “number is prime” The computational cost is  (sqr(n)), much faster Refined Algorithm for Testing Primality

Sorting Sequence of Numbers

Input : A sequence of n numbers (distinct) Output : A permutation, of the input sequence such that Sorting Algorithm An Example of Algorithm

Sort the array [2, 4, 1, 3] in increasing order s1 = [4,3,2,1],s2 = [4,3,1,2],s3 = [4,1,2,3] s4 = [4,2,3,1],s5 = [4,1,3,2],s6 = [4,2,1,3] s7 = [3,4,2,1],s8 = [3,4,1,2],s9 = [3,1,2,4] s10 = [3,2,4,1],s11 = [3,1,4,2],s12 = [3,2,1,4] s13 = [2,3,4,1],s14 = [2,3,1,4],s15 = [2,1,4,3] s16 = [2,4,3,1],s17 = [2,1,3,4],s18 = [2,4,1,3] s19 = [1,3,2,4],s20 = [1,3,1,4],s21 = [1,4,2,3] s22 = [1,2,3,4],s23 = [1,4,3,2],s24 = [1,2,4,3] There are 4! = 24 number of permutations. For n number of elements there will be n! number of permutations. Hence cost of order n! for sorting. Sorting Algorithm: Brute Force Approach

Permute (i) \\initial call Permute(1)\\initial if i == N output A[N] else for j = i to N do swap(A[i], A[j]) permute(i+1) swap(A[i], A[j]) There are 4! = 24 number of permutations. For n number of elements there will be n! number of permutations. Hence cost of order n! for sorting. Generating Permutations

Theorem Prove, by mathematical induction, that computational cost of generating permutations is n!. Proof If n = 1, then the statement is true, because 1! =1 If there are k elements in set then no. of permutation = k! If we add one more element in any of the permutations, there will be k+1 number of ways to add it, resulting k+1 no. of permutations. Now total no. of permutations = k!(k+1) = (k+1)! Hence true for all n. Generating Permutations

0-1 Knapsack Problems

The knapsack problem arises whenever there is resource allocation with no financial constraints Problem Statement You are in Japan on an official visit and want to make shopping from a store (Best Denki) You have a list of required items You have also a bag (knapsack), of fixed capacity, and only you can fill this bag with the selected items Every item has a value (cost) and weight, And your objective is to seek most valuable set of items which you can buy not exceeding bag limit. 0-1 Knapsack Problem Statement

0-1 Knapsack Example Input Given n items each –weight w i –value v i Knapsack of capacity W Output: Find most valuable items that fit into the knapsack Example: item weight value knapsack capacity W =

0-1 Knapsack Problem Example Subset Total weight Total value 1.  0 0#WV 2. {1} {2} {3} {4} {1,2} {1,3} {1,4} {2,3} {2,4} {3,4} {1,2,3} 17 not feasible 13. {1,2,4} {1,3,4} 17 not feasible 15. {2,3,4} 20 not feasible 16.{1,2,3,4} 22 not feasible

Knapsack-BF (n, V, W, C) Compute all subsets, s, of S = {1, 2, 3, 4} forall s  S weight = Compute sum of weights of these items if weight > C, not feasible new solution = Compute sum of values of these items solution = solution  {new solution} Return maximum of solution 0-1 Knapsack Algorithm

0-1 Knapsack Algorithm Analysis Approach In brute force algorithm, we go through all combinations and find the one with maximum value and with total weight less or equal to W = 16 Complexity Cost of computing subsets O(2 n ) for n elements Cost of computing weight = O(2 n ) Cost of computing values = O(2 n ) Total cost in worst case: O(2 n )