forked from ngiengkianyew/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_234.py
78 lines (56 loc) · 1.66 KB
/
problem_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
74
75
76
77
78
class Node:
def __init__(self, val):
self.val = val
def __hash__(self):
return hash(self.val)
def __repr__(self):
return str(self.val)
class Edge:
def __init__(self, target, weight):
self.target = target
self.weight = weight
def __hash__(self):
return hash(self.target)
def __repr__(self):
return "-{}-> {}".format(self.weight, self.target)
class Graph:
def __init__(self):
self.nodes = set()
self.edges = dict()
def add_node(self, node):
self.nodes.add(node)
self.edges[node] = set()
def add_edge(self, source, target, weight):
if source not in self.nodes:
self.add_node(source)
if target not in self.nodes:
self.add_node(target)
self.edges[source].add(Edge(target, weight))
self.edges[target].add(Edge(source, weight))
def get_max_span_helper(g, start, remaining, score):
if not remaining:
return score
scores = list()
for edge in g.edges[start]:
if edge.target in remaining:
rem_cp = remaining.copy()
rem_cp.remove(edge.target)
new_score = get_max_span_helper(
g, edge.target, rem_cp, score + edge.weight)
scores.append(new_score)
return max(scores)
def get_max_span(g):
remaining = g.nodes.copy()
start_node = list(remaining)[0]
remaining.remove(start_node)
score = get_max_span_helper(g, start_node, remaining, 0)
return score
# Tests
g = Graph()
a = Node('a')
b = Node('b')
c = Node('c')
g.add_edge(a, b, 1)
g.add_edge(a, c, 2)
g.add_edge(b, c, 3)
assert get_max_span(g) == 5