class Graph:
    def __init__(self):
        self.graph = {}

    def add_edge(self, u, v):
        if u not in self.graph:
            self.graph[u] = []
        self.graph[u].append(v)

    def dfs(self, start):
        stack = [start]
        visited = set()

        while stack:
            current_node = stack.pop()
            if current_node not in visited:
                print(current_node, end=' ')
                visited.add(current_node)
                stack.extend(neighbor for neighbor in self.graph.get(current_node, []) if neighbor not in visited)

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)

start_node = 0
print("DFS starting from node", start_node)
g.dfs(start_node)

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: