BFS & DFS - NTUA

32
. . BFS & DFS Lydia Zakynthinou Algo I MPLA November 6, 2014 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Transcript of BFS & DFS - NTUA

Page 1: BFS & DFS - NTUA

.

...... BFS & DFS

Lydia Zakynthinou

Algo IMPLA

November 6, 2014

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

Page 2: BFS & DFS - NTUA

..........

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

......

.....

.....

.

Outline

...1 Graph Representation

...2 Breadth First Search

...3 Depth First Search

Lydia Zakynthinou (NTUA) BFS & DFS November 6, 2014 2 / 22

Page 3: BFS & DFS - NTUA

..........

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

......

.....

.....

.

Outline

...1 Graph Representation

...2 Breadth First Search

...3 Depth First Search

Lydia Zakynthinou (NTUA) BFS & DFS November 6, 2014 3 / 22

Page 4: BFS & DFS - NTUA

..........

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

......

.....

.....

.

Adjacency Matrix

A(i, j) ={1 if (ui, vj) ∈ E0 if (ui, vj) /∈ E

Undirected G ⇒ A symmetric.Θ(n2) space.O(n) time to find all neighbours, O(1) time to check adjacency.

Lydia Zakynthinou (NTUA) BFS & DFS November 6, 2014 4 / 22

Page 5: BFS & DFS - NTUA

..........

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

......

.....

.....

.

Adjacency Matrix

A(i, j) ={1 if (ui, vj) ∈ E0 if (ui, vj) /∈ E

Undirected G ⇒ A symmetric.Θ(n2) space.O(n) time to find all neighbours, O(1) time to check adjacency.

Lydia Zakynthinou (NTUA) BFS & DFS November 6, 2014 4 / 22

Page 6: BFS & DFS - NTUA

..........

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

......

.....

.....

.

Adjacency Matrix

A(i, j) ={1 if (ui, vj) ∈ E0 if (ui, vj) /∈ E

Undirected G ⇒ A symmetric.Θ(n2) space.O(n) time to find all neighbours, O(1) time to check adjacency.

Lydia Zakynthinou (NTUA) BFS & DFS November 6, 2014 4 / 22

Page 7: BFS & DFS - NTUA

..........

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

......

.....

.....

.

Adjacency Matrix

A(i, j) ={1 if (ui, vj) ∈ E0 if (ui, vj) /∈ E

Undirected G ⇒ A symmetric.Θ(n2) space.O(n) time to find all neighbours, O(1) time to check adjacency.

Lydia Zakynthinou (NTUA) BFS & DFS November 6, 2014 4 / 22

Page 8: BFS & DFS - NTUA

..........

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

......

.....

.....

.

Adjacency Matrix

A(i, j) ={1 if (ui, vj) ∈ E0 if (ui, vj) /∈ E

Undirected G ⇒ A symmetric.Θ(n2) space.O(n) time to find all neighbours, O(1) time to check adjacency.

Lydia Zakynthinou (NTUA) BFS & DFS November 6, 2014 4 / 22

Page 9: BFS & DFS - NTUA

..........

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

......

.....

.....

.

Adjacency List

Θ(m) space.O(deg(u)) time to find all neighbours, O(deg(u)) time to checkadjacency.

Lydia Zakynthinou (NTUA) BFS & DFS November 6, 2014 5 / 22

Page 10: BFS & DFS - NTUA

..........

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

......

.....

.....

.

Adjacency List

Θ(m) space.O(deg(u)) time to find all neighbours, O(deg(u)) time to checkadjacency.

Lydia Zakynthinou (NTUA) BFS & DFS November 6, 2014 5 / 22

Page 11: BFS & DFS - NTUA

..........

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

......

.....

.....

.

Adjacency List

Θ(m) space.O(deg(u)) time to find all neighbours, O(deg(u)) time to checkadjacency.

Lydia Zakynthinou (NTUA) BFS & DFS November 6, 2014 5 / 22

Page 12: BFS & DFS - NTUA

..........

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

......

.....

.....

.

Outline

...1 Graph Representation

...2 Breadth First Search

...3 Depth First Search

Lydia Zakynthinou (NTUA) BFS & DFS November 6, 2014 6 / 22

Page 13: BFS & DFS - NTUA

..........

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

......

.....

.....

.

BFS

E. F. Moore (1959), The shortest path through a maze. InProceedings of the International Symposium on the Theory ofSwitching, Harvard University Press, pp. 285–292.C. Y. Lee (1961), An algorithm for path connection and itsapplications. IRE Transactions on Electronic Computers, EC-10(3),pp. 346–365.

Idea: Start from a vertex s and explore all the vertices reachable from s. Ineach round i explore vertices within a distance i (”breadth”).

Lydia Zakynthinou (NTUA) BFS & DFS November 6, 2014 7 / 22

Page 14: BFS & DFS - NTUA

..........

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

......

.....

.....

.

BFS

E. F. Moore (1959), The shortest path through a maze. InProceedings of the International Symposium on the Theory ofSwitching, Harvard University Press, pp. 285–292.C. Y. Lee (1961), An algorithm for path connection and itsapplications. IRE Transactions on Electronic Computers, EC-10(3),pp. 346–365.

Idea: Start from a vertex s and explore all the vertices reachable from s. Ineach round i explore vertices within a distance i (”breadth”).

Lydia Zakynthinou (NTUA) BFS & DFS November 6, 2014 7 / 22

Page 15: BFS & DFS - NTUA

..........

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

......

.....

.....

.

BFS: The Algorithm

Running time: Θ(n + m)

Lydia Zakynthinou (NTUA) BFS & DFS November 6, 2014 8 / 22

Page 16: BFS & DFS - NTUA

..........

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

......

.....

.....

.

BFS: Example

Lydia Zakynthinou (NTUA) BFS & DFS November 6, 2014 9 / 22

Page 17: BFS & DFS - NTUA

..........

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

......

.....

.....

.

BFS: Applications

Explore all vertices in the same connected component with s.Shortest paths from s to every other vertex in the connectedcomponent (for unweighted graphs).Testing BipartitenessUseful subroutine for Ford-Fulkerson method (maximum flow).

Lydia Zakynthinou (NTUA) BFS & DFS November 6, 2014 10 / 22

Page 18: BFS & DFS - NTUA

..........

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

......

.....

.....

.

Outline

...1 Graph Representation

...2 Breadth First Search

...3 Depth First Search

Lydia Zakynthinou (NTUA) BFS & DFS November 6, 2014 11 / 22

Page 19: BFS & DFS - NTUA

..........

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

......

.....

.....

.

DFS

Invented by Charles Pierre Trémaux (1859-1882) as a maze solvingalgorithm.

Idea: Start from a vertex s and explore a path as far as it goes. For everyvertex u, we explore recursively all reachable vertices from u before we arefinished with it (stack).

Lydia Zakynthinou (NTUA) BFS & DFS November 6, 2014 12 / 22

Page 20: BFS & DFS - NTUA

..........

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

......

.....

.....

.

DFS

Invented by Charles Pierre Trémaux (1859-1882) as a maze solvingalgorithm.

Idea: Start from a vertex s and explore a path as far as it goes. For everyvertex u, we explore recursively all reachable vertices from u before we arefinished with it (stack).

Lydia Zakynthinou (NTUA) BFS & DFS November 6, 2014 12 / 22

Page 21: BFS & DFS - NTUA

..........

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

......

.....

.....

.

DFS: The Algorithm

Running time: Θ(n + m)

Lydia Zakynthinou (NTUA) BFS & DFS November 6, 2014 13 / 22

Page 22: BFS & DFS - NTUA

..........

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

......

.....

.....

.

DFS: Edges

Graph edges belong to four categories:

Tree edges: part of the DFS forestBack edges: lead to an ancestor in the DFS treeForward edges: lead from a vertex to a nonchild descendantCross edges: lead to a node that has already been explored (neitherancestor nor descendant)

In undirected graphs there are only tree and back edges.Lydia Zakynthinou (NTUA) BFS & DFS November 6, 2014 14 / 22

Page 23: BFS & DFS - NTUA

..........

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

......

.....

.....

.

DFS: Example

Lydia Zakynthinou (NTUA) BFS & DFS November 6, 2014 15 / 22

Page 24: BFS & DFS - NTUA

..........

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

......

.....

.....

.

DFS:Pre/Post-visit

Connected Components:

Orderings:

Lydia Zakynthinou (NTUA) BFS & DFS November 6, 2014 16 / 22

Page 25: BFS & DFS - NTUA

..........

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

......

.....

.....

.

DFS: Example with Timestamps

Lydia Zakynthinou (NTUA) BFS & DFS November 6, 2014 17 / 22

Page 26: BFS & DFS - NTUA

..........

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

......

.....

.....

.

DFS: DAGs

Directed Acyclic Graphs: Directed graphs without cycles (no DFS backedges). Is it possible to linearize a DAG?

Lydia Zakynthinou (NTUA) BFS & DFS November 6, 2014 18 / 22

Page 27: BFS & DFS - NTUA

..........

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

......

.....

.....

.

DFS: Linearization

Perform tasks in decreasing order of their post-numbers.Find a source, put it in first in the order, delete it from the graph,repeat.

Lydia Zakynthinou (NTUA) BFS & DFS November 6, 2014 19 / 22

Page 28: BFS & DFS - NTUA

..........

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

......

.....

.....

.

DFS: Linearization

Perform tasks in decreasing order of their post-numbers.Find a source, put it in first in the order, delete it from the graph,repeat.

Lydia Zakynthinou (NTUA) BFS & DFS November 6, 2014 19 / 22

Page 29: BFS & DFS - NTUA

..........

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

......

.....

.....

.

DFS: Strongly Connected ComponentsAny directed graph is a DAG of its strongly connected components.

How can we find a decomposition of a graph to its strongly connectedcomponents?

Lydia Zakynthinou (NTUA) BFS & DFS November 6, 2014 20 / 22

Page 30: BFS & DFS - NTUA

..........

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

......

.....

.....

.

DFS: Applications

Strongly Connected ComponentsPlanarity Testing (de Fraysseix–Rosenstiehl planarity criterion)

Lydia Zakynthinou (NTUA) BFS & DFS November 6, 2014 21 / 22

Page 31: BFS & DFS - NTUA

..........

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

......

.....

.....

.

DFS: Applications

Strongly Connected ComponentsPlanarity Testing (de Fraysseix–Rosenstiehl planarity criterion)

Lydia Zakynthinou (NTUA) BFS & DFS November 6, 2014 21 / 22

Page 32: BFS & DFS - NTUA

..........

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

.....

.....

......

.....

......

.....

.....

.

Thank you!

Lydia Zakynthinou (NTUA) BFS & DFS November 6, 2014 22 / 22