43
Data Structures in Java Session 22 Instructor: Bert Huang http://www.cs.columbia.edu/~bert/courses/3134

Data Structures in Javabert/courses/3134/slides/Lecture22.pdfAnnouncements • Homework 5 solutions posted • Homework 6 to be posted this weekend • Final exam Thursday, Dec. 17th,

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Data Structures in Javabert/courses/3134/slides/Lecture22.pdfAnnouncements • Homework 5 solutions posted • Homework 6 to be posted this weekend • Final exam Thursday, Dec. 17th,

Data Structures in JavaSession 22

Instructor: Bert Huanghttp://www.cs.columbia.edu/~bert/courses/3134

Page 2: Data Structures in Javabert/courses/3134/slides/Lecture22.pdfAnnouncements • Homework 5 solutions posted • Homework 6 to be posted this weekend • Final exam Thursday, Dec. 17th,

Announcements• Homework 5 solutions posted

• Homework 6 to be posted this weekend

• Final exam Thursday, Dec. 17th, 4-7 PM, Hamilton 602 (this room)

• same format as midterm (open book/notes)

• Distinguished Lecture: Barbara Liskov, MIT. Turing Award Winner 2009. 11 AM Monday. Davis Auditorium, CEPSR/Schapiro

Page 3: Data Structures in Javabert/courses/3134/slides/Lecture22.pdfAnnouncements • Homework 5 solutions posted • Homework 6 to be posted this weekend • Final exam Thursday, Dec. 17th,

Review

• Sorting Algorithm Examples

• QuickSort space clarification

• External sorting*

Page 4: Data Structures in Javabert/courses/3134/slides/Lecture22.pdfAnnouncements • Homework 5 solutions posted • Homework 6 to be posted this weekend • Final exam Thursday, Dec. 17th,

Todayʼs Plan

• Correction on external sorting

• Complexity Classes

• P, NP, NP-Complete, NP-Hard

Page 5: Data Structures in Javabert/courses/3134/slides/Lecture22.pdfAnnouncements • Homework 5 solutions posted • Homework 6 to be posted this weekend • Final exam Thursday, Dec. 17th,

External Sorting

Disk 3

Disk 2

Disk 1

Disk 0

Page 6: Data Structures in Javabert/courses/3134/slides/Lecture22.pdfAnnouncements • Homework 5 solutions posted • Homework 6 to be posted this weekend • Final exam Thursday, Dec. 17th,

Disk 3

Disk 2

Disk 1

Disk 0

External Sorting

Page 7: Data Structures in Javabert/courses/3134/slides/Lecture22.pdfAnnouncements • Homework 5 solutions posted • Homework 6 to be posted this weekend • Final exam Thursday, Dec. 17th,

Disk 3

Disk 2

Disk 1

Disk 0

External Sorting

Page 8: Data Structures in Javabert/courses/3134/slides/Lecture22.pdfAnnouncements • Homework 5 solutions posted • Homework 6 to be posted this weekend • Final exam Thursday, Dec. 17th,

Disk 3

Disk 2

Disk 1

Disk 0

External Sorting

Page 9: Data Structures in Javabert/courses/3134/slides/Lecture22.pdfAnnouncements • Homework 5 solutions posted • Homework 6 to be posted this weekend • Final exam Thursday, Dec. 17th,

Disk 3

Disk 2

Disk 1

Disk 0

External Sorting

Page 10: Data Structures in Javabert/courses/3134/slides/Lecture22.pdfAnnouncements • Homework 5 solutions posted • Homework 6 to be posted this weekend • Final exam Thursday, Dec. 17th,

Complexity of Problems

• Weʼve been concerned with the complexity of algorithms

• It is important to also consider the complexity of problems

• Understanding complexity is important for theory, and also for practice

• understanding the hardness of problems helps us build better algorithms

Page 11: Data Structures in Javabert/courses/3134/slides/Lecture22.pdfAnnouncements • Homework 5 solutions posted • Homework 6 to be posted this weekend • Final exam Thursday, Dec. 17th,

Three Graph Problems

• Euler Path: does a path exist that uses every edge exactly once?

• Hamiltonian Path: does a path exist that visits every node exactly once?

• Traveling Salesman: find the shortest path that visits every node exactly once?

Page 12: Data Structures in Javabert/courses/3134/slides/Lecture22.pdfAnnouncements • Homework 5 solutions posted • Homework 6 to be posted this weekend • Final exam Thursday, Dec. 17th,

Complexity Classes• P - solvable in polynomial time

• NP - solvable in polynomial time by a nondeterministic computer

• i.e., you can check a solution in polynomial time

• NP-complete - a problem in NP such that any problem in NP is polynomially reducible to it

• Undecidable - no algorithm can solve the problem

Page 13: Data Structures in Javabert/courses/3134/slides/Lecture22.pdfAnnouncements • Homework 5 solutions posted • Homework 6 to be posted this weekend • Final exam Thursday, Dec. 17th,

Probable Complexity Class Hierarchy

P

NP

Undecidable

NP-Complete

NP-Hard

Page 14: Data Structures in Javabert/courses/3134/slides/Lecture22.pdfAnnouncements • Homework 5 solutions posted • Homework 6 to be posted this weekend • Final exam Thursday, Dec. 17th,

Polynomial Time P

• All the algorithms we cover in class are solvable in polynomial time

• An algorithm that runs in polynomial time is considered efficient

• A problem solvable in polynomial time is considered tractable

Page 15: Data Structures in Javabert/courses/3134/slides/Lecture22.pdfAnnouncements • Homework 5 solutions posted • Homework 6 to be posted this weekend • Final exam Thursday, Dec. 17th,

Nondeterministic Polynomial Time NP• Consider a magical nondeterministic

computer

• infinitely parallel computer

• Equivalently, to solve any problem, check every possible solution in parallel

• return one that passes the check

Page 16: Data Structures in Javabert/courses/3134/slides/Lecture22.pdfAnnouncements • Homework 5 solutions posted • Homework 6 to be posted this weekend • Final exam Thursday, Dec. 17th,

NP-Complete• Special class of NP problems that can be

used to solve any other NP problem

• Hamiltonian Path, Satisfiability, Graph Coloring etc.

• NP-Complete problems can be reduced to other NP-Complete problems:

• polynomial time algorithm to convert the input and output of algorithms

Page 17: Data Structures in Javabert/courses/3134/slides/Lecture22.pdfAnnouncements • Homework 5 solutions posted • Homework 6 to be posted this weekend • Final exam Thursday, Dec. 17th,

NP-Hard

• A problem is NP-Hard if it is at least as complex as all NP-Complete problems

• NP-hard problems may not even be NP

Page 18: Data Structures in Javabert/courses/3134/slides/Lecture22.pdfAnnouncements • Homework 5 solutions posted • Homework 6 to be posted this weekend • Final exam Thursday, Dec. 17th,

Undecidable• No algorithm can solve undecidable problems

• halting problem - given a computer program, determine if the computer program will finish

• Sketch of undecidability proof: Assume we have an algorithm that detects infinite loops

• Write program LOOP(P) that runs P on itself

• If P(P) halts, infinite loop, otherwise output

Page 19: Data Structures in Javabert/courses/3134/slides/Lecture22.pdfAnnouncements • Homework 5 solutions posted • Homework 6 to be posted this weekend • Final exam Thursday, Dec. 17th,

Halting ProblemLOOP(P):If P(P) will halt: infinite loopIf P(P) runs forever: output

• What happens if we call LOOP(LOOP)?

LOOP(LOOP):If LOOP(LOOP) will halt: infinite loopIf LOOP(LOOP) runs forever: output

Page 20: Data Structures in Javabert/courses/3134/slides/Lecture22.pdfAnnouncements • Homework 5 solutions posted • Homework 6 to be posted this weekend • Final exam Thursday, Dec. 17th,

P vs. NP• So far, nobody has proven a super-polynomial

lower bound on any NP-Complete problems,

• nor has anyone found a polynomial time algorithm for an NP-complete problem

• Therefore no problem is proven to be in one class but not the other;

• it is unknown if P and NP are the same

Page 21: Data Structures in Javabert/courses/3134/slides/Lecture22.pdfAnnouncements • Homework 5 solutions posted • Homework 6 to be posted this weekend • Final exam Thursday, Dec. 17th,

P?

Complexity Class Hierarchy

P?

NP

Undecidable

NP-Complete

NP-Hard

Page 22: Data Structures in Javabert/courses/3134/slides/Lecture22.pdfAnnouncements • Homework 5 solutions posted • Homework 6 to be posted this weekend • Final exam Thursday, Dec. 17th,

P Problems• Most problems weʼve solved

• Proving a polynomial (or logarithmic) bound on any algorithm proves a problem is in P

• Euler paths - does a path exist that uses every edge?

• Return if there are zero or two nodes with odd degree.

Page 23: Data Structures in Javabert/courses/3134/slides/Lecture22.pdfAnnouncements • Homework 5 solutions posted • Homework 6 to be posted this weekend • Final exam Thursday, Dec. 17th,

Finding an Euler Circuit

• Run a partial DFS; search down a path until you need to backtrack (mark edges instead of nodes)

• At this point, you will have found a circuit

• Find first node along the circuit that has unvisited edges; run a DFS starting with that edge

• Splice the new circuit into the main circuit, repeat until all edges are visited

Page 24: Data Structures in Javabert/courses/3134/slides/Lecture22.pdfAnnouncements • Homework 5 solutions posted • Homework 6 to be posted this weekend • Final exam Thursday, Dec. 17th,

Euler Circuit Example

2 3

1

5 6

4

7

Page 25: Data Structures in Javabert/courses/3134/slides/Lecture22.pdfAnnouncements • Homework 5 solutions posted • Homework 6 to be posted this weekend • Final exam Thursday, Dec. 17th,

7

Euler Circuit Example

2 3

1

5 6

4

1

Page 26: Data Structures in Javabert/courses/3134/slides/Lecture22.pdfAnnouncements • Homework 5 solutions posted • Homework 6 to be posted this weekend • Final exam Thursday, Dec. 17th,

7

Euler Circuit Example

2 3

1

5 6

4

1

2

Page 27: Data Structures in Javabert/courses/3134/slides/Lecture22.pdfAnnouncements • Homework 5 solutions posted • Homework 6 to be posted this weekend • Final exam Thursday, Dec. 17th,

7

Euler Circuit Example

2 3

1

5 6

4

1

2

3

Page 28: Data Structures in Javabert/courses/3134/slides/Lecture22.pdfAnnouncements • Homework 5 solutions posted • Homework 6 to be posted this weekend • Final exam Thursday, Dec. 17th,

7

Euler Circuit Example

2 3

1

5 6

4

1

2

3

1

Page 29: Data Structures in Javabert/courses/3134/slides/Lecture22.pdfAnnouncements • Homework 5 solutions posted • Homework 6 to be posted this weekend • Final exam Thursday, Dec. 17th,

7

Euler Circuit Example

2 3

1

5 6

4

1

2

3

1

4

Page 30: Data Structures in Javabert/courses/3134/slides/Lecture22.pdfAnnouncements • Homework 5 solutions posted • Homework 6 to be posted this weekend • Final exam Thursday, Dec. 17th,

7

Euler Circuit Example

2 3

1

5 6

4

1

2

3

1

4

3

Page 31: Data Structures in Javabert/courses/3134/slides/Lecture22.pdfAnnouncements • Homework 5 solutions posted • Homework 6 to be posted this weekend • Final exam Thursday, Dec. 17th,

7

Euler Circuit Example

2 3

1

5 6

4

1

2

3

1

4

3

6

Page 32: Data Structures in Javabert/courses/3134/slides/Lecture22.pdfAnnouncements • Homework 5 solutions posted • Homework 6 to be posted this weekend • Final exam Thursday, Dec. 17th,

7

Euler Circuit Example

2 3

1

5 6

4

1

2

3

1

4

3

6

4

Page 33: Data Structures in Javabert/courses/3134/slides/Lecture22.pdfAnnouncements • Homework 5 solutions posted • Homework 6 to be posted this weekend • Final exam Thursday, Dec. 17th,

7

Euler Circuit Example

2 3

1

5 6

4

1

2

3

1

4

3

6

4

5

Page 34: Data Structures in Javabert/courses/3134/slides/Lecture22.pdfAnnouncements • Homework 5 solutions posted • Homework 6 to be posted this weekend • Final exam Thursday, Dec. 17th,

7

Euler Circuit Example

2 3

1

5 6

4

1

2

3 1

4

3

6

4

5

2

Page 35: Data Structures in Javabert/courses/3134/slides/Lecture22.pdfAnnouncements • Homework 5 solutions posted • Homework 6 to be posted this weekend • Final exam Thursday, Dec. 17th,

7

Euler Circuit Example

2 3

1

5 6

4

1

2

3 1

4

3

6

4

5

2

6

Page 36: Data Structures in Javabert/courses/3134/slides/Lecture22.pdfAnnouncements • Homework 5 solutions posted • Homework 6 to be posted this weekend • Final exam Thursday, Dec. 17th,

7

Euler Circuit Example

2 3

1

5 6

4

1

2

3 1

4

3

6

4

5

2

6

7

Page 37: Data Structures in Javabert/courses/3134/slides/Lecture22.pdfAnnouncements • Homework 5 solutions posted • Homework 6 to be posted this weekend • Final exam Thursday, Dec. 17th,

7

Euler Circuit Example

2 3

1

5 6

4

1

2

3 1

4

3

6

4

5

2

6

7 5

Page 38: Data Structures in Javabert/courses/3134/slides/Lecture22.pdfAnnouncements • Homework 5 solutions posted • Homework 6 to be posted this weekend • Final exam Thursday, Dec. 17th,

NP-Complete ProblemsSatisfiability

• Given Boolean expression of N variables, can we set variables to make expression true?

• First NP-Complete proof because Cookʼs Theorem gave polynomial time procedure to convert any NP problem to a Boolean expression

• I.e., if we have efficient algorithm for Satisfiability, we can efficiently solve any NP problem

Page 39: Data Structures in Javabert/courses/3134/slides/Lecture22.pdfAnnouncements • Homework 5 solutions posted • Homework 6 to be posted this weekend • Final exam Thursday, Dec. 17th,

NP-Complete Problems Graph Coloring

• Given a graph is it possible to color with k colors all nodes so no adjacent nodes are the same color?

• Coloring countries on a map

• Sudoku is a form of this problem. All squares in a row, column and blocks are connected. k = 9

Page 40: Data Structures in Javabert/courses/3134/slides/Lecture22.pdfAnnouncements • Homework 5 solutions posted • Homework 6 to be posted this weekend • Final exam Thursday, Dec. 17th,

NP-Complete ProblemsHamiltonian Path

• Given a graph with N nodes, is there a path that visits each node exactly once?

Page 41: Data Structures in Javabert/courses/3134/slides/Lecture22.pdfAnnouncements • Homework 5 solutions posted • Homework 6 to be posted this weekend • Final exam Thursday, Dec. 17th,

NP-Hard Problems Traveling Salesman• Closely related to Hamiltonian Path problem

• Given complete graph G, find the shortest path that visits all nodes

• If we are able to solve TSP, we can find a Hamiltonian Path; set connected edge weight to constant, disconnected to infinity

• TSP is NP-hard

Page 42: Data Structures in Javabert/courses/3134/slides/Lecture22.pdfAnnouncements • Homework 5 solutions posted • Homework 6 to be posted this weekend • Final exam Thursday, Dec. 17th,

http://www.tsp.gatech.edu/gallery/itours/usa13509.html

Page 43: Data Structures in Javabert/courses/3134/slides/Lecture22.pdfAnnouncements • Homework 5 solutions posted • Homework 6 to be posted this weekend • Final exam Thursday, Dec. 17th,

Reading

• Weiss 9.7

• http://www.tsp.gatech.edu/