Graph traversal BFS and DFS study guide

This study guide covers the fundamental concepts of graph traversal techniques, focusing on Breadth-First Search (BFS) and Depth-First Search (DFS), along with their applications and differences.

Liam2007·48 flashcards·48 questions
collegecomputer_sciencealgorithms
0
Known
1 / 48
0
Learning
Front

Definition of a graph

Tap to flip
Back

A graph is a collection of nodes (vertices) connected by edges. Examples: social networks, transportation systems.

Tap to flip
Got it
Still learning

Quiz(48 questions)

Question 1 of 48

1. Which algorithm requires more memory due to storing all nodes at the current level?

Terms in this Study Set(48)

Graph Basics(12)

Definition of a graph

A graph is a collection of nodes (vertices) connected by edges. Examples: social networks, transportation systems.

True or False: A graph can have multiple edges between the same nodes.

True. This is known as a multigraph, where multiple edges can exist between any two vertices.

Vertices vs. Edges

Vertices: the fundamental units of a graph. Edges: the connections between vertices.

What is an undirected graph?

An undirected graph has edges that do not have a direction. Example: a friendship relation.

Directed vs. Undirected

Directed: edges have a direction. Undirected: edges have no direction. - Example: Twitter (directed), Facebook (undirected).

Fill in the blank: A ______ graph has no cycles.

A forest graph has no cycles. It is a disjoint union of trees.

What is a weighted graph?

A weighted graph assigns a weight or cost to each edge, enabling analysis of shortest paths.

Degree of a vertex

The degree of a vertex is the number of edges connected to it. For directed graphs, consider in-degree and out-degree.

Cause → Effect: Adding an edge to a graph

Adding an edge increases the graph's connectivity and can create new paths between vertices.

Adjacency list vs. Adjacency matrix

Adjacency list uses lists to represent connections. Adjacency matrix uses a 2D array. - Space: matrix O(V^2), list O(V + E).

Connected graph definition

A connected graph has a path between every pair of vertices. If disconnected, it has multiple components.

What is a cyclic graph?

A cyclic graph contains at least one cycle, a path that starts and ends at the same vertex.

Breadth-First Search (BFS)(14)

What does BFS stand for?

BFS stands for Breadth-First Search, a graph traversal algorithm.

BFS explores nodes based on which principle?

BFS explores all neighbors of a node before moving to the next level.

True or False: BFS can find the shortest path in unweighted graphs.

True. BFS guarantees the shortest path in unweighted graphs by exploring all nodes at the present depth before moving on.

What data structure is primarily used in BFS?

BFS uses a queue to keep track of nodes to explore next.

Fill in the blank: BFS processes nodes in order of their __________.

distance from the starting node.

How does BFS handle cycles in graphs?

BFS keeps track of visited nodes to prevent infinite loops.

List three applications of BFS.

- Shortest path in unweighted graphs - Finding connected components - Web crawling

What is the time complexity of BFS?

The time complexity of BFS is O(V+E)\displaystyle O(V + E) where V\displaystyle V is vertices and E\displaystyle E is edges.

Describe the process of BFS in one sentence.

BFS starts at the source node, explores all neighboring nodes, and continues level by level.

Why is BFS not ideal for weighted graphs?

BFS does not account for edge weights, potentially missing the shortest path.

How do you implement BFS?

1. Initialize a queue with the starting node. 2. Mark it as visited. 3. While the queue is not empty: dequeue, process, enqueue unvisited neighbors.

BFS produces which type of tree?

BFS produces a breadth-first tree or level order tree representing the traversal.

In BFS, what role do visited nodes play?

Visited nodes prevent re-exploration, ensuring efficient traversal.

What is the space complexity of BFS?

The space complexity of BFS is O(V)\displaystyle O(V), since it stores all vertices in memory.

Depth-First Search (DFS)(14)

Depth-First Search (DFS) definition

DFS is an algorithm for traversing or searching tree or graph data structures. It explores as far as possible along each branch before backtracking.

What data structure is primarily used for DFS?

DFS uses a stack data structure, either explicitly or through recursion.

True or False: DFS can get stuck in loops.

True. If cycles are present in a graph without proper checks, DFS may revisit nodes indefinitely.

DFS traversal order

DFS visits nodes in a depthward motion before moving laterally: - Start at root - Explore child nodes until leaf - Backtrack

Applications of DFS

- Pathfinding in mazes - Topological sorting - Cycle detection - Solving puzzles like Sudoku

What is backtracking in DFS?

Backtracking in DFS refers to the process of returning to the previous node when no further exploration is possible.

Fill in the blank: DFS is typically implemented using __________.

recursion or an explicit stack.

Difference between DFS and BFS

DFS explores deeper into one branch first, while BFS explores all neighbors at the present depth prior to moving on.

Complexity of DFS in terms of time

The time complexity of DFS is O(V+E)\displaystyle O(V + E), where V\displaystyle V is the number of vertices and E\displaystyle E is the number of edges.

True or False: DFS is less memory efficient than BFS.

False. DFS can be more memory efficient, especially in sparse graphs, due to its depth-first nature.

Example of DFS in action

Given a graph with nodes A, B, C, and D: Starting at A, DFS visits A, then recursively visits B, C, then backtracks to A before visiting D.

What happens when DFS hits a dead end?

When DFS hits a dead end, it backtracks to the last node with unexplored neighbors and continues the search.

Cause of stack overflow in DFS

A stack overflow can occur in DFS when the recursion goes too deep, particularly in very deep or infinitely branching graphs.

How does DFS handle disconnected graphs?

DFS can handle disconnected graphs by initiating a new DFS from each unvisited node to ensure all components are explored.

Comparative Analysis(8)

BFS vs. DFS: Memory Usage

BFS requires more memory due to storing all nodes at the current level. - Queue-based storage - Space complexity: O(b^d) - b = branching factor, d = depth

True or False: BFS can find the shortest path.

True. BFS explores nodes level by level, guaranteeing the shortest path in unweighted graphs.

DFS: Advantage over BFS

DFS uses less memory in deep graphs because it explores as far as possible along each branch before backtracking.

BFS traversal order

BFS visits all neighbors before moving to the next level, resulting in a layer-wise traversal.

Fill in the blank: DFS is more suitable for __________.

DFS is more suitable for scenarios with deep and narrow search spaces, like puzzle solving.

Comparison: Backtracking in DFS vs. BFS

DFS utilizes backtracking to find solutions in complex paths. - Efficient for maze problems - BFS does not backtrack.

Disadvantage of BFS

BFS can be less efficient in terms of time if the solution is located deep in the graph. - High time complexity in wide graphs.

Question: Which algorithm is better for large graphs?

Neither is universally better. - BFS is better for shortest paths. - DFS is better for memory usage.

Questions in this Study Set(48)

1. Which algorithm requires more memory due to storing all nodes at the current level?

A.BFS
B.DFS
C.Dijkstra's
D.A*

2. What is a graph?

A.A collection of nodes connected by edges.
B.A linear sequence of numbers.
C.A function mapping inputs to outputs.
D.A hierarchical structure of data.

3. What is the primary purpose of the Breadth-First Search algorithm?

A.To find the shortest path in unweighted graphs
B.To sort nodes by edge weight
C.To perform depth-first traversal
D.To maximize distance between nodes

4. What best describes Depth-First Search (DFS)?

A.An algorithm that explores as far as possible along each branch before backtracking.
B.A method that visits all nodes at the present depth prior to moving deeper.
C.An algorithm that only traverses tree structures, not graphs.
D.A search algorithm that cannot handle cycles.

5. True or False: BFS guarantees finding the shortest path in unweighted graphs.

A.True
B.False
C.Only in weighted graphs
D.Only in trees

6. Which of the following statements is true regarding multigraphs?

A.A multigraph can have multiple edges between the same nodes.
B.A multigraph has no edges.
C.A multigraph is a type of tree.
D.A multigraph must be directed.

7. In BFS, which data structure is used to manage the nodes to be explored?

A.Stack
B.Queue
C.Array
D.Linked List

8. Which data structure is essential for implementing DFS?

A.Stack
B.Queue
C.Linked list
D.Array

9. In which scenario is DFS more suitable?

A.Wide graphs
B.Deep and narrow search spaces
C.Finding shortest paths
D.Graphs with uniform weights

10. What distinguishes vertices from edges in a graph?

A.Vertices are points; edges are connections.
B.Vertices are always weighted; edges never are.
C.Vertices can be directed; edges cannot.
D.Vertices cannot be connected.

11. Which statement best describes how BFS handles graph cycles?

A.It ignores cycles completely
B.It uses a stack to manage revisits
C.It marks nodes as visited to avoid revisiting
D.It cannot handle cycles

12. When using DFS, what occurs if the algorithm encounters a cycle?

A.It may enter an infinite loop if not managed properly.
B.It will skip the node and continue searching.
C.It will restart the search from the beginning.
D.It will only visit the node once.

13. What traversal order does BFS follow?

A.Depth-first
B.Layer-wise
C.Random
D.In-order

14. What is an undirected graph?

A.A graph where edges have no direction.
B.A graph that can be traversed in one way.
C.A graph with weights assigned to edges.
D.A graph that contains cycles.

15. Which of the following is NOT an application of BFS?

A.Finding connected components
B.Web crawling
C.Finding shortest paths in weighted graphs
D.Level order traversal of trees

16. What is the traversal order of DFS?

A.Deeply explore child nodes before moving laterally.
B.Visit all neighbors before going deeper.
C.Alternates between depth and breadth.
D.Only traverses leaves in a tree.

17. Which method is NOT utilized by DFS when searching for solutions?

A.Backtracking
B.Queue storage
C.Depth exploration
D.Recursive calls

18. In which type of graph do edges have specified directions?

A.Directed graph
B.Undirected graph
C.Cyclic graph
D.Weighted graph

19. What is the time complexity of the BFS algorithm?

A.O(V^2)
B.O(V + E)
C.O(E log V)
D.O(V log V)

20. In which scenario is DFS typically useful?

A.Solving puzzles like Sudoku.
B.Finding the shortest path between two nodes.
C.Performing breadth-first traversals.
D.Iterating through a list of elements.

21. What is a significant disadvantage of BFS?

A.It cannot backtrack
B.It may be time-inefficient for deep solutions
C.It uses less memory
D.It always finds the solution faster

22. A forest graph is characterized by which of the following?

A.It has no cycles.
B.It contains at least one cycle.
C.It is always connected.
D.It has a single vertex.

23. What type of traversal tree does BFS produce?

A.Depth-first tree
B.Binary search tree
C.Breadth-first tree
D.Random tree

24. What does backtracking mean in the context of DFS?

A.Returning to a previous node to explore other paths.
B.Starting the search over from the root.
C.Moving laterally across sibling nodes.
D.Ignoring already visited nodes.

25. Which algorithm is considered better for finding solutions in complex paths?

A.DFS
B.BFS
C.Dijkstra's
D.Best-first

26. What is the purpose of weights in a weighted graph?

A.To assign costs to edges for path analysis.
B.To determine the direction of edges.
C.To represent the degree of vertices.
D.To show the number of cycles.

27. How does BFS differ from Depth-First Search (DFS)?

A.BFS uses a stack while DFS uses a queue
B.DFS explores nodes level by level while BFS explores all neighbors first
C.BFS explores nodes level by level while DFS goes as deep as possible
D.DFS is only for unweighted graphs

28. DFS is typically implemented using which method?

A.Recursion or an explicit stack.
B.A queue for breadth-first search.
C.Linear search techniques.
D.Graph traversal without data structures.

29. Between BFS and DFS, which has the advantage of lower memory usage in deep graphs?

A.BFS
B.DFS
C.Both are equal
D.Neither

30. How is the degree of a vertex defined?

A.The number of edges connected to it.
B.The weight of the edges connected to it.
C.The number of vertices adjacent to it.
D.The distance to the farthest vertex.

31. What is the space complexity of the BFS algorithm?

A.O(1)
B.O(V)
C.O(E)
D.O(V + E)

32. How does DFS differ from BFS?

A.DFS goes deep into one branch before exploring others; BFS explores all neighbors first.
B.BFS explores deeper paths first, unlike DFS.
C.DFS and BFS yield the same traversal order.
D.DFS is faster than BFS in all scenarios.

33. What happens when an edge is added to a graph?

A.It increases the graph's connectivity.
B.It decreases the number of vertices.
C.It changes the graph type to a tree.
D.It removes existing edges.

34. Which of the following best describes the order of node processing in BFS?

A.Nodes are processed in a random order
B.Nodes are processed according to their edge weights
C.Nodes are processed by distance from the starting node
D.Nodes are processed in reverse order of discovery

35. What is the time complexity of DFS?

A.O(V + E)
B.O(V^2)
C.O(E)
D.O(V log V)

36. Which representation uses a 2D array to show connections in a graph?

A.Adjacency matrix
B.Adjacency list
C.Edge list
D.Incidence matrix

37. Why is BFS not suitable for finding the shortest path in weighted graphs?

A.It doesn't explore enough nodes
B.It doesn't use a priority queue
C.It doesn't account for edge weights
D.It requires more memory

38. Is it true that DFS is less memory efficient than BFS?

A.False.
B.True.
C.Only in dense graphs.
D.Depends on the implementation.

39. What defines a connected graph?

A.There is a path between every pair of vertices.
B.It contains at least one cycle.
C.It has no edges.
D.All vertices are isolated.

40. In which scenario would BFS be the preferred algorithm?

A.Finding the longest path in a graph
B.Finding the shortest path in an unweighted graph
C.Sorting elements of a graph
D.Finding a path in a directed acyclic graph

41. If DFS encounters a dead end, what action does it take?

A.It backtracks to the last node with unexplored paths.
B.It restarts the search from the root.
C.It terminates the search process.
D.It skips to the next node in the graph.

42. Which type of graph contains at least one cycle?

A.Cyclic graph
B.Acyclic graph
C.Tree graph
D.Forest graph

43. What happens if BFS is applied on a disconnected graph?

A.It will not return any results
B.It will only traverse one connected component
C.It will traverse all components in one run
D.It will fail to execute

44. What may cause a stack overflow in a DFS implementation?

A.Excessive recursion depth in deep or infinitely branching graphs.
B.Using an iterative approach without recursion.
C.Having few nodes in the graph.
D.Not using a stack at all.

45. Which property of BFS allows it to guarantee the shortest path in unweighted graphs?

A.Processing all nodes in random order
B.Exploring all neighbors before moving deeper
C.Using a stack to track nodes
D.Ignoring visited nodes

46. How does DFS manage disconnected graphs?

A.It initiates a new search from each unvisited node.
B.It cannot handle disconnected graphs.
C.It connects all components into one graph.
D.It stops searching once a connected component is found.

47. Which of the following best explains how BFS determines the order of node exploration?

A.It explores nodes based on their distance from the starting node.
B.It explores nodes based on their depth in the graph.
C.It explores nodes randomly regardless of their connections.
D.It explores nodes in reverse order of their connections.

48. Which of the following statements is NOT true about Depth-First Search (DFS)?

A.DFS explores nodes by going deep into one branch at a time.
B.DFS can potentially reach infinite depth in certain structures.
C.DFS always finds the shortest path in weighted graphs.
D.DFS uses a stack data structure for traversal.

Related Study Sets

Create Your Own Study Set

Upload a PDF, paste your notes, or describe a topic – AI generates flashcards, quizzes and more in seconds.