Paths On the Reasonability of Finding Paths in
Paths On the Reasonability of Finding Paths in Graphs Complexity ©D. Moshkovitz 1
Introduction • Objectives: – To introduce more graph theory problems. • Overview: – Hamiltonian paths – Eulerian paths Complexity ©D. Moshkovitz 2
Hamiltonian Path Instance: a directed graph G=(V, E) and two vertices s, t V. Problem: To decide if there exists a path from s to t, which goes through each node once. Complexity ©D. Moshkovitz 3
Can You Find One Here? s Complexity ©D. Moshkovitz t 4
HAMPATH is in NP • Non-deterministically choose v 1, …, vn V (n=|V|). • For any 1 i, j n, verify vi vj. • Verify s=v 1 and t=vn. • For any 1 i n, verify (vi, vi+1) E. Complexity ©D. Moshkovitz 5
SIP 262 -267 HAMPATH is NP-Complete Proof: We’ll show 3 SAT p. HAMPATH. t p s Complexity ©D. Moshkovitz . . . 6
Representing Variables For any variable xi, . . . Diamond Complexity ©D. Moshkovitz 7
Representing Clauses • For any clause ci, Complexity ©D. Moshkovitz 8
High-Level Structure s x 1 c 1 . . . c 2 x 2. . . c 3 . . xl . . . ck t Complexity ©D. Moshkovitz 9
The Internal Structure of the Diamonds k pairs of internal nodes . . . Diamond Complexity ©D. Moshkovitz 10
Connecting Clauses to Variables If the clause cj contains the literal xi, cj xi. . . j’th pair Complexity ©D. Moshkovitz 11
Connecting Clauses to Variables If the clause cj contains the literal xi, cj xi. . . j’th pair Complexity ©D. Moshkovitz 12
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 13
Completeness • Assume there exists a satisfying assignment for the 3 CNF formula. • Let us demonstrate a Hamiltonian tour on the graph. Complexity ©D. Moshkovitz 14
Assignment Path If the variable xi is assigned TRUE, . . . Complexity ©D. Moshkovitz 15
Assignment Path If the variable xi is assigned FALSE, . . . Complexity ©D. Moshkovitz 16
Covering the Clauses If the clause cj is satisfied due to the literal x i, cj xi. . . j’th pair Complexity ©D. Moshkovitz 17
Covering the Clauses If the clause cj is satisfied due to the literal xi, cj xi. . . j’th pair Complexity ©D. Moshkovitz 18
Soundness • Assume there exists a Hamiltonian tour on the graph, • Let us construct a satisfying assignment for the 3 CNF formula. Complexity ©D. Moshkovitz 19
Observation • Every Hamiltonian path must contain either the entire right path or the entire left path of each diamond. . Complexity ©D. Moshkovitz 20
Path Assignment • For each variable, decide on its truth value, • According to the direction of the path in its diamond. Complexity ©D. Moshkovitz . . . 21
Observation A Hamiltonian path cannot jump from inside one diamond to another Proof Idea: Assume it can and observe this node s x. . . 1 cj x. . . 2. . . x. . . l t Complexity ©D. Moshkovitz 22
Satisfaction • Note that because of the observation • And the fact the path goes through all clause nodes, • All clauses are satisfied Complexity ©D. Moshkovitz 23
The Punch Line HAMPATH is NP-Complete. Complexity ©D. Moshkovitz 24
Food for Thought… Is HAMCYCLE (Whethere exists a simple cycle which goes through all nodes) NP-Complete? Complexity ©D. Moshkovitz 25
Food for Thought… What about finding Hamiltonian paths in undirected graphs? Complexity ©D. Moshkovitz 26
Similar Problem • Children usually like this riddle: • Can you draw the following shape in one line? Complexity ©D. Moshkovitz 27
Eulerian Path Instance: an undirected graph G=(V, E) and two vertices s t V. Problem: To decide if there exists a path from s to t, which goes through each edge exactly once. Complexity ©D. Moshkovitz 28
Can You Find One Here? s Complexity ©D. Moshkovitz t 29
Eulerian Paths Vs. Hamiltonian Paths • What do you make of this problem? • Is it reducible to HAMPATH? • Or vice-versa? ? Complexity ©D. Moshkovitz 30
Complexity ©D. Moshkovitz 31
The Euler 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 32
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 33
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 34
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 35
Walks Complexity ©D. Moshkovitz • Start at s, • While there are new edges – Arbitrarily choose such, – And walk on it. 36
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 37
Were All Edges Visited? Not necessarily! Check out this example: s Complexity ©D. Moshkovitz t 38
Observation Claim: If there are edges not visited, than there exists such edge which “hits” a visited vertex. Proof: By graph connectivity. s Complexity ©D. Moshkovitz t 39
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 40
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 Complexity ©D. Moshkovitz … … t 41
The Punch Line There is a polynomial time algorithm for the Eulerian path problem. Complexity ©D. Moshkovitz 42
Food for Thought… How would you efficiently implement the Euler algorithm? Complexity ©D. Moshkovitz 43
Summary HAMPATH EULERPATH NP P NPC Complexity ©D. Moshkovitz 44
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 NPHard. • While we can efficiently find paths which visit all edges (Eulerian). Complexity ©D. Moshkovitz 45
- Slides: 45