Exemplo n.º 1
0
    def create_simple(self):
        node_names = ['x', 'a', 's', 'b']
        node_pos = [(0, 10), (-10, 0), (0, 0), (10, 0)]
        nodes_dict = {}

        # Create nodes and add them to the dictionary
        for index in range(0, len(node_names)):
            key = node_names[index]
            value = Node(node_names[index], node_pos[index])
            nodes_dict[key] = value

        edges_dict = {}
        e1 = Edge( "e1", nodes_dict['s'], nodes_dict['x'], 90, 270)
        e2 = Edge( "e2", nodes_dict['a'], nodes_dict['s'], 0, 180)
        e3 = Edge( "e3", nodes_dict['b'], nodes_dict['s'], 180, 0)

        edges_dict['sx'] = e1
        edges_dict['as'] = e2
        edges_dict['bs'] = e3

        # add e1 to both vertices
        nodes_dict['s'].edges.append(e1)
        nodes_dict['x'].edges.append(e1)

        # add e2 to both vertices
        nodes_dict['a'].edges.append(e2)
        nodes_dict['s'].edges.append(e2)

        # add e3 to both vertices
        nodes_dict['b'].edges.append(e3)
        nodes_dict['s'].edges.append(e3)

        # set class vars
        self.nodes_dict = nodes_dict
        self.edges_dict = edges_dict
    def ClipWithBottomLeftCorner(self, dcel):

        # find clipping points
        x = dcel.lx
        y = dcel.GetYOfParabolaIntersectionGivenX(
            self.triplet.middle.siteEvent, self.triplet.middle.siteEvent, x)
        leftClip = Vertex(x, y)
        dcel.vertexList.Add(leftClip)

        y = dcel.ly
        x = dcel.GetXOfParabolaIntersectionGivenY(
            self.triplet.middle.siteEvent, self.triplet.left.siteEvent, y)
        bottomClip = Vertex(x, y)
        dcel.vertexList.Add(bottomClip)

        # create two pseudo edges for the clips for the middle face
        cornerLeft = Edge(True)
        cornerBottom = Edge(bottomClip, True)
        dcel.edgeList.Add(cornerLeft)
        dcel.edgeList.Add(cornerBottom)

        self.triplet.middle.siteEvent.face.AppendToForwardList(cornerBottom)
        self.triplet.middle.siteEvent.face.PrependToBackwardList(
            cornerLeft, leftClip)

        # create another bottom left vertex
        bottomLeftCorner = Vertex(dcel.lx, dcel.ly, True)
        dcel.vertexList.Add(bottomLeftCorner)
        self.triplet.middle.siteEvent.face.ConnectBackwardAndForwardListsAt(
            bottomLeftCorner)
Exemplo n.º 3
0
    def __init__(self, numInputNeurons, numHiddenLayers):

        # Create Layers
        self.inputLayer = [Neuron() for _ in range(numInputNeurons)]
        self.numHiddenLayers = numHiddenLayers
        self.hiddenLayers = [[Neuron() for _ in range(numInputNeurons)]
                             for _ in range(numHiddenLayers)]
        self.outputNeuron = Neuron()

        if numHiddenLayers > 0:
            # Create edges to connect input neurons to first hidden layer neurons (if exists)
            for i_neuron in self.inputLayer:
                for h_neuron in self.hiddenLayers[0]:
                    Edge(i_neuron, h_neuron)

            # Create edges to connect hidden layer neurons to each other
            for h_layer1, h_layer2 in [
                (self.hiddenLayers[i], self.hiddenLayers[i + 1])
                    for i in range(self.numHiddenLayers - 1)
            ]:
                for h_neuron1 in h_layer1:
                    for h_neuron2 in h_layer2:
                        Edge(h_neuron1, h_neuron2)

            # Create edges to connect last hidden layer neurons to output neuron
            for h_neuron in self.hiddenLayers[-1]:
                Edge(h_neuron, self.outputNeuron)

        else:
            # Create edges to connect input neurons to output neuron
            for i_neuron in self.inputLayer:
                Edge(i_neuron, self.outputNeuron)
Exemplo n.º 4
0
 def get_moralized_dag(self):
     if self.type != "directed":
         raise RuntimeError('Expected a directed graph as input.')
     # starting to construct the moralized DAG from the existing DAG
     moralized_dag_nodes = self.nodes_set.copy()
     moralized_dag_definition = self.edges_set.copy()
     # looping on the transpose of the adjacency matrix to find the unmarried parents in the original DAG
     for col in self.adjacency_matrix.T:
         # acting only if the considered node has 2 common parents
         if Counter(col)[1] >= 2:
             indeces = [idx for idx, value in enumerate(col) if value == 1]
             # iterating over all pairs of parents that have a common child
             for pair in combinations(indeces, 2):
                 # creating the new edge
                 new_edge = Edge(Node.get_node_by_id(self, pair[0]).variable_name, Node.get_node_by_id(self, pair[1]).variable_name, "directed")
                 # adding an undirected edge to the moralized dag definition
                 if new_edge not in moralized_dag_definition:
                     moralized_dag_definition.append(new_edge)
                 if new_edge.reverse() not in moralized_dag_definition:
                     moralized_dag_definition.append(new_edge.reverse())
     # adding the reverse version of each edge if not present
     for edge in moralized_dag_definition:
         if edge.reverse() not in moralized_dag_definition:
             moralized_dag_definition.append(edge.reverse())
     # creating the moralized version of the DAG
     definition = ''
     for edge in moralized_dag_definition:
         definition += edge.definition + ','
     # adding leftover nodes which were not connected by edges
     for node in moralized_dag_nodes:
         if node.variable_name not in definition:
             definition += node.variable_name + ','
     moralized_dag = Graph.create_graph(definition[:-1], "undirected")
     return moralized_dag
Exemplo n.º 5
0
    def add_edge_object(self, edge: Edge):

        # If the nodes the specified edge is connected
        if edge.get_src() not in self.__nodes or edge.get_dest(
        ) not in self.__nodes:
            return False

        # Objects of type Node - declared so we could access their list of ingoing and outgoing edges
        src = self.__nodes[edge.get_src()]
        dest = self.__nodes[edge.get_dest()]

        # A dictionary representation of all the outgoing connections of src
        src_outs = src.get_out()

        # If they are the same node
        if src.get_key() == dest.get_key():
            return False

        # If they are already connected
        elif dest.get_key() in src_outs:
            return False

        # Making the connection
        else:
            src.add_out(dest.get_key(), edge)
            dest.add_in(src.get_key(), edge)
            self.__edge_size += 1
            self.__mc += 1
            return True
Exemplo n.º 6
0
    def add_edge(self,
                 from_vertex_id,
                 to_vertex_id,
                 edge_type='identity',
                 depth_factor=1,
                 filter_half_width=None,
                 filter_half_height=None,
                 stride_scale=0):
        """
        Adds an edge to the DNA graph, ensuring internal consistency.
        """
        edge = Edge(from_vertex=self.vertices[from_vertex_id],
                    to_vertex=self.vertices[to_vertex_id],
                    type=edge_type,
                    depth_factor=depth_factor,
                    filter_half_width=filter_half_width,
                    filter_half_height=filter_half_height,
                    stride_scale=stride_scale)
        edge.model_id = -1

        self.edges.append(edge)
        self.vertices[from_vertex_id].edges_out.add(edge)
        self.vertices[to_vertex_id].edges_in.add(edge)

        return edge
    def test_scenario_03(self):
        graph = Graph(3)
        graph.add_edge(Edge("s1", "s2", 1))
        graph.add_edge(Edge("s2", "s3", 1))
        graph.add_edge(Edge("s3", "s4", 1))
        graph.add_edge(Edge("s4", "s5", 1))
        graph.add_edge(Edge("s3", "s6", 1))

        graph.partition(weight_criteria=3.0,
                        merge_criteria=0.0,
                        backwards=False)

        self.assertEqual(len(graph.partitions), 2,
                         "There should be 2 partitions")
        self.assertEqual(True, {'s1', 's2', 's3', 's4',
                                's6'} in graph.partitions,
                         "{'s1', 's2', 's3', 's4', 's6'} missing")
        self.assertEqual(True, {'s5'} in graph.partitions, "{'s5'} missing")

        max_node_weights = set(
            [graph.find_max_node_weight_in_partitions(i) for i in range(0, 2)])
        self.assertEqual(max_node_weights,
                         {0, 3.0}.intersection(max_node_weights),
                         "Partitions should have max node weight of {0, 3.0}")

        longest_paths = set(
            [graph.find_longest_path_in_partitions(i) for i in range(0, 2)])
        self.assertEqual(
            longest_paths, {0, 3.0}.intersection(longest_paths),
            "Partitions should have longest paths of size {0, 3.0}")
Exemplo n.º 8
0
    def addEdge(self, point):
        if (self.adding):
            self.adding = False
        else:
            for node in self.nodes:
                if (node.insideNode(point, self.padding)):

                    if (node in self.edge):
                        self.edge = []
                        node.color = Node.default_color
                    else:
                        self.edge.append(node)
                    self.edge_sound.play()

                    if len(self.edge) == 2:
                        if (Edge(self.edge[0], self.edge[1]) not in self.edges):
                            edge = Edge(self.edge[0], self.edge[1], self.directed,self.surface, self.screen)
                            self.edges.append(edge)
                            self.edge[0].addConnection(node, edge)
                            if not(self.directed):
                                self.edge[1].addConnection(self.edge[0], edge)
                        
                        self.edge[0].color = Node.default_color


                        self.edge = []
Exemplo n.º 9
0
    def __SetNodeNeighbors(self, node, data, i, j):
        # Pentru fiecare verific daca pozitia vecinului este un element din clasa sau nu este un loc liber
        if i - 1 >= 0 and data[i - 1][j] != "liber":
            node2 = Node(data[i - 1][j])
            edge = Edge(node, node2, 1)
            self.edges.append(edge)

        if i + 1 < len(data) and data[i + 1][j] != "liber":
            node2 = Node(data[i + 1][j])
            edge = Edge(node, node2, 1)
            self.edges.append(edge)
        #cand schimba coloana verific daca are coleg de banca in dreapta sau daca este in ultimele doua bangi
        if j - 1 >= 0 and data[i][j - 1] != "liber":
            if j % 2 != 0 or i > len(
                    data[i]
            ) - 2:  #daca sunt in ultimele 2 randuri sau au coleg de banca in dreapta
                node2 = Node(data[i][j - 1])
                edge = Edge(node, node2, 1)
                self.edges.append(edge)
        if j + 1 < len(data[i]) and data[i][j + 1] != "liber":
            if j % 2 == 0 or i > len(
                    data[i]
            ) - 2:  #daca sunt in ultimele 2 randuri sau au coleg de banca in dreapta
                node2 = Node(data[i][j + 1])
                edge = Edge(node, node2, 1)
                self.edges.append(edge)
Exemplo n.º 10
0
	def loadFeature(self, name, file):
		print "* loading feature: " + str(file) 
		sys.stdout.flush()
		fileObj = open(file, "rb")
		tmp = pickle.load(fileObj)
		fileObj.close()

		numNone = 0
		numTotal = 0
		for docpair in tmp.keys():

			val = tmp[docpair]
			if val != None:
				if docpair in self.docPairsToRawEdges.keys():
						self.docPairsToRawEdges[docpair].addFeature(name, tmp[docpair])
				else:
					e = Edge(docpair)
					e.addFeature(name, tmp[docpair])
					self.docPairsToRawEdges[docpair] = e
					#self.edges.add(e)
			else:
				numNone = numNone + 1
			numTotal = numTotal + 1

		print "total: " + str(numTotal)
		print "none: " + str(numNone)
		sys.stdout.flush()
Exemplo n.º 11
0
    def create_edges(self):
        unused_edges = set(
            sum(
                map(lambda b: [Edge(a, b) for a in range(b + 1, self.n)],
                    range(self.n)), []))
        edge_set = set()
        #first connect the unconnected edges
        for node in range(1, self.n):
            rand = random.randint(0, node - 1)
            a = max(rand, node)
            b = min(rand, node)
            new_edge = Edge(a, b)
            edge_set.add(new_edge)
            unused_edges.remove(new_edge)

        while len(edge_set) < self.e:
            new_edge = random.sample(unused_edges, 1)[0]
            edge_set.add(new_edge)
            unused_edges.remove(new_edge)

        edges = [[0] * m for m in range(self.n)]

        for e in edge_set:
            edges[e.a][e.b] = 1

        return edges
Exemplo n.º 12
0
def test_graph():
    g1 = TermGraph()
    v1 = Vertex('vert1', 2)
    v2 = Vertex('vert2', 2)
    e1 = Edge(v1, v2)
    g1.add_edge(e1)

    v3 = Vertex('vert3', 3)
    v4 = Vertex('vert4', 4)
    e2 = Edge(v3, v4)
    g1.add_edge(e2)

    e3 = Edge(v1, v4)
    g1.add_edge(e3)
    g1.add_edge(e1)

    print g1._verticlesJoins
    print g1._verticles
    print g1._edges

    g1.recalc_edges()
    g1.recalc_vert_weights()
    g1.recalc_vert_weights()

    for x in xrange(0, 10):
        print '--------' + str(x) + '------'
        g1.recalc_vert_weights()
    def addNode(self, node, friendNodes): # friendNodes er en liste med Node-objekter som allerede eksisterer

        if node in self.nodes.values():
            if not friendNodes:
                print("The node '" + node.getName() + "' already exists.")
            else:
                for friend in friendNodes:
                    if self.edgeExist(node, friend):
                        continue
                    else:
                        if friend not in self.nodes.values():
                            self.nodes[friend.getName()] = friend
                        newEdge = Edge(node, friend)
                        node.addEdge(newEdge)
                        friend.addEdge(newEdge)
                        self.edges.append(newEdge)
        else:
            if not friendNodes:
                self.nodes[node.getName()] = node
            else:
                self.nodes[node.getName()] = node
                for friend in friendNodes:
                    if self.edgeExist(node, friend):
                        continue
                    else:
                        newEdge = Edge(node, friend)
                        node.addEdge(newEdge)
                        friend.addEdge(newEdge)
                        self.edges.append(newEdge)
Exemplo n.º 14
0
    def init_edge_list(self):
        area = self.num_cols * self.num_rows
        priority = PriorityQ()

        for row in range(self.num_rows):
            for col in range(self.num_cols):
                curr_node = self.grid[row][col]

                # if possible create a mock edge between current node add
                # node above
                if row > 0:
                    up_neighbor = self.grid[row - 1][col]
                    edge_1 = Edge(curr_node, up_neighbor, rand.randrange(area))
                    edge_2 = Edge(up_neighbor, curr_node, rand.randrange(area))
                    priority.insert(edge_1)
                    priority.insert(edge_2)

                # if possible create a mock edge between current node add
                # node to the right
                if col > 0:
                    left_neighbor = self.grid[row][col - 1]
                    edge_1 = Edge(curr_node, left_neighbor,
                                  rand.randrange(area))
                    edge_2 = Edge(left_neighbor, curr_node,
                                  rand.randrange(area))
                    priority.insert(edge_1)
                    priority.insert(edge_2)

        return priority
Exemplo n.º 15
0
    def __init__(self, numpoints, dimension, k_n):

        self.Dimension = dimension  # dimension of vertex

        self.V = [Vertex(dimension, i)
                  for i in range(numpoints)]  # index range {0,1,2,...,V-1}

        self.num_V = numpoints
        self.num_E = 0  # number of edges in graph

        self.adj = {}  # adjacent list

        # initialize adjacent dictionary
        for i in range(self.num_V):
            self.adj[i] = []

        # if dimension = 0 -> assign weights randomly
        if self.Dimension == 0:
            for i in range(numpoints - 1):
                for j in range(i + 1, numpoints):
                    new_edge = Edge(self.V[i], self.V[j], 'random')
                    if new_edge.weight <= k_n:
                        self.adj[i].append(new_edge)
                        self.adj[j].append(new_edge)
                        self.num_E += 1

        else:
            # initialize edge for complete undirected graph
            for i in range(numpoints - 1):
                for j in range(i + 1, numpoints):
                    if Vertex.Euclidean_distance(self.V[i], self.V[j]) < k_n:
                        new_edge = Edge(self.V[i], self.V[j], 'Euclidean')
                        self.adj[i].append(new_edge)
                        self.adj[j].append(new_edge)
                        self.num_E += 1
Exemplo n.º 16
0
    def runalgorithm(self):
        for i in range(0, len(graph)):
            s = Set()
            s.vertices.append(i)
            self.listset.append(s)

        for i in range(0, len(graph)):
            for j in range(i, len(graph)):
                if i != j and graph[i][j] > 0:
                    e = Edge()
                    e.v1 = i
                    e.v2 = j
                    e.weight = graph[i][j]
                    self.listedges.append(e)

        self.listedges.sort()
        while len(self.listedges) > 0 and not self.not_found_mst():
            removed_edge = self.listedges[0]
            self.listedges.remove(removed_edge)
            v1 = removed_edge.v1
            v2 = removed_edge.v2
            s1 = self.find_set(v1)
            s2 = self.find_set(v2)

            if not self.is_same_set(s1, s2):
                self.union(s1, s2)
                self.MSTEdges.append(removed_edge)

        print("MST Found")

        self.print_mst_result()
Exemplo n.º 17
0
def construct_graph(reads, k=5):
    """ Construct de bruijn graph from sets of short reads with k length word"""
    edges = dict()
    vertices = dict()

    for read in reads:
        i = 0
        while i + k < len(read):
            v1 = read[i:i + k]
            v2 = read[i + 1:i + k + 1]
            if v1 in edges.keys():
                vertices[v1].outdegree += 1
                edges[v1] += [Edge(v2)]
            else:
                vertices[v1] = Node(v1)
                vertices[v1].outdegree += 1
                edges[v1] = [Edge(v2)]
            if v2 in edges.keys():
                vertices[v2].indegree += 1
            else:
                vertices[v2] = Node(v2)
                vertices[v2].indegree += 1
                edges[v2] = []
            i += 1

    return (vertices, edges)
Exemplo n.º 18
0
 def DrawEdges(self, NewAnt: Ant):
     for CurrentAnt in self.Ants:
         if CurrentAnt.Address != NewAnt.Address:
             NewEdge = Edge(NewAnt.Address, CurrentAnt.Address, 0, 0, 0)
             self.Network.AddEdge(NewEdge)
             NewEdge = Edge(CurrentAnt.Address, NewAnt.Address, 0, 0, 0)
             self.Network.AddEdge(NewEdge)
Exemplo n.º 19
0
    def testChangeInQSlow(self):
        self.logger.info("BEGIN")

        n0 = Node(clusterId="c1", nodeId=0)
        n1 = Node(clusterId="c1", nodeId=1)
        n3 = Node(clusterId="c1", nodeId=3)

        e1 = Edge(weight=1.0, srcId=0, targetId=1)
        n0._addEdge(e1)
        e2 = Edge(weight=1.0, srcId=0, targetId=2)
        n0._addEdge(e2)
        e3 = Edge(weight=1.0, srcId=0, targetId=3)
        n0._addEdge(e3)

        e4 = Edge(weight=1.0, srcId=1, targetId=0)
        n1._addEdge(e4)

        e5 = Edge(weight=1.0, srcId=3, targetId=0)
        n3._addEdge(e5)

        cluster1 = Cluster(clusterId="1", nodeList=[n0, n1, n3])

        n2 = Node(clusterId="c2", nodeId=2)
        e6 = Edge(weight=1.0, srcId=2, targetId=0)
        n2._addEdge(e6)

        n4 = Node(clusterId="c2", nodeId=4)
        n5 = Node(clusterId="c2", nodeId=5)
        e7 = Edge(weight=1.0, srcId=4, targetId=5)
        n4._addEdge(e7)
        e8 = Edge(weight=1.0, srcId=5, targetId=4)
        n5._addEdge(e8)

        e9 = Edge(weight=1.0, srcId=4, targetId=2)
        n4._addEdge(e9)

        e10 = Edge(weight=1.0, srcId=2, targetId=4)
        n2._addEdge(e10)

        cluster2 = Cluster(clusterId="2", nodeList=[n2, n4, n5])

        louvain1 = Louvain("changeInQ1", [cluster1, cluster2])

        # calculate modularity of original graph
        self.logger.info("louvain1._Q:{}".format(louvain1._Q))
        self.assertEqual(louvain1._Q, 0.5599999999999999)

        # move node 2 from cluster 2 to cluster 1
        n2._clusterId = "c1"
        cluster1 = Cluster(clusterId="1", nodeList=[n0, n1, n2, n3])
        cluster2 = Cluster(clusterId="2", nodeList=[n4, n5])

        # calculate modularity
        louvain2 = Louvain("changeInQ2", [cluster1, cluster2])
        self.logger.info("louvain2._Q:{}".format(louvain2._Q))
        self.assertEqual(louvain2._Q, 0.5199999999999999)

        self.logger.info("change in modularity:{}".format(louvain1._Q -
                                                          louvain2._Q))
Exemplo n.º 20
0
 def __DestroyUpsetsEdges(self, upsets):
     for upset in upsets:  #elimin muchiile dintre perosanele suparate
         node1 = Node(upset[0])
         node2 = Node(upset[1])
         edge = Edge(node1, node2, 1)
         edgeReverse = Edge(node2, node1, 1)
         self.RemoveEdge(edge)
         self.RemoveEdge(edgeReverse)
Exemplo n.º 21
0
 def create_edges(self):
     i = 0
     for key in self.data['routes']:
         self.edges.append(Edge(self.data['routes'][i]))
         return_edge = Edge(self.data['routes'][i])
         return_edge.home, return_edge.dest = return_edge.dest, return_edge.home
         self.edges.append(return_edge)
         i+=1
Exemplo n.º 22
0
    def add_edge(self, n, m, weight):
        assert self.v > n >= 0, '请输入正确的节点'
        assert self.v > m >= 0, '请输入正确的节点'

        self.info[n].append(Edge(n, m, weight))
        if n != m and not self.directed:
            self.info[m].append(Edge(m, n, weight))
        self.e += 1
Exemplo n.º 23
0
 def connect(source, target):
     new_edge = Edge.find_by_source_and_target(source.id, target.id)
     if not new_edge:
         Edge(source.id, target.id)
     else:
         return
     source.add_out_neighbour(target)
     target.add_in_neighbour(source)
Exemplo n.º 24
0
 def test_to_dict(self):
     e = Edge(1, 2, 3, "info", 4655)
     data = e.to_dict()
     self.assertEqual(e.src, data.get('src'))
     self.assertEqual(e.dest, data.get('dest'))
     self.assertEqual(e.weight, data.get('weight'))
     self.assertEqual(e.info, data.get('info'))
     self.assertEqual(e.tag, data.get('tag'))
def edgeCounterToSet(nCounter):
	res = Set([]);
	for key in nCounter:
		newEdge = Edge(key);
		newEdge.setefreq(nCounter[key]);
		if not(newEdge.isDump()):
			res.add(newEdge);
	
	return res;
Exemplo n.º 26
0
 def addEdge(self, vertexFromIndex, vertexToIndex, weight):
     edge = Edge()
     self.add_widget(edge)
     edge.setWeight(weight)
     edge.setVertices(self.listOfVertices[vertexFromIndex], self.listOfVertices[vertexToIndex])
     self.listOfEdges.append(edge)
     self.listOfVertices[vertexFromIndex].addOutgoingEdgeIndex(self.currentEdge)
     self.listOfVertices[vertexToIndex].addIncomingEdgeIndex(self.currentEdge)
     self.currentEdge = self.currentEdge + 1
Exemplo n.º 27
0
    def set_edge(self, edge: Edge, mode="n"):
        """
        Vlozit hranu mezi dva vrcholy grafu
        ---------------------------------------------------------
        Mode
        > mode = "n" ... vychozi hodnota, pokud nejsou vrcholy hrany v grafu, vyhodi vyjimku
        > mode = "a" ... pokud nejsou vrcholy hrany v grafu, pridam je
        """

        for e in self.edges:

            # Pokud hrana mezi vrcholy j*z existuje
            if (e.vertex_1.id == edge.vertex_1.id
                    and e.vertex_2.id == edge.vertex_2.id) or (
                        e.vertex_1.id == edge.vertex_2.id
                        and e.vertex_2.id == edge.vertex_1.id):

                e.weight = edge.weight

                # Indexy vrcholu v poli
                v1_index = self.vertices[edge.vertex_1.id]._index
                v2_index = self.vertices[edge.vertex_2.id]._index

                # Neorientovany graf -> symetricka matice
                self.matrix.array[v1_index][v2_index] = edge.weight
                self.matrix.array[v2_index][v1_index] = edge.weight

                return self

        if mode == "n":
            if not edge.vertex_1.id in self.vertices or not edge.vertex_2.id in self.vertices:
                raise Exception("No such a vertex in graph")
        elif mode == "a":
            if edge.vertex_1.id == "" or edge.vertex_2.id == "":
                raise Exception("Missing vertex id")

            if not edge.vertex_1.id in self.vertices:
                self.set_vertex(edge.vertex_1)
            if not edge.vertex_2.id in self.vertices:
                self.set_vertex(edge.vertex_2)

        # Indexy vrcholu v poli
        v1_index = self.vertices[edge.vertex_1.id]._index
        v2_index = self.vertices[edge.vertex_2.id]._index

        # Neorientovany graf -> symetricka matice
        self.matrix.array[v1_index][v2_index] = edge.weight
        self.matrix.array[v2_index][v1_index] = edge.weight

        # Nastavim vrcholy hrany
        edge.vertex_1 = self.vertices[edge.vertex_1.id]
        edge.vertex_2 = self.vertices[edge.vertex_2.id]

        # Pridam hranu
        self.edges.append(edge)

        return self
Exemplo n.º 28
0
def edgeCounterToSet(nCounter):
    res = Set([])
    for key in nCounter:
        newEdge = Edge(key)
        newEdge.setefreq(nCounter[key])
        if not (newEdge.isDump()):
            res.add(newEdge)

    return res
Exemplo n.º 29
0
 def addEdge(self, v, w, weight):
     if 0 <= v < self.n and 0 <= w < self.n:
         if self.hasEdge(v, w):
             return
         self.graph[v].append(Edge(v, w, weight))
         if v != w and self.directed is False:
             self.graph[w].append(Edge(w, v, weight))
         self.m += 1
     else:
         raise Exception('Vertex not in the graph')
Exemplo n.º 30
0
 def add_edge(self, from_, to, capacity):
     # Note that we first append a forward edge and then a backward edge,
     # so all forward edges are stored at even indices (starting from 0),
     # whereas backward edges are stored at odd indices.
     forward_edge = Edge(from_, to, capacity)
     backward_edge = Edge(to, from_, 0)
     self.graph[from_].append(len(self.edges))
     self.edges.append(forward_edge)
     self.graph[to].append(len(self.edges))
     self.edges.append(backward_edge)
Exemplo n.º 31
0
 def addEdge(self, v, w, weight):
     if 0 <= v < self.n and 0 <= w < self.n:
         if self.hasEdge(v, w):
             return
         self.martix[v][w] = Edge(v, w, weight)
         if v != w and self.directed is False:
             self.martix[w][v] = Edge(v, w, weight)
         self.m += 1
     else:
         raise Exception('Vertex not in the graph')
Exemplo n.º 32
0
 def addEdge(self, start_node_id, end_node_id, value):
     for node in self.__nodeMap.keys():
         if node.getID() == start_node_id:
             start_node = node
         if node.getID() == end_node_id:
             end_node = node
     if not self.edgeExist(start_node, end_node):
         new_edge = Edge(start_node, end_node, value)
         self.__nodeMap[start_node].append(new_edge)
         new_edge = Edge(end_node, start_node, value)
         self.__nodeMap[end_node].append(new_edge)
Exemplo n.º 33
0
def create_topology(node_num, edge_num, max_weight, file_path):
    """
    生成有向随机拓扑
    :param node_num: 节点数
    :param edge_num: 边数
    :param max_weight: 最大权重,表示权重范围在(0,max_weight)之间
    :param file_path: 数据写入文件路径
    :return:
    """
    NODE_NUM = node_num  # 节点数
    EDGE_NUM = edge_num  # 边数
    MAX_WEIGHT = max_weight  # 最大权重
    list_nodes = {}  # 节点邻接字典
    neighbor_nodes = {}
    tree_nodes = []
    list_nodes[str(1)] = list()
    neighbor_nodes[str(1)] = list()
    tree_nodes.append(1)
    for i in range(2, NODE_NUM + 1):
        tree_nodes.append(i)
        list_nodes[str(i)] = list()
        neighbor_nodes[str(i)] = list()
        new_edge = Edge(random.randint(1, i - 1), i,
                        random.randint(1, MAX_WEIGHT))  # 随机生成一条边
        neighbor_nodes[str(new_edge.get_node_a())].append(
            str(new_edge.get_node_b()))
        list_nodes[str(new_edge.get_node_a())].append(new_edge)
    with open(file_path, 'a') as file:
        for key in list_nodes.keys():
            node_a = key
            for edge in list_nodes[key]:
                node_b = edge.get_node_b()
                weight = edge.get_weight()
                file.write(
                    str(node_a) + '\t' + str(node_b) + '\t' + str(weight) +
                    '\n')
        file.close()
    """
    第二步:随机在两个节点之间添加边
    """
    for i in range(1, EDGE_NUM - NODE_NUM + 2):
        while True:
            node_1 = random.randint(1, NODE_NUM)  # 随机选两个节点连线
            node_2 = random.randint(1, NODE_NUM)
            if node_1 != node_2:  # 边连接的两个节点不是同一个点
                if str(node_2) not in neighbor_nodes[str(
                        node_1)]:  # 如果这两个节点未曾连线
                    # 将该条边加入到
                    new_edge = Edge(node_1, node_2,
                                    random.randint(1, MAX_WEIGHT))
                    list_nodes[str(new_edge.get_node_a())].append(new_edge)
                    neighbor_nodes[str(node_1)].append(str(node_2))
                    # 将该条边
                    with open(file_path, 'a') as file:
                        file.write(
                            str(node_1) + '\t' + str(node_2) + '\t' +
                            str(new_edge.get_weight()) + '\n')
                        file.close()
                    break
Exemplo n.º 34
0
    def addEadge(self, v, w,weight):
        # 如果v,w之间已经有边了,什么也不做,很好的处理了平行边
        if self.hasEdge(v, w):
            return
        #如果v,w之间没有边
        self.g[v][w] = Edge(v, w, weight)

        #判断其是否时有向图,当不是有向图时,self.g[v][w],self.g[w][v] 之间的边是一样的
        # 条件中加入了 v != w,很好的处理了自环边(即不会重复的添加自环边)
        if v != w and self.directed != True:
            self.g[w][v] = Edge(v, w, weight)
        self.m += 1
Exemplo n.º 35
0
 def toCubieCube(self):
     from CubieCube import CubieCube
     """ generated source for method toCubieCube """
     ori = int()
     ccRet = CubieCube()
     i = 0
     while i < 8:
         ccRet.cp[i] = Corner.URF
         i += 1
     i = 0
     while i < 12:
         ccRet.ep[i] = Edge.UR
         i += 1
     col1 = Color()
     col2 = Color()
     for i in Corner.values():
         while ori < 3:
             if self.f[self.cornerFacelet[i.ordinal()][ori].ordinal()] == Color.U or self.f[self.cornerFacelet[i.ordinal()][ori].ordinal()] == Color.D:
                 break
             ori += 1
         col1 = self.f[self.cornerFacelet[i.ordinal()][(ori + 1) % 3].ordinal()]
         col2 = self.f[self.cornerFacelet[i.ordinal()][(ori + 2) % 3].ordinal()]
         for j in Corner.values():
             if col1 == self.cornerColor[j.ordinal()][1] and col2 == self.cornerColor[j.ordinal()][2]:
                 ccRet.cp[i.ordinal()] = j
                 ccRet.co[i.ordinal()] = int((ori % 3))
                 break
     for i in Edge.values():
         for j in Edge.values():
             if self.f[self.edgeFacelet[i.ordinal()][0].ordinal()] == self.edgeColor[j.ordinal()][0] and self.f[self.edgeFacelet[i.ordinal()][1].ordinal()] == self.edgeColor[j.ordinal()][1]:
                 ccRet.ep[i.ordinal()] = j
                 ccRet.eo[i.ordinal()] = 0
                 break
             if self.f[self.edgeFacelet[i.ordinal()][0].ordinal()] == self.edgeColor[j.ordinal()][1] and self.f[self.edgeFacelet[i.ordinal()][1].ordinal()] == self.edgeColor[j.ordinal()][0]:
                 ccRet.ep[i.ordinal()] = j
                 ccRet.eo[i.ordinal()] = 1
                 break
     return ccRet
Exemplo n.º 36
0
	def AddEdge(self,newEdge,logCollection):
		if (type(newEdge) == type(Edge("to be"))): # a Type of Edge 
			if (not(newEdge.getWords() in self.edgesTup)):
				self.edges.add(newEdge);							# Freq and Time are also transfered
				self.edgesTup.add(newEdge.getWords());
			else: # Find and update the exist one
				edge="";
				for edge in self.edges:
					if (edge.getWords() == newEdge.getWords()): # found the word
						newEdge.increaseFreq(edge.getfreq());
						newEdge.resetTime();
						break;
				self.edges.remove(edge);
				self.edges.add(newEdge);
#		elif (type(newEdge) == type(("to","be"))): # a Tuple
#			self.nodes.add(Edge(newEdge));					# Freq and Time ARE NOT transfered
			logCollection.insert_one({'action':'addEdge', 'data': str(newEdge.getword0() + ' ' + newEdge.getword1()), 'timestamp': datetime.now()});		# Write log
		
		else:									# A text, type: word1[space]word2
			#tup = tuple(sub(r'[^\w\s]', '', newEdge.strip()).split());
			#if  (len(tup)>2): print "Warning! Found Tupple size > 2 while adding edge! Keep 2 fist items by default."
			nEdge = Edge(newEdge);
			if not(nEdge.isDump()):
				self.AddEdge(nEdge,logCollection);
def BigramNodeMerge(nodes,edges,classifier):


	# Find mergable nodes pair
	mergeable_list =[];
	co_left = Counter();	# (A,x) Counter to compute number of its second element, from 1 to n. Key is A, value id 1..n
	co_right = Counter();	# (x,A) Counter to compute number of its first element, from 1 to n. Key is A, value id 1..n
	for node1 in nodes:
		word1 = node1.getWord();
		for node2 in nodes:
			word2 = node2.getWord();
			if (word1==word2 or word1.find('_')>=0 or word2.find('_')>=0): continue; 
			if (classifier.classify({'word1':word1, 'word2':word2})): # Merge able
				mergeable_list.append((node1,node2));
				co_left[node1] +=1;
				co_right[node2] +=1;
	print 'mergeable_list: ' +str(mergeable_list);
	# Deal with overlap - CURRENTLY NOT APPLICABLE
	A_1eft_list = [];
	A_right_list = [];
	# get elements that count ==1
	for e in co_left.elements():
		if (co_left[e]==1): A_1eft_list.append(e); 
	
	for e in co_right.elements():
		if (co_right[e]==1): A_right_list.append(e); 
	print 'co_left: ' +str(co_left);
	print 'co_right: ' +str(co_right);
	print 'A_1eft_list: ' +str(A_1eft_list);
	print 'A_right_list: ' +str(A_right_list);
	final_mergeable_set = [];
	for node_left in A_1eft_list:
		local_mergeable_list = [];
		print '     In merge node: ' + node_left.toString();
		for node_right in A_right_list:
			if (node_left.getword() == node_right.getword()): 
				print '     mergeable escape: duplicate label';
				continue;
			if ((node_left,node_right) in mergeable_list):
				local_mergeable_list.append((node_left,node_right));
#				if ((node_right,node_left) in mergeable_list): 
#					print '     mergeable escape: contradiction';
#					continue;
#				else: 
#					local_mergeable_list.append((node_left,node_right));
		print '     local_mergeable_list: ' + str(local_mergeable_list);
		if (local_mergeable_list == []): continue;
		else: final_mergeable_set.add(random.choice(local_mergeable_list));
	#Merging items in set
	print 'final_mergeable_set: ' + str(final_mergeable_set);
	for (node1,node2) in final_mergeable_set:
		# disregard if any node node in node list
		if ((node1 not in nodes) or (node2 not in nodes)): continue;
		node1_in_edges = []; # list of edges that point TO node1
		node1_out_edges= []; # list of edges that point FROM node1
		node2_in_edges = []; # list of edges that point TO node2
		node2_out_edges= []; # list of edges that point FROM node2
		for ed in edges: 
			# delete connection betweent these node, if exist
			if (ed.getWords() == (node1.getWord(),node2.getWord())): 
				edges.remove(ed);
			# identify incoming nodes to node1
			if (ed.getWord0() == node1.getWord()): node1_out_edges.append(ed);
			if (ed.getWord1() == node1.getWord()): node1_in_edges.append(ed);
			if (ed.getWord0() == node2.getWord()): node2_in_edges.append(ed);
			if (ed.getWord1() == node2.getWord()): node2_out_edges.append(ed);
		# Start merging
		#	initialize new node
		new_node = Node(node1.getWord() + '_' + node2.getWord());
		new_node.setwfreq(node1.getfreq() + node2.getfreq());
		if (node1.gettimeAdded() < node2.gettimeAdded()):
			new_node.setTime(node2.gettimeAdded());
		else:
			new_node.setTime(node1.gettimeAdded());
		if (new_node.isDump()): continue;
		#	redirect edges:
		new_edges = [];
		# 	Incomming nodes
		for ed in (node1_in_edges+node2_in_edges):
			newEdge = Edge(ed.getWord0()+ " " + new_node.getWord());
			newEdge.setefreq(ed.getfreq());
			newEdge.setTime(ed.gettimeAdded());
			if not(newEdge.isDump()):
				res.add(newEdge);
			new_edges.append(newEdge);
		# 	Outgoing nodes
		for ed in (node1_out_edges+node2_out_edges):
			newEdge = Edge(new_node.getWord()+ " " + ed.getWord1());
			newEdge.setefreq(ed.getfreq());
			newEdge.setTime(ed.gettimeAdded());
			if not(newEdge.isDump()):
				new_edges.append(newEdge);
		# Now interfere the data !!!!!!!!!!!!!!!!
		edges = edges.difference(set(node1_in_edges + node1_out_edges + node2_in_edges + node2_out_edges));
		nodes = nodes.difference(set([node1,node2]));
		edges.update(new_edges);
		nodes.add(new_node);
		print '@@@' + new_node.toString();
		print '* Merged ' + node1.getWord() + ' and ' + node2.getWord();
	return (nodes,edges);
def MergeDuplicate(nodes,edges,lemmatizer):
	new_nodes = [];
	eli_nodes = [];
	new_edges = [];
	eli_edges = [];
	for node1 in nodes:
		word1 = node1.getWord();
		node1_in_edges = []; # list of edges that point TO node1
		node1_out_edges= []; # list of edges that point FROM node1
		node2_in_edges = []; # list of edges that point TO node2
		node2_out_edges= []; # list of edges that point FROM node2
		for node2 in nodes:
			word2 = node2.getWord();
			if (word1 != word2 and (node1 not in eli_nodes) and (node2 not in eli_nodes) and (word1.lower() == word2.lower() or lemmatizer.lemmatize(word1.lower()) == lemmatizer.lemmatize(word2.lower()))):
				# Start merging
				#	initialize new node
				new_node = Node(word1.title());
				new_node.setwfreq(node1.getfreq() + node2.getfreq());
				if (node1.gettimeAdded() < node2.gettimeAdded()):
					new_node.setTime(node2.gettimeAdded());
				else:
					new_node.setTime(node1.gettimeAdded());
				if (new_node.isDump()): continue;
				new_nodes.append(new_node);
				#	redirect edges:
				
				# 	Incomming nodes
				for ed in (node1_in_edges+node2_in_edges):
					newEdge = Edge(ed.getWord0()+ " " + new_node.getWord());
					newEdge.setefreq(ed.getfreq());
					newEdge.setTime(ed.gettimeAdded());
					if not(newEdge.isDump()):
						new_edges.append(newEdge);
				# 	Outgoing nodes
				for ed in (node1_out_edges+node2_out_edges):
					newEdge = Edge(new_node.getWord()+ " " + ed.getWord1());
					newEdge.setefreq(ed.getfreq());
					newEdge.setTime(ed.gettimeAdded());
					if not(newEdge.isDump()):
						new_edges.append(newEdge);
				eli_nodes = eli_nodes +[node1,node2];
				eli_edges = eli_edges + node1_in_edges + node1_out_edges + node2_in_edges + node2_out_edges;
	# Now interfere the data !!!!!!!!!!!!!!!!
	for ed in eli_nodes:
		edges.discard(ed);
	for nd in eli_nodes:
		nodes.discard(nd);

	edges.update(new_edges);
	nodes.update(new_nodes);
	return (nodes,edges);		
Exemplo n.º 39
0
 def add_edge(self,source,target,weight):
     edge = Edge()
     edge.set_info(source, target, weight)
     self.edges.append(edge)
from DijkstraShortestPath import dijkstrashortestpath
from Edge import Edge

file_handle = open ("dijkstraData.txt", "r")
graph = {}
dist ={}

for line in file_handle:
    v = int(line.split()[0])
    graph[v] = {}

    for vertex in line.split()[1:]:
        edge = Edge(v, int(vertex.split(",")[0]), int(vertex.split(",")[1]))
        graph[v][edge.other(v)] = edge.weight
file_handle.close()


dist = dijkstrashortestpath(1,graph)

for keys in (7,37,59,82,99,115,133,165,188,197):
    print(dist[keys])


#print(dist)






Exemplo n.º 41
0
	def normalizeFeatures(self):
		
		# goes through each feature
		for feature in self.featureNames:
			print "----------\nfeature :" + str(feature)
			sys.stdout.flush()
			min = 99999
			max = -99999
			sum = 0
			
			# the sum of these two will total the size of self.docPairsToRawEdges
			missingValue = 0
			hadValue = 0

			values = []

			# goes through all docpairs
			for docpair in self.docPairsToRawEdges.keys():
				e = self.docPairsToRawEdges[docpair]

				# checks if the current docpair has the feature (it could be missing)
				if feature in e.features.keys():
					
					val = e.features[feature] # appends to the list of vals (bc we will randomly sample from this)
					values.append(val)
					sum = sum + val
					if val < min:
						min = val
					if val > max:
						max = val
					hadValue = hadValue + 1
				else:
					missingValue = missingValue + 1
			avg = float(sum) / float(hadValue)
			print "min: " + str(min)
			print "max: " + str(max)
			print "avg: " + str(avg)
			print str(missingValue) + " missing values, out of " + str(len(self.docPairsToRawEdges)) + " total (" + str(float(missingValue)/float(len(self.docPairsToRawEdges))) + ")" 
			sys.stdout.flush()
			denom = max - min

			# goes through all docpairs again, to:
			# - normalizes all values; and
			# - fills in missing values by randomly sampling (fills in for both raw and normalized values)
			for docpair in self.docPairsToRawEdges.keys():
				e = self.docPairsToRawEdges[docpair]

				# checks if the current docpair has the feature (it could be missing)
				if feature in e.features.keys():
					normVal = float(e.features[feature] - min) / float(denom)

					# a normalized Edge already exists for the given docpair
					if docpair in self.docPairsToNormEdges.keys():
						enorm = self.docPairsToNormEdges[docpair]
						enorm.addFeature(feature, normVal)
					else:
				 		enorm = Edge(docpair)
				 		enorm.addFeature(feature, normVal)
				 		self.docPairsToNormEdges[docpair] = enorm
				else: # this docpair edge doesn't have the feature, let's randomly pick a value
					randvalue = values[randint(0,len(values)-1)]
					normVal = float(randvalue - min) / float(denom)

					#print "rand value: " + str(randvalue)
					e.addFeature(feature, randvalue)

					# a normalized Edge already exists for the given docpair
					if docpair in self.docPairsToNormEdges.keys():
						enorm = self.docPairsToNormEdges[docpair]
						enorm.addFeature(feature, normVal)
					else:
				 		enorm = Edge(docpair)
				 		enorm.addFeature(feature, normVal)
				 		self.docPairsToNormEdges[docpair] = enorm
			print "done: "
			print "size of rawedges: " + str(len(self.docPairsToRawEdges))
			print "size of normedges: " + str(len(self.docPairsToNormEdges))
			sys.stdout.flush()