Nondeterministic Programming in C++

Similar documents
The Critical-Path Algorithm

The Traveling Salesman Problem Brute Force Method

Minimal Spanning Trees

The Traveling Salesman Problem Nearest-Neighbor Algorithm

Street-Routing Problems

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College

The Traveling Salesman Problem Cheapest-Link Algorithm

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

Rotations and Translations

The Decreasing-Time Algorithm

Scheduling and Digraphs

Scope and Parameter Passing

Sampling Distribution Examples Sections 15.4, 15.5

Scope and Parameter Passing

Recursion. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 17, 2018

The string Class. Lecture 21 Sections 2.9, 3.9, Robb T. Koether. Wed, Oct 17, Hampden-Sydney College

Solving Recursive Sequences by Iteration

Boolean Expressions. Lecture 31 Sections 6.6, 6.7. Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

XPath Lecture 34. Robb T. Koether. Hampden-Sydney College. Wed, Apr 11, 2012

Recognition of Tokens

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

The Pairwise-Comparison Method

Recursion. Lecture 26 Sections , Robb T. Koether. Hampden-Sydney College. Mon, Apr 6, 2015

Regular Expressions. Lecture 10 Sections Robb T. Koether. Hampden-Sydney College. Wed, Sep 14, 2016

LR Parsing - Conflicts

Implementing Linked Lists

The CYK Parsing Algorithm

Recursive Descent Parsers

The Graphics Pipeline

The Coefficient of Determination

Magnification and Minification

Density Curves Sections

Boxplots. Lecture 17 Section Robb T. Koether. Hampden-Sydney College. Wed, Feb 10, 2010

Shader Programs. Lecture 30 Subsections 2.8.2, Robb T. Koether. Hampden-Sydney College. Wed, Nov 16, 2011

Linked Lists. Lecture 16 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 22, 2017

More NP-complete Problems. CS255 Chris Pollett May 3, 2006.

Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015

XPath. Lecture 36. Robb T. Koether. Wed, Apr 16, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, / 28

Displaying Distributions - Quantitative Variables

Introduction to Databases

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

The Plurality-with-Elimination Method

The Graphics Pipeline

The Class Construct Part 1

Recursive Linked Lists

NP-Complete Reductions 2

Aggregation. Lecture 7 Section Robb T. Koether. Hampden-Sydney College. Wed, Jan 29, 2014

Binary Tree Applications

Shading Triangles. Lecture 37. Robb T. Koether. Hampden-Sydney College. Mon, Nov 30, 2015

PHP Queries and HTML Forms Lecture 23

List Iterators. Lecture 27 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

The Normal Distribution

Building the Abstract Syntax Trees

XML Attributes. Lecture 33. Robb T. Koether. Hampden-Sydney College. Wed, Apr 25, 2018

Friends and Unary Operators

Integer Overflow. Lecture 8 Section 2.5. Robb T. Koether. Hampden-Sydney College. Mon, Jan 27, 2014

Dynamic Allocation of Memory

The Mesh Class. Lecture 26. Robb T. Koether. Wed, Oct 28, Hampden-Sydney College

Webpage Navigation. Lecture 27. Robb T. Koether. Hampden-Sydney College. Mon, Apr 2, 2018

List Iterators. Lecture 34 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 24, 2013

Total Orders. Lecture 41 Section 8.5. Robb T. Koether. Hampden-Sydney College. Mon, Apr 8, 2013

The Constructors. Lecture 7 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 1, 2017

Introduction to Compiler Design

Mipmaps. Lecture 35. Robb T. Koether. Hampden-Sydney College. Wed, Nov 18, 2015

Programming Languages

Programming Languages

Form Validation. Lecture 25. Robb T. Koether. Hampden-Sydney College. Wed, Mar 23, 2018

The Projection Matrix

The Mesh Class. Lecture 23. Robb T. Koether. Hampden-Sydney College. Wed, Oct 18, 2017

Function Definition Syntax Tree

Specular Reflection. Lecture 19. Robb T. Koether. Hampden-Sydney College. Wed, Oct 4, 2017

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

MySQL Creating a Database Lecture 3

8 NP-complete problem Hard problems: demo

Relational Databases Lecture 2

Inheritance: The Fundamental Functions

Operators. Lecture 12 Section Robb T. Koether. Hampden-Sydney College. Fri, Feb 9, 2018

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Mon, Jan 20, 2014

Applying Textures. Lecture 27. Robb T. Koether. Hampden-Sydney College. Fri, Nov 3, 2017

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Mon, Feb 22, 2016

Stacks and their Applications

PHP Arrays. Lecture 20. Robb T. Koether. Hampden-Sydney College. Wed, Feb 28, 2018

Abstract Data Types. Lecture 23 Section 7.1. Robb T. Koether. Hampden-Sydney College. Wed, Oct 24, 2012

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Fri, Feb 16, 2018

The Class Construct Part 2

Inheritance: The Fundamental Functions

Basic PHP. Lecture 19. Robb T. Koether. Hampden-Sydney College. Mon, Feb 26, 2108

Function Usage. Lecture 15 Sections 6.3, 6.4. Robb T. Koether. Hampden-Sydney College. Mon, Oct 1, 2018

Dynamic Allocation of Memory

Introduction to Databases

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

The x86 Instruction Set

Fundamental Data Types

XQuery FLOWR Expressions Lecture 35

Triggers. Lecture 14. Robb T. Koether. Hampden-Sydney College. Wed, Feb 14, 2018

Basic PHP Lecture 17

Review. Lecture 39. Robb T. Koether. Mon, Dec 5, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) Review Mon, Dec 5, / 14

List Iterator Implementation

Ambient and Diffuse Light

Transcription:

Nondeterministic Programming in C++ Lecture 37 Sections 14.5 Robb T. Koether Hampden-Sydney College Wed, Nov 30, 2016 Robb T. Koether (Hampden-Sydney College) Nondeterministic Programming in C++ Wed, Nov 30, 2016 1 / 22

1 Nondeterministic Programming in C++ 2 The Composites Problem 3 The Subset Sum Problem 4 The Traveling Salesman Problem 5 Collected 6 Assignment Robb T. Koether (Hampden-Sydney College) Nondeterministic Programming in C++ Wed, Nov 30, 2016 2 / 22

Outline 1 Nondeterministic Programming in C++ 2 The Composites Problem 3 The Subset Sum Problem 4 The Traveling Salesman Problem 5 Collected 6 Assignment Robb T. Koether (Hampden-Sydney College) Nondeterministic Programming in C++ Wed, Nov 30, 2016 3 / 22

The Nondeterministic Library We will assume that we have available the following three functions. accept() reject() choose() The function accept() outputs accept and exits the program. The function reject() outputs reject and exits the program. Robb T. Koether (Hampden-Sydney College) Nondeterministic Programming in C++ Wed, Nov 30, 2016 4 / 22

The choose() Function The function choose() takes a set S as its parameter. choose(s) will return a single value from the set S. If there is a choice of values from S that will eventually lead the algorithm to call accept() and a choice that leads it to call reject(), then choose() will choose at random a value that leads to accept(). Robb T. Koether (Hampden-Sydney College) Nondeterministic Programming in C++ Wed, Nov 30, 2016 5 / 22

The choose() Function If there is no such choice, i.e., every choice leads to accept() or every choice leads to reject(), then choose() will choose a random value from the set S. The execution time of choose(s) is O( S ). Robb T. Koether (Hampden-Sydney College) Nondeterministic Programming in C++ Wed, Nov 30, 2016 6 / 22

Outline 1 Nondeterministic Programming in C++ 2 The Composites Problem 3 The Subset Sum Problem 4 The Traveling Salesman Problem 5 Collected 6 Assignment Robb T. Koether (Hampden-Sydney College) Nondeterministic Programming in C++ Wed, Nov 30, 2016 7 / 22

The Composites Problem Definition (Composite Number) A positive integer n is composite if there exist integers a and b, with 1 < a < n and 1 < b < n such that n = ab. Robb T. Koether (Hampden-Sydney College) Nondeterministic Programming in C++ Wed, Nov 30, 2016 8 / 22

The Composites Problem Definition (Composite Number) A positive integer n is composite if there exist integers a and b, with 1 < a < n and 1 < b < n such that n = ab. Definition (The Composites Problem) Given a integer n, the Composites Problem (COMP) asks whether n is composite. Robb T. Koether (Hampden-Sydney College) Nondeterministic Programming in C++ Wed, Nov 30, 2016 8 / 22

The Composites Problem Definition (Composite Number) A positive integer n is composite if there exist integers a and b, with 1 < a < n and 1 < b < n such that n = ab. Definition (The Composites Problem) Given a integer n, the Composites Problem (COMP) asks whether n is composite. Theorem COMP NP Robb T. Koether (Hampden-Sydney College) Nondeterministic Programming in C++ Wed, Nov 30, 2016 8 / 22

The Composites Problem The Composites Problem COMPOSITES(int m) { int n = log2(m) + 1; int a = 0; int b = 0; // Generate a solution (two integers) for (i = 1; i <= n; i++) { a = 2*a + choose(0, 1); b = 2*b + choose(0, 1); } // Verify the solution if (m == a*b && a > 1 && b > 1) accept(); else reject(); } Robb T. Koether (Hampden-Sydney College) Nondeterministic Programming in C++ Wed, Nov 30, 2016 9 / 22

The Composites Problem Why not choose from the set of possible divisors? int a = choose(2, 3, 4,..., m - 1); int b = choose(2, 3, 4,..., m - 1); Robb T. Koether (Hampden-Sydney College) Nondeterministic Programming in C++ Wed, Nov 30, 2016 10 / 22

Outline 1 Nondeterministic Programming in C++ 2 The Composites Problem 3 The Subset Sum Problem 4 The Traveling Salesman Problem 5 Collected 6 Assignment Robb T. Koether (Hampden-Sydney College) Nondeterministic Programming in C++ Wed, Nov 30, 2016 11 / 22

The Subset Sum Problem Definition (The Subset Sum Problem) Given a set of integers A = {a 1, a 2,..., a n } and an integer k, the Subset Sum Problem (SUBSET) asks there is a subset B A whose elements a i1, a i2,..., a im add up to k. Robb T. Koether (Hampden-Sydney College) Nondeterministic Programming in C++ Wed, Nov 30, 2016 12 / 22

The Subset Sum Problem Definition (The Subset Sum Problem) Given a set of integers A = {a 1, a 2,..., a n } and an integer k, the Subset Sum Problem (SUBSET) asks there is a subset B A whose elements a i1, a i2,..., a im add up to k. Theorem SUBSET NP Robb T. Koether (Hampden-Sydney College) Nondeterministic Programming in C++ Wed, Nov 30, 2016 12 / 22

The Subset Sum Problem Example (The Subset Sum Problem) Let the set be A = {297, 820, 987, 713, 638, 545, 68, 981, 324, 559, 422, 246, 642, 485, 734, 352, 268, 431, 601, 979} and let k = 1856. Robb T. Koether (Hampden-Sydney College) Nondeterministic Programming in C++ Wed, Nov 30, 2016 13 / 22

The Subset Sum Problem The Subset Sum Problem SUBSET(Set A, int k) { // Generate a solution (a subset) Set B; for (int i = 0; i < A.size(); i++) { bool b = choose(true, false); if (b) B += i; } // Verify the solution int sum = 0; for (int i = 0; i < B.size(); i++) sum += A.element(i); if (sum == k) accept(); else reject(); } Robb T. Koether (Hampden-Sydney College) Nondeterministic Programming in C++ Wed, Nov 30, 2016 14 / 22

Outline 1 Nondeterministic Programming in C++ 2 The Composites Problem 3 The Subset Sum Problem 4 The Traveling Salesman Problem 5 Collected 6 Assignment Robb T. Koether (Hampden-Sydney College) Nondeterministic Programming in C++ Wed, Nov 30, 2016 15 / 22

The Traveling Salesman Problem Definition (The Traveling Salesman Problem) Given a complete weighted graph G and an integer k, the Traveling Salesman Problem (TS) asks whether G contain a circuit that passes through every vertex and has total weight less than or equal to k? Robb T. Koether (Hampden-Sydney College) Nondeterministic Programming in C++ Wed, Nov 30, 2016 16 / 22

The Traveling Salesman Problem Definition (The Traveling Salesman Problem) Given a complete weighted graph G and an integer k, the Traveling Salesman Problem (TS) asks whether G contain a circuit that passes through every vertex and has total weight less than or equal to k? Theorem TS NP Robb T. Koether (Hampden-Sydney College) Nondeterministic Programming in C++ Wed, Nov 30, 2016 16 / 22

The Traveling Salesman Problem Example (The Traveling Salesman Problem) Let the distances between cities be A B C D E F G H I J and let k = 487. A 0 12 33 29 53 36 58 48 73 57 B 12 0 26 41 54 41 58 53 84 69 C 33 26 0 59 33 31 36 42 88 84 D 29 41 59 0 68 47 73 53 53 28 E 53 54 33 68 0 21 5 21 73 84 F 36 41 31 47 21 0 26 12 58 64 G 58 58 36 73 5 26 0 26 78 90 H 48 53 42 53 21 12 26 0 52 65 I 73 84 88 53 73 58 78 52 0 37 J 57 69 84 28 84 64 90 65 37 0 Robb T. Koether (Hampden-Sydney College) Nondeterministic Programming in C++ Wed, Nov 30, 2016 17 / 22

The Traveling Salesman Problem The Traveling Salesman Problem TS(Graph G, int k) { // Generate a solution (a permutation) Set V = G.vertices(); int path[g.size()]; for (int i = 0; i < G.size(); i++) { path[i] = choose(v); V -= path[i]; } // Verify the solution int dist = 0; for (int i = 0; i < G.size() - 1; i++) dist += G.weight(path[i], path[i + 1]); dist += G.weight(path[G.size() - 1], path[0]); if (dist <= k) accept(); else reject(); } Robb T. Koether (Hampden-Sydney College) Nondeterministic Programming in C++ Wed, Nov 30, 2016 18 / 22

Outline 1 Nondeterministic Programming in C++ 2 The Composites Problem 3 The Subset Sum Problem 4 The Traveling Salesman Problem 5 Collected 6 Assignment Robb T. Koether (Hampden-Sydney College) Nondeterministic Programming in C++ Wed, Nov 30, 2016 19 / 22

Collected Collected To be collect on Friday, December 2. Section 12.1 Exercises 3, 13. Section 12.2 Exercise 4. Section 14.1 Exercises 1a, 2. Section 14.2 Exercises 4, 5. Robb T. Koether (Hampden-Sydney College) Nondeterministic Programming in C++ Wed, Nov 30, 2016 20 / 22

Outline 1 Nondeterministic Programming in C++ 2 The Composites Problem 3 The Subset Sum Problem 4 The Traveling Salesman Problem 5 Collected 6 Assignment Robb T. Koether (Hampden-Sydney College) Nondeterministic Programming in C++ Wed, Nov 30, 2016 21 / 22

Assignment Assignment Write C++ programs to solve the following problems nondeterministically in polynomial time using the functions choose(), accept(), and reject(). (A) Solve the Hamiltonian Path Problem HAMPATH. (B) Solve the Clique Problem CLIQ. (C) Solve the Vertex Cover Problem VC. Robb T. Koether (Hampden-Sydney College) Nondeterministic Programming in C++ Wed, Nov 30, 2016 22 / 22