complexity ©d.moshkovitz 1 paths on the reasonability of finding paths in graphs
Post on 21-Dec-2015
230 views
TRANSCRIPT
Complexity©D.Moshkovitz
1
PathsOn the Reasonability of Finding Paths in Graphs
Complexity©D.Moshkovitz
2
Introduction
• Objectives:– To introduce more graph theory
problems.
• Overview:– Hamiltonian paths– Eulerian paths
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.
Complexity©D.Moshkovitz
4
Can You Find One Here?
s t
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.
Complexity©D.Moshkovitz
6
HAMPATH is NP-Complete
Proof: We’ll show 3SATpHAMPATH.
SIP 262-267
..)..(.......)..(..
s
t
. . .
p
Complexity©D.Moshkovitz
7
Representing Variables
For any variable xi,
Diamond
. . .
Complexity©D.Moshkovitz
8
Representing Clauses
• For any clause ci,
Complexity©D.Moshkovitz
9
High-Level Structure
. . .
x1
x2
.
.
.
xl
c1
c2
c3
.
.
.
ck
. . .
. . .
s
. . .
. . .
t
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
. . .
Complexity©D.Moshkovitz
11
Connecting Clauses to Variables
If the clause cj contains the literal xi,
j’th pair
xi
cj
. . .
Complexity©D.Moshkovitz
12
Connecting Clauses to Variables
If the clause cj contains the literal xi,
j’th pair
xi
cj
. . .
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.
Complexity©D.Moshkovitz
14
Completeness
• Assume there exists a satisfying assignment for the 3CNF formula.
• Let us demonstrate a Hamiltonian tour on the graph.
Complexity©D.Moshkovitz
15
Assignment Path
If the variable xi is assigned TRUE,
. . .
Complexity©D.Moshkovitz
16
Assignment Path
If the variable xi is assigned FALSE,
. . .
Complexity©D.Moshkovitz
17
Covering the ClausesIf the clause cj is satisfied due to the literal
xi,
xi
cj
. . .
j’th pair
Complexity©D.Moshkovitz
18
. . .
Covering the ClausesIf the clause cj is satisfied due to the literal
xi,
xi
cj
j’th pair
Complexity©D.Moshkovitz
19
Soundness
• Assume there exists a Hamiltonian tour on the graph,
• Let us construct a satisfying assignment for the 3CNF formula.
Complexity©D.Moshkovitz
20
Observation
• Every Hamiltonian path must contain either the entire right path or the entire left path of each diamond.
. . .
Complexity©D.Moshkovitz
21
Path Assignment
• For each variable, decide on its truth value,
• According to the direction of the path in its diamond.
. . .
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
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
Complexity©D.Moshkovitz
24
The Punch Line
HAMPATH is NP-Complete.
Complexity©D.Moshkovitz
25
Food for Thought…
Is HAMCYCLE (Whether there exists a simple cycle which goes through all
nodes) NP-Complete?
Complexity©D.Moshkovitz
26
Food for Thought…
What about finding Hamiltonian paths in undirected graphs?
Complexity©D.Moshkovitz
27
Similar Problem
• Children usually like this riddle:
• Can you draw the following shape in one line?
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.
Complexity©D.Moshkovitz
29
Can You Find One Here?
s t
Complexity©D.Moshkovitz
30
Eulerian Paths Vs. Hamiltonian Paths
• What do you make of this problem?
• Is it reducible to HAMPATH?• Or vice-versa?
?
Complexity©D.Moshkovitz
31
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.
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.
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.
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.
Complexity©D.Moshkovitz
36
Walks• Start at s,• While there are new edges
– Arbitrarily choose such,– And walk on it.
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.
Complexity©D.Moshkovitz
38
Were All Edges Visited?
Not necessarily! Check out this example:
s t
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
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.
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
… …
Complexity©D.Moshkovitz
42
The Punch Line
There is a polynomial time algorithm for the Eulerian path problem.
Complexity©D.Moshkovitz
43
Food for Thought…
How would you efficiently implement the Euler algorithm?
Complexity©D.Moshkovitz
44
Summary
NP
P
NPC
EULERPATHHAMPATH
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).