BFS & DFS - NTUA
Transcript of BFS & DFS - NTUA
.
...... BFS & DFS
Lydia Zakynthinou
Algo IMPLA
November 6, 2014
..... ..... ..... . ..................... ..................... ..................... ..... ..... . ..... ..... ..... .
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
Outline
...1 Graph Representation
...2 Breadth First Search
...3 Depth First Search
Lydia Zakynthinou (NTUA) BFS & DFS November 6, 2014 2 / 22
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
Outline
...1 Graph Representation
...2 Breadth First Search
...3 Depth First Search
Lydia Zakynthinou (NTUA) BFS & DFS November 6, 2014 3 / 22
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
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
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
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
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
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
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
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
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
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
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
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
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
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
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
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
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
Outline
...1 Graph Representation
...2 Breadth First Search
...3 Depth First Search
Lydia Zakynthinou (NTUA) BFS & DFS November 6, 2014 6 / 22
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
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
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
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
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
BFS: The Algorithm
Running time: Θ(n + m)
Lydia Zakynthinou (NTUA) BFS & DFS November 6, 2014 8 / 22
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
BFS: Example
Lydia Zakynthinou (NTUA) BFS & DFS November 6, 2014 9 / 22
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
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
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
Outline
...1 Graph Representation
...2 Breadth First Search
...3 Depth First Search
Lydia Zakynthinou (NTUA) BFS & DFS November 6, 2014 11 / 22
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
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
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
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
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
DFS: The Algorithm
Running time: Θ(n + m)
Lydia Zakynthinou (NTUA) BFS & DFS November 6, 2014 13 / 22
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
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
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
DFS: Example
Lydia Zakynthinou (NTUA) BFS & DFS November 6, 2014 15 / 22
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
DFS:Pre/Post-visit
Connected Components:
Orderings:
Lydia Zakynthinou (NTUA) BFS & DFS November 6, 2014 16 / 22
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
DFS: Example with Timestamps
Lydia Zakynthinou (NTUA) BFS & DFS November 6, 2014 17 / 22
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
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
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
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
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
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
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
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
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
DFS: Applications
Strongly Connected ComponentsPlanarity Testing (de Fraysseix–Rosenstiehl planarity criterion)
Lydia Zakynthinou (NTUA) BFS & DFS November 6, 2014 21 / 22
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
DFS: Applications
Strongly Connected ComponentsPlanarity Testing (de Fraysseix–Rosenstiehl planarity criterion)
Lydia Zakynthinou (NTUA) BFS & DFS November 6, 2014 21 / 22
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
Thank you!
Lydia Zakynthinou (NTUA) BFS & DFS November 6, 2014 22 / 22