ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Discrete Structures
    GE-167
    Progress0 / 67 topics
    Topics
    1. Mathematical Reasoning: Propositional and Predicate Logic2. Propositional Logic: Logical Operators3. Translations Between Symbolic Expressions and Formal English Expression4. Logical Equivalences5. Predicate Logic: Quantifiers6. Nested Quantification7. Equivalences in Predicate Logic8. Translations Between Symbolic Forms and Formal English9. Rules of Inference: Proof Methods and Strategies10. Direct Proof11. Proof by Contraposition12. Proof by Induction13. Proof by Implication14. Existence Proof15. Uniqueness Proofs16. Trivial Proofs17. Vacuous Proofs18. Sets: Notations and Set Operations19. Venn Diagrams20. Countable and Uncountable Sets21. Relations: Equivalence Relations and Partitions22. Partial Orderings23. Recurrence Relations24. Functions: Injective, Surjective, Bijective25. Special Types of Functions26. Function Composition27. Inverse Functions28. Recursive Functions29. Compositions30. Number Theory: Sequences and Series31. Counting: Inclusion and Exclusion Principle32. Pigeonhole Principle33. Permutations and Combinations34. Integers and Divisibility: Division Theorem35. Modular Arithmetic36. LCM and GCD37. Euclidean and Extended Euclidean Method38. Finding Solutions to Congruence39. Primes: Fundamental Theorem of Arithmetic40. Characterizations of Primes41. Mersenne Primes42. Induction: Weak Induction43. Strong Induction44. Recursion and Recurrences: Formulation of Recurrences45. Closed Formulas46. Counting: Product Rule and Sum Rule47. Principle of Inclusion-Exclusion48. Binomial Coefficients49. Pascal's Identity and Pascal’s Triangle50. Binomial Theorem51. Relations: Reflexive, Symmetric, Transitive, and Antisymmetric52. Equivalence Relations and Equivalence Classes53. Partial Orders54. Graph Theory: Terminologies55. Elements of Graph Theory56. Planar Graphs57. Graph Coloring58. Euler Graph59. Hamiltonian Path60. Rooted Trees61. Graph Traversals62. Handshaking Lemma and Corollary63. Special Families of Graphs64. Graph Isomorphism65. Planarity in Graphs66. Eulerian and Hamiltonian Graphs67. Trees in Graph Theory
    GE-167›Graph Traversals
    Discrete StructuresTopic 61 of 67

    Graph Traversals

    7 minread
    1,200words
    Intermediatelevel

    Graph Traversals

    Graph traversal refers to the process of visiting all the vertices (nodes) in a graph, and in some cases, processing the edges between the vertices as well. There are two primary types of graph traversal: Depth-First Search (DFS) and Breadth-First Search (BFS). These methods are used for various purposes, such as searching for specific nodes, checking connectivity, or finding shortest paths in unweighted graphs.


    1. Types of Graph Traversals

    Depth-First Search (DFS)

    • Depth-First Search (DFS) is an algorithm that starts at a source vertex and explores as far as possible along each branch before backtracking.
    • In DFS, the algorithm follows a deep path, visiting a node and then recursively visiting each unvisited adjacent node until there are no more unvisited adjacent nodes. It backtracks when it reaches a dead-end (a node with no unvisited adjacent nodes).
    • DFS is typically implemented using recursion or an explicit stack data structure to keep track of the nodes to visit.

    Steps for DFS:

    1. Start at a source node.
    2. Visit the node and mark it as visited.
    3. Explore all the adjacent nodes that have not been visited.
    4. If there are no unvisited adjacent nodes, backtrack to the previous node.
    5. Repeat the process until all reachable nodes are visited.

    DFS Algorithm (Pseudocode):

    DFS(graph, node):
      mark node as visited
      for each adjacent node of node:
        if node is not visited:
          DFS(graph, adjacent node)
    

    Time Complexity:

    • The time complexity of DFS is O(V+E)O(V + E)O(V+E), where VVV is the number of vertices and EEE is the number of edges. This is because each vertex and edge is explored once.

    Applications of DFS:

    • Topological Sorting: In Directed Acyclic Graphs (DAGs), DFS can be used to find the topological order of vertices.
    • Cycle Detection: DFS can be used to detect cycles in a graph, especially in directed graphs.
    • Pathfinding: DFS can be used to find a path between two nodes, although it does not guarantee the shortest path.
    • Connected Components: DFS can identify all the vertices connected to a particular vertex in an undirected graph.

    Breadth-First Search (BFS)

    • Breadth-First Search (BFS) is an algorithm that explores all the nodes at the present depth level before moving on to nodes at the next depth level. It visits all the nodes level by level, starting from the source node.
    • BFS is typically implemented using a queue to store the nodes to visit next. It explores all neighbors of a node before moving to the next level of neighbors.

    Steps for BFS:

    1. Start at a source node.
    2. Mark the node as visited and enqueue it.
    3. While the queue is not empty:
      • Dequeue a node from the queue.
      • Visit all adjacent nodes that have not been visited.
      • Enqueue the unvisited adjacent nodes.

    BFS Algorithm (Pseudocode):

    BFS(graph, start):
      create an empty queue
      mark start node as visited
      enqueue start node
      while queue is not empty:
        node = dequeue
        for each adjacent node of node:
          if adjacent node is not visited:
            mark adjacent node as visited
            enqueue adjacent node
    

    Time Complexity:

    • The time complexity of BFS is O(V+E)O(V + E)O(V+E), where VVV is the number of vertices and EEE is the number of edges. This is because every vertex and edge is processed once.

    Applications of BFS:

    • Shortest Path in Unweighted Graphs: BFS can find the shortest path between two nodes in an unweighted graph, as it explores the nearest nodes first.
    • Connected Components: BFS can be used to find all nodes in the same connected component in an undirected graph.
    • Level Order Traversal: BFS is used to traverse a tree level by level, which can be applied to tree data structures as well.

    2. Comparison Between DFS and BFS

    Aspect Depth-First Search (DFS) Breadth-First Search (BFS)
    Traversal Order Goes deep into the graph before backtracking. Explores all neighbors at the current level before moving deeper.
    Data Structure Uses a stack (either recursion or explicit). Uses a queue.
    Time Complexity O(V+E)O(V + E)O(V+E) O(V+E)O(V + E)O(V+E)
    Space Complexity O(V)O(V)O(V) (for recursive stack or explicit stack) O(V)O(V)O(V) (for queue storage)
    Pathfinding Finds a path but does not guarantee the shortest path. Guarantees the shortest path in unweighted graphs.
    Cycle Detection Effective for cycle detection in both directed and undirected graphs. Less useful for cycle detection.
    Memory Usage Lower memory usage in sparse graphs, but deep recursion may lead to stack overflow. Higher memory usage due to storing all nodes in the queue.

    3. Applications and Use Cases

    DFS Applications:

    • Topological Sorting: DFS can be used in Directed Acyclic Graphs (DAGs) to produce a topological sort (an ordering of vertices such that for every directed edge uvuvuv, vertex uuu comes before vvv).
    • Cycle Detection: DFS is effective for detecting cycles in directed and undirected graphs. For directed graphs, DFS can detect back edges (which form a cycle).
    • Pathfinding: DFS can be used to find a path from one node to another, though it does not guarantee the shortest path.

    BFS Applications:

    • Shortest Path in Unweighted Graphs: BFS is ideal for finding the shortest path in an unweighted graph because it explores all nodes at the current depth before moving deeper, ensuring that the first time a node is visited, it is along the shortest path.
    • Connected Components: BFS can identify all vertices connected to a specific vertex in an undirected graph.
    • Level-Order Traversal: BFS is used for traversing trees level by level.

    4. Example of DFS and BFS

    Let’s take an example graph and apply both DFS and BFS.

    Graph:

        A -- B -- E
        |    |
        C -- D
    
    1. DFS Traversal starting from node A:

      • Stack-based DFS: Start at A, then go deeper to B, then D, then C, and backtrack to visit E.
      • DFS Order: A, B, D, C, E
    2. BFS Traversal starting from node A:

      • Queue-based BFS: Start at A, then visit all neighbors of A (B and C), then visit their neighbors (D and E).
      • BFS Order: A, B, C, D, E

    5. Conclusion

    • DFS and BFS are the two fundamental graph traversal algorithms. While both explore all vertices and edges, they differ in the order of exploration and the data structures they use (stack for DFS, queue for BFS).
    • DFS is useful for tasks like cycle detection, topological sorting, and pathfinding in deep structures.
    • BFS is ideal for finding the shortest path in unweighted graphs and is commonly used in scenarios like level-order traversal and searching for all reachable vertices from a source node.

    Both traversal methods are critical tools in graph theory, and their applications span various domains, including networking, social network analysis, and solving complex puzzles.

    Previous topic 60
    Rooted Trees
    Next topic 62
    Handshaking Lemma and Corollary

    Past Papers

    Open this section to load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time7 min
      Word count1,200
      Code examples0
      DifficultyIntermediate