0

I have a homework question for an algorithm class regarding transforming an s-clique into a s-independent set. Below is code and the function at the very bottom independent_set_decision(H,s) is what I need to finish. I am stumped.

def k_subsets(lst, k):
    if len(lst) < k:
        return []
    if len(lst) == k:
        return [lst]
    if k == 1:
        return [[i] for i in lst]
    return k_subsets(lst[1:],k) + map(lambda x: x + [lst[0]], k_subsets(lst[1:], k-1))

# Checks if the given list of nodes forms a clique in the given graph.
def is_clique(G, nodes):
    for pair in k_subsets(nodes, 2):
        if pair[1] not in G[pair[0]]:
            return False
    return True

# Determines if there is clique of size k or greater in the given graph.
def k_clique_decision(G, k):
    nodes = G.keys()
    for i in range(k, len(nodes) + 1):
        for subset in k_subsets(nodes, i):
            if is_clique(G, subset):
                return True
    return False

def make_link(G, node1, node2):
    if node1 not in G:
        G[node1] = {}
    (G[node1])[node2] = 1
    if node2 not in G:
        G[node2] = {}
    (G[node2])[node1] = 1
    return G

def break_link(G, node1, node2):
    if node1 not in G:
        print "error: breaking link in a non-existent node"
        return
    if node2 not in G:
        print "error: breaking link in a non-existent node"
        return
    if node2 not in G[node1]:
        print "error: breaking non-existent link"
        return
    if node1 not in G[node2]:
        print "error: breaking non-existent link"
        return
    del G[node1][node2]
    del G[node2][node1]
    return G

# This function should use the k_clique_decision function
# to solve the independent set decision problem
def independent_set_decision(H, s):
   #insert code here
    return True
Andy
  • 275
  • 2
  • 14
  • I will add that if I just return k_clique_decision(H,s), these are the test cases that pass and fail: '**`SUCCESS`**`: Test case input: {1:{}}, 1` **`SUCCESS`**`: Test case input: {1:{2:1}, 2:{1:1}}, 1` **`FAILURE`**`: Test case input: {1:{2:1}, 2:{1:1}}, 2.` Expected result: False **`FAILURE`**`: Test case input: {1:{2:1, 3:1}, 2:{1:1}, 3:{1:1}, 4:{}}, 3.` Expected result: True **`SUCCESS`**`: Test case input: {1:{2:1, 3:1}, 2:{1:1}, 3:{1:1}, 4:{}}, 4` ' – Andy Mar 13 '14 at 21:17

1 Answers1

0

Let's take a look at the definitions of "independent set" and "clique":

  • Independent set: A set of nodes where no two are adjacent

  • Clique: A set of nodes where every pair is adjacent

By these definitions, a set of nodes is independent if in the complement of the graph, the set is a clique. What can you do with the complement of the graph and the k_clique_decision function to solve your problem, then?

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • So I need to create a new graph, the complement, and then run 'k_clique_decision' on it? And I do this by making a link between any node that doesn't have a link with another node? Am i getting warmer? :) – Andy Mar 13 '14 at 21:38
  • i updated code here http://stackoverflow.com/questions/22394164/creating-clique-graph-to-determine-independent-set however i cannot seem to get the complement graph created right. What is wrong with the code? – Andy Mar 14 '14 at 01:20