§6 applications of depth-first search /* a generalization of preorder traversal */ void dfs (...

15
§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 */ 0 1 2 3 4 5 6 DFS ( 0 ) 1. Undirected Graphs void ListComponents ( Graph G ) { for ( each V in G ) if ( !visited[ V ] ) { DFS( V ); printf(“\n“); } } 7 8 0 1 4 6 5 2 3 7 8 1/15

Upload: kyleigh-pearcy

Post on 16-Dec-2015

222 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: §6 Applications of Depth-First Search /* a generalization of preorder traversal */ void DFS ( Vertex V ) /* this is only a template */ { visited[ V ]

§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 */

0

1 2 3

4 5

6

DFS ( 0 )

1. Undirected Graphs

void ListComponents ( Graph G ) { for ( each V in G ) if ( !visited[ V ] ) {

DFS( V ); printf(“\n“); }}

7

8

0 1 4 6 5 2 37 8

1/15

Page 2: §6 Applications of Depth-First Search /* a generalization of preorder traversal */ void DFS ( Vertex V ) /* this is only a template */ { visited[ V ]

§6 Applications of Depth-First Search2. Biconnectivity

Articulation

pointBiconnected

graph

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.

A biconnected component is a maximal biconnected subgraph.

〖 Example〗

0

1

2 3

4

8

7

5

6

9

Connected graph

0

1

1

2 3

4

3 5

8

7

7

5

6

7

9

Biconnected components

Note: No edges can be shared by two or more biconnected components. Hence E(G) is partitioned by the biconnected components of G.

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

Page 3: §6 Applications of Depth-First Search /* a generalization of preorder traversal */ void DFS ( Vertex V ) /* this is only a template */ { visited[ V ]

§6 Applications of Depth-First Search

Finding the biconnected components of a connected undirected G

0

1

2 3

4

8

7

5

6

9

Use depth first search to obtain a spanning tree of G

DFS ( 3 )

0

1

2

3

4

5

6

7

89

Depth first spanning tree

3

4 5

2

1

0

6

7

9 8

0

1

2

3

4

5

6

7

8 9

Depth first number (Num)

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.

Back edges::= (u, v) treeand u (or v) isan ancestor of

v (or u).

Note: If u is an ancestor of v, then Num( u ) < Num( v ).

Note: If u is an ancestor of v, then Num( u ) < Num( v ).

3/15

Page 4: §6 Applications of Depth-First Search /* a generalization of preorder traversal */ void DFS ( Vertex V ) /* this is only a template */ { visited[ V ]

§6 Applications of Depth-First Search

3

4 5

2

1

0

6

7

9 8

0

1

2

3

4

5

6

7

8 9

}}edge back a is ),(|)(min{ }, of child a is |)(min{

),(min{)(

wuwNumuwwLow

uNumuLow

vertexNumLow

0 1 2 3 4 5 6 7 8 94 3 2 0 1 5 6 7 9 8

00 54 0 0 55 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

Page 5: §6 Applications of Depth-First Search /* a generalization of preorder traversal */ void DFS ( Vertex V ) /* this is only a template */ { visited[ V ]

§6 Applications of Depth-First Search3. Euler Circuits

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

〖 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

Page 6: §6 Applications of Depth-First Search /* a generalization of preorder traversal */ void DFS ( Vertex V ) /* this is only a template */ { visited[ V ]

§6 Applications of Depth-First Search

2

6

98

3

7

1

12

4

10

5

11

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| )

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| )

Find a simple cycle in an undirected graph that visits every vertex – Hamilton cycle

Home work:

Self-study the digraphs

and strong components

on p. 331-334

and do p.342 9.27

DFS

6/15

Page 7: §6 Applications of Depth-First Search /* a generalization of preorder traversal */ void DFS ( Vertex V ) /* this is only a template */ { visited[ V ]

§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

Page 8: §6 Applications of Depth-First Search /* a generalization of preorder traversal */ void DFS ( Vertex V ) /* this is only a template */ { visited[ V ]

§7 NP-Completeness

Easy vs. Hard

The easiest: O( N ) – since we have to read inputs at least once.

The hardest: – undecidable problems.

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.

This statement is a lie.

8/15

Page 9: §6 Applications of Depth-First Search /* a generalization of preorder traversal */ void DFS ( Vertex V ) /* this is only a template */ { visited[ V ]

§7 NP-Completeness

【 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 to Loop( Loop ) ?

Terminates /* 2 */ is true Loops

Loops /* 1 */ is true Terminates

9/15

Page 10: §6 Applications of Depth-First Search /* a generalization of preorder traversal */ void DFS ( Vertex V ) /* this is only a template */ { visited[ V ]

§7 NP-Completeness

The Class NP

TURING MACHINE

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.

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

Page 11: §6 Applications of Depth-First Search /* a generalization of preorder traversal */ void DFS ( Vertex V ) /* this is only a template */ { visited[ V ]

§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

Page 12: §6 Applications of Depth-First Search /* a generalization of preorder traversal */ void DFS ( Vertex V ) /* this is only a template */ { visited[ V ]

§7 NP-Completeness

TaskTask 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).

ComponentsComponents Infinite Memory Scannerand

OperationsOperations Change the finite control state.

Erase the symbol in the unit currently pointed by head, and write a new symbol in.

Head moves one unit to the left (L), or to the right (R), or stays at its current position (S).

Scanner

... ...... ... ... ...

... ... ... ...

...

...

Infinite 1-dimensional tape(s) divided into units.

DATA storage

Finite state controller:control head movements according to

current state and the symbol that head is pointing to.

INSTRUCTION storage

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.

12/15

Page 13: §6 Applications of Depth-First Search /* a generalization of preorder traversal */ void DFS ( Vertex V ) /* this is only a template */ { visited[ V ]

§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.

Undecidable problems are still undecid

able.

NP: Nondeterministic polynomial-time

The problem is NP if we can prove any solution is true in polynomial time.

【 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.

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

Page 14: §6 Applications of Depth-First Search /* a generalization of preorder traversal */ void DFS ( Vertex V ) /* this is only a template */ { visited[ V ]

§7 NP-Completeness

NP-Complete Problems -- the hardest

An NP-complete problem has the property that any problem in NP can be polynomially reduced to it.

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!

【 Example 】 Suppose that we already know that the Hamilton 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

Page 15: §6 Applications of Depth-First Search /* a generalization of preorder traversal */ void DFS ( Vertex V ) /* this is only a template */ { visited[ V ]

Proof: TSP is obviously in NP since it is easy to check a solution.

§7 NP-Completeness

V1

V2V3

V4 V5

1

1

1

1

1

12

222

K = |V|

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.

Home work:

p.344 9.39

Show that the clique problem is

polynomially reducible to

The vertex cover problem.

15/15