-
Notifications
You must be signed in to change notification settings - Fork 0
/
182.py
62 lines (47 loc) · 1.67 KB
/
182.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""
Problem:
A graph is minimally-connected if it is connected and there is no edge that can be
removed while still leaving the graph connected. For example, any binary tree is
minimally-connected.
Given an undirected graph, check if the graph is minimally-connected. You can choose to
represent the graph as either an adjacency matrix or adjacency list.
"""
from copy import deepcopy
from DataStructures.Graph import GraphUndirectedUnweighted
from DataStructures.Queue import Queue
def is_minimally_connected(graph: GraphUndirectedUnweighted) -> bool:
graph_copy = GraphUndirectedUnweighted()
graph_copy.connections, graph_copy.nodes = deepcopy(graph.connections), graph.nodes
# getting a random node for starting the traversal
for node in graph.connections:
start = node
break
# running bfs and checking if a node is visited more than once
# (redundant edges present => not a minimally connected graph)
visited = set([start])
queue = Queue()
queue.enqueue(start)
while not queue.is_empty():
node = queue.dequeue()
for neighbour in graph_copy.connections[node]:
graph_copy.connections[neighbour].remove(node)
queue.enqueue(neighbour)
if neighbour in visited:
return False
visited.add(neighbour)
return True
if __name__ == "__main__":
graph = GraphUndirectedUnweighted()
graph.add_edge(1, 2)
graph.add_edge(1, 3)
graph.add_edge(3, 4)
print(graph)
print(is_minimally_connected(graph))
graph.add_edge(1, 4)
print(graph)
print(is_minimally_connected(graph))
"""
SPECS:
TIME COMPLEXITY: O(n x e)
SPACE COMPLEXITY: O(n + e)
"""