ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalGE-167Graph Traversals
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    GE-167

    Discrete Structures

    Progress0 / 67 done
    Topics67
    1Propositional and Predicate Logic
    2Logical Operators3Translations Between Symbolic Expressions and Formal English Expression4Logical Equivalences
    5Quantifiers6Nested Quantification7Equivalences in Predicate Logic8Translations Between Symbolic Forms and Formal English
    9Proof Methods and Strategies10Direct Proof11Proof by Contraposition12Proof by Induction13Proof by Implication14Existence Proof15Uniqueness Proofs16Trivial Proofs17Vacuous Proofs
    18Notations and Set Operations19Venn Diagrams20Countable and Uncountable Sets
    21Equivalence Relations and Partitions22Partial Orderings23Recurrence Relations51Reflexive, Symmetric, Transitive, and Antisymmetric52Equivalence Relations and Equivalence Classes53Partial Orders
    24Injective, Surjective, Bijective25Special Types of Functions26Function Composition27Inverse Functions28Recursive Functions29Compositions
    30Sequences and Series
    31Inclusion and Exclusion Principle32Pigeonhole Principle33Permutations and Combinations46Product Rule and Sum Rule47Principle of Inclusion-Exclusion48Binomial Coefficients49Pascal's Identity and Pascal’s Triangle50Binomial Theorem
    34Division Theorem35Modular Arithmetic36LCM and GCD37Euclidean and Extended Euclidean Method38Finding Solutions to Congruence
    39Fundamental Theorem of Arithmetic40Characterizations of Primes41Mersenne Primes
    42Weak Induction43Strong Induction
    44Formulation of Recurrences45Closed Formulas
    54Terminologies55Elements of Graph Theory56Planar Graphs57Graph Coloring58Euler Graph59Hamiltonian Path60Rooted Trees61Graph Traversals62Handshaking Lemma and Corollary63Special Families of Graphs64Graph Isomorphism65Planarity in Graphs66Eulerian and Hamiltonian Graphs67Trees in Graph Theory
    7 min read
    Topic 61 of 67
    Topic Study Note Illustration
    61

    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....

    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)

    Breadth-first search
    Visual ReferenceBreadth-First Search
    Wikipedia
    • 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 view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    7mremaining
    1.2kwords
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Discrete Structures