Informatique Fondamentale IMA S8

Similar documents
Polycopié d'informatique Fondamentale IMA4-SC

VIII. NP-completeness

Limitations of Algorithm Power

NP-Completeness. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Data Structures in Java

Introduction to Complexity Theory

NP-Completeness. Andreas Klappenecker. [based on slides by Prof. Welch]

1. Introduction Recap

Complexity, P and NP

CS 320, Fall Dr. Geri Georg, Instructor 320 NP 1

CHAPTER 3 FUNDAMENTALS OF COMPUTATIONAL COMPLEXITY. E. Amaldi Foundations of Operations Research Politecnico di Milano 1

Undecidable Problems. Z. Sawa (TU Ostrava) Introd. to Theoretical Computer Science May 12, / 65

NP-complete problems. CSE 101: Design and Analysis of Algorithms Lecture 20

Harvard CS 121 and CSCI E-121 Lecture 22: The P vs. NP Question and NP-completeness

NP-Complete problems

NP-Completeness. Until now we have been designing algorithms for specific problems

NP Complete Problems. COMP 215 Lecture 20

NP-Complete Problems. More reductions

Computational complexity theory

Graph Theory and Optimization Computational Complexity (in brief)

Unit 1A: Computational Complexity

Theory of Computation CS3102 Spring 2014 A tale of computers, math, problem solving, life, love and tragic death

Computational Complexity and Intractability: An Introduction to the Theory of NP. Chapter 9

CS/COE

NP-Completeness. NP-Completeness 1

Nondeterministic Polynomial Time

BBM402-Lecture 11: The Class NP

Analysis of Algorithms. Unit 5 - Intractable Problems

NP-completeness. Chapter 34. Sergey Bereg

Polynomial-time Reductions

CS 241 Analysis of Algorithms

Complexity Theory of Polynomial-Time Problems

Computational complexity theory

ECS 120 Lesson 24 The Class N P, N P-complete Problems

Circuits. CSE 373 Data Structures

Lecture 4: NP and computational intractability

CS 583: Algorithms. NP Completeness Ch 34. Intractability

P is the class of problems for which there are algorithms that solve the problem in time O(n k ) for some constant k.

Polynomial-time reductions. We have seen several reductions:

Polynomial-Time Reductions

ECS122A Handout on NP-Completeness March 12, 2018

Correctness of Dijkstra s algorithm

Lecture 24: How to become Famous with P and NP

NP-Completeness. Subhash Suri. May 15, 2018

NP Completeness and Approximation Algorithms

Easy Problems vs. Hard Problems. CSE 421 Introduction to Algorithms Winter Is P a good definition of efficient? The class P

Summer School on Introduction to Algorithms and Optimization Techniques July 4-12, 2017 Organized by ACMU, ISI and IEEE CEDA.

Computational Complexity

P, NP, NP-Complete. Ruth Anderson

NP Completeness. CS 374: Algorithms & Models of Computation, Spring Lecture 23. November 19, 2015

CSC 8301 Design & Analysis of Algorithms: Lower Bounds

Algorithms. Grad Refresher Course 2011 University of British Columbia. Ron Maharik

Tractability. Some problems are intractable: as they grow large, we are unable to solve them in reasonable time What constitutes reasonable time?

Lecture 25: Cook s Theorem (1997) Steven Skiena. skiena

Chapter 34: NP-Completeness

NP-Completeness. ch34 Hewett. Problem. Tractable Intractable Non-computable computationally infeasible super poly-time alg. sol. E.g.

CS 350 Algorithms and Complexity

NP-Completeness I. Lecture Overview Introduction: Reduction and Expressiveness

Computers and Intractability. The Bandersnatch problem. The Bandersnatch problem. The Bandersnatch problem. A Guide to the Theory of NP-Completeness

COMP/MATH 300 Topics for Spring 2017 June 5, Review and Regular Languages

Computers and Intractability

Design and Analysis of Algorithms

1 Reductions and Expressiveness

The P versus NP Problem. Ker-I Ko. Stony Brook, New York

Algorithm Design Strategies V

13.1 Nondeterministic Polynomial Time

Show that the following problems are NP-complete

CSCI3390-Lecture 14: The class NP

More on NP and Reductions

CS 350 Algorithms and Complexity

Artificial Intelligence. 3 Problem Complexity. Prof. Dr. Jana Koehler Fall 2016 HSLU - JK

COSE215: Theory of Computation. Lecture 20 P, NP, and NP-Complete Problems

P,NP, NP-Hard and NP-Complete

NP-Completeness. f(n) \ n n sec sec sec. n sec 24.3 sec 5.2 mins. 2 n sec 17.9 mins 35.

Bounds on the Traveling Salesman Problem

Automata Theory CS Complexity Theory I: Polynomial Time

Comparison of several polynomial and exponential time complexity functions. Size n

CS 301: Complexity of Algorithms (Term I 2008) Alex Tiskin Harald Räcke. Hamiltonian Cycle. 8.5 Sequencing Problems. Directed Hamiltonian Cycle

Algorithms and Complexity theory

UC Berkeley CS 170: Efficient Algorithms and Intractable Problems Handout 22 Lecturer: David Wagner April 24, Notes 22 for CS 170

3130CIT Theory of Computation

Algorithms: COMP3121/3821/9101/9801

Hamiltonian Graphs Graphs

FINAL EXAM PRACTICE PROBLEMS CMSC 451 (Spring 2016)

Algorithms Design & Analysis. Approximation Algorithm

Notes for Lecture 21

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Limitations of Algorithms

University of Toronto Department of Electrical and Computer Engineering. Final Examination. ECE 345 Algorithms and Data Structures Fall 2016

1. (a) Explain the asymptotic notations used in algorithm analysis. (b) Prove that f(n)=0(h(n)) where f(n)=0(g(n)) and g(n)=0(h(n)).

Algorithm Design and Analysis

Introduction. Pvs.NPExample

Algorithms. NP -Complete Problems. Dong Kyue Kim Hanyang University

Exercises NP-completeness

Computability and Complexity Theory

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: NP-Completeness I Date: 11/13/18

NP-Completeness. Algorithmique Fall semester 2011/12

Friday Four Square! Today at 4:15PM, Outside Gates

4/12/2011. Chapter 8. NP and Computational Intractability. Directed Hamiltonian Cycle. Traveling Salesman Problem. Directed Hamiltonian Cycle

Admin NP-COMPLETE PROBLEMS. Run-time analysis. Tractable vs. intractable problems 5/2/13. What is a tractable problem?

} } } Lecture 23: Computational Complexity. Lecture Overview. Definitions: EXP R. uncomputable/ undecidable P C EXP C R = = Examples

Transcription:

Informatique Fondamentale IMA S8 Cours 4 : graphs, problems and algorithms on graphs, (notions of) NP completeness Laure Gonnord http://laure.gonnord.org/pro/teaching/ Laure.Gonnord@polytech-lille.fr Université Lille 1 - Polytech Lille 2012

IV - Graphs, problems and algorithms on graphs, NP-completeness Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 2 / 64

Some generalities on graphs 1 Some generalities on graphs Generalities and first definitions Internal representation 2 Paths finding - search, and applications 3 Algorithms specific to oriented graphs 4 More difficult problems on graphs 5 NP - completeness 6 Docs Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 3 / 64

Some generalities on graphs Generalities and first definitions Goal Model : relationships between objects ( I know him, This register is in conflict with with one, there is a road between these two towns ) classical problems of paths ( Do I have a friend that knows a friend... who knows the Dalai-Lama?, It is possible to go from this town to this another in less than 600 km?, how to get out of the labyrinth? ) Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 4 / 64

Some generalities on graphs Generalities and first definitions Who? Euler (1740) formalised the theory and the Konisberg problem : Problem : compute an eulerian path. Source (fr) : http://fr.wikipedia.org/wiki/th%c3%a9orie_des_graphes Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 5 / 64

Some generalities on graphs Generalities and first definitions Other classical problems The travelling salesman problem : TSP Task scheduling (on parallel machines or not!), the PERT method. Coloring maps Covering (telecom) trees Minimal paths... Graphs are everywhere. Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 6 / 64

Some generalities on graphs Generalities and first definitions General definition Graph A finite set of vertices (a vertex) : V A finite set of edges. E V V v 1 v3 e 1 e 3 v2 A simplier model than the previous ones! Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 7 / 64

Some generalities on graphs Generalities and first definitions Other definitions Non Oriented Graph iff (v1, v2), (v1, v2) E (v2, v1) E. Tree (non oriented acyclic connex) - Forest. Directed Acyclic Graph : DAG. Oriented Graph with no cycle. trees are dags! v1 v3 v2 v4 Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 8 / 64

Some generalities on graphs Internal representation Sources Picture and Java codes from http://pauillac.inria.fr/~levy/courses/x/if/a1/a1.html Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 9 / 64

Some generalities on graphs Internal representation Adjacency matrix 3 4 0 1 2 0 1 0 0 0 1 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 1 0 If the graph is oriented, then the matrix is NOT symmetric Memory complexity : O(V 2 ) Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 10 / 64

Some generalities on graphs Internal representation Adjacency matrix - Java boolean [ ] [ ] m; Graph ( i n t n ) { m = new boolean [ n ] [ n ] ; for ( i n t i = 0; i < n ; ++ i ) for ( i n t j = 0; j < n ; ++ j ) m[ x ] [ y ] = f a l s e ; } s t a t i c void addedge ( Graph g, i n t x, i n t y ) { g.m[ x ] [ y ] = t r u e ; g.m[ y ] [ x ] = t r u e / / only i f non o r i e n t e d Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 11 / 64

Some generalities on graphs Internal representation Array of lists 3 4 0 2 1 Memory complexity : O(V + E) Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 12 / 64

Some generalities on graphs Internal representation Array of lists - Java L i s t e [ ] succ ; Graph ( i n t n ) { succ = new L i s t e [ n ] ; } } s t a t i c void addedge ( Graph g, i n t x, i n t y ) { g. succ [ x ] = new L i s t e ( y, g. succ [ x ] ) ; / / i f non o r i e n t e d also modify g. succ [ y ]! } Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 13 / 64

Paths finding - search, and applications 1 Some generalities on graphs 2 Paths finding - search, and applications Looking for all nodes Connexity Topological Sort 3 Algorithms specific to oriented graphs 4 More difficult problems on graphs 5 NP - completeness 6 Docs Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 14 / 64

Paths finding - search, and applications Looking for all nodes Search Algorithms! Translation problem! (en) Search Algorithms - (fr) Parcours de Graphes Reminder : search/do algorithm on a binary tree : 1 If the tree is not empty, do f(root). 2 recursively apply the function on the left sub-tree 3 recursively apply the function on the right sub-tree Deep-First Search Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 15 / 64

Paths finding - search, and applications Looking for all nodes Deep First Search on Graphs On graphs, there can be cycles! Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 16 / 64

Paths finding - search, and applications Looking for all nodes Deep First Search on Graphs - Java s t a t i c i n t [ ] num; / / numbering the edges according to the v i s i t i n g or s t a t i c void v i s i t e r ( Graphe g ) { i n t n = g. succ. l e n g t h ; num = new i n t [ n ] ; numordre = 1; for ( i n t x = 0; x < n ; ++x ) num [ x ] = 1; / / 1 = not seen for ( i n t x = 0; x < n ; ++x ) i f (num [ x ] == 1) dfs ( g, x ) ; } s t a t i c void dfs ( Graphe g, i n t x ) { num [ x ] = ++numordre ; for ( L i s t e l s = g. succ [ x ] ; l s!= n u l l ; l s = l s. s u i v a n t ) { i n t y = l s. v a l ; i f (num [ y ] == 1) dfs ( g, y ) ; } } Complexity = O(V + E). Tarjan [1972] Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 17 / 64

Paths finding - search, and applications Looking for all nodes Breadth First Search on Graphs Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 18 / 64

Paths finding - search, and applications Looking for all nodes Conventions BLANC = not (yet) processed NOIR = has been processed GRIS = being processed f i n a l s t a t i c i n t BLANC = 0, GRIS = 1, NOIR = 2; s t a t i c i n t [ ] couleur ; Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 19 / 64

Paths finding - search, and applications Looking for all nodes Breadth First Search on Graphs - Java i n t n = g. succ. l e n g th ; couleur = new i n t [ n ] ; FIFO f = new FIFO ( n ) ; for ( i n t x = 0; x < n ; ++x ) couleur [ x ] = BLANC; for ( i n t x = 0; x < n ; ++x ) i f ( couleur [ x ] == BLANC) { FIFO. a j o u t e r ( f, x ) ; couleur [ x ] = GRIS ; bfs ( g, f ) ; } s t a t i c void bfs ( Graphe g, FIFO f ) { while (! f. vide ( ) ) { i n t x = FIFO. supprimer ( f ) ; for ( L i s t e l s = g. succ [ x ] ; l s!= n u l l ; l s = l s. s u i v a n t ) { i n t y = l s. v a l ; i f ( couleur [ y ] == BLANC) { FIFO. a j o u t e r ( f, y ) ; couleur [ y ] = GRIS ; } couleur [ x ] = NOIR ; } } } Complexity? Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 20 / 64

Paths finding - search, and applications Looking for all nodes Application : Labyrinth Finds exit, ie a path from d to s? } couleur [ d ] = GRIS ; i f ( d == s ) return new L i s t e ( d, n u l l ) ; for ( L i s t e l s = g. succ [ d ] ; l s!= n u l l ; l s = l s. s u i v a n t ) { i n t x = l s. v a l ; i f (num [ x ] == BLANC) { L i s t e r = chemin ( g, x, s ) ; i f ( r!= n u l l ) return new L i s t e ( d, r ) ; } } return n u l l ; Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 21 / 64

Paths finding - search, and applications Connexity Definition - Connex Component Connex component A connex component is a maximal set of connected vertices. Exercise : Algorithm for computing CCs. Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 22 / 64

Paths finding - search, and applications Connexity Definition - Connex Graph Connex graph A graph is connex if it has only one connex component. Exercise : Algorithm that tests the connexity of a graph. Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 23 / 64

Paths finding - search, and applications Topological Sort Other Algorithms There exists variants of DFS algo to : Compute Cover Forets (in bold in the picture) : Detect Cycles. Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 24 / 64

Algorithms specific to oriented graphs 1 Some generalities on graphs 2 Paths finding - search, and applications 3 Algorithms specific to oriented graphs 4 More difficult problems on graphs 5 NP - completeness 6 Docs Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 25 / 64

Algorithms specific to oriented graphs Topological sorting of a DAG Give a list of vertices in an order compatible with the DAG order : v i < v j v j v i (total order from a partial header). Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 26 / 64

Algorithms specific to oriented graphs Topological sorting of a DAG - Example 1 4 6 2 5 7 3 If we know an entrant node, a dfs gives the result. Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 27 / 64

Algorithms specific to oriented graphs Topological sorting of a DAG - implementation f i n a l s t a t i c i n t BLANC = 0, GRIS = 1, NOIR = 2; s t a t i c L i s t e afaire ( Graphe g, i n t x ) { i n t n = g. succ. l e n g t h ; i n t [ ] couleur = new i n t [ n ] ; for ( i n t x = 0; x < n ; ++x ) couleur [ x ] = BLANC; return dfs ( g, x, n u l l, couleur ) ; } s t a t i c L i s t e dfs ( Graphe g, i n t x, L i s t e r, i n t [ ] couleur ) { couleur [ x ] = GRIS ; for ( L i s t e l s = g. succ [ x ] ; l s!= n u l l ; l s = l s. s u i v a n t ) { i n t y = l s. v a l ; i f ( couleur [ y ] == BLANC) r = dfs ( g, y, r, couleur ) ; } return new L i s t e ( x, r ) ; } dfs here returns a list. Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 28 / 64

Algorithms specific to oriented graphs Topological sorting of a DAG - Applications instruction scheduling cell evaluation in spreadsheets order of compilation tasks (Makefiles) symbol dependencies in linkers sheduling in project managements (PERT, 1960) http://en.wikipedia.org/wiki/topological_sorting Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 29 / 64

Algorithms specific to oriented graphs Transitive closure The graph G + = (V, E + ) of the transitive closure of G = (V, E) is such that E + is the smallest set verifying : E E + (x, y) E + and (y, z) E + (x, z) E + Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 30 / 64

Algorithms specific to oriented graphs Algorithm for Transitive closure If E is the adjacency matrix : E + = E + E 2 +... E n 1. An algorithm in O(n 4 ). Is it possible to have a better complexity? Yes (Warshall, in O(n 3 ), see later). Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 31 / 64

Algorithms specific to oriented graphs Definition of oriented valuated graph Valuated graph It s a graph with a function d : V V N called distance. G is represented by an adjacency matrix with integer values, or + if there is no edge. Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 32 / 64

Algorithms specific to oriented graphs The shortest path problem Given G, find a path whose sum of distances is minimal! more variants on http://en.wikipedia.org/wiki/shortest_path_problem Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 33 / 64

Algorithms specific to oriented graphs Multiple Source Shortest Path with dynamic programming -1 Between all the vertices! Floyd-Warshall algorithm Principle : Let d k x,y be the minimal distance between x and y passing through vertices whose number is < k. Then d 0 x,y = d x,y and d k x,y = min{d k 1 x,y, d k 1 x,k + dk 1 k,y } Store all these temporary distances. Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 34 / 64

Algorithms specific to oriented graphs Multiple Source Shortest Path with dynamic programming -2 s t a t i c Graphe pluscourtschemins ( Graphe g ) { i n t n = g. d. l e n g t h ; Graphe gplus = copiegraphe ( g ) ; for ( i n t k = 0; k < n ; ++k ) for ( i n t x = 0; x < n ; ++x ) for ( i n t y = 0; y < n ; ++y ) gplus. d [ x ] [ y ] = Math. min ( gplus. d [ x ] [ y ], gplus. d [ x ] [ k ] + gplus. d [ k ] [ y ] ) ; return gplus ; } Complexity O(n 3 ), space O(n 2 ). Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 35 / 64

Algorithms specific to oriented graphs Simple Source Shortest Path All d are > 0 : Dijkstra, 1959 Negative distances : Bellman Ford 1960 Routing algorithms (Bellman uses only local information). Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 36 / 64

More difficult problems on graphs 1 Some generalities on graphs 2 Paths finding - search, and applications 3 Algorithms specific to oriented graphs 4 More difficult problems on graphs 5 NP - completeness 6 Docs Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 37 / 64

More difficult problems on graphs Eulerian and Hamiltonian paths Eulerian/Hamiltonian Path An Eulerian path : each edge is seen only once. An Hamiltonien path : each node... Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 38 / 64

More difficult problems on graphs Hamiltonian Tour, one solution Extend a path until not possible Backtrack one time, and try with another neighbour And so on. Worst case complexity time : exponential in n Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 39 / 64

More difficult problems on graphs Application 1 : Knight s Tour Is it possible to make a Knight s tour? «The knight is placed on the empty board and, moving according to the rules of chess, must visit each square exactly once.» A variant of the Hamiltonian tour that can be solved in polynomial time. Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 40 / 64

More difficult problems on graphs The Travelling Salesman Problem with valuated (non oriented) graph. Find an hamiltonian cycle of minimum weight Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 41 / 64

More difficult problems on graphs Travelling Salesman Problem - 2 A (non-polynomial) algorithm for this problem : Transform into a linear programming problem with integers Solve it with the simplex! This algorithm is exponential in the size of the graph Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 42 / 64

More difficult problems on graphs Graph Coloring Problem Application to the register allocation in compilers. The sudoku problem (9-coloring of a 81-vertices graph) Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 43 / 64

More difficult problems on graphs Graph Coloring Problem - 2 Are you able to design a polynomial algorithm? Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 44 / 64

More difficult problems on graphs Graph Coloring Problem - 3 We do not know any polynomial algorithm for this problem. Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 45 / 64

NP - completeness 1 Some generalities on graphs 2 Paths finding - search, and applications 3 Algorithms specific to oriented graphs 4 More difficult problems on graphs 5 NP - completeness 6 Docs Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 46 / 64

NP - completeness Complexity The complexity of an implementation / an algorithm is linked to Turing machines : The number of steps in the execution of a TM gives the complexity in time, The number of seen squares in the execution of a TM gives the complexity in space. We can replace the TM by a C implementation. Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 47 / 64

NP - completeness P Problems P A problem (or a language) belongs to P if there exists a deterministic Turing Machine that gives the result in polynomial time. there is a polynom p such that for all entry x, the Turing machine stops (with the good result) in less than p( x ) steps. Example : L = a n b n c n, the TM of the previous course checks any a i b i c i in O(n) steps Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 48 / 64

NP - completeness P Problems Integer multiplication Eulerian tour (each edge once) Linear programming (recall that polynomial diophantine equations are undecidible.) Primality test http://www.cs.uu.nl/groups/ad/compl-diaz.pdf Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 49 / 64

NP - completeness NP Problems NP A problem (or a language) belongs to NP if there exists a deterministic Turing Machine that checks the result in polynomial time. Example : there exists a TM that is able to check if a given path is an hamiltonien tour, in polynomial time. Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 50 / 64

NP - completeness NP Problems - Examples Factoring 3-SAT (formulae in CNF with 3 litterals on each term) Hamiltonian tour 3-colorability http://www.cs.uu.nl/groups/ad/compl-diaz.pdf Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 51 / 64

NP - completeness NP vs P - some facts If a problem belongs to NP, then there exists an exponential brute-force deterministic algorithm to solve it (easy to prove) P NP (trivial) P = NP? P NP? Open question! Problem of the millenium! Is finding more difficult that verifying? Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 52 / 64

NP - completeness NP-complete problems - 1 Given a problem : if we find a polynomial algorithm P if we find a polynomial time checking algorithm NP, but we want to know more : it it really hard to solve it? is it worth looking for a better algorithm? Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 53 / 64

NP - completeness NP-complete problems - 2 The key notion is the notion of polynomial reduction Polynomial reduction of problems Given two problems P 1 and P 2, P 1 reduces polynomially to P 2 if there exists an f such that : f is polynomial x 1 solution of P 1 iff f(x 1 ) solution of P 2. Example : Hamiltonian circuit is polynomially reductible to TSP. Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 54 / 64

NP - completeness NP-complete problems - 3 The most difficult problems in NP. All problems in NPC are computationally equivalent in the sense that, if one problem is easy, all the problems in the class are easy. Therefore, if one problem in NPC turns out to be in P, then P=NP Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 55 / 64

NP - completeness NP-complete problems - 4 Reminder : we want to characterise the most difficult problems in NP. NP-complete A problem is said to be NP-complete if : It belongs to NP All other problems in NP reduce polynomially to it. In other words, if we solve any NP-complete in polynomial time, we have proved P = NP (and we become rich) Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 56 / 64

NP - completeness NP-complete problems - 4 Problem : how to prove that a problem is NP-complete? The very first one is hard to prove Then, we use the following result : If two problems / languages L 1 and L 2 belong to NP and L 1 is NP-complete, and L 1 reduces polynomially to L 2, then L 2 is NP-complete. Pick one NP complete problem and try to reduce it to your problem. Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 57 / 64

NP - completeness NP-complete problems - The historical example SATISFIABILITY is NP-complete (Cook, 1971) SAT reduces polynomially to 3SAT 3SAT reduces polynomially to Vertex Cover Vertex Cover reduces polynomially to Hamiltonian circuit So Hamiltonian circuit, then TSP are also NP-complete problems Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 58 / 64

NP - completeness Some NP-complete (common) problems Some classical examples : Hamiltonian cycle and TSP Graph Coloring Problem Vertex cover Knapsack (integer) Flow shop problem Sudoku The book : Computers and Intractability : A Guide to the Theory of NP-Completeness. A short list on the french webpage : http://fr.wikipedia.org/wiki/liste_de_probl%c3%a8mes_ NP-complets Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 59 / 64

NP - completeness Conclusion - 1 Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 60 / 64

NP - completeness Conclusion - 2 Knowing that a given problem is NP complete is just the beginning : handle less general classes of inputs compute less precise solutions Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 61 / 64

Docs 1 Some generalities on graphs 2 Paths finding - search, and applications 3 Algorithms specific to oriented graphs 4 More difficult problems on graphs 5 NP - completeness 6 Docs Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 62 / 64

Docs More docs on graphs In french : http://laure.gonnord.org/site-ens/mim/ graphes/cours/cours_graphes.pdf or type cours de Théorie des graphes in Google In english : an interactive tutorial here : http://primes. utm.edu/cgi-bin/caldwell/tutor/graph/intro. (en) The theorical book Graph Theory, R. Diestel (electronic version : http://diestel-graph-theory.com/basic.html) (en) e-book for algorithms : http: //code.google.com/p/graph-theory-algorithms-book/ Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 63 / 64

Docs More docs on complexity The Book : computers and intractability, a guide to the theory of NP completeness, by Garey/Johnson Laure Gonnord (Lille1/Polytech) Informatique Fondamentale IMA S8 2012 S8-SC 64 / 64