Lecture 2 - BFS and DFS Flashcards
What is DFS/BFS categorised as?
A traversal algorithm.
How do we know if BFS/DFS is efficient?
If it runs in O(V + E) time.
What are the steps of Depth First Search?
- Follow a path of unvisited verticies until path cannot be extended further
- Backtrack until a node with unvisited nodes
- Repeat until all nodes visited
Does DFS work with non-conncted graph?
Yes, with some changes.
What do the edges traversed form?
A spanning tree (forest).
What does a spanning tree consist of in reference to original graph?
All vertices and some or all edges.
What is the complexity of DFS?
O(m+n) using an adjacency list.
O(n^2) using an adjacency matrix.
Some applications of DFS ?
- to determine if a graph is connected
- to identify connected components of a graph
- check whether a graph has a cycle
- to determine if a graph is bipartite
Steps of Breadth First Search?
- add each adjacent vertex of current vertex to list
- repeat for each node in list until no nodes left
The actual order of traversal and hence spanning tree depends on specific implementation of algorithm.
What is the complexity of BFS?
O(m+n) for adjacency list
O(n^2) for adjacency matrix
What are the applications of BFS?
- finding distance between 2 verticies
- shortest path from v to w in terms of edges