Ejemplo n.º 1
0
    def fastBuildGraph(node, edge, r_flag, e_flag):
        """Costruisce un grafo tramite liste di incidenza
          con i valori dei nodi , archi , passati dal Parsers,
          funzione asintoticamente più veloce della precedente
        """
        g = Graph()
        n = []
        for i in range(node):
            n.append(g.insertNode(i))

        for j in range(edge):

            tail = random.randint(0, node - 1)
            head = random.randint(0, node - 1)

            if r_flag:  # Flag a True creo pesi random da perturbare
                weight = round(random.randint(0, 1000) +   \
                        (random.random() * (10 ** -3)), 5)

            elif e_flag:  # Flag a True creo archi con pesi uguali

                weight = random.choice(range(0, 10))

            else:
                weight = random.choice(range(0, 1000, 2))

            if tail == head:  # elimino gli archi A -> A
                continue
            g.insertEdge(n[tail].index, n[head].index, weight)
            g.insertEdge(n[head].index, n[tail].index, weight)

        GraphHelper.cleanGraph(g)  # Utilizzo la funzione cleanGraph

        return g
Ejemplo n.º 2
0
def buildGraph():
    g = Graph()
    ginfo = ((0, 1, 3.0), (0, 3, 1.0), (1, 3, 2.0), (1, 2, 4.0), (2, 3, 5.0))
    n = []
    for i in range(4):
        n.append(g.insertNode(i))
    for e in ginfo:
        g.insertEdge(n[e[0]].index, n[e[1]].index, e[2])
        g.insertEdge(n[e[1]].index, n[e[0]].index, e[2])
    return g
Ejemplo n.º 3
0
 def buildGraph():
     """Costruisce un grafo tramite liste di incidenza."""
     g = Graph()
     ginfo = ((0, 1, 3.0), (0, 3, 1.0), (1, 3, 2.0), (1, 2, 4.0)\
             , (2, 3, 5.0))
     n = []
     for i in range(4):
         n.append(g.insertNode(i))
     for e in ginfo:
         g.insertEdge(n[e[0]].index, n[e[1]].index, e[2])
         g.insertEdge(n[e[1]].index, n[e[0]].index, e[2])
     return g
Ejemplo n.º 4
0
    def buildGraph(node, edge, r_flag, e_flag):
        """Costruisce un grafo tramite liste di incidenza
          con i valori dei nodi , archi , passati dal Parsers.
        """
        g = Graph()
        tail = []
        head = []
        weight = []
        listEdge = ()
        n = []
        for i in range(node):
            n.append(g.insertNode(i))
        for x in range(edge):
            tail.append(random.randint(0, node - 1))
            head.append(random.randint(0, node - 1))
            weight = random.sample(range(1000000), edge)  #Pesi random univoci
        if not (e_flag or r_flag):
            print('Pesi archi univoci=\n', weight, '\n')

        if e_flag:  #Flag a True creo archi con pesi uguali
            i = 0
            while i < len(weight) - 2:
                weight[i] = weight[i + 2]
                weight[i + 1] = weight[i + 2]
                i += 3
            print('pesi archi uguali= \n', weight, '\n')

        if r_flag:  #Flag a True creo pesi random da perturbare
            weight.clear()
            for i in range(edge):  # sto perturbando l'arco
                weight.append(random.uniform(1, 100))

                val = weight.pop(i)
                _val = round(val + (random.random() * 10**-3), 5)
                weight.insert(i, _val)
            print('pesi archi random perturbati=\n', weight, '\n')

        for u in range(edge):
            listEdge += ((tail[u], head[u], weight[u]), )

        ginfo = listEdge

        for e in ginfo:
            if e[0] == e[1]:  # elimino gli archi A -> A
                continue
            g.insertEdge(n[e[0]].index, n[e[1]].index, e[2])
            g.insertEdge(n[e[1]].index, n[e[0]].index, e[2])
        GraphHelper.cleanGraph(g)  # Utilizzo la funzione cleanGraph

        return g
Ejemplo n.º 5
0
    def buildGraph(num_nodes):
        """
        Build a sample complete graph.
        :param num_nodes number of nodes.
        :return: a sample complete graph.
        """
        graph = Graph()

        nodes = []
        for i in range(num_nodes):
            nodes.append(graph.addNode(i))

        for src in nodes:
            for dst in nodes:
                if dst.id > src.id:
                    graph.insertEdge(src.id, dst.id, num_nodes - src.id)

        return graph
Ejemplo n.º 6
0
                currWeight = currentWeight[head]
                # relaxation step
                if currWeight == INFINITE:
                    currentWeight[head] = weight
                elif weight < currWeight:
                    currentWeight[head] = weight
            curr = curr.next

    return mstWeight, tree


if __name__ == "__main__":
    #graph = GraphHelper.buildGraph(5)
    #graph.print()

    graph = Graph()
    graph.addNode('A')
    graph.addNode('B')
    graph.addNode('C')
    graph.addNode('D')
    graph.addNode('E')
    graph.addNode('F')
    graph.addNode('G')

    graph.insertEdge(0, 1, 7)
    graph.insertEdge(0, 2, 14)
    graph.insertEdge(0, 3, 30)
    graph.insertEdge(1, 2, 21)
    graph.insertEdge(2, 3, 10)
    graph.insertEdge(2, 4, 1)
    graph.insertEdge(4, 5, 6)