Skip to content

Commit 3989a25

Browse files
author
Hamid Gasmi
committed
#153 is started
1 parent bf16017 commit 3989a25

File tree

1 file changed

+27
-47
lines changed
  • 4-np-complete-problems/2-coping_with_np_completeness

1 file changed

+27
-47
lines changed

4-np-complete-problems/2-coping_with_np_completeness/plan_party.py

+27-47
Original file line numberDiff line numberDiff line change
@@ -4,65 +4,45 @@
44

55
# This problem can be reduced to Independent Set problem (ISP) which is an NP problem
66
# It's a special case of ISP
7-
# It's can be solved effeciently with Dynamic Programming technique:
8-
# Let's W(v) reprensents the Total Fun (weight) in the party, then:
9-
# F(v) = Max(weight(v) + sum(weight(u) for all u grand-children of v),
10-
# sum(weight(u) for all u children of v))
11-
12-
class Vertex:
13-
def __init__(self, weight):
14-
self.weight = weight
15-
self.children = []
7+
# It's can be solved effeciently with Greedy Algorithm technique:
8+
# Greedy Choice: Select all leaves of the tree and Remove all their parents + Iterate
9+
# This choice is safe:
10+
# since the number of leaves >= the leaves' parents
11+
# This choice guarantees to get an IS longer or equal to the IS that we would have got if we selected the parents
1612

1713
class Tree:
18-
def __init__(self, size, weights):
14+
def __init__(self, size, edges):
1915
self.size = size
20-
self.weights = [weights[i] for i in range(size)]
21-
22-
23-
def ReadTree():
24-
25-
26-
27-
def dfs(tree, vertex, parent):
28-
for child in tree[vertex].children:
29-
if child != parent:
30-
dfs(tree, child, vertex)
31-
32-
# This is a template function for processing a tree using depth-first search.
33-
# Write your code here.
34-
# You may need to add more parameters to this function for child processing.
16+
self.build_children_list(edges)
3517

18+
def build_children_list(self, edges):
19+
self.children_list = [[] for _ in range(self.size)]
20+
for p, c in edges:
21+
self.children_list[p - 1].append(c - 1)
22+
self.children_list[c - 1].append(p - 1)
3623

37-
def MaxWeightIndependentTreeSubset(tree):
38-
size = len(tree)
39-
if size == 0:
40-
return 0
41-
dfs(tree, 0, -1)
42-
# You must decide what to return.
43-
return 0
44-
24+
def isp_dfs(self, v, parent):
4525

26+
list = []
27+
return list
28+
4629
def main():
47-
tree = ReadTree()
48-
weight = MaxWeightIndependentTreeSubset(tree)
49-
print(weight)
30+
31+
size = int(input())
32+
edges = []
33+
for _ in range(1, size):
34+
edges.append(list(map(int, input().split())))
35+
tree = Tree(size, edges)
36+
37+
isp_list = tree.isp_dfs(0, -1)
38+
print(len(isp_list))
39+
print(isp_list)
5040

5141
if __name__ == "__main__":
5242

5343
# To avoid stack overflow issues
5444
sys.setrecursionlimit(10**6) # max depth of recursion
5545
threading.stack_size(2**26) # new thread will get stack of such size
5646

57-
size = int(input())
58-
weights = map(int, input().split())
59-
print(weights)
60-
#tree = [Vertex(w) for w in map(int, input().split())]
61-
#for i in range(1, size):
62-
# a, b = list(map(int, input().split()))
63-
# tree[a - 1].children.append(b - 1)
64-
# tree[b - 1].children.append(a - 1)
65-
#return tree
66-
6747
# This is to avoid stack overflow issues
68-
#threading.Thread(target=main).start()
48+
threading.Thread(target=main).start()

0 commit comments

Comments
 (0)