Skip to content

Commit

Permalink
add exhaustive check of independent sets.
Browse files Browse the repository at this point in the history
  • Loading branch information
prestonhansen committed Mar 19, 2017
1 parent 083bdea commit a3ca3ce
Showing 1 changed file with 38 additions and 11 deletions.
49 changes: 38 additions & 11 deletions indset.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import networkx as nx
import matplotlib.pyplot as plt
#import matplotlib.pyplot as plt
import itertools
from random import choice


#todo: see if there is a speed difference
#between constructing a set of the neighbors and the nodes
#then computing the intersection vs just using
#list comprehension on the lists themselves.
def isIndependent(graph, nodeSet):
#print nodeSet
for n in nodeSet:
if len(set(graph.neighbors(n)) & nodeSet) != 0:
return False

return True

def setUtil(graph):
if graph.number_of_nodes() == 0:#G is empty
Expand All @@ -20,11 +32,11 @@ def setUtil(graph):
if len(v_one_degree) != 0:
v = choice(v_one_degree)
w = graph.copy().neighbors(v)[0]
w_neighborhood = graph.copy().neighbors(w) + [w]
w_neighborhood = graph.neighbors(w) + [w]
withW = graph.copy()
withW.remove_nodes_from(w_neighborhood)

v_neighborhood = graph.copy().neighbors(v) + [v]
v_neighborhood = graph.neighbors(v) + [v]
withV = graph.copy()
withV.remove_nodes_from(v_neighborhood)

Expand All @@ -35,7 +47,7 @@ def setUtil(graph):
#G has a vertex degree >=3
if len(v_three_or_more_degree) != 0:
v = choice(v_three_or_more_degree)
neighborhood = graph.copy().neighbors(v) + [v]
neighborhood = graph.neighbors(v) + [v]

withV = graph.copy()
withV.remove_nodes_from(neighborhood)
Expand All @@ -50,9 +62,9 @@ def setUtil(graph):
v = choice(graph.nodes())
#should be exactly two of these
u,w = graph.copy().neighbors(v)
u_neighborhood = graph.copy().neighbors(u) + [u]
v_neighborhood = graph.copy().neighbors(v) + [v]
w_neighborhood = graph.copy().neighbors(w) + [w]
u_neighborhood = graph.neighbors(u) + [u]
v_neighborhood = graph.neighbors(v) + [v]
w_neighborhood = graph.neighbors(w) + [w]

withU = graph.copy()
withU.remove_nodes_from(u_neighborhood)
Expand All @@ -66,10 +78,25 @@ def setUtil(graph):

return max(1+setUtil(withU), 1+setUtil(withV), 1+setUtil(withW))

G = nx.cycle_graph(5)
G = nx.cycle_graph(7)
S = nx.strong_product(G, G)

G2 = nx.cycle_graph(7)
S2 = nx.strong_product(G2, G2)
# a = nx.convert.to_dict_of_lists(G)

# print [r for r in a]

for n in itertools.combinations(S.nodes(), setUtil(S)):
if isIndependent(S,set(n)):
print n



#for s in c:
# print isIndependent(G, s)

# G2 = nx.cycle_graph(7)
# S2 = nx.strong_product(G2, G2)



print setUtil(S)
#print setUtil(S2)

0 comments on commit a3ca3ce

Please sign in to comment.