complexity ©d.moshkovitz 1 paths on the reasonability of finding paths in graphs

45
Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Post on 21-Dec-2015

230 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Complexity©D.Moshkovitz

1

PathsOn the Reasonability of Finding Paths in Graphs

Page 2: Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Complexity©D.Moshkovitz

2

Introduction

• Objectives:– To introduce more graph theory

problems.

• Overview:– Hamiltonian paths– Eulerian paths

Page 3: Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Complexity©D.Moshkovitz

3

Hamiltonian Path

Instance: a directed graph G=(V,E) and two vertices s,tV.

Problem: To decide if there exists a path from s to t, which goes through each node once.

Page 4: Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Complexity©D.Moshkovitz

4

Can You Find One Here?

s t

Page 5: Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Complexity©D.Moshkovitz

5

HAMPATH is in NP

• Non-deterministically choose v1,…,vnV (n=|V|).

• For any 1i,jn, verify vivj.

• Verify s=v1 and t=vn.

• For any 1in, verify (vi,vi+1)E.

Page 6: Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Complexity©D.Moshkovitz

6

HAMPATH is NP-Complete

Proof: We’ll show 3SATpHAMPATH.

SIP 262-267

..)..(.......)..(..

s

t

. . .

p

Page 7: Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Complexity©D.Moshkovitz

7

Representing Variables

For any variable xi,

Diamond

. . .

Page 8: Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Complexity©D.Moshkovitz

8

Representing Clauses

• For any clause ci,

Page 9: Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Complexity©D.Moshkovitz

9

High-Level Structure

. . .

x1

x2

.

.

.

xl

c1

c2

c3

.

.

.

ck

. . .

. . .

s

. . .

. . .

t

Page 10: Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Complexity©D.Moshkovitz

10

The Internal Structure of the Diamonds

k pairs of internal nodesk pairs of internal nodesk pairs of internal nodesk pairs of internal nodes

Diamond

. . .

Page 11: Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Complexity©D.Moshkovitz

11

Connecting Clauses to Variables

If the clause cj contains the literal xi,

j’th pair

xi

cj

. . .

Page 12: Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Complexity©D.Moshkovitz

12

Connecting Clauses to Variables

If the clause cj contains the literal xi,

j’th pair

xi

cj

. . .

Page 13: Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Complexity©D.Moshkovitz

13

Construction Completed

• This concludes the construction.• Its size is polynomial in the size of

the formula (Check!).

• We proceed to prove correctness.

Page 14: Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Complexity©D.Moshkovitz

14

Completeness

• Assume there exists a satisfying assignment for the 3CNF formula.

• Let us demonstrate a Hamiltonian tour on the graph.

Page 15: Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Complexity©D.Moshkovitz

15

Assignment Path

If the variable xi is assigned TRUE,

. . .

Page 16: Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Complexity©D.Moshkovitz

16

Assignment Path

If the variable xi is assigned FALSE,

. . .

Page 17: Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Complexity©D.Moshkovitz

17

Covering the ClausesIf the clause cj is satisfied due to the literal

xi,

xi

cj

. . .

j’th pair

Page 18: Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Complexity©D.Moshkovitz

18

. . .

Covering the ClausesIf the clause cj is satisfied due to the literal

xi,

xi

cj

j’th pair

Page 19: Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Complexity©D.Moshkovitz

19

Soundness

• Assume there exists a Hamiltonian tour on the graph,

• Let us construct a satisfying assignment for the 3CNF formula.

Page 20: Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Complexity©D.Moshkovitz

20

Observation

• Every Hamiltonian path must contain either the entire right path or the entire left path of each diamond.

. . .

Page 21: Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Complexity©D.Moshkovitz

21

Path Assignment

• For each variable, decide on its truth value,

• According to the direction of the path in its diamond.

. . .

Page 22: Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Complexity©D.Moshkovitz

22

x1

x2

.

.

.

xl

. . .

. . .

s

. . .

. . .

t

Observation

cj

A Hamiltonian path cannot jump from inside one

diamond to another

Proof Idea: Assume it can and

observe this node

Page 23: Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Complexity©D.Moshkovitz

23

Satisfaction

• Note that because of the observation

• And the fact the path goes through all clause nodes,

• All clauses are satisfied

Page 24: Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Complexity©D.Moshkovitz

24

The Punch Line

HAMPATH is NP-Complete.

Page 25: Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Complexity©D.Moshkovitz

25

Food for Thought…

Is HAMCYCLE (Whether there exists a simple cycle which goes through all

nodes) NP-Complete?

Page 26: Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Complexity©D.Moshkovitz

26

Food for Thought…

What about finding Hamiltonian paths in undirected graphs?

Page 27: Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Complexity©D.Moshkovitz

27

Similar Problem

• Children usually like this riddle:

• Can you draw the following shape in one line?

Page 28: Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Complexity©D.Moshkovitz

28

Eulerian Path

Instance: an undirected graph G=(V,E) and two vertices stV.

Problem: To decide if there exists a path from s to t, which goes through each edge exactly once.

Page 29: Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Complexity©D.Moshkovitz

29

Can You Find One Here?

s t

Page 30: Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Complexity©D.Moshkovitz

30

Eulerian Paths Vs. Hamiltonian Paths

• What do you make of this problem?

• Is it reducible to HAMPATH?• Or vice-versa?

?

Page 31: Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Complexity©D.Moshkovitz

31

Page 32: Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Complexity©D.Moshkovitz

32

The Euler Theorem

Theorem: A connected graph has an Eulerian path from s to t iff

1. s and t’s degrees are odd.2. the degree of all other

vertices is even.

Page 33: Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Complexity©D.Moshkovitz

33

First Direction

• Assume there is an Eulerian path from s to t in the graph.

• Let us prove all degrees, but s and t’s, are odd.

Page 34: Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Complexity©D.Moshkovitz

34

Analyzing Degrees

• Observe the path. It passes through all edges.• For internal vertices, it gets out every time it gets

in, thus adding 2 to the degree each time.• Except it first gets out of s and finally gets in to t.

Page 35: Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Complexity©D.Moshkovitz

35

Second Direction

• Assume all degrees, but s and t’s, are even.

• Let us describe an algorithm which finds an Eulerian path from s to t.

Page 36: Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Complexity©D.Moshkovitz

36

Walks• Start at s,• While there are new edges

– Arbitrarily choose such,– And walk on it.

Page 37: Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Complexity©D.Moshkovitz

37

Observation

Claim: Such a walk must get stuck at t.

Proof: After leaving s, t is the only vertex whose degree is odd. Thus we can get out of every other vertex we visit.

Page 38: Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Complexity©D.Moshkovitz

38

Were All Edges Visited?

Not necessarily! Check out this example:

s t

Page 39: Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Complexity©D.Moshkovitz

39

Observation

Claim: If there are edges not visited, than there exists such edge which “hits” a visited vertex.

Proof: By graph connectivity.

st

Page 40: Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Complexity©D.Moshkovitz

40

Observation

Claim: A walk that starts at the visited vertex must get stuck at that vertex itself.

Proof: After leaving the visited vertex it remains the only vertex the number of new edges hitting it is odd.

Page 41: Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Complexity©D.Moshkovitz

41

Putting the Pieces Together

• We can add the new walk to the old one and so cover at least one more edge.

• Now repeat this process if there are more uncovered edges.…

s t

… …

Page 42: Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Complexity©D.Moshkovitz

42

The Punch Line

There is a polynomial time algorithm for the Eulerian path problem.

Page 43: Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Complexity©D.Moshkovitz

43

Food for Thought…

How would you efficiently implement the Euler algorithm?

Page 44: Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Complexity©D.Moshkovitz

44

Summary

NP

P

NPC

EULERPATHHAMPATH

Page 45: Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs

Complexity©D.Moshkovitz

45

Summary

• In this lecture we’ve examined few problems related to paths in graphs.

• We’ve shown that finding a path which visits all vertices (Hamiltonian) is NP-Hard.

• While we can efficiently find paths which visit all edges (Eulerian).