Graphs Flashcards
What is the difference between BFS and DFS?
BFS explores level by level (queue), DFS goes deep first (stack).
What data structure is used for BFS?
Queue
What data structure is used for DFS?
Stack (or recursion).
What is Dijkstra’s algorithm used for?
Finds shortest path in weighted graphs (O(V + E log V)).
What is the difference between Prim’s and Kruskal’s algorithms?
: Prim’s for dense graphs, Kruskal’s for sparse graphs (both find MST).
What is a topological sort, and when is it used?
Orders tasks when some must be done before others.
What is the Bellman-Ford algorithm, and how does it differ from Dijkstra’s?
Bellman-Ford works with negative weights.
What is Floyd-Warshall used for?
Finds shortest paths between all nodes (O(n³)).
How can we detect cycles in a graph?
Use DFS or Union-Find.
What is the difference between an adjacency matrix and an adjacency list?
Matrix uses O(V²) space; List is O(V + E).