Skip to main content

CT Week 11

DFS and Recursive Procedure Call

📚

DFS and Recursive Procedure Call

Depth First Search (DFS)

Depth First Search (DFS) is an algorithm for traversing or searching tree or graph data structures. The algorithm starts at the root node (selecting some arbitrary node as the root in the case of a graph) and explores as far as possible along each branch before backtracking.

How DFS Works

DFS uses a stack data structure, either implicitly through recursion or explicitly. The steps are:

  1. Start at the root node.
  2. Mark the current node as visited and print it.
  3. Traverse all the adjacent and unmarked nodes. Push each adjacent node into a stack.
  4. Repeat the process until the stack is empty.

Pseudocode for DFS


function DFS(graph, start):
    let S be a stack
    S.push(start)
    while S is not empty:
        vertex = S.pop()
        if vertex is not labeled as discovered:
            label vertex as discovered
            for all edges from vertex to neighbor in graph:
                S.push(neighbor)
    

Recursion

Recursion is a method of solving a problem where the solution depends on solutions to smaller instances of the same problem. A recursive function calls itself to solve these smaller instances.

How Recursion Works

A recursive function has two main parts:

  • Base Case: The condition under which the recursion ends.
  • Recursive Case: The part of the function where the function calls itself.

Pseudocode for a Recursive Function


function recursiveFunction(parameters):
    if base case condition:
        return base case value
    else:
        return recursiveFunction(modified parameters)
    

Procedure Call

A procedure call is a statement in a program that causes a procedure to be executed. In the context of recursion, a procedure call is when a function calls itself.

Procedure for Recursion

The procedure for recursion involves:

  1. Defining the base case to stop the recursion.
  2. Defining the recursive case where the function calls itself with modified parameters.
  3. Ensuring that each recursive call progresses towards the base case.

DFS Using Recursion

DFS can also be implemented using recursion. Here is the pseudocode for DFS using a recursive approach:


function DFS(graph, vertex, visited):
    visited[vertex] = true
    for neighbor in graph[vertex]:
        if not visited[neighbor]:
            DFS(graph, neighbor, visited)
    

In this approach, we use a recursive function to visit all the vertices of the graph. The function marks the current vertex as visited and then recursively visits all the unvisited neighbors.

This PDF contains a summary covering weeks 1 to 11.

PA

GA

Comments

Popular post

IITM Notes

Course Overview “These handwritten notes encompass topics in data science and civil services. The beauty of knowledge is that you don’t need to belong to any specific group; simply maintain your curiosity, and knowledge will find its way to you. I hope these notes are helpful. If they are, please consider leaving a comment below and follow my blog for updates.” Mathematics 1 👉 Select Week Week 1 Week 2 Week 3 Week 4 Week 5 Week 6 Week 7 Week 8 Week 9 Week 10 Week 11 Revision Statistics 1 👉 Select Week Week 1 Week 2 Week 3 Week 4 Week 5 Week 6 Week 7 Week 8 Week 9 Week 10 Week 11

Maths 1 week 1 Summary

Number System and Set Theory 📚 Number System and Set Theory This week, our teacher covered the basics of the number system. We were instructed to consider 0 as part of the natural numbers, as it will be treated as such in future subjects like Python. However, in exams, it will be explicitly stated whether 0 should be considered a natural number. The key topics from this week include set theory and the relationship between two sets. In set theory, we focused on three Venn diagram problems. In the context of relations, we discussed the concepts of reflexive, symmetric, transitive, and equivalence relations. Detailed Explanation 1.Union of Two Sets The union of two sets A and B is the set of elements that are in either A , B , or both. It is denoted as A ∪ B . 2.Intersection of Two Sets The intersection of two sets A and B is the set of elements that are in both A and B . It is denoted as A ∩ B . 3.Subt

Community page

Welcome To our IITM BS Students Community This community is a student commune where IIT Madras Bachelor of Science students are studying. Our community is managed by 15 community admins who oversee our WhatsApp community, Discord, and Telegram profiles. With more than 1000+ active members, we study together, share memes, watch movies, play games, and have fun. Our goal is to bring all online IITM students together to excel in exams while having fun. Community Admins Agampreet LinkedIn Ansh Ashwin Ambatwar Arti Dattu Dolly Elango Koushik Shrijanani Saksham Shivamani Shivam Instagram LinkedIn Join Our Community Subscribe to our YouTube page Join our meme team on