dfs
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nfrom typing import List\n\nclass Solution:\n    def minMalwareSpread(self, graph: List[List[int]], initial: List[int]) -> int:\n        # Initialize variables to keep track of the best node to remove and the result\n        best = -1\n        res = min(initial)  # Start with the smallest infected node\n        infected = set(initial)  # Convert the initial list to a set for efficient lookups\n\n        # DFS function to explore the graph from a given node\n        def dfs(node, visited):\n            if node in infected:\n                return 0  # Return 0 if the node is infected\n            visited.add(node)  # Mark the node as visited\n            counts = 1  # Start counting from the current node\n            for i in range(len(graph[node])):\n                if graph[node][i] == 1 and i not in visited:  # If there\'s a connection and it\'s not visited\n                    count = dfs(i, visited)  # Recursively explore the connected node\n                    if count == 0:\n                        infected.add(node)  # If the exploration returns 0, it means a connected node is infected\n                        return 0\n                    counts += count\n            return counts\n        \n        # Iterate over each initially infected node to simulate its removal\n        for node in initial:\n            visited = set([node])  # Initialize the visited set with the current node\n            count = 0\n            for i in range(len(graph)):\n                if graph[node][i] == 1 and i not in visited:\n                    count += dfs(i, visited)  # Explore connected nodes and add up the counts\n            \n            # If the current node leads to a higher count of non-infected nodes, update the result\n            # Or if the count is the same but the node index is smaller, also update the result\n            if count > best or (count == best and node < res):\n                res = node\n                best = count\n                \n        return res\n\n\n```
['Python3']