Dynamic Programming: Weighted Independent Sets in Path Graphs

Problem Description:

Independent means that not any two vertices in the set is adjacent.

Algorithm using dynamic programming:

a = list()
w = list() # weight for each vertex
a[0] = 0
a[1] = 1
for i in range(2, n + 1):
    a[i] = max(a[i - 1], a[i - 2] + w[i])

for each current vertex i, the WIS has 2 cases, one case is that the max WIS is a[i - 1], another case is that the max WIS is a[i - 2] + w[i].

Time complexity:

O(n)

posted on 2016-04-24 20:04  dingjunnan  阅读(149)  评论(0编辑  收藏  举报

导航