LeongHW, SoC, NUS (UIT2201: AI) Page 1 © Leong Hon Wai, 2003-2008 Integrating Different Ideas Together  Reading Materials:  Ch 3.6 of [SG]  Contents:

Slides:



Advertisements
Similar presentations
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 © Leong Hon Wai, Animation of Algorithm Goal: To understand an algorithm by animating its execution,
Advertisements

CSC 2300 Data Structures & Algorithms March 16, 2007 Chapter 7. Sorting.
Discrete Structure Li Tak Sing( 李德成 ) Lectures
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 Animation of Algorithm Goal: To understand an algorithm by animating its execution, step-by-step. Algorithm:
LeongHW, SOC, NUS (UIT2201:3 Database) Page 1 Copyright © by Leong Hon Wai Database – Info Storage and Retrieval  Aim: Understand basics of  Info.
LeongHW, SOC, NUS (UIT2201:3 Database) Page 1 Copyright © by Leong Hon Wai Database – Info Storage and Retrieval  Aim: Understand basics of  Info.
Courtesy Costas Busch - RPI1 A Universal Turing Machine.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
CHAPTER 2 CS
1 Data Structures A program solves a problem. A program solves a problem. A solution consists of: A solution consists of:  a way to organize the data.
CS333 / Cutler Amortized Analysis 1 Amortized Analysis The average cost of a sequence of n operations on a given Data Structure. Aggregate Analysis Accounting.
Sorting Lower Bound Andreas Klappenecker based on slides by Prof. Welch 1.
Fall 2004COMP 3351 A Universal Turing Machine. Fall 2004COMP 3352 Turing Machines are “hardwired” they execute only one program A limitation of Turing.
1 Discrete Mathematics Summer 2004 By Dan Barrish-Flood originally for Fundamental Algorithms For use by Harper Langston in D.M.
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.
Data Structure Algorithm Analysis TA: Abbas Sarraf
- ABHRA DASGUPTA Solving Adhoc and Math related problems.
Divide-and-Conquer 7 2  9 4   2   4   7
CS 473Lecture 121 CS473-Algorithms I Lecture 12 Amortized Analysis.
Ragesh Jaiswal Indian Institute of Technology Delhi Threshold Direct Product Theorems: a survey.
Algorithm Efficiency CS 110: Data Structures and Algorithms First Semester,
LeongHW, SoC, NUS (UIT2201: Database) Page 1 © Leong Hon Wai, Animation of SQL Queries To illustrate three SQL queries: –Q1: simple select (one.
LeongHW, SOC, NUS (UIT2201:3 Database) Page 1 Copyright © by Leong Hon Wai Database – Info Storage and Retrieval  Aim: Understand basics of  Info.
Time Complexity UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Analysis of Algorithms
1 Recurrences Algorithms Jay Urbain, PhD Credits: Discrete Mathematics and Its Applications, by Kenneth Rosen The Design and Analysis of.
Chapter 3 Sec 3.3 With Question/Answer Animations 1.
10/14/ Algorithms1 Algorithms - Ch2 - Sorting.
1 Chapter 7 Computer Arithmetic Smruti Ranjan Sarangi Computer Organisation and Architecture PowerPoint Slides PROPRIETARY MATERIAL. © 2014 The McGraw-Hill.
Unsolvability and Infeasibility. Computability (Solvable) A problem is computable if it is possible to write a computer program to solve it. Can all problems.
Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 31.
The Integers. The Division Algorithms A high-school question: Compute 58/17. We can write 58 as 58 = 3 (17) + 7 This forms illustrates the answer: “3.
CSC 413/513: Intro to Algorithms NP Completeness.
Week 10Complexity of Algorithms1 Hard Computational Problems Some computational problems are hard Despite a numerous attempts we do not know any efficient.
A Universal Turing Machine
1 Chapter 17: Amortized Analysis I. 2 About this lecture Given a data structure, amortized analysis studies in a sequence of operations, the average time.
Advanced Algorithm Design and Analysis (Lecture 12) SW5 fall 2004 Simonas Šaltenis E1-215b
Amortized Analysis We examined worst-case, average-case and best-case analysis performance In amortized analysis we care for the cost of one operation.
LeongHW, SoC, NUS (UIT2201: Networks) Page 1© Leong Hon Wai, (MACP) Medium Access Control Protocol.
Amortized Analysis Some of the slides are from Prof. Leong Hon Wai’s resources at National University of Singapore.
1 Linear Bounded Automata LBAs. 2 Linear Bounded Automata (LBAs) are the same as Turing Machines with one difference: The input string tape space is the.
1 Turing’s Thesis. 2 Turing’s thesis: Any computation carried out by mechanical means can be performed by a Turing Machine (1930)
© Leong Hon Wai, LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 Animation of Algorithm Goal: To understand an algorithm by animating its execution,
COMPSCI 102 Discrete Mathematics for Computer Science.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
CSCE 411H Design and Analysis of Algorithms Set 10: Lower Bounds Prof. Evdokia Nikolova* Spring 2013 CSCE 411H, Spring 2013: Set 10 1 * Slides adapted.
Amortized Analysis. Problem What is the time complexity of n insert operations into an dynamic array, which doubles its size each time it is completely.
Introduction to Algorithms Amortized Analysis My T. UF.
Big O David Kauchak cs302 Spring Administrative Assignment 1: how’d it go? Assignment 2: out soon… Lab code.
Linear Sorting. Comparison based sorting Any sorting algorithm which is based on comparing the input elements has a lower bound of Proof, since there.
The NP class. NP-completeness Lecture2. The NP-class The NP class is a class that contains all the problems that can be decided by a Non-Deterministic.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS Spring 2003 Lecture 2Jan 16, 2003Carnegie Mellon University.
LeongHW, SoC&USP, NUS (UTT2201: Introduction) Page 1 © Leong Hon Wai, UIT2201: CS & IT Revolution  Assessment:  Participation: 05 %  Tutorials:20.
Introduction to Algorithms: Amortized Analysis. Introduction to Algorithms Amortized Analysis Dynamic tables Aggregate method Accounting method Potential.
Probabilistic Algorithms
Analysis of Algorithms
Decision trees Polynomial-Time
Chapter 17: Amortized Analysis I
CPSC 411 Design and Analysis of Algorithms
CSCE 411 Design and Analysis of Algorithms
CS 2210 Discrete Structures Algorithms and Complexity
CSCE 411 Design and Analysis of Algorithms
Enumerating Distances Using Spanners of Bounded Degree
Discovery and Design of Algorithms
CPSC 411 Design and Analysis of Algorithms
Divide-and-Conquer 7 2  9 4   2   4   7
CS 2210 Discrete Structures Algorithms and Complexity
Presentation transcript:

LeongHW, SoC, NUS (UIT2201: AI) Page 1 © Leong Hon Wai, Integrating Different Ideas Together  Reading Materials:  Ch 3.6 of [SG]  Contents:  Incrementing a Binary Counter  How many subsets, Representation  Printing All Subsets Problem  Exponential Time Algorithms

LeongHW, SoC, NUS (UIT2201: AI) Page 2 © Leong Hon Wai, Four seemingly unrelated problems P1: You are given a bit-array A = (A[n−1], A[n−2],... A[1] A[0]), where each A[k] is 0 or 1. Given an algorithm for incrementing A by 1. P2: Suppose you are given a set S = {s 1, s 2,..., s n } of n objects, and a subset X of S. How can we use the bit-vector A to represent the subset X? P3: Using P1 and P2 above, or otherwise, give a simple algorithm to generate and print all subsets of S. P4: What is the running time of your algorithm? [Note: Recall that there are 2 n subsets altogether. (A proof is provided by P1 and P2 above.)]

Hon Wai Leong, NUS (CS5234, 28 Aug 2007) Page L3.3 Copyright © by Leong Hon Wai) n-bit Binary Counter xA[4]A[3]A[2]A[1]A[0]Cost

Hon Wai Leong, NUS (CS5234, 28 Aug 2007) Page L3.4 Copyright © by Leong Hon Wai) Incrementing a Binary Counter  n-bit Binary Counter: A[0..n  1] x = A[n−1] ⋅ 2 (n−1) A[1] ⋅ A[0] ⋅ 2 0 I NCREMENT (A) 1. i  0 2. while i < length[A] and A[i] = 1 3. do A[i]  0 ⊳ reset a bit 4. i  i if i < length[A] 6. then A[i]  1 ⊳ set a bit

LeongHW, SoC, NUS (UIT2201: AI) Page 5 © Leong Hon Wai, P1: You are given a bit-array A = (A[n−1], A[n−2],... A[1] A[0]), where each A[k] is 0 or 1. Given an algorithm for incrementing A by 1. Note: For UIT2201, only need to understand the first 2 slides on the problem P1 (incrementing a binary counter). (The other slides are included for info only.) Four seemingly unrelated problems

Hon Wai Leong, NUS (CS5234, 28 Aug 2007) Page L3.6 Copyright © by Leong Hon Wai) Worst-case analysis Consider a sequence of n insertions. The worst-case time to execute one insertion is  (k). Therefore, the worst-case time for all the n insertions is n ·  (k) =  (n  k). In fact, WRONG! In fact, the worst-case cost for n insertions is only  (n) ≪  (n  k). Let’s see why.

Hon Wai Leong, NUS (CS5234, 28 Aug 2007) Page L3.7 Copyright © by Leong Hon Wai) Tighter analysis xA[4]A[3]A[2]A[1]A[0]Cost A[0] flipped every op n A[1] flipped every 2 ops n/2 A[2] flipped every 4 ops n/2 2 A[3] flipped every 8 ops n/2 3 … … … … … A[i] flipped every 2 i ops n/2 i Total cost of n operations

Hon Wai Leong, NUS (CS5234, 28 Aug 2007) Page L3.8 Copyright © by Leong Hon Wai) Tighter analysis (continued) Cost of n increments. Thus, the average cost of each increment operation is  (n)/n =  (1).

Hon Wai Leong, SoC, NUS (Creative Problem Solving) Page 9 Copyright © by Leong Hon Wai P2: Using n-bit-array to represent subsets?

LeongHW, SoC, NUS (UIT2201: AI) Page 10 © Leong Hon Wai, Four seemingly unrelated problems P1: You are given a bit-array A = (A[n−1], A[n−2],... A[1] A[0]), where each A[k] is 0 or 1. Given an algorithm for incrementing A by 1. P2: Suppose you are given a set S = {s 1, s 2,..., s n } of n objects, and a subset X of S. How can we use the bit-vector A to represent the subset X? Note: For UIT2201, only need to understand bit-representation method for problem P2. (The other slides are included for info only.)

Hon Wai Leong, SoC, NUS (Creative Problem Solving) Page 11 Copyright © by Leong Hon Wai PS: (Power-Set Example) (1) Problem: Given a set S with n elements. How many subsets of S are there? Stage 1: Understanding the Problem PQ: What is the unknown? [# subsets of S] PQ: What is the data? [A set S with n elements] PQ: What is the condition? [Subsets of S. Need to count all of them.] PQ: Is it sufficient? [Yes? Can count one-by-one, but tedious]

Hon Wai Leong, SoC, NUS (Creative Problem Solving) Page 12 Copyright © by Leong Hon Wai PS: (Power-Set Example) (2) Stage 2: Devising a Plan PQ: Have you seen the problem before? PQ: Can you try to work out some small instances? PQ: Can you see any pattern? Stage 3: Carrying out the Plan. PQ: Can you prove the result? n S P(S) # 0 ϕ ϕ 1 1 {x 1 } ϕ, {x 1 } 2 2 {x 1, x 2 } ϕ, {x 1 }, { x 2 }, {x 1, x 2 } 4 3 {x 1, x 2, x 3 } ϕ, {x 1 }, { x 2 }, {x 1, x 2 } {x 3 }, { x 1, x 3 },{ x 2, x 3 }, {x 1, x 2, x 3 } 8

Hon Wai Leong, SoC, NUS (Creative Problem Solving) Page 13 Copyright © by Leong Hon Wai PS: (Power-Set Example) (3) Another Approach? PQ: Can you solve it differently? PQ: Introduce suitable notations? Let A n be the # of subsets of a S with n element. Now, consider the set S’ = S U {x} with n+1 elements. Divide subsets of S’ into (P1) those containing element x, and (P2) those that do not contain element x. Those in P2 are exactly all the subsets of S (and we have A n of them). For every subset T of S in P2, there is a corresponding subset that contains x, namely T U {x} in P1. Thus, there is a 1-1 correspondence betw subsets in P1 and those in P2. Therefore, A n+1 = 2  A n for all n ≥ 0. In addition, we know A 0 = 1.

Hon Wai Leong, SoC, NUS (Creative Problem Solving) Page 14 Copyright © by Leong Hon Wai PS: (Power-Set Example) (4) Yet Another Approach? PQ: Draw a figure? c'c' cc'c' ccc'c' cc'c' bb'b aa' Where are the subset?

Hon Wai Leong, SoC, NUS (Creative Problem Solving) Page 15 Copyright © by Leong Hon Wai PS: (Power-Set Example) (5) Yet Another Method? PQ: Look at the unknown. [Subsets! They come in different sizes] IDEA: Let’s count those of the same size! How many of size 0?1 How many of size 1? n How many of size 2? n(n-1)/2 = n C 2... How many of size k? n C k... How many of size n–1?n How many of size n? 1 Total # of subsets is

Hon Wai Leong, SoC, NUS (Creative Problem Solving) Page 16 Copyright © by Leong Hon Wai PS: (Power-Set Example) (6) Yet another method? PQ: How can the subsets be represented? Let the vector (b 1, b 2, …, b n ) represent the subset T where b k =1, if the element x k is in the set T, and 0 otherwise. PQ: How many such bit-strings are there? Subset Bit-Representation ϕ (0,0,…,0,0) {x 1 } (1,0,…,0,0) {x 2 } (0,1,…,0,0) {x 1, x 2 } (1,1,…,0,0) {x 1, x 2,..., x n } (1,1,…,1,1)

LeongHW, SoC, NUS (UIT2201: AI) Page 17 © Leong Hon Wai, Four seemingly unrelated problems P1: You are given a bit-array A = (A[n−1], A[n−2],... A[1] A[0]), where each A[k] is 0 or 1. Given an algorithm for incrementing A by 1. P2: Suppose you are given a set S = {s 1, s 2,..., s n } of n objects, and a subset X of S. How can we use the bit-vector A to represent the subset X? P3: Using P1 and P2 above, or otherwise, give a simple algorithm to generate and print all subsets of S. P4: What is the running time of your algorithm? [Note: Recall that there are 2 n subsets altogether. (A proof is provided byP1 and P2 above.)]

Hon Wai Leong, SoC, NUS (Creative Problem Solving) Page 18 Copyright © by Leong Hon Wai Solution to P3 and P4 Outline of algorithm for P3: Represent subsets using bit vector A 1. Start with A = (0, 0,..., 0, 0) 2. Repeat these steps 3. Increment the bit vector A (use algorithm from P1) 4. Print out the subset corresponding to A 5. until A = (1, 1,..., 1, 1) Analysis (P4): Steps 3,4 done 2 n time; (there are 2 n subsets!)

Hon Wai Leong, SoC, NUS (Creative Problem Solving) Page 19 Copyright © by Leong Hon Wai Solution to P3 and P4 Analysis (P4): Steps 3,4 done 2 n time; (there are 2 n subsets!) Step 3 takes time at most O(n) Step 4 takes time at most O(n) Total time: 2 n * O(n) = O(n 2 n ) This is an exponential time algorithm! Lower Bound: Since there 2 n subsets; Just printing them will take at least 2 n time!

Hon Wai Leong, SoC, NUS (Creative Problem Solving) Page 20 Copyright © by Leong Hon Wai One Final Question ?

LeongHW, SoC, NUS (UIT2201: AI) Page 21 © Leong Hon Wai, Exponential Time Problems…  Intel designs CPU chips for most of todays’ computers (IBM-compatible, Macs, Linux, etc)  If Intel wants to thoroughly verify their multiplication circuit is correct (i.e. produces the correct product for all input combinations), what does it need to do?  Check that for every a, b compute and verify the product (a * b)  Do you think that Intel has actually done that for their 32-bit and 64-bit processors?

Hon Wai Leong, SoC, NUS (Creative Problem Solving) Page 22 Copyright © by Leong Hon Wai Thank you!

LeongHW, SoC, NUS (UIT2201: AI) Page 23 © Leong Hon Wai,