-
Notifications
You must be signed in to change notification settings - Fork 0
/
234.py
73 lines (54 loc) · 1.74 KB
/
234.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
63
64
65
66
67
68
69
70
71
72
73
"""
Problem:
Recall that the minimum spanning tree is the subset of edges of a tree that connect all
its vertices with the smallest possible total edge weight. Given an undirected graph
with weighted edges, compute the maximum weight spanning tree.
"""
from typing import Set
from DataStructures.Graph import GraphUndirectedWeighted
def get_maximum_spanning_tree_helper(
graph: GraphUndirectedWeighted,
curr_node: int,
remaining_nodes: Set[int],
weight: int,
) -> int:
if not remaining_nodes:
return weight
scores = []
for destination in graph.connections[curr_node]:
if destination in remaining_nodes:
rem_cp = set(remaining_nodes)
rem_cp.remove(destination)
new_score = get_maximum_spanning_tree_helper(
graph,
destination,
rem_cp,
weight + graph.connections[curr_node][destination],
)
scores.append(new_score)
return max(scores)
def get_maximum_spanning_tree(graph: GraphUndirectedWeighted) -> int:
node_set = set(graph.connections.keys())
start_node = node_set.pop()
weight = get_maximum_spanning_tree_helper(graph, start_node, node_set, 0)
return weight
if __name__ == "__main__":
graph = GraphUndirectedWeighted()
graph.add_edge(1, 2, 5)
graph.add_edge(1, 3, 2)
graph.add_edge(3, 2, 1)
graph.add_edge(3, 4, 3)
graph.add_edge(2, 4, 4)
print(graph)
print(get_maximum_spanning_tree(graph))
graph = GraphUndirectedWeighted()
graph.add_edge(1, 2, 1)
graph.add_edge(1, 3, 2)
graph.add_edge(3, 2, 3)
print(graph)
print(get_maximum_spanning_tree(graph))
"""
SPECS:
TIME COMPLEXITY: O(n x e)
SPACE COMPLEXITY: O(n x e)
"""