Exemple #1
0
def upper_bound_from_cardinality_2(graph):
    nodes = graph.nodes()
    get_induced_subgraph = graph.induced_subgraph
    ub_min = len(nodes) + 1
    ub_max = 0
    adj_list = graph.data
    for node in nodes:
        #### determiniamo il sottografo ####
        subgraph = Graph()
        subgraph.data = {}
        sub_nodes = set(adj_list[node])
        sub_nodes.add(node)
        for adj_node in sub_nodes:
            subgraph.data[adj_node] = [x for x in adj_list[adj_node] if x in sub_nodes]
        ####################################
        #subgraph = get_induced_subgraph(node)
        #sub_nodes = subgraph.nodes()
        size_neighborhood_list = [(len(subgraph[sub_node]) + 1)
                                  for sub_node in sub_nodes]
        size_neighborhood_list.sort()
        size_neighborhood_list.reverse()
        length = len(size_neighborhood_list) + 1
        for i in xrange(length):
            if size_neighborhood_list[i-1] <= i:
                ub = i
                break
        if ub < ub_min:
            ub_min = ub
            node_min = node
        elif ub > ub_max:
            ub_max = ub
        #if ub < ub_min:
            #ub_min = ub
            #node_min = node
    return node_min, ub_min, ub_max
Exemple #2
0
def upper_bound_from_linear_coloring_1(graph):
    nodes = graph.nodes()
    #get_induced_subgraph = graph.induced_subgraph
    #upper_bounds = []
    #upper_bounds_append = upper_bounds.append
    #unique = usefulFunctions.unique
    ub_min = len(nodes) + 1
    ub_max = 0
    adj_list = graph.data
    for node in nodes:
        #### determiniamo il sottografo ####
        subgraph = Graph()
        subgraph.data = {}
        sub_nodes = set(adj_list[node])
        sub_nodes.add(node)
        for adj_node in sub_nodes:
            subgraph.data[adj_node] = [x for x in adj_list[adj_node] if x in sub_nodes]
        ####################################
        #subgraph = get_induced_subgraph(node)
        data = {}
        colors = set()
        #adj_colors = []
        #sub_nodes = subgraph.nodes()
        colors_add = colors.add
        for sub_node in sub_nodes:
            adj_colors = set([data[adj_node] for adj_node in subgraph[sub_node]
                                if adj_node in data])
            length = len(subgraph[sub_node])
            for i in xrange(length):
                if not i in adj_colors:
                    data[sub_node] = i
                    break
            colors_add(data[sub_node])
            #adj_colors = unique([data[adj_node] for adj_node in graph[sub_node]
                                #if adj_node in data])
            #length = len(adj_colors)
            #if length == 0:
                #data[sub_node] = 0
            #else:
                #if length != adj_colors[length - 1] + 1:
                    #adj_colors.sort()
                    #for i in xrange(length):
                        #if i != adj_colors[i]:
                            #data[sub_node] = i
                            #break
                #else:
                    #data[sub_node] = length
            #colors_add(data[sub_node])
        ub = len(colors)
        if ub < ub_min:
            ub_min = ub
            node_min = node
            sub_min = subgraph
        #upper_bounds_append((node, len(colors), subgraph))
    return node_min, ub_min, sub_min
Exemple #3
0
def upper_bound_from_cardinality(nfile, graph):
    l = str(len(graph))
    e = str(graph.number_of_edges())
    start = time.time()
    getter = operator.itemgetter
    nodes = graph.nodes()
    get_induced_subgraph = graph.induced_subgraph
    upper_bounds = []
    upper_bounds_append = upper_bounds.append
    adj_list = graph.data
    subgraph = Graph()
    for node in nodes:
        #### determiniamo il sottografo ####
        subgraph.data = {}
        sub_nodes = set(adj_list[node])
        sub_nodes.add(node)
        for adj_node in sub_nodes:
            subgraph.data[adj_node] = [x for x in adj_list[adj_node] if x in sub_nodes]
        ####################################
        #subgraph = get_induced_subgraph(node)
        #sub_nodes = subgraph.nodes()
        size_neighborhood_list = [(len(subgraph[sub_node]) + 1)
                                  for sub_node in sub_nodes]
        size_neighborhood_list.sort()
        size_neighborhood_list.reverse()
        length = len(size_neighborhood_list) + 1
        for i in xrange(length):
            if size_neighborhood_list[i-1] <= i:
                ub = i
                break
        upper_bounds_append((node, ub))
    upper_bounds.sort(key=getter(1))
    min_item = upper_bounds[0]
    max_item = upper_bounds[len(upper_bounds) - 1]
    ub_min = min_item[1]
    ub_max = max_item[1]
    t_tot = round(time.time() - start, 2)
    text = ''.join([str(nfile), "; ", l, "; ", e, "; ", str(ub_min), "; ", str(ub_max), "; ", str(t_tot)])
    logging.info(text)
    print text
Exemple #4
0
def upper_bound_from_cardinality_new(nfile, graph):
    l = str(len(graph))
    e = str(graph.number_of_edges())
    start = time.time()
    nodes = graph.nodes()
    get_induced_subgraph = graph.induced_subgraph
    ub_min = len(nodes) + 1
    ub_max = 0
    adj_list = graph.data
    subgraph = Graph()
    for node in nodes:
        #### determiniamo il sottografo ####
        subgraph.data = {}
        sub_nodes = set(adj_list[node])
        sub_nodes.add(node)
        for adj_node in sub_nodes:
            subgraph.data[adj_node] = [x for x in adj_list[adj_node] if x in sub_nodes]
        ####################################
        #subgraph = get_induced_subgraph(node)
        #sub_nodes = subgraph.nodes()
        size_neighborhood_list = [(len(subgraph[sub_node]) + 1)
                                  for sub_node in sub_nodes]
        size_neighborhood_list.sort()
        size_neighborhood_list.reverse()
        length = len(size_neighborhood_list) + 1
        print length
        print size_neighborhood_list
        for i in xrange(length):
            if size_neighborhood_list[i-1] <= i:
                ub = i
                break
        if ub < ub_min:
            ub_min = ub
            node_min = node
        elif ub > ub_max:
            ub_max = ub
    t_tot = round(time.time() - start, 2)
    text = ''.join([str(nfile), "; ", l, "; ", e, "; ", str(ub_min), "; ", str(ub_max), "; ", str(t_tot)])
    logging.info(text)
    print text
Exemple #5
0
def upper_bound_from_linear_coloring_2(graph):
    nodes = graph.nodes()
    #get_induced_subgraph = graph.induced_subgraph
    #upper_bounds = []
    #upper_bounds_append = upper_bounds.append
    #unique = usefulFunctions.unique
    ub_min = len(nodes) + 1
    ub_max = 0
    adj_list = graph.data
    for node in nodes:
        #### determiniamo il sottografo ####
        subgraph = Graph()
        subgraph.data = {}
        sub_nodes = set(adj_list[node])
        sub_nodes.add(node)
        for adj_node in sub_nodes:
            subgraph.data[adj_node] = [x for x in adj_list[adj_node] if x in sub_nodes]
        ####################################
        #subgraph = get_induced_subgraph(node)
        #sub_nodes = subgraph.nodes()
        data = {}
        colors = set()
        #adj_colors = []
        #adj_colors = set()
        colors_add = colors.add
        for sub_node in sub_nodes:
            adj_colors = set([data[adj_node] for adj_node in subgraph[sub_node]
                                if adj_node in data])
            length = len(subgraph[sub_node])
            for i in xrange(length):
                if not i in adj_colors:
                    data[sub_node] = i
                    break
            colors_add(data[sub_node])
            #[adj_colors.add(data[adj_node]) for adj_node in subgraph[sub_node] if adj_node in data]
            #adj_colors = set(adj_colors)
            ## eliminiamo da adj_colors i colori che si ripetono
            ## metodo + veloce di list(set())
            #adj_colors = set(adj_colors)
            #for x in adj_colors:
                #keys.add(x)
            #adj_colors = keys.keys()
            ####################################################
            #length = len(adj_colors)

            #if length == 0:
                #data[sub_node] = 0
            #else:
                #adj_colors.sort()
                #if length != adj_colors[length - 1] + 1:
                    ##adj_colors.sort()
                    #for i in xrange(length):
                        #if i != adj_colors[i]:
                            #data[sub_node] = i
                            #break
                #else:
                    #data[sub_node] = length
        #colors = set(data.values())
        #upper_bounds_append((node, len(colors)))
        ub = len(colors)
        if ub < ub_min:
            ub_min = ub
            node_min = node
        elif ub > ub_max:
            ub_max = ub
    return node_min, ub_min, ub_max
Exemple #6
0
def upper_bound_from_dsatur_2(graph):
    #inizializzazione
    unique = usefulFunctions.unique
    nodes = graph.nodes()
    #get_induced_subgraph = graph.induced_subgraph
    upper_bounds = []
    upper_bounds_append = upper_bounds.append
    ub_min = len(nodes) + 1
    ub_max = 0
    adj_list = graph.data
    for node in nodes:
        #### determiniamo il sottografo ####
        subgraph = Graph()
        subgraph.data = {}
        sub_nodes = set(adj_list[node])
        sub_nodes.add(node)
        for adj_node in sub_nodes:
            subgraph.data[adj_node] = [x for x in adj_list[adj_node] if x in sub_nodes]
        ####################################
        #subgraph = get_induced_subgraph(node)
        dst = dsatur.DSaturClass(subgraph)
        dst_color = dst.color
        dst_get_node_max_degree = dst.get_node_max_degree
        dst_update = dst.update
        dst_ungraph = dst.uncolored_graph
        dst_get_node_max_satur_degree = dst.get_node_max_satur_degree
        colors_list = []
        append_color = colors_list.append
        #estriamo il nodo con grado maggiore
        sub_node_sel = dst_get_node_max_degree()
        #coloriamo il nodo e aggiorniamo
        ncolor = dst_color(sub_node_sel)
        #color_list: lista in cui verrano memorizzati i colori con cui verranno
        #colorati i nodi
        append_color(ncolor)
        iterations = len(dst_ungraph) - 1
        #si itera su tutti i nodi rimasti da colorare
        for i in xrange(iterations):
            #si trova il nodo di grado di saturazione minore
            sub_node_sel, check_eq = dst_get_node_max_satur_degree()
            if check_eq is False:
                #se vi è un solo nodo con grado di saturazione massimo
                ncolor = dst_color(sub_node_sel)
                append_color(ncolor)
                dst_update(sub_node_sel)
            else:
                #se vi è un conflitto si estrae un qualunque nodo di grado maggiore
                #nel sottografo di nodi non ancora colorati
                sub_node_sel = dst_get_node_max_degree()
                ncolor = dst_color(sub_node_sel)
                append_color(ncolor)
                dst_update(sub_node_sel)
        sub_node_sel = dst_ungraph.nodes()[0]
        ncolor = dst_color(sub_node_sel)
        append_color(ncolor)
        ub = len(unique(colors_list))
        if ub < ub_min:
            ub_min = ub
            node_min = node
        elif ub > ub_max:
            ub_max = ub
        #elif ub < ub_min:
            #ub_min = ub
            #node_min = node
        #upper_bounds_append((node, ub))
    return node_min, ub_min, ub_max