source
stringclasses
6 values
id
int64
0
111k
instruction
stringlengths
167
4.39k
response
stringlengths
244
6.1k
code_exercise_27k
17,061
Write a Python function to find the minimum spanning tree of a given graph using Kruskal's algorithm. The graph is represented as an adjacency list where each key is a node and the value is a list of tuples representing the adjacent nodes and the corresponding edge weights. The function should return the minimum spanni...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List, Tuple def find_minimum_spanning_tree(graph: Dict[int, List[Tuple[int, int]]]) -> List[Tuple[int, int, int]]: """ Find the minimum spanning tree of a graph using Kruskal's algorithm. Args: ...
code_exercise_27k
17,069
Write a Python function to determine if a given undirected graph is a tree using Depth-First Search (DFS). The graph is represented as an adjacency list. A tree is defined as an acyclic connected graph. The function should return `True` if the graph is a tree and `False` otherwise. The code should start with: ```pytho...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List def is_tree(graph: Dict[int, List[int]]) -> bool: """ Determine if a given undirected graph is a tree using Depth-First Search (DFS). Parameters: graph (Dict[int, List[int]]): An adjacency...
code_exercise_27k
17,077
Write a Python function to check if a given directed graph contains a cycle using Depth-First Search (DFS). The graph is represented as an adjacency list. The function should return `True` if the graph contains a cycle, and `False` otherwise. The code should start with: ```python from typing import Dict, Set def has_...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, Set def has_cycle_in_graph(graph: Dict[int, Set[int]]) -> bool: """ Check if a given directed graph contains a cycle using Depth-First Search (DFS). Parameters: - graph (Dict[int, Set[int]]): A...
code_exercise_27k
17,078
Write a Python function to calculate the in-degree of a vertex in a directed graph. The function should take a graph represented as a dictionary and a vertex as input, and return the in-degree of the specified vertex. The in-degree of a vertex is the number of edges that are directed towards it. The code should start ...
Here is the Python code to solve the given problem in the markdown block: ```python def calculate_in_degree(graph: dict, vertex: str) -> int: """ Calculate the in-degree of a vertex in a directed graph. Parameters: graph (dict): The directed graph represented as a dictionary where each key is a vertex ...
code_exercise_27k
17,082
Write a Python function to check if a given string of parentheses, brackets, and braces is balanced using a stack. The string may contain any combination of the following characters: `(`, `)`, `[`, `]`, `{`, `}`. The function should return `True` if the string is balanced (i.e., every opening character has a correspond...
Here is the Python code to solve the given problem in the markdown block: ```python def is_balanced_parentheses(string: str) -> bool: """ Checks if a given string of parentheses, brackets, and braces is balanced. Args: string (str): A string containing only parentheses, brackets, and braces. R...
code_exercise_27k
17,084
Write a Python function to calculate the out-degree of a vertex in a directed graph. The function should take a dictionary representing the directed graph and a vertex as input, and return the out-degree of the specified vertex. The out-degree of a vertex is defined as the number of outgoing edges from that vertex. Th...
Here is the Python code to solve the given problem in the markdown block: ```python def get_out_degree(graph: dict, vertex: str) -> int: """ Calculates the out-degree of a vertex in a directed graph. Args: graph (dict): A dictionary representing the directed graph. Each key is a vertex, and the cor...
code_exercise_27k
17,088
Write a Python function to calculate the degree of a vertex in an undirected graph. The graph is represented as an adjacency list, where keys are vertices and values are lists of adjacent vertices. The function should handle the case where the vertex is not present in the graph by raising a `KeyError`. The code should...
Here is the Python code to solve the given problem in the markdown block: ```python def calculate_degree(graph: dict, vertex: str) -> int: """ Calculate the degree of a vertex in an undirected graph. Args: graph (dict): An undirected graph represented as an adjacency list, where keys are vertices a...
code_exercise_27k
17,106
Write a Python function to determine if a given directed graph is acyclic or not using Depth-First Search (DFS). The function should take a dictionary representing the graph as input and return a boolean indicating whether the graph is acyclic. The code should start with: ```python from typing import Dict, List def i...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List def is_acyclic_graph(graph: Dict[int, List[int]]) -> bool: """ Determines whether a given directed graph is acyclic or not using Depth-First Search (DFS). Args: graph (Dict[int, List[i...
code_exercise_27k
17,109
Write a Python function to determine if a given directed graph is a valid Directed Acyclic Graph (DAG). The graph is represented as an adjacency list, where each key is a vertex and the corresponding value is a list of vertices that the key vertex points to. The function should return `True` if the graph is a valid DAG...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List def is_valid_dag(graph: Dict[int, List[int]]) -> bool: """ Determines if the given directed graph is a valid Directed Acyclic Graph (DAG). Args: graph (Dict[int, List[int]]): An adjace...
code_exercise_27k
17,114
Write a Python function to reverse the order of words in a given sentence using a stack. The function should handle sentences with multiple spaces between words and leading/trailing spaces. The code should start with: ```python def reverse_words_with_stack(sentence: str) -> str: ```
Here is the Python code to solve the given problem in the markdown block: ```python def reverse_words_with_stack(sentence: str) -> str: """ Reverses the order of words in a given sentence using a stack. Args: sentence (str): The input sentence to be reversed. Returns: str: The sentence...
code_exercise_27k
17,116
Write a Python function to calculate the transitive reduction of a directed graph. The function should take a dictionary representing the directed graph as input and return a dictionary representing the transitive reduction of the graph. The transitive reduction of a graph is a subgraph that retains only the essential ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List def transitive_reduction(graph: Dict[str, List[str]]) -> Dict[str, List[str]]: """ Calculates the transitive reduction of a directed graph. Parameters: graph (dict): A dictionary represent...
code_exercise_27k
17,119
Write a Python function to find all possible paths from a start node to an end node in an undirected graph using Depth-First Search (DFS). The graph is represented as an adjacency list. The function should return a list of all possible paths, where each path is a list of nodes. The code should start with: ```python fr...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Dict def find_all_paths(graph: Dict[int, List[int]], start: int, end: int) -> List[List[int]]: """ Find all possible paths from the start node to the end node in an undirected graph using DFS. Args...
code_exercise_27k
17,127
Write a Python function to find the next greater element for each element in a given array using a stack. The function should return a list where each element at index `i` contains the next greater element for the element at index `i` in the input array. If there is no greater element, the function should return `-1` f...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_next_greater_elements(arr: List[int]) -> List[int]: """ Finds the next greater element for each element in the given array using a stack. Args: arr: A list of integers. Returns...
code_exercise_27k
17,135
Write a Python function to find the largest rectangular area in a histogram using a stack. The histogram is represented by a list of integers where each integer represents the height of a bar. The width of each bar is 1. The function should return the area of the largest rectangle that can be formed within the histogra...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_largest_rectangular_area(heights: List[int]) -> int: """ Finds the largest rectangular area in a histogram using a stack. The function takes a list of integer values representing the height...
code_exercise_27k
17,139
Write a Python function to determine if a directed graph has an Eulerian path. An Eulerian path in a directed graph is a path that visits every edge exactly once. The function should take a dictionary representing the directed graph as input and return a boolean indicating whether the graph has an Eulerian path. The gr...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List def has_eulerian_path(graph: Dict[str, List[str]]) -> bool: """ Determine if a directed graph has an Eulerian path. An Eulerian path in a directed graph is a path that visits every edge exactl...
code_exercise_27k
17,144
Write a Python function to determine if a given graph is bipartite using Depth-First Search (DFS) and backtracking. A bipartite graph is a graph whose vertices can be divided into two disjoint sets such that no two graph vertices within the same set are adjacent. The graph is represented as an adjacency list. The code...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List def is_bipartite_graph(graph: Dict[int, List[int]]) -> bool: """ Determine if a given graph is bipartite using DFS and backtracking. Args: graph (Dict[int, List[int]]): The input graph...
code_exercise_27k
17,175
Write a Python function to check if a given directed graph is strongly connected or not using Depth-First Search (DFS). A directed graph is strongly connected if there is a path between any pair of vertices in both directions. The code should start with: ```python from typing import Dict, List def is_strongly_connect...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List def is_strongly_connected(graph: Dict[int, List[int]]) -> bool: """ Check if a given directed graph is strongly connected using DFS. Parameters: graph (Dict[int, List[int]]): The directed ...
code_exercise_27k
17,177
Write a Python function to check if a given graph is connected using Depth-First Search (DFS). The graph is represented as an adjacency list in the form of a dictionary where each key is a node and its value is a list of adjacent nodes. The function should return `True` if the graph is connected (i.e., there is a path ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List def is_graph_connected(graph: Dict[int, List[int]]) -> bool: """ Check if a given graph is connected using Depth-First Search (DFS). Args: graph (Dict[int, List[int]]): A dictionary re...
code_exercise_27k
17,188
Write a Python function to reverse the elements of a given queue. The function should take a `queue.Queue` object as input and return the reversed queue. The queue should be reversed in place, meaning the original queue object should be modified directly. The code should start with: ```python from queue import Queue ...
Here is the Python code to solve the given problem in the markdown block: ```python from queue import Queue def reverse_queue(queue: Queue) -> Queue: """ Reverse the elements of a given queue. Args: queue (Queue): The queue to be reversed. Returns: Queue: The reversed queue. """ ...
code_exercise_27k
17,195
Write a Python function to determine if a given graph represented as an adjacency list is a valid tree. A graph is considered a valid tree if it is connected (there is a path between any two nodes) and acyclic (there are no cycles). The code should start with: ```python from typing import Dict, List, Tuple def is_val...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List, Tuple def is_valid_tree(graph: Dict[int, List[int]]) -> bool: """ Determine if the given graph represented as an adjacency list is a valid tree. A graph is considered a valid tree if it is co...
code_exercise_27k
17,198
Write a Python function to find the diameter of an undirected, unweighted graph. The graph is represented as an adjacency list using a dictionary where keys are vertices and values are lists of adjacent vertices. The diameter of a graph is defined as the longest shortest path between any two vertices in the graph. The...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List def find_graph_diameter(graph: Dict[str, List[str]]) -> int: """ Calculate the diameter of an undirected, unweighted graph. This function uses the breadth-first search algorithm to find the ...
code_exercise_27k
17,203
Write a Python function to find the maximum element in a queue implemented using a `deque` from the `collections` module. The function should handle an empty queue by raising a `ValueError`. The code should start with: ```python from collections import deque def find_max_element(queue: deque) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python from collections import deque def find_max_element(queue: deque) -> int: """ Find the maximum element in a queue implemented using a deque. Args: queue (deque): A deque object containing the elements of the queue. ...
code_exercise_27k
17,210
Write a Python function to copy the elements of a queue to another queue. The function should take a source queue as input and return a new queue containing the same elements in the same order. The source queue should remain unchanged after the operation. The code should start with: ```python from queue import Queue ...
Here is the Python code to solve the given problem in the markdown block: ```python from queue import Queue def copy_queue(src_queue: Queue) -> Queue: """ Copies the elements of a source queue to a new queue. Args: src_queue (Queue): The source queue to be copied. Returns: Queue: A ne...
code_exercise_27k
17,228
Write a Python function to merge two queues into a single queue, alternating between the elements from each queue. If one queue is longer than the other, the remaining elements from the longer queue should be appended to the merged queue. The code should start with: ```python from queue import Queue from typing import...
Here is the Python code to solve the given problem in the markdown block: ```python from queue import Queue from typing import Any def merge_queues_alternating(queue1: Queue, queue2: Queue) -> Queue: """ Merge two queues into a single queue with alternating elements. The `merge_queues_alternating` functio...
code_exercise_27k
17,231
Write a Python function to check if a given graph contains a Hamiltonian path. A Hamiltonian path is a path in an undirected or directed graph that visits each vertex exactly once. The function should return `True` if the graph contains a Hamiltonian path, and `False` otherwise. The graph is represented as a dictionar...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List def check_hamiltonian_path(graph: Dict[int, List[int]]) -> bool: """ Checks if a given graph contains a Hamiltonian path. Parameters: graph (Dict[int, List[int]]): A dictionary representin...
code_exercise_27k
17,237
Write a Python function to count the occurrences of an element in a queue. The queue is represented as a `deque` from the `collections` module. The function should handle the case where the input queue is not of type `deque` by raising a `TypeError`. The code should start with: ```python from collections import deque ...
Here is the Python code to solve the given problem in the markdown block: ```python from collections import deque def count_occurrences(queue: deque, element) -> int: """ Count the occurrences of an element in a queue. Args: queue (deque): The queue to search for occurrences. element: The ...
code_exercise_27k
17,243
Write a Python function to find the maximum independent set in a graph using a greedy algorithm. The function should take a graph represented as a dictionary where keys are vertices and values are sets of neighboring vertices. The function should return a set containing the maximum independent set of vertices. The cod...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, Set def find_maximum_independent_set(graph: Dict[int, Set[int]]) -> Set[int]: """ Finds the maximum independent set in a graph using the greedy algorithm. Args: graph: A dictionary represen...
code_exercise_27k
17,255
Write a Python function to find all even numbers in a given queue. The queue contains integers, and the function should return a list of all even numbers present in the queue. The function should handle an empty queue gracefully and return an empty list in such cases. The code should start with: ```python from collect...
Here is the Python code to solve the given problem in the markdown block: ```python from collections import deque from typing import List def find_even_numbers(queue: deque) -> List[int]: """ Find all even numbers in a given queue of integers. Args: queue (deque): A queue containing integers. ...
code_exercise_27k
17,259
Write a Python function to find all odd numbers in a given queue. The function should return a list of all odd numbers found in the queue. The queue is implemented using the `queue.Queue` class from the Python standard library. The code should start with: ```python from queue import Queue from typing import List def ...
Here is the Python code to solve the given problem in the markdown block: ```python from queue import Queue from typing import List def find_odd_numbers(queue: Queue) -> List[int]: """ Find all odd numbers in a given queue. Args: queue (Queue): The input queue containing integers. Returns: ...
code_exercise_27k
17,262
Write a Python function to remove all even numbers from a given queue of integers. The function should return a new queue containing only the odd numbers from the original queue. The original queue should not be modified. The code should start with: ```python from queue import Queue def remove_even_numbers(queue: Que...
Here is the Python code to solve the given problem in the markdown block: ```python from queue import Queue def remove_even_numbers(queue: Queue) -> Queue: """ Remove all even numbers from a given queue of integers and return a new queue with only odd numbers. Args: queue (Queue): A queue of integ...
code_exercise_27k
17,264
Write a Python function to remove all odd numbers from a given queue of integers. The function should return a new queue containing only the even numbers from the original queue. The original queue should remain unchanged. The code should start with: ```python from queue import Queue def remove_odd_numbers(queue: Que...
Here is the Python code to solve the given problem in the markdown block: ```python from queue import Queue def remove_odd_numbers(queue: Queue) -> Queue: """ Removes all odd numbers from a given queue of integers and returns a new queue containing only the even numbers. Args: queue (Queue): A que...
code_exercise_27k
17,274
Write a Python function to find the average of all elements in a given queue. The queue contains numerical elements. The function should handle an empty queue by returning `None`. The code should start with: ```python from queue import Queue def find_queue_average(queue: Queue) -> float: ```
Here is the Python code to solve the given problem in the markdown block: ```python from queue import Queue def find_queue_average(queue: Queue) -> float: """ Calculates the average of all elements in the given queue. Args: queue (Queue): A queue object containing numerical elements. ...
code_exercise_27k
17,297
Write a Python function to find the greatest common divisor (GCD) of two numbers using the Euclidean algorithm. The function should be able to handle both positive and negative integers. The code should start with: ```python def gcd(a: int, b: int) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python def gcd(a: int, b: int) -> int: """ Calculates the greatest common divisor (GCD) of two numbers using the Euclidean algorithm. Args: a (int): The first number. b (int): The second number. Returns: ...
code_exercise_27k
17,298
Write a Python function to find the largest connected component in an undirected graph using Depth First Search (DFS). The graph is represented as an adjacency list. The function should return the largest connected component as a list of nodes. The code should start with: ```python from typing import Dict, List def l...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List def largest_connected_component(graph: Dict[int, List[int]]) -> List[int]: """ Finds the largest connected component in an undirected graph using Depth First Search (DFS). Args: graph ...
code_exercise_27k
17,299
Write a Python function to calculate the sum of the first K elements of a given list. The function should handle cases where K is greater than the length of the list by summing all elements in the list. The function should also handle cases where K is negative by returning 0. The code should start with: ```python from...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def calculate_list_sum(lst: List[int], k: int) -> int: """ Calculate the sum of the first K elements of a given list. Args: lst (List[int]): The list of integers from which to calculate the ...
code_exercise_27k
17,306
Write a Python function to calculate the product of the first K elements of a given queue. The function should handle cases where the queue does not contain enough elements and should raise a `ValueError` in such cases. Additionally, the function should ensure that all elements in the queue are numeric, raising a `Type...
Here is the Python code to solve the given problem in the markdown block: ```python from queue import Queue def product_of_k_elements(queue: Queue, k: int) -> int: """ Calculates the product of the first K elements of a queue. Args: queue (Queue): A queue containing numeric elements. k (in...
code_exercise_27k
17,307
Write a Python function to find the number of connected components in an undirected graph. The graph is represented as a dictionary where keys are nodes and values are lists of neighboring nodes. The function should start with: ```python from typing import Dict, List def count_connected_components(graph: Dict[int, Lis...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List def count_connected_components(graph: Dict[int, List[int]]) -> int: """ Count the number of connected components in an undirected graph. Args: graph (Dict[int, List[int]]): An undirect...
code_exercise_27k
17,310
Write a Python function to calculate the product of the last K elements in a given queue. The queue is represented using Python's `deque` from the `collections` module. If the queue does not have enough elements to calculate the product of the last K elements, the function should return `None`. The code should start w...
Here is the Python code to solve the given problem in the markdown block: ```python from collections import deque def calculate_product_of_last_k_elements(queue: deque, k: int) -> int: """ Calculate the product of the last K elements in the given queue. Args: queue (deque): A queue representation ...
code_exercise_27k
17,314
Write a Python function to find the articulation points in an undirected graph using Depth-First Search (DFS). An articulation point (or cut vertex) is a vertex whose removal increases the number of connected components in the graph. The function should take a graph represented as an adjacency list and return a list of...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List def find_articulation_points(graph: Dict[int, List[int]]) -> List[int]: """ Find the articulation points in an undirected graph using DFS. Args: graph: A dictionary representing the un...
code_exercise_27k
17,315
Write a Python function to calculate the average of the last K elements in a given queue. The queue is represented by a `deque` object from the `collections` module. The function should handle cases where K is greater than the number of elements in the queue or where K is less than or equal to 0 by raising a `ValueErro...
Here is the Python code to solve the given problem in the markdown block: ```python from collections import deque def average_of_last_k(queue: deque, k: int) -> float: """ Calculates the average of the last K elements in a queue. Args: queue (deque): A deque object representing the queue. ...
code_exercise_27k
17,319
Write a Python function to remove duplicate elements from a given queue and return a new queue with unique elements while maintaining the order of their first appearance. The function should use the `deque` data structure from the `collections` module. The code should start with: ```python from collections import dequ...
Here is the Python code to solve the given problem in the markdown block: ```python from collections import deque def remove_duplicates_from_queue(queue: deque) -> deque: """ Returns a new queue with duplicate elements removed while maintaining the order of their first appearance. Args: queue (deq...
code_exercise_27k
17,321
Write a Python function to return a new queue with only unique elements from a given queue. The function should maintain the order of elements as they appear in the original queue. The code should start with: ```python from collections import deque def get_unique_elements(queue: deque) -> deque: ```
Here is the Python code to solve the given problem in the markdown block: ```python from collections import deque def get_unique_elements(queue: deque) -> deque: """ Return a new queue with only unique elements from the given queue. Args: queue (deque): The input queue containing elements. ...
code_exercise_27k
17,328
Write a Python function to count the number of positive elements in a given queue. The queue is implemented using the `deque` class from the `collections` module. The function should iterate through the queue and count the number of elements that are greater than zero. The code should start with: ```python from collec...
Here is the Python code to solve the given problem in the markdown block: ```python from collections import deque def count_positive_elements(queue: deque) -> int: """ Counts the number of positive elements in a given queue. Args: queue (deque): The input queue containing integers. Returns: ...
code_exercise_27k
17,330
Write a Python function to find the longest common path in a directed acyclic graph (DAG) using Depth-First Search (DFS) and memoization. The function should return the longest path starting from a given node in the graph. The graph is represented as an adjacency list, where each key is a node and the corresponding val...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List def find_longest_path(graph: Dict[str, List[str]], start_node: str) -> List[str]: """ Find the longest path in a directed acyclic graph (DAG) starting from a given node using DFS and memoization. ...
code_exercise_27k
17,333
Write a Python function to count the number of negative elements in a given queue. The queue is represented as a `deque` from the `collections` module. The function should iterate through the queue and count the negative elements efficiently. The code should start with: ```python from collections import deque def cou...
Here is the Python code to solve the given problem in the markdown block: ```python from collections import deque def count_negative_elements_in_queue(queue: deque) -> int: """ Count the number of negative elements in the given queue. Args: queue (deque): A queue represented as a deque. Retur...
code_exercise_27k
17,339
Write a Python function to check if a given directed graph contains a zero-weight cycle using Depth-First Search (DFS). A zero-weight cycle is a cycle in the graph where the sum of the weights of all edges in the cycle is zero. The graph is represented as a dictionary where keys are vertices and values are lists of tup...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List, Tuple def contains_zero_weight_cycle(graph: Dict[int, List[Tuple[int, int]]]) -> bool: """ Check if a given directed graph contains a zero-weight cycle using DFS. Args: graph: A dicti...
code_exercise_27k
17,347
Write a Python function to remove all elements of a queue except the last K elements. The queue is represented by a `deque` from the `collections` module. The function should handle cases where K is greater than the length of the queue by raising a `ValueError`. The function should also handle cases where K is negative...
Here is the Python code to solve the given problem in the markdown block: ```python from collections import deque def remove_elements_from_queue(queue: deque, k: int) -> deque: """ Remove all elements of a queue except the last K elements. Args: queue (deque): A deque representing the queue. ...
code_exercise_27k
17,349
Write a Python function to remove all elements from a queue except the first K elements. The queue is represented using the `deque` class from the `collections` module. The function should modify the queue in place and return the modified queue. The code should start with: ```python from collections import deque def ...
Here is the Python code to solve the given problem in the markdown block: ```python from collections import deque def keep_first_k_elements(queue: deque, k: int) -> deque: """ Remove all elements from a queue except the first K elements. Args: queue (deque): The input queue. k (int): The n...
code_exercise_27k
17,359
Write a Python function to find the shortest path between two vertices in a weighted graph using the Floyd-Warshall algorithm. The function should return the shortest path as a list of vertex indices. The graph is represented as an adjacency matrix where `graph[i][j]` represents the weight of the edge from vertex `i` t...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_shortest_path(graph: List[List[int]], start: int, end: int) -> List[int]: """ Finds the shortest path between two vertices in a weighted graph using the Floyd-Warshall algorithm. Args: ...
code_exercise_27k
17,363
Write a Python function to find the maximum flow in a network with limited capacity per edge using Depth-First Search (DFS). The network is represented as an adjacency list where each node points to a list of tuples representing the edges and their corresponding capacities. The function should return the maximum flow f...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List, Tuple from collections import defaultdict def find_max_flow(graph: Dict[str, List[Tuple[str, int]]], source: str, sink: str) -> int: """ Finds the maximum flow in a network with limited capacity p...
code_exercise_27k
17,377
Write a Python function to find the largest element in a queue without changing the order of elements. The function should take a `Queue` object as input and return the largest element in the queue. The order of elements in the queue should remain unchanged after the function call. The code should start with: ```pytho...
Here is the Python code to solve the given problem in the markdown block: ```python from queue import Queue def find_largest_element(q: Queue) -> int: """ Finds the largest element in a queue without changing the order of elements. Args: q (Queue): The queue object to search for the largest elemen...
code_exercise_27k
17,378
Write a Python function to find the smallest element in a queue without changing the order of elements. The function should take a `queue.Queue` object as input and return the smallest element. The queue should remain unchanged after the function call. The code should start with: ```python from queue import Queue def...
Here is the Python code to solve the given problem in the markdown block: ```python from queue import Queue def find_smallest_element(queue: Queue) -> int: """ Finds the smallest element in a queue without changing the order of elements. Args: queue (Queue): The queue object containing elements. ...
code_exercise_27k
17,382
Write a Python function to find the largest and smallest elements in a queue without changing the order of elements. The function should take a `Queue` object as input and return a tuple containing the largest and smallest elements. The order of elements in the queue should remain unchanged after the function call. Th...
Here is the Python code to solve the given problem in the markdown block: ```python from queue import Queue def find_largest_smallest(queue: Queue) -> tuple: """ Finds the largest and smallest elements in a queue without changing the order of elements. Args: queue (Queue): The input queue. Re...
code_exercise_27k
17,383
Write a Python function to determine if a weighted directed graph contains a positive cycle reachable from a given start node using Depth-First Search (DFS). The graph is represented as a dictionary where each key is a node, and its value is a list of tuples representing the outgoing edges from that node. Each tuple co...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List, Tuple def has_positive_cycle(graph: Dict[str, List[Tuple[str, int]]], start: str) -> bool: """ Determine if a weighted directed graph contains a positive cycle reachable from the start node using ...
code_exercise_27k
17,388
Write a Python function to calculate the sum of all even numbers in a queue without changing the order of elements. The queue is represented as a `deque` object from the `collections` module. The function should iterate through the queue, identify even numbers, and sum them up. The original order of elements in the que...
Here is the Python code to solve the given problem in the markdown block: ```python from collections import deque def sum_of_even_numbers(queue: deque) -> int: """ Calculate the sum of all even numbers in a queue without changing the order of elements. Args: queue (deque): A queue (represented as ...
code_exercise_27k
17,392
Write a Python function to calculate the average of all elements in a queue without changing the order of elements. The function should handle an empty queue by raising a `ValueError`. The code should start with: ```python from queue import Queue def calculate_average(queue: Queue) -> float: ```
Here is the Python code to solve the given problem in the markdown block: ```python from queue import Queue def calculate_average(queue: Queue) -> float: """ Calculate the average of all elements in a queue without changing the order of elements. Args: queue (Queue): The queue containing the eleme...
code_exercise_27k
17,401
Write a Python function to remove all duplicate elements from a queue without changing the order of elements. The function should take a `deque` object as input and return a `deque` object with all duplicates removed. The original order of elements should be preserved. The code should start with: ```python from collec...
Here is the Python code to solve the given problem in the markdown block: ```python from collections import deque def remove_duplicates_from_queue(queue: deque) -> deque: """ Remove all duplicate elements from a queue without changing the order of elements. Args: queue (deque): The input queue con...
code_exercise_27k
17,403
Write a Python function to determine if a given graph is chordal. A graph is chordal if every cycle of four or more nodes has a chord (an edge connecting two non-adjacent nodes in the cycle). The graph is represented as an adjacency list where the keys are nodes and the values are lists of neighbors for each node. The...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List, Set def is_chordal_graph(graph: Dict[int, List[int]]) -> bool: """ Determines if a given graph is chordal. Args: graph: An adjacency list representing the graph. The keys are nodes ...
code_exercise_27k
17,406
Write a Python function to count the occurrences of each element in a queue without changing the order of elements. The queue is represented as a `deque` object from the `collections` module. The function should return a dictionary where the keys are the elements of the queue and the values are their respective counts....
Here is the Python code to solve the given problem in the markdown block: ```python from collections import deque def count_occurrences(queue: deque) -> dict: """ Count the occurrences of each element in a queue without changing the order of elements. Args: queue (deque): A queue represented as a ...
code_exercise_27k
17,421
Write a Python function to find the second largest and second smallest elements in a queue without changing the order of elements. The function should take a `Queue` object as input and return a tuple containing the second largest and second smallest elements. The function should handle edge cases such as an empty queu...
Here is the Python code to solve the given problem in the markdown block: ```python from queue import Queue from typing import Tuple def find_second_largest_and_smallest(queue: Queue) -> Tuple[int, int]: """ Find the second largest and second smallest elements in a queue without changing the order of elements....
code_exercise_27k
17,434
Write a Python function to count the number of prime numbers in a given queue of integers. The function should take a `queue.Queue` object as input and return the count of prime numbers in the queue. The function should use a helper function to check if a number is prime. The code should start with: ```python import q...
Here is the Python code to solve the given problem in the markdown block: ```python import queue import math def is_prime(num: int) -> bool: """ Check if a number is prime. Parameters: num (int): The number to be checked. Returns: bool: True if the number is prime, False otherwise. """ ...
code_exercise_27k
17,438
Write a Python function to count the number of elements in a queue that are perfect squares. The function should take a `queue.Queue` object as input and return the count of perfect squares. The function should also include a helper function to check if a number is a perfect square. The code should start with: ```pyth...
Here is the Python code to solve the given problem in the markdown block: ```python import queue def count_perfect_squares(queue: queue.Queue) -> int: """ Counts the number of elements in a queue that are perfect squares. Args: queue (queue.Queue): A queue containing elements. Returns: ...
code_exercise_27k
17,450
Write a Python function to determine if a weighted directed graph contains a cycle with a positive weight using Depth-First Search (DFS). The graph is represented as an adjacency list where keys represent the vertices and the values are dictionaries representing outgoing edges and their weights. The function should ret...
Here is the Python code to solve the given problem in the markdown block: ```python from collections import defaultdict def has_positive_weight_cycle(graph: dict) -> bool: """ Determine if a weighted directed graph contains a cycle with a positive weight using Depth-First Search (DFS). Parameters: gra...
code_exercise_27k
17,457
Write a Python function to remove all prime numbers from a given queue without changing the order of the remaining elements. The function should use the `queue.Queue` class from the `queue` module. The function should start with: ```python from queue import Queue def remove_prime_numbers_from_queue(queue: Queue) -> Qu...
Here is the Python code to solve the given problem in the markdown block: ```python from queue import Queue def is_prime(n: int) -> bool: """ Check if a number is prime. Parameters: n (int): The number to check. Returns: bool: True if n is prime, False otherwise. """ if n <= 1...
code_exercise_27k
17,459
Write a Python function to generate the adjacency matrix of a given graph represented as an adjacency list. The adjacency list is a dictionary where the keys are the vertices and the values are lists of adjacent vertices. The function should return a 2D list representing the adjacency matrix. The code should start wit...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List def generate_adjacency_matrix(graph: Dict[str, List[str]]) -> List[List[int]]: """ Generate the adjacency matrix of a given graph represented as an adjacency list. Args: graph (Dict[st...
code_exercise_27k
17,461
Write a Python function to perform depth-first search (DFS) traversal of a graph. The graph is represented as an adjacency list where each key is a node and the value is a list of its neighbors. The function should return the nodes visited during the DFS traversal in the order they were visited. The code should start ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List, Any def dfs(graph: Dict[Any, List[Any]], start: Any) -> List[Any]: """ Perform a depth-first search (DFS) traversal of a graph. Args: graph (dict): The graph, represented as an adjace...
code_exercise_27k
17,462
Write a Python function to find the shortest path between two nodes in a weighted graph using Dijkstra's algorithm. The graph is represented as a dictionary where the keys are nodes and the values are dictionaries of adjacent nodes and their corresponding edge weights. The function should return a tuple containing the ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List, Tuple import heapq def find_shortest_path(graph: Dict[str, Dict[str, int]], start_node: str, end_node: str) -> Tuple[List[str], int]: """ Find the shortest path between two nodes in a weighted gra...
code_exercise_27k
17,467
Write a Python function to calculate the total weight of a given path in a directed weighted graph. The graph is represented as a dictionary where the keys are nodes and the values are dictionaries representing the outgoing edges from each node. Each inner dictionary contains the neighboring nodes as keys and the weigh...
Here is the Python code to solve the given problem in the markdown block: ```python def calculate_total_weight(graph: dict, path: list) -> float: """ Calculate the total weight of a given path in a directed weighted graph. Args: graph (dict): A directed weighted graph represented as a dictionary, ...
code_exercise_27k
17,477
Write a Python function to find the node with the highest degree in a given graph represented using an adjacency list. The function should return the node with the highest degree. If there are multiple nodes with the same highest degree, return any one of them. The code should start with: ```python from typing import ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List, Union def find_node_with_highest_degree(graph: Dict[int, List[int]]) -> Union[int, None]: """ Find the node with the highest degree in a given graph represented using an adjacency list. Args:...
code_exercise_27k
17,478
Write a Python function to find the number of connected components in an undirected graph using Depth-First Search (DFS). The graph is represented using an adjacency list. The function should start with: ```python from typing import Dict, Set def count_connected_components(graph: Dict[int, Set[int]]) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, Set def count_connected_components(graph: Dict[int, Set[int]]) -> int: """ Count the number of connected components in an undirected graph. Args: graph (Dict[int, Set[int]]): A dictionary r...
code_exercise_27k
17,481
Write a Python function to find the element that occurs the most number of times in a given list. The function should return a tuple containing the most common element and its count. If the list is empty, the function should raise a `ValueError`. The code should start with: ```python from typing import List, Tuple fro...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple from collections import Counter def find_most_common_element(queue: List[int]) -> Tuple[int, int]: """ Find the element that occurs the most number of times in a given list. Args: que...
code_exercise_27k
17,482
Write a Python function to determine if a given undirected graph is a tree. A tree is defined as an undirected graph in which any two vertices are connected by exactly one path and there are no cycles. The graph is represented as an adjacency list where keys are vertices and values are lists of adjacent vertices. The ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List def is_tree(graph: Dict[int, List[int]]) -> bool: """Determine if a given undirected graph is a tree. A tree is an undirected graph in which any two vertices are connected by exactly one path and ...
code_exercise_27k
17,491
Write a Python function to detect cycles in a directed graph using Depth-First Search (DFS). The graph is represented as a dictionary where keys are nodes and values are lists of adjacent nodes. The function should return `True` if there is at least one cycle in the graph, and `False` otherwise. The code should start ...
Here is the Python code to solve the given problem in the markdown block: ```python from collections import defaultdict def has_cycle(graph: dict) -> bool: """ Detects cycles in a directed graph using Depth-First Search (DFS). Args: graph (dict): A dictionary representing a directed graph. The key...
code_exercise_27k
17,494
Write a Python function to find all possible paths between two nodes in a directed graph. The graph is represented as a dictionary where keys are nodes and values are lists of adjacent nodes. The function should return a list of all possible paths from the start node to the end node. Each path should be represented as ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List, Any def find_all_paths(graph: Dict[Any, List[Any]], start_node: Any, end_node: Any) -> List[List[Any]]: """ Find all possible paths between two nodes in a directed graph. Args: graph ...
code_exercise_27k
17,560
Implement a function to find the shortest paths from a given starting vertex to all other vertices in a graph with negative edge weights using the Bellman-Ford algorithm. The function should return the shortest distances and the previous vertices in the shortest paths. The code should start with: ```python from typing...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List, Tuple def bellman_ford_shortest_paths(graph: Dict[str, List[Tuple[str, int]]], start: str) -> Tuple[Dict[str, int], Dict[str, str]]: """ Finds the shortest paths from a given starting vertex to al...
code_exercise_27k
17,576
Write a Python function to detect if a directed graph contains a cycle. The graph is represented as a dictionary where keys are nodes and values are lists of adjacent nodes. The function should return `True` if a cycle is found, and `False` otherwise. The code should start with: ```python from collections import defau...
Here is the Python code to solve the given problem in the markdown block: ```python from collections import defaultdict def is_cyclic_util(graph: dict, visited: dict, rec_stack: set, node: str) -> bool: """ A utility function used by is_cyclic() to detect cycles in a graph. Parameters: - graph: Th...
code_exercise_27k
17,577
Write a Python function to extract the minimum element from a min-heap represented as a list. The function should remove the minimum element from the heap and maintain the heap property. The code should start with: ```python from typing import List def extract_min_from_heap(heap: List[int]) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def extract_min_from_heap(heap: List[int]) -> int: """ Extracts the minimum element from a min-heap represented as a list and maintains the heap property. Args: heap (List[int]): The min-hea...
code_exercise_27k
17,621
Write a Python function to find the kth smallest element in a binary heap. The binary heap is represented as a list. The function should handle the following: 1. Validate the input to ensure the heap is not empty and k is within the valid range. 2. Use the `heapq` module to maintain the heap property. 3. Efficiently fi...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List import heapq def find_kth_smallest_element(heap: List[int], k: int) -> int: """ Finds the kth smallest element in a binary heap. Args: heap (List[int]): The binary heap represented as a list...
code_exercise_27k
17,626
Write a Python function to find the k largest elements in a binary heap efficiently. The binary heap is represented as a list. The function should return a list containing the k largest elements in descending order. The code should start with: ```python from typing import List def find_k_largest_elements(heap: List[in...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_k_largest_elements(heap: List[int], k: int) -> List[int]: """ Finds the k largest elements in a binary heap. Args: heap (List[int]): The binary heap represented as a list. k...
code_exercise_27k
17,638
Write a Python function to find the kth largest element in a binary heap efficiently. The binary heap is represented as a list where the parent of the element at index `i` is at index `(i-1)//2`, the left child is at index `2*i + 1`, and the right child is at index `2*i + 2`. The function should handle the case where `...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_kth_largest_in_heap(heap: List[int], k: int) -> int: """ Find the kth largest element in a binary heap. Args: heap (List[int]): A list representing a binary heap. k (int): T...
code_exercise_27k
17,695
Write a Python function to find the maximum size star in a given graph. A star in a graph is defined as a node with the maximum number of direct connections (edges) to other nodes. The graph is represented as a dictionary where keys are nodes and values are lists of nodes that the key node is directly connected to. Th...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List def find_max_star(graph: Dict[str, List[str]]) -> int: """ Finds the maximum size star in a graph. Args: graph (dict): The input graph represented as a dictionary where keys are nodes ...
code_exercise_27k
17,735
Write a Python function to find the minimum spanning arborescence in a weighted directed graph. The function should return a dictionary representing the minimum spanning arborescence, where each key is a vertex and the corresponding value is the parent vertex in the arborescence. The root vertex will have `None` as its...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, Any from heapq import heappop, heappush def minimum_spanning_arborescence(graph: Dict[Any, Dict[Any, int]], root: Any) -> Dict[Any, Any]: """ Find the minimum spanning arborescence in a weighted directe...
code_exercise_27k
17,764
Write a Python function to find the depth of a graph using Breadth-First Search (BFS). The graph is represented as an adjacency list in a dictionary, where the keys are the nodes and the values are lists of adjacent nodes. The function should start with: ```python from collections import deque def find_depth_bfs(graph...
Here is the Python code to solve the given problem in the markdown block: ```python from collections import deque def find_depth_bfs(graph: dict, start_node: any) -> int: """ Find the depth of a graph using Breadth-First Search (BFS). Args: graph (dict): A dictionary representing the graph, where ...
code_exercise_27k
17,773
Write a Python function to perform a depth-first search (DFS) on a graph. The graph is represented as a dictionary where keys are nodes and values are lists of neighboring nodes. The function should return a list of nodes visited during the search, in the order they were visited. The code should start with: ```python ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List, Any def depth_first_search(graph: Dict[Any, List[Any]], start: Any) -> List[Any]: """ Perform a depth-first search on a graph. Args: graph (Dict[Any, List[Any]]): A dictionary represe...
code_exercise_27k
17,792
Write a Python function to determine if a given graph is bipartite. A graph is bipartite if its vertices can be divided into two disjoint sets such that no two graph vertices within the same set are adjacent. The graph is represented as an adjacency dictionary where each key is a vertex and the value is a list of verti...
Here is the Python code to solve the given problem in the markdown block: ```python from collections import deque def is_bipartite_graph(graph: dict) -> bool: """ Determine if a given graph is bipartite. Args: graph (dict): An adjacency dictionary representing the graph. Returns: bool...
code_exercise_27k
17,803
Write a Python function to find the number of connected components in an undirected graph. The graph is represented as a dictionary where keys are nodes and values are lists of adjacent nodes. The function should return the number of connected components in the graph. The code should start with: ```python from typing ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List def count_connected_components(graph: Dict[int, List[int]]) -> int: """ Find the number of connected components in an undirected graph. Args: graph (Dict[int, List[int]]): A dictionary...
code_exercise_27k
17,812
Write a Python function to determine if there is a path between two given vertices in a directed graph using Breadth-First Search (BFS). The graph is represented as a dictionary where keys are vertices and values are lists of adjacent vertices. The function should return a boolean value indicating whether a path exists...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List, Union from collections import deque def has_path_bfs(graph: Dict[str, List[str]], start: str, end: str) -> bool: """ Check if there is a path between two vertices in a directed graph using Breadth...
code_exercise_27k
17,836
Write a Python function to calculate the Euclidean distance between two points on a 2D grid. The function should handle input validation to ensure that both points are tuples of two integers. If the input is invalid, the function should raise a `ValueError`. The code should start with: ```python import math def eucli...
Here is the Python code to solve the given problem in the markdown block: ```python import math def euclidean_distance(point1: tuple, point2: tuple) -> float: """ Calculates the Euclidean distance between two points on a 2D grid. Args: point1 (tuple): The coordinates of the first point as a tuple ...
code_exercise_27k
17,837
Write a Python function to find the diameter of an undirected graph. The diameter of a graph is defined as the length of the longest shortest path between any pair of nodes. The function should use Breadth First Search (BFS) to find the shortest path between every pair of nodes. The code should start with: ```python f...
Here is the Python code to solve the given problem in the markdown block: ```python from collections import deque from typing import Dict, List def find_diameter(graph: Dict[str, List[str]]) -> int: """ Find the diameter of an undirected graph. The diameter of a graph is defined as the length of the l...
code_exercise_27k
17,845
Write a Python function to determine if a given directed graph is a Directed Acyclic Graph (DAG). The graph is represented by a list of vertices and a list of directed edges. The function should return `True` if the graph is a DAG, and `False` otherwise. The code should start with: ```python from typing import List, T...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def is_directed_acyclic_graph(vertices: List, edges: List[Tuple]) -> bool: """ Determines if the given directed graph is a Directed Acyclic Graph (DAG). Args: vertices (List): List of...
code_exercise_27k
17,846
Write a Python function to find the neighboring nodes of a given node on a grid. The grid is represented as a two-dimensional list, and the node is represented by its coordinates (row, column). The function should return a list of tuples representing the coordinates of the neighboring nodes. The neighboring nodes are d...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def find_neighbors(grid: List[List[int]], node: Tuple[int, int]) -> List[Tuple[int, int]]: """ Find the neighboring nodes of a given node on a grid. Args: grid (List[List[int]]): A tw...
code_exercise_27k
17,850
Write a Python function to calculate the Manhattan distance between two points on a 2D grid. The function should take two tuples representing the coordinates of the points and return the Manhattan distance as an integer. The code should start with: ```python def manhattan_distance(p1: tuple, p2: tuple) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python def manhattan_distance(p1: tuple, p2: tuple) -> int: """ Calculate the Manhattan distance between two points on a 2D grid. Args: p1 (tuple): The coordinates of the first point in the format (x1, y1). p2 (tup...
code_exercise_27k
17,875
Write a Python function to find the shortest distances from a source vertex to all other vertices in a given graph using Dijkstra's algorithm. The graph is represented as an adjacency list where each node is a key and the value is a list of tuples (neighbour, distance). The function should return a dictionary where the...
Here is the Python code to solve the given problem in the markdown block: ```python from collections import defaultdict import heapq def dijkstra_shortest_path(graph: defaultdict, source: int) -> dict: """ Find the shortest distances from a source vertex to all other vertices in a graph using Dijkstra's algori...
code_exercise_27k
17,880
Write a Python function to extract the path from the result of the A* algorithm. The result is a list of nodes representing the path from the start node to the goal node. Each node is an object with a `name` attribute. The function should return a list of node names representing the path. If the input result is empty o...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Optional def extract_path(result: Optional[List[object]]) -> List[str]: """ Extracts the path from the result of the A* algorithm. Args: result (Optional[List[object]]): List of nodes repre...
code_exercise_27k
17,882
Write a Python function to detect if a given undirected graph contains a cycle or not. The graph is represented as a dictionary where keys are nodes and values are lists of adjacent nodes. The function should return `True` if the graph contains a cycle, and `False` otherwise. The code should start with: ```python from...
Here is the Python code to solve the given problem in the markdown block: ```python from collections import defaultdict def has_cycle(graph: dict) -> bool: """ Detects cycles in an undirected graph using depth-first search. Parameters: graph (dict): An undirected graph in the form of a dictionary, whe...
code_exercise_27k
17,894
Write a Python function to determine if a given graph is bipartite. A graph is bipartite if its vertices can be divided into two disjoint and independent sets such that every edge connects a vertex in one set to a vertex in the other set. The graph is represented as an adjacency list. The code should start with: ```py...
Here is the Python code to solve the given problem in the markdown block: ```python from collections import deque def is_bipartite_graph(graph: dict) -> bool: """ Determine if a given graph is bipartite. A graph is bipartite if its vertices can be divided into two disjoint and independent sets such th...
code_exercise_27k
17,900
Write a Python function to determine if a given undirected graph is a tree. An undirected graph is considered a tree if it is connected and acyclic (i.e., it has no cycles). The graph is represented as an adjacency list. The code should start with: ```python from typing import Dict, Set def is_tree(graph: Dict[int, S...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, Set def is_tree(graph: Dict[int, Set[int]]) -> bool: """ Determines if the given undirected graph is a tree. A graph is considered a tree if it is connected and acyclic. Args: graph (D...
code_exercise_27k
17,904
Write a Python function to count the number of connected components in an undirected graph. The graph is represented as an adjacency list, where the keys are the nodes and the values are lists of neighboring nodes. The function should return the number of connected components in the graph. The code should start with: ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List def count_connected_components(graph: Dict[str, List[str]]) -> int: """ Count the number of connected components in an undirected graph. Parameters: graph (dict): An undirected graph repre...
code_exercise_27k
17,923
Write a Python function to dynamically update a grid for the A* pathfinding algorithm. The function should take the current grid, start and goal positions, and a list of obstacles, and return an updated grid. The updated grid should reflect the new obstacle positions and ensure that the start and goal positions are not...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def update_grid_for_astar(grid: List[List[int]], start: Tuple[int, int], goal: Tuple[int, int], obstacles: List[Tuple[int, int]]) -> List[List[int]]: """ Dynamically update a grid for the A* pathf...
code_exercise_27k
17,939
Write a Python function to determine the minimum number of colors required to color a given graph such that no two adjacent vertices have the same color. The graph is represented as an adjacency list, where the keys are the vertices and the values are lists of adjacent vertices. The function should start with: ```pyth...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List def min_colors_for_graph(graph: Dict[str, List[str]]) -> int: """ Finds the minimum number of colors required to color a graph such that no two adjacent vertices have the same color. Args: ...