🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
The Depth-First Search (DFS) is a fundamental algorithm used to traverse or search through graphs and trees. It explores as far as possible along each branch before backtracking. The main idea behind DFS is to go deeper into the graph whenever possible.
2. Program Overview
In this program, we will:
1. Create a representation for a graph using an adjacency list.
2. Implement the DFS algorithm to traverse through the graph and print each vertex.
3. Code Program
class Graph:
def __init__(self):
# Dictionary to store the adjacency list of the graph
self.graph = dict()
def add_edge(self, u, v):
# Add an edge to the adjacency list
if u not in self.graph:
self.graph[u] = []
self.graph[u].append(v)
def dfs(self, v, visited=None):
# Mark the current node as visited
if visited is None:
visited = set()
visited.add(v)
print(v, end=' ')
# Recur for all adjacent vertices
for neighbour in self.graph.get(v, []):
if neighbour not in visited:
self.dfs(neighbour, visited)
return visited
# Create a sample graph and add edges
g = Graph()
g.add_edge(0, 1)
g.add_edge(0, 2)
g.add_edge(1, 2)
g.add_edge(2, 0)
g.add_edge(2, 3)
g.add_edge(3, 3)
# Perform DFS from vertex 2
print("DFS starting from vertex 2:")
g.dfs(2)
Output:
DFS starting from vertex 2: 2 0 1 3
4. Step By Step Explanation
1. We initialize an empty graph with the Graph class, which uses a dictionary to represent the adjacency list.
2. The add_edge method allows adding edges to the graph.
3. The dfs method is used to perform a depth-first traversal of the graph. It uses a set visited to keep track of visited vertices.
4. In the sample provided, we created a graph with 4 vertices and then executed DFS starting from vertex 2. The output represents the order in which nodes are visited.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment