Beispiel #1
0
 def comper(self, g: GraphInterface) -> bool:
     if self.v_size() == g.v_size() and self.e_size() == g.e_size():
         for node in g.get_all_v().values():
             if node.getKey() not in self.graph:
                 return False
             else:
                 if not node.comper(self.graph.get(node.getKey())):
                     return False
             for e in g.all_in_edges_of_node(
                     node.getKey()).items():  # e is tuple (other noe id, w)
                 if e[0] not in self.neighborsDest.get(node.getKey()):
                     return False
                 else:
                     if e[1] != self.neighborsDest.get(node.getKey()).get(
                             e[0]):
                         return False
             for e in g.all_out_edges_of_node(
                     node.getKey()).items():  # e is tuple (other noe id, w)
                 if e[0] not in self.neighborsSrc.get(node.getKey()):
                     return False
                 else:
                     if e[1] != self.neighborsSrc.get(node.getKey()).get(
                             e[0]):
                         return False
         return True
     return False
def put_nodes(graph: GraphInterface):
    for node in graph.get_all_v().values():
        plt.plot(node.get_pos()[0],
                 node.get_pos()[1],
                 'ro',
                 label='Inline label')
        plt.annotate(node.get_key(), (node.get_pos()[0], node.get_pos()[1]),
                     color='blue',
                     fontsize=14)
    def Dijkstra(self, src: int, graph: GraphInterface) -> dict:
        # the tuple in dict is used to show whieght and fater kay (weight, sun key, fatter key)
        queue = []
        dict = {}
        dict[src] = (0, src, -1)
        priorityQueue.heappush(queue, (0, src, -1))

        while len(queue) != 0:
            node = priorityQueue.heappop(queue)
            if graph.get_all_v().get(node[1]).getInfo() == "x":
                for edge in graph.all_out_edges_of_node(node[1]).items(
                ):  # tuple of pair (neighbor node id, weight))
                    if graph.get_all_v().get(edge[0]).getInfo() == "x":
                        weight = node[0] + edge[1]
                        if edge[0] not in dict or dict.get(
                                edge[0])[0] > weight:
                            priorityQueue.heappush(queue,
                                                   (weight, edge[0], node[1]))
                            dict[edge[0]] = (weight, edge[0], node[1])
                graph.get_all_v().get(node[1]).setInfo("v")
        return dict
Beispiel #4
0
def get_frame(graph: GraphInterface) -> tuple:
    global min_x, min_y, max_x, max_y
    for node in graph.get_all_v().values():
        if node.get_pos() is not None:
            if node.get_pos()[0] > max_x:
                max_x = node.get_pos()[0]
            if node.get_pos()[0] < min_x:
                min_x = node.get_pos()[0]
            if node.get_pos()[1] > max_y:
                max_y = node.get_pos()[1]
            if node.get_pos()[1] < min_y:
                min_y = node.get_pos()[1]
    return min_x, max_x, min_y, max_y
Beispiel #5
0
def center_and_scale(g: GraphInterface, width: float, height: float):
    frame = get_frame(g)
    curr_width = frame[1] - frame[0]
    curr_height = frame[3] - frame[2]
    # scale to right size of box
    scale_x = width / curr_width
    scale_y = height / curr_height
    if scale_x < scale_y:
        temp = scale_x
    else:
        temp = scale_y
    scale = 0.9 * temp
    #  offset for all positions
    center = [frame[1] + frame[0], frame[2] + frame[3]]
    offset = [center[0] / 2.0 * scale, center[0] / 2.0 * scale]
    for v in g.get_all_v().values():
        x = v.get_pos()[0]
        y = v.get_pos()[1]
        v.set_pos((x * scale - offset[0], y * scale - offset[1], 0))
def put_edges(graph: GraphInterface):
    global dist
    for node in graph.get_all_v().values():
        for dest in graph.all_out_edges_of_node(node.get_key()).keys():
            plt.arrow(
                node.get_pos()[0],
                node.get_pos()[1],
                graph.get_all_v().get(dest).get_pos()[0] - node.get_pos()[0],
                graph.get_all_v().get(dest).get_pos()[1] - node.get_pos()[1],
                width=node.distance(graph.get_all_v().get(dest).get_pos()) /
                500,
                head_width=3 *
                node.distance(graph.get_all_v().get(dest).get_pos()) / 100,
                head_length=6 *
                node.distance(graph.get_all_v().get(dest).get_pos()) / 100,
                facecolor='black',
                length_includes_head=True)
Beispiel #7
0
def circle(g: GraphInterface):
    angle = 2.0 * math.pi / g.v_size()
    for v in g.get_all_v().values():
        x = math.cos(v.get_key() * angle)
        y = math.sin(v.get_key() * angle)
        v.set_pos((x, y, 0))
def fruchterman_reingold(g: GraphInterface):
    k = 15
    # math.sqrt(1 / (g.v_size()))
    t = math.sqrt(g.v_size())
    k_sqed = k * k
    # repulsion force
    # push the two nodes from each other
    nodes = []
    for v in g.get_all_v().values():
        # Repulsion Force
        for u in g.get_all_v().values():
            if v != u:
                delta = [
                    v.get_pos()[0] - u.get_pos()[0],
                    v.get_pos()[1] - u.get_pos()[1]
                ]
                dist = np.linalg.norm(delta)
                if dist <= 1000 and nodes.__contains__(u.get_key()) is False:
                    rep = k_sqed / dist
                    v.dx += delta[0] / dist * rep
                    u.dx -= delta[0] / dist * rep
                    v.dy += delta[1] / dist * rep
                    u.dy -= delta[1] / dist * rep
        #  Attraction Force
        for e in g.all_out_edges_of_node(v.get_key()):
            u = g.get_all_v().get(e)
            delta = [
                v.get_pos()[0] - u.get_pos()[0],
                v.get_pos()[1] - u.get_pos()[1]
            ]
            dist = np.linalg.norm(delta)
            if dist == 0:
                continue
            att = dist * dist / k
            v.dx -= delta[0] / dist * att
            u.dx += delta[0] / dist * att
            v.dy -= delta[1] / dist * att
            u.dy += delta[1] / dist * att
        nodes.append(v.get_key())

    # attraction force
    # every two nodes that are connected to each other are attracted to each other
    # for v in g.get_all_v().values():

    for v in g.get_all_v().values():
        displacement_norm = np.linalg.norm([v.dx, v.dy])
        if displacement_norm < 1:
            continue
        capped_norm = min(t, displacement_norm)
        capped = [
            v.dx / displacement_norm * capped_norm,
            v.dy / displacement_norm * capped_norm
        ]
        x = v.get_pos()[0] + capped[0]
        y = v.get_pos()[1] + capped[1]
        v.set_pos((x, y, 0))

    if t > 1.5:
        t *= 0.85
    else:
        t = 1.5