Depth-First Search (DFS)Implement Depth-First Search (DFS) for a given undirected graph. Print the vertices in the order they are visited.Constraints:NAExample:Sample Input:4 40 10 21 22 3Sample Output:DFS starting from vertex 0: 0 1 2 3 Explanation:Input Format:The first line contains two integers, V (number of vertices) and E (number of edges).The next E lines contain two integers each, u and v, representing an edge between vertices u and v.Output Format:Print the vertices in the order they are visited using DFS starting from vertex 0.
Question
Depth-First Search (DFS)Implement Depth-First Search (DFS) for a given undirected graph. Print the vertices in the order they are visited.Constraints:NAExample:Sample Input:4 40 10 21 22 3Sample Output:DFS starting from vertex 0: 0 1 2 3 Explanation:Input Format:The first line contains two integers, V (number of vertices) and E (number of edges).The next E lines contain two integers each, u and v, representing an edge between vertices u and v.Output Format:Print the vertices in the order they are visited using DFS starting from vertex 0.
Solution
Here is a Python implementation of Depth-First Search (DFS) for a given undirected graph:
from collections import defaultdict
class Graph:
def __init__(self, vertices):
self.V = vertices
self.graph = defaultdict(list)
def addEdge(self, u, v):
self.graph[u].append(v)
def DFSUtil(self, v, visited):
visited[v] = True
print(v, end=' ')
for i in self.graph[v]:
if visited[i] == False:
self.DFSUtil(i, visited)
def DFS(self, v):
visited = [False] * (max(self.graph) + 1)
self.DFSUtil(v, visited)
# Driver code
if __name__ == '__main__':
g = Graph(4)
g.addEdge(0, 1)
g.addEdge(0, 2)
g.addEdge(1, 2)
g.addEdge(2, 0)
g.addEdge(2, 3)
g.addEdge(3, 3)
print("DFS starting from vertex 0:")
g.DFS(0)
In this code, we first create a graph using the Graph class. The addEdge method is used to add an edge between two vertices in the graph. The DFS method is used to perform a depth-first search starting from a given vertex. The DFSUtil method is a utility function that is used by the DFS method to perform the actual depth-first search.
When you run this code, it will print the vertices in the order they are visited using DFS starting from vertex 0.
Similar Questions
Perform a Depth First Traversal of the graph for undirected graphYou are given a connected undirected graph. Perform a Depth First Traversal of the graph.Note: Use a recursive approach to find the DFS traversal of the graph starting from the 0th vertex from left to right according to the graph.Your task:You don't need to read input or print anything. Your task is to complete the function dfsOfGraph() which takes the integer V denoting the number of vertices and adjacency list as input parameters and returns a list containing the DFS traversal of the graph starting from the 0th vertex from left to right according to the graph.Expected Time Complexity: O(V + E)Expected Auxiliary Space: O(V)Constraints:1 ≤ V, E ≤ 104Sample input5 4 0 1 0 2 0 3 2 4Sample outputoutput=0 3 2 4 1
What is the role of Depth-First Search (DFS) in graph traversal?A) Ensures shortest path between nodesB) Visits nodes depth-wise until no more unvisited nodes are leftC) Calculates the average distance between nodesD) Finds the maximum flow in a graph
Consider the following sequence of nodes for the undirected graph given below.a b e f d g ca b e f c g da d g e b c fa d b c g e fA Depth First Search (DFS) is started at node a. The nodes are listed in the order they are first visited. Which of the above is (are) possible output(s)?Marks : 1Negative Marks : 0Answer here2, 3, and 4 only1 and 3 only2 and 3 only1, 2, and 3
In a depth-first search (DFS) traversal of a graph, which data structure is used to store visited vertices?StackQueueHeapHash table
The running time complexity of DFS (Depth-first search) traversal algorithm is a. O(logV*logE), where V is the number of vertices and E is the number of edges b. O(logV+logE), where V is the number of vertices and E is the number of edges c. O(V+E), where V is the number of vertices and E is the number of edges d. O(V*E), where V is the number of vertices and E is the number of edges
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.