Graphs: Representation and Traversals Flashcards
(5 cards)
1
Q
How can a graph be represented in memory?
A
Using adjacency lists (efficient for sparse graphs) or adjacency matrices (efficient for dense graphs).
2
Q
What is Breadth-First Search (BFS)?
A
Explores the graph level by level using a queue. Suitable for shortest path in unweighted graphs.
3
Q
What is Depth-First Search (DFS)?
A
Explores the graph deeply before backtracking, often implemented with recursion or a stack.
4
Q
What is the time complexity of BFS and DFS?
A
Both have O(V + E) time complexity for a graph with V vertices and E edges.
5
Q
What is a visited array used for in graph traversal?
A
To track already visited nodes and prevent infinite loops in cyclic graphs.