1 / 15

§6 Applications of Depth-First Search

0. 1. 2. 3. 4. 5. 6. 7. 8. §6 Applications of Depth-First Search. /* a generalization of preorder traversal */. void DFS ( Vertex V ) /* this is only a template */ { visited[ V ] = true; /* mark this vertex to avoid cycles */ for ( each W adjacent to V )

lora
Download Presentation

§6 Applications of Depth-First Search

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 0 1 2 3 4 5 6 7 8 §6 Applications of Depth-First Search /* a generalization of preorder traversal */ void DFS ( Vertex V ) /* this is only a template */ { visited[ V ] = true; /* mark this vertex to avoid cycles */ for ( each W adjacent to V ) if ( !visited[ W ] ) DFS( W ); } /* T = O( |E| + |V| ) as long as adjacency lists are used */ 1. Undirected Graphs DFS ( 0 ) void ListComponents ( Graph G ) { for ( each V in G ) if ( !visited[ V ] ) { DFS( V ); printf(“\n“); } } 0 1 4 6 5 2 3 7 8 1/15

  2. §6 Applications of Depth-First Search   0 8 9 0 8 1 7  1 1 7 7 9 2 3 5 2 3 3 5 5  7  4 6 4 6 Connected graph Biconnected components 2. Biconnectivity v is an articulation point if G’ = DeleteVertex( G, v ) has at least 2 connected components.  G is a biconnected graph if G is connected and has no articulation points. Biconnected graph Articulation point  A biconnected component is a maximal biconnected subgraph. 〖Example〗 Note: No edges can be shared by two or more biconnected components. Hence E(G) is partitioned by the biconnected components of G. 2/15

  3. §6 Applications of Depth-First Search 0 Back edges ::= (u, v)  tree and u (or v) is an ancestor of v (or u). 0 8 9 3 1 5 1 7 4 5 2 6 2 3 5 2 6 3 7 4 6 1 7 4 8 9 0 9 8 Finding the biconnected components of a connected undirected G  Use depth first search to obtain a spanning tree of G Depth first spanning tree DFS ( 3 ) Depth first number (Num) 9 8 4 Note: If u is an ancestor of v, then Num( u ) < Num( v ). 3 7 2 0 5 1 6  Find the articulation points in G  The root is an articulation point iff it has at least 2 children  Any other vertex u is an articulation point iff u has at least 1 child, and it is impossible to move down at least 1 step and then jump up to u’s ancestor. 3/15

  4. §6 Applications of Depth-First Search vertex 0 1 2 3 4 5 6 7 8 9 0 Num 4 3 2 0 1 5 6 7 9 8 Low 3 1 5 4 5 2 6 2 6 3 7 1 7 4 8 9 0 9 8 4 0 0 0 0 5 5 5 9 8 Therefore, u is an articulation point iff (1) u is the root and has at least 2 children; or (2) u is not the root, and has at least 1 child such that Low( child )  Num( u ). Please read the pseudocodes on p.327 and p.329 for more details. 4/15

  5. §6 Applications of Depth-First Search Draw each line exactly once without lifting your pen from the paper – Euler tour Draw each line exactly once without lifting your pen from the paper, AND finish at the starting point – Euler curcuit 3. Euler Circuits 〖Proposition〗An Euler circuit is possible only if the graph is connected and each vertex has an even degree. 〖Proposition〗An Euler tour is possible if there are exactly two vertices having odd degree. One must start at one of the odd-degree vertices. 5/15

  6. §6 Applications of Depth-First Search 1 2 3 4 5 6 7 8 9 10 11 12 Find a simple cycle in an undirected graph that visits every vertex – Hamilton cycle DFS Home work: Self-study the digraphs and strong components on p. 331-334 and do p.342 9.27 • Note: • The path should be maintained as a linked list. • For each adjacency list, maintain a pointer to the last edge scanned. • T = O( |E| + |V| ) 6/15

  7. §7 NP-Completeness  Recall: Euler circuit problem: Find a path that touches every edge exactly once.  Hamilton cycle problem: Find a single cycle that contains every vertex.  Single-source unweighted shortest-path problem  Single-source unweighted longest-path problem  No known algorithms that are guaranteed to run in polynomial time. 7/15

  8. §7 NP-Completeness The great mathematician David Hilbert at the 1900 International Congress of Mathematicians outlined 23 research problems to be investigated in the coming century. One of the problems is the question of Decidability — Could there exist, at least in principle, any definite method or process by which all mathematical questions could be decided? Kurt Gödel proved in 1931 that not all true statements that evolve from an axiomatic system can be proven – we can never know everything nor prove everything we discover. Easy vs. Hard The easiest: O( N ) – since we have to read inputs at least once. The hardest: – undecidable problems. This statement is a lie. 8/15

  9. §7 NP-Completeness /* 2 */ is true /* 1 */ is true Loops Terminates 【Example】Halting problem: Is it possible to have your C compiler detect all infinite loops? Answer: No. Proof:If there exists an infinite loop-checking program, then surely it could be used to check itself. Loop( P ) { /* 1 */if ( P(P) loops ) print (YES); /* 2 */ else infinite_loop(); } What will happen toLoop( Loop )?  Terminates  Loops 9/15

  10. §7 NP-Completeness Alan Mathison Turing was born on 23 June 1912 in a nursing home in Paddington, London. In four inadequate words Alan Turing appears now as the Founder of Computer Science. The Class NP TURING MACHINE During the Second World War, Turing made an important contribution on penetrating the mechanical Enigma cipher used by Germany. In 1944, at the invasion of Normandy that Allied control of the Atlantic allowed, Alan Turing was almost uniquely in possession of three key ideas: (1) his own 1936 concept of the universal machine (2) the potential speed and reliability of electronic technology (3) the inefficiency in designing different machines for different logical processes 10/15

  11. §7 NP-Completeness Combined, these ideas provided the principle, the practical means, and the motivation for the modern computer, a single machine capable of handling any programmed task. He himself was as eager as anyone in the world to bring them together, and was spurred even more by a fourth idea: that the universal machine should be able to acquire and exhibit the faculties of the human mind. Even in 1944 he spoke to Donald Bayley of 'building a brain'. He was found by his cleaner when she came in on 8 June 1954. He had died the day before of cyanide poisoning, a half-eaten apple beside his bed. His mother believed he had accidentally ingested cyanide from his fingers after an amateur chemistry experiment, but it is more credible that he had successfully contrived his death to allow her alone to believe this. The coroner's verdict was suicide. More details can be found at http:\\www.turing.org.uk . 11/15

  12. §7 NP-Completeness To simulate any kind of computation which a mathematician can do by some arithmetical method (assuming that the mathematician has infinite time, energy, paper and pen, and is completely dedicated to the work). ... ... ... ... ... ... ... ... ... ... ... Task Finite state controller: control head movements according to current state and the symbol that head is pointing to. INSTRUCTION storage Infinite 1-dimensional tape(s) divided into units. DATA storage Components Infinite Memory Scanner and Operations One head can point to one and only one unit at each state, and it may move at most one unit to the left or right.  Change the finite control state.  Erase the symbol in the unit currently pointed by head, and write a new symbol in. Scanner ...  Head moves one unit to the left (L), or to the right (R), or stays at its current position (S). 12/15

  13. §7 NP-Completeness A Deterministic Turing Machine executes one instruction at each point in time. Then depending on the instruction, it goes to the next unique instruction. A Nondeterministic Turing Machine is free to choose its next step from a finite set. And if one of these steps leads to a solution, it will always choose the correct one. NP: Nondeterministic polynomial-time The problem is NP if we can prove any solution is true in polynomial time. Undecidable problems are still undecidable. 【Example】Hamilton cycle problem: Find a single cycle that contains every vertex – does this simple circuit include all the vertices? NP Note:Not all decidable problems are in NP. For example, consider the problem of determining whether a graph does not have a Hamiltonian cycle. 13/15

  14. §7 NP-Completeness If we can solve any NP-complete problem in polynomial time, then we will be able to solve, in polynomial time, all the problems in NP! NP-Complete Problems -- the hardest An NP-complete problem has the property that any problem in NP can be polynomially reduced to it. 【Example】Suppose that we already know that theHamilton cycle problem is NP-complete. Prove that the traveling salesman problem is NP-complete as well. Traveling salesman problem: Given a complete graph G=(V, E), with edge costs, and an integer K, is there a simple cycle that visits all vertices and has total cost  K? 14/15

  15. §7 NP-Completeness V1 1 1 2 V3 V2 1 2 2 2 1 1 V4 V5 1 Proof:TSP is obviously in NP since it is easy to check a solution. K = |V| Home work: p.344 9.39 Show that the clique problem is polynomially reducible to The vertex cover problem. G has a Hamilton cycle iff G’ has a traveling salesman tour of total weight |V|.  The first problem that was proven to be NP-complete was the satisfiability problem: Input a boolean expression and ask if it has an assignment to the variables that gives the expression a value of 1. Cook showed in 1971 that all the problems in NP could be transformed to satisfiability. He proved it by solving this problem on a Turing nondeterministic machine in polynomial time. 15/15

More Related