A graph is connected if there is a route between every pair of nodes. Your task is to determine if a given graph is connected.
For example, the following graph is connected, because there is a route from any node to any other node.
The graph below is not connected, because there is no route from the node to the node .
In a file connectivity.py
, implement the function connected
, whose parameters are the lists of the nodes and the edges of a graph. The function should return True
if the graph is connected and False
otherwise.
def connected(nodes, edges): # TODO if __name__ == "__main__": nodes = [1, 2, 3, 4, 5] edges = [(1, 2), (1, 3), (1, 4), (2, 4), (2, 5), (3, 4), (4, 5)] print(connected(nodes, edges)) # True nodes = [1, 2, 3, 4, 5, 6, 7, 8] edges = [(1, 2), (1, 3), (2, 3), (4, 5), (4, 6), (5, 7), (6, 7)] print(connected(nodes, edges)) # False nodes = [1, 2, 3, 4, 5] edges = [] print(connected(nodes, edges)) # False nodes = [1, 2, 3, 4, 5] edges = [(1, 2), (1, 3), (1, 4), (1, 5)] print(connected(nodes, edges)) # True