コード例 #1
0
def calculateFictifGainUnique(s,P1,P2,graph):
    # déplacement du sommet s de P1 vers P2
    if s in P1:
        newG = objf.nodeGain(s, P1, graph)
    else :
        newG = objf.nodeGain(s, P2, graph)
    return newG
コード例 #2
0
def gloutonWithCut(k, graph, verbose,log):
    if log:
        print "Starting Glouton With Cut.."
        startTime = time.time()
    # initialisation
    initGraph = graph.copy()
    niList = []
    markingList = [] 
    potentialNodesList = []
    partitionList = []
    # initialisation du sommet de départ
    s0 = nodeWithLessNeighbor(graph, markingList)
    markingList.append(s0)
    potentialNodesList = setPotentialNodesList(graph,s0, markingList, potentialNodesList)    
    if verbose: 
        print "Sommet de depart :", s0    
        print "PotentialNodesList : ", potentialNodesList
    
    # initialisation de i : numéro de partition courante
    i = 1
    while i < k:
        # Calcul de ni : nombre de sommets pour la prochaine partition
        niList = calculateSizeListSubGraph(k, i, niList, initGraph)
        ni = niList[i-1]
        # j : nombre de sommets actuellement dans la partition courante
        j=1 	# on compte le sommet de départ
        while potentialNodesList != [] and j < ni:
            minCut = sys.maxint
            #remainingNodes = [x for x in graph.nodes() if x not in markingList]
            for node in potentialNodesList:
                fictifGain = objf.nodeGain(node[0], markingList, graph)
                if  fictifGain < minCut:
                    minCut = fictifGain
                    minNode = node[0]
                    #print "Nouveau Gain Fictif : ", fictifGain, "Noeud:", minNode
                
            #print "prochain noeud ajouté dans la partition :", minNode
            markingList.append(minNode)
            potentialNodesList = setPotentialNodesList(graph,minNode,markingList,potentialNodesList)
            j = j+1
        # Affectation de la partition créée à la liste de partitions
        partitionList.append(markingList)
        # Réinitialisation des structures
        # Restructuration du graphe : suppression des sommets marqués
        graph.remove_nodes_from(markingList)
        markingList = [] 
        potentialNodesList = []
        i = i+1
        # Initialisation de la prochaine partition
        if i != k :
            s0 = nodeWithLessNeighbor(graph, markingList)
            markingList.append(s0)
            potentialNodesList = setPotentialNodesList(graph,s0, markingList, potentialNodesList)
    # partitionList ← sommets restants 
    partitionList.append(graph.nodes())
    graph = initGraph
    # probleme : si on ne return pas le graphe, ici il est bien égal
    # à tout le graphe mais sans le return il vaut une partition...

    stopTime = time.time()
    if verbose:
        print "Partition finale 1:", partitionList[0]
        print "Partition finale 2:", partitionList[1]
    if log:
        print "Optimum trouve:", objf.calculateCut(partitionList[0],partitionList[1],graph)
        print "Execution Time :", stopTime-startTime
    
    return partitionList, graph