def bilancedPartion(self,subTree, threshold, G):
     monoWeight = True
     if (subTree.data.number_of_nodes() <= 1):  # if subtree is a leaf, return it
         return subTree
     else:
         balanceCut = int(round(((self.epsilon / 3) / (
         1 + self.epsilon / 3)) / 2 * subTree.data.number_of_nodes()))  # Evaluation of epsilon according to formula provided in paper
         listOfNodes = subTree.data.nodes()  # Selection of two randon nodes in order to divide the graph in two separate parts
         print("Avvio del taglio")
         partition = kernighan_lin_bisection(subTree.data)
         X = G.subgraph(partition[0])  # spotting the two sub-sets of division inside the original Graph G
         Y = G.subgraph(partition[1])
         print(str(len(subTree.data)) + "  " + str(len(X)) + "  " + str(len(Y)))
         leftChild = Tree()  # declaration of offsprings
         leftChild.data = X  # Right Child data assignment
         leftChild.left = None
         leftChild.right = None
         rightChild = Tree()
         rightChild.data = Y  # Left Child data
         rightChild.left = None
         rightChild.right = None
         subTree.left = leftChild
         subTree.right = rightChild
         if (len(partition[0]) > threshold):
             self.bilancedPartion(leftChild, threshold,G)
         if (len(partition[1]) > threshold):
             self.bilancedPartion(rightChild, threshold,G)
 def bilancedPartition(self, subTree, threshold, G):
     if (subTree.data.number_of_nodes() <=
             1):  # if subtree is a leaf, return it
         return subTree
     else:
         partition = kernighan_lin_bisection(subTree.data)
         X = G.subgraph(
             partition[0]
         )  # spotting the two sub-sets of division inside the original Graph G
         Y = G.subgraph(partition[1])
         leftChild = Tree()  # declaration of offsprings
         leftChild.data = X  # Right Child data assignment
         leftChild.left = None
         leftChild.right = None
         rightChild = Tree()
         rightChild.data = Y  # Left Child data
         rightChild.left = None
         rightChild.right = None
         subTree.left = leftChild
         subTree.right = rightChild
         if (
                 self.getWeightsNodes(leftChild.data.node) > threshold
         ):  #comments are the same of createSubTree function (before seen)
             self.bilancedPartition(leftChild, threshold, G)
         if (self.getWeightsNodes(rightChild.data.node) > threshold):
             self.bilancedPartition(rightChild, threshold, G)
 def createSubTree(self, partition, subTree, threshold, G):  #
     X = G.subgraph(str(x) for x in partition[0])  # spotting the two sub-sets divided inside the original Graph G
     Y = G.subgraph(str(x) for x in partition[1])
     leftChild = Tree()  # declaration offsprings
     leftChild.data = X  # Right Child data assignment
     leftChild.left = None
     leftChild.right = None
     rightChild = Tree()
     rightChild.data = Y  # Left Child data assignment
     rightChild.left = None
     rightChild.right = None
     subTree.left = leftChild  # childreen relation assignment
     subTree.right = rightChild
     if (len(partition[0]) > threshold):  # progressive recursive approach from root to leaves
         self.division(leftChild, threshold, G)
     if (len(partition[
                 1]) > threshold):  # treshold is evaluated in order to avoid the creation of parts that need to be removed  (epsilon*n)/ 3*k
         self.division(rightChild, threshold, G)
Esempio n. 4
0
 def buildTree(self,input):
   i=0
   lhp,rhp=self.parser.getProdAtIndex(input[i])
   root=Node(lhp)
   for symbol in rhp:
     root.addChild(Node(symbol))
   i+=1
   self.Util(root,i,input)
   tree=Tree(root)
   return tree
Esempio n. 5
0
def tree_structure(file):
    line = file.readline()
    tree_temp = Tree(line)
    tree_temp.get_root().set_parent(None)
    node = tree_temp.get_root()
    branch = 0

    for line in file:
        # print(line)
        count = 0
        while line[0] == '=':
            line = line[1:]
            count += 1

        if count == branch:
            branch -= 1
            temp = node
            node = node.get_parent()
            node = node.add_child(Node(line))
            node.set_parent(temp)

        elif count > branch:
            branch += 1
            temp = node
            node = node.add_child(Node(line))
            node.set_parent(temp)

        elif count < branch:
            levels = branch - count
            branch -= levels
            for i in range(levels+1):
                node = node.get_parent()

            temp = node
            node = node.add_child(Node(line))
            node.set_parent(temp)

    return tree_temp
Esempio n. 6
0
            node.set_parent(temp)

        elif count < branch:
            levels = branch - count
            branch -= levels
            for i in range(levels+1):
                node = node.get_parent()

            temp = node
            node = node.add_child(Node(line))
            node.set_parent(temp)

    return tree_temp


if __name__ == "__main__":
    # f = open(sys.argv[1], "r")
    filename = "testdata2.ir"
    f = open(filename, "r")

    try:
        tree = tree_structure(f)
        representation = ""
        Tree.depth_tree(tree.get_root())
        f.close()
    except IOError as erno:
        print("Could not find the file")
        print("Error number is:", erno)
    finally:
        print("Done")