示例#1
0
def main():
    G = DiGraph()
    undershorts = Node("undershorts")
    pants = Node("pants")
    belt = Node("belt")
    shirt = Node("shirt")
    tie = Node("tie")
    jacket = Node("jacket")
    socks = Node("socks")
    shoes = Node("shoes")
    watch = Node("watch")

    G.add_edge(shirt, belt)
    G.add_edge(shirt, tie)
    G.add_edge(tie, jacket)
    G.add_edge(belt, jacket)
    G.add_edge(pants, belt)
    G.add_edge(undershorts, shoes)
    G.add_edge(undershorts, pants)
    G.add_edge(pants, shoes)
    G.add_edge(socks, shoes)
    G.add_edge(watch, watch)

    vertices_sorted = topological_sort(G)
    # Can have more than 1 answers
    print(vertices_sorted)
示例#2
0
文件: scc.py 项目: whikwon/algorithms
def main():
    G = DiGraph()
    a = Node("a")
    b = Node("b")
    c = Node("c")
    d = Node("d")
    e = Node("e")
    f = Node("f")
    g = Node("g")
    h = Node("h")

    G.add_edge(a, b)
    G.add_edge(e, a)
    G.add_edge(b, e)
    G.add_edge(b, f)
    G.add_edge(b, c)
    G.add_edge(e, f)
    G.add_edge(f, g)
    G.add_edge(g, f)
    G.add_edge(c, g)
    G.add_edge(c, d)
    G.add_edge(d, c)
    G.add_edge(d, h)
    G.add_edge(g, h)
    G.add_edge(h, h)

    groups = scc(G)
    print(groups)
示例#3
0
def main():
    # Load the graph
    #G = DiGraph("net5")
    #G = DiGraph("internet2")
    G = DiGraph("AS3356")
    
    # Get the painting object and set its properties.
    #paint = G.painter()
    #paint.set_source_sink("C", "H")
    #paint.set_rank_same(['C', 'D', 'F'])
    #paint.set_rank_same(['E', 'G', 'H'])
    
    # Generate the graph using the painter we configured.
    #G.export(False, paint)
    
    # Get 30 shortest paths from the graph.
    #items = algorithms.ksp_yen(G, "C", "H", 30)
    #for path in items:
    #    print "Cost:%s\t%s" % (path['cost'], "->".join(path['path']))
    #      
    #items = algorithms.ksp_yen(G, "C", "C", 30)
    #for path in items:
    #    print "Cost:%s\t%s" % (path['cost'], "->".join(path['path']))

    getDisjointPaths(G, "1", "62", 10)
    getDisjointPaths(G, "1", "10", 10)
    getDisjointPaths(G, "1", "2", 10)
示例#4
0
    def create_function(self, bea, haddr):
        """
            Create function descriptor from VM IP and address of handler
            :param bea: Begin IP of function in VM disassembler trace
            :param haddr: Address of handler in binary

            :return Success status
        """
        print "[+] Analyzing function by address %s" % hex(bea)
        dg = None
        try:
            dg = DiGraph(bea, self.instruction_size)
            dg.parse(self.instructions)
        except Exception:
            print "[~] Unhandled exception in create graph routine"
            return False

        if dg.is_bad_cfg:
            print "[!] Function at address: %s invalid" % hex(bea).replace("0x", "").replace("L", "")
            return False

        f = Function(bea, dg.get_low_address(), dg, "sub_%s" % hex(bea).replace("0x", "").replace("L", ""), haddr)
        self.functions[bea] = f

        return True
示例#5
0
def getDirections(start, end):

    # Load the graph
    G = DiGraph("shortestPath")

    # Get 3 shortest paths from the graph.
    items = algorithms.ksp_yen(G, start, end, 3)
    return items
示例#6
0
 def __init__(self):
     self.__g = DiGraph()  # vertex = activity
     self.__d = {}  # vertex : duration
     self.__ES = {}  # vertex : Earliest Start time
     self.__EF = {}  # vertex : Earliest Finish time
     self.__LS = {}  # vertex : Latest Start time
     self.__LF = {}  # vertex : Latest Finish time
     self.__total = 0  # total time of the project
示例#7
0
def main(carData, roadData):
    G = DiGraph("net5")
    manager = Manager()
    return_dict1 = manager.dict()
    return_dict2 = manager.dict()
    return_dict3 = manager.dict()
    return_dict4 = manager.dict()

    start = datetime.now()
    p = Pool(4)
    p.apply_async(thread1, args=(G, carData, roadData, return_dict1))
    p.apply_async(thread2, args=(G, carData, roadData, return_dict2))
    p.apply_async(thread3, args=(G, carData, roadData, return_dict3))
    p.apply_async(thread4, args=(G, carData, roadData, return_dict4))
    p.close()
    p.join()

    # for carNum in range(int(len(carData) / 3), len(carData)):
    #     # items = algorithms.ksp_yen(G, '51', '3', 5)
    #     items = algorithms.ksp_yen(G, str(carData[carNum][1]), str(carData[carNum][2]), 2)
    #     finalPath = []
    #     for path in items:
    #         print("333333333333333333333")
    #         # print(str(carNum) + "Cost:%s\t%s" % (path['cost'], "->".join(path['path'])))

    end = datetime.now()
    print((end - start).seconds)

    subResult1 = return_dict1['result']
    subReuslt2 = return_dict2['result']
    subResult3 = return_dict3['result']
    subReuslt4 = return_dict4['result']

    allCarRoute = subResult1 + subReuslt2 + subResult3 + subReuslt4

    with open('./eightPath.txt', 'w') as f:
        for i in range(len(carData)):
            f.write(str(carData[i][0]))
            f.write('\n')

            for pathNum in range(3):
                for j in range(len(allCarRoute[0])):
                    f.write(str(allCarRoute[0][j]))
                    if j != len(allCarRoute[0]) - 1:
                        f.write(',')
                f.write('\n')
                allCarRoute.pop(0)

            # for j in range(len(allCarRoute[0])):
            #     f.write(str(allCarRoute[0][j]))
            #     if j != len(allCarRoute[0]) - 1:
            #         f.write(',')
            # f.write('\n')
            # allCarRoute.pop(0)

        f.close()

    return 0
示例#8
0
文件: scc.py 项目: whikwon/algorithms
def scc(G):
    """Strongly Connected Components"""
    G_reversed = DiGraph()

    for u in G.adj:
        for v in G.adj[u]:
            G_reversed.add_edge(v, u, attr=G.adj[u][v])

    # topologically high to low
    sorted_vertices = topological_sort(G)[::-1]

    groups = _scc(G_reversed, sorted_vertices)
    return groups
示例#9
0
 def __loadFromFile(self):
     # may raise IOError if invalid filename
     f = open(self.__fName, "r")
     n, m = f.readline().strip().split()
     n = int(n)
     m = int(m)
     self.__graph = DiGraph(n)
     for i in range(m):
         x, y, cost = f.readline().strip().split()
         x = int(x)
         y = int(y)
         cost = int(cost)
         self.__graph.addEdge(x, y)
         self.__graph.setCost(x, y, cost)
示例#10
0
 def __init__(self, file):
     '''
     Constructor for controller
     Input: f (string) filename to read a DiGraph from
     '''
     f = open(file, "r")
     n, m = f.readline().strip().split()
     n = int(n)
     m = int(m)
     self.__dg = DiGraph(n)
     for i in range(m):
         x, y, cost = f.readline().strip().split()
         x = int(x)
         y = int(y)
         cost = int(cost)
         self.__dg.addEdge(x, y)
         #self.__dg.setCost(x, y, cost) #! do not
     f.close()
示例#11
0
def main():
    # Load the graph
    G = DiGraph("net5")

    # Get the painting object and set its properties.
    paint = G.painter()
    paint.set_source_sink("C", "H")
    paint.set_rank_same(['C', 'D', 'F'])
    paint.set_rank_same(['E', 'G', 'H'])

    # Generate the graph using the painter we configured.
    G.export(False, paint)

    # Get 30 shortest paths from the graph.
    items = algorithms.ksp_yen(G, "C", "H", 30)
    for path in items:
        print "Cost:%s\t%s" % (path['cost'], "->".join(path['path']))

    return 0
示例#12
0
文件: rdf.py 项目: perezzini/vinci
    def get_dfs_tree(self, property, node):
        if property not in self.properties:
            raise Exception(property + ' is not a SKOS property')
        di_graph = DiGraph()

        for s, p, o in self.vocab.triples(None, SKS[property], None):
            di_graph.add_edge(s, o)

        tree_edges = di_graph.dfs_tree(node)

        di_graph.clear()
        di_graph.add_edges_from(list(tree_edges))

        # label nodes with literals
        for n1, n2 in di_graph.edges():
            di_graph.nodes()[n1]['label'] = self.get_term_from_uri(n1)
            di_graph.nodes()[n2]['label'] = self.get_term_from_uri(n2)

        return di_graph
示例#13
0
def test_bellman_ford():
    G = DiGraph()
    s = Node("s")
    t = Node("t")
    y = Node("y")
    x = Node("x")
    z = Node("z")

    G.add_edge(s, t, w=6)
    G.add_edge(s, y, w=7)
    G.add_edge(t, y, w=8)
    G.add_edge(t, x, w=5)
    G.add_edge(t, z, w=-4)
    G.add_edge(x, t, w=-2)
    G.add_edge(y, x, w=-3)
    G.add_edge(y, z, w=9)
    G.add_edge(z, x, w=7)
    G.add_edge(z, s, w=2)

    assert bellman_ford(G, s)
示例#14
0
def test_dijkstra():
    G = DiGraph()
    s = Node("s")
    t = Node("t")
    y = Node("y")
    x = Node("x")
    z = Node("z")

    G.add_edge(s, t, w=10)
    G.add_edge(s, y, w=5)
    G.add_edge(t, y, w=2)
    G.add_edge(y, t, w=3)
    G.add_edge(t, x, w=1)
    G.add_edge(y, x, w=9)
    G.add_edge(y, z, w=2)
    G.add_edge(z, s, w=7)
    G.add_edge(z, x, w=6)
    G.add_edge(x, z, w=4)

    dijkstra(G, s)

    assert [u.d for u in G.node] == [0, 8, 5, 9, 7]
示例#15
0
def test_dag_shortest_path():
    G = DiGraph()
    r = Node("r")
    s = Node("s")
    t = Node("t")
    x = Node("x")
    y = Node("y")
    z = Node("z")

    G.add_edge(r, s, w=5)
    G.add_edge(r, t, w=3)
    G.add_edge(s, x, w=6)
    G.add_edge(s, t, w=2)
    G.add_edge(t, x, w=7)
    G.add_edge(t, y, w=4)
    G.add_edge(t, z, w=2)
    G.add_edge(x, y, w=-1)
    G.add_edge(x, z, w=1)
    G.add_edge(y, z, w=-2)

    dag_shortest_path(G, s)

    assert [u.d for u in G.node] == [float('inf'), 0, 2, 6, 5, 3]
示例#16
0
  def build_flowgraph(self, blocks):
    g = DiGraph()

    self.block_offsets = {}
    self.block_nodes = {}

    # Add nodes
    for block in self.blocks:
      self.block_offsets[block.start_offset] = block
      block_node = g.make_add_node(block)
      self.block_nodes[block] = block_node

    # Compute a block's immediate predecessors and successors
    for block in self.blocks:
      for jump_offset in block.jump_offsets:
        assert jump_offset in self.block_offsets
        successor_block = self.block_offsets[jump_offset]
        successor_block.predecessors.add(block)
        block.successors.add(successor_block)
      if ( block.follow_offset
           and (not (jump_flags & block.flags or
                     (BB_NOFOLLOW in block.flags))) ):
        assert block.follow_offset in self.block_offsets
        successor_block = self.block_offsets[block.follow_offset]
        successor_block.predecessors.add(block)
        block.successors.add(successor_block)

    assert(len(self.blocks) > 0)
    self.entry_node = self.blocks[0]

    sorted_blocks = sorted(self.blocks, key=attrgetter('index'))
    for i, block in enumerate(sorted_blocks):

      # Is this this dead code? (Remove self loops in calculation)
      # Entry node, blocks[0] is never unreachable
      if not block.predecessors - set([block]) and block != blocks[0]:
        block.unreachable = True

      block = sorted_blocks[i]
      if block.follow_offset:
          if jump_flags & block.flags:
            kind = 'jump'
          elif BB_NOFOLLOW in block.flags:
            kind = 'no fallthrough'
          else:
            kind = 'fallthrough'
          g.make_add_edge(
              self.block_nodes[block],
              self.block_nodes[self.block_offsets[block.follow_offset]],
              kind)
      # Connect the current block to its jump targets
      for jump_index in block.jump_offsets:
          target_block = self.block_offsets[jump_index]
          if jump_index > block.start_offset:
            if BB_LOOP in block.flags:
              edge_type = 'forward_scope'
            else:
              edge_type = 'forward'
          else:
            edge_type = 'backward'

          if self.block_nodes[target_block] == self.block_nodes[block]:
            edge_type = 'self-loop'

          g.make_add_edge(
              self.block_nodes[block],
              self.block_nodes[target_block],
              edge_type)
          pass
      pass

    self.graph = g
    return
示例#17
0
文件: model.py 项目: daokouer/dgl
    def __init__(
            self,
            n_children=2,
            n_depth=3,
            h_dims=128,
            node_tag_dims=128,
            edge_tag_dims=128,
            n_classes=10,
            steps=5,
            filters=[16, 32, 64, 128, 256],
            kernel_size=(3, 3),
            final_pool_size=(2, 2),
            glimpse_type='gaussian',
            glimpse_size=(15, 15),
    ):
        '''
        Basic idea:
        * We detect objects through an undirected graphical model.
        * The graphical model consists of a balanced tree of latent variables h
        * Each h is then connected to a bbox variable b and a class variable y
        * b of the root is fixed to cover the entire canvas
        * All other h, b and y are updated through message passing
        * The loss function should be either (not completed yet)
            * multiset loss, or
            * maximum bipartite matching (like Order Matters paper)
        '''
        NN.Module.__init__(self)
        self.n_children = n_children
        self.n_depth = n_depth
        self.h_dims = h_dims
        self.node_tag_dims = node_tag_dims
        self.edge_tag_dims = edge_tag_dims
        self.h_dims = h_dims
        self.n_classes = n_classes
        self.glimpse = create_glimpse(glimpse_type, glimpse_size)
        self.steps = steps

        self.cnn = build_cnn(
            filters=filters,
            kernel_size=kernel_size,
            final_pool_size=final_pool_size,
        )

        # Create graph of latent variables
        G = nx.balanced_tree(self.n_children, self.n_depth)
        nx.relabel_nodes(G, {i: 'h%d' % i
                             for i in range(len(G.nodes()))}, False)
        self.h_nodes_list = h_nodes_list = list(G.nodes)
        for h in h_nodes_list:
            G.node[h]['type'] = 'h'
        b_nodes_list = ['b%d' % i for i in range(len(h_nodes_list))]
        y_nodes_list = ['y%d' % i for i in range(len(h_nodes_list))]
        self.b_nodes_list = b_nodes_list
        self.y_nodes_list = y_nodes_list
        hy_edge_list = [(h, y) for h, y in zip(h_nodes_list, y_nodes_list)]
        hb_edge_list = [(h, b) for h, b in zip(h_nodes_list, b_nodes_list)]
        yh_edge_list = [(y, h) for y, h in zip(y_nodes_list, h_nodes_list)]
        bh_edge_list = [(b, h) for b, h in zip(b_nodes_list, h_nodes_list)]

        G.add_nodes_from(b_nodes_list, type='b')
        G.add_nodes_from(y_nodes_list, type='y')
        G.add_edges_from(hy_edge_list)
        G.add_edges_from(hb_edge_list)

        self.G = DiGraph(nx.DiGraph(G))
        hh_edge_list = [
            (u, v) for u, v in self.G.edges()
            if self.G.node[u]['type'] == self.G.node[v]['type'] == 'h'
        ]

        self.G.init_node_tag_with(node_tag_dims,
                                  T.nn.init.uniform_,
                                  args=(-.01, .01))
        self.G.init_edge_tag_with(edge_tag_dims,
                                  T.nn.init.uniform_,
                                  args=(-.01, .01),
                                  edges=hy_edge_list + hb_edge_list +
                                  bh_edge_list)
        self.G.init_edge_tag_with(h_dims * n_classes,
                                  T.nn.init.uniform_,
                                  args=(-.01, .01),
                                  edges=yh_edge_list)

        # y -> h.  An attention over embeddings dynamically generated through edge tags
        self.G.register_message_func(self._y_to_h,
                                     edges=yh_edge_list,
                                     batched=True)

        # b -> h.  Projects b and edge tag to the same dimension, then concatenates and projects to h
        self.bh_1 = NN.Linear(self.glimpse.att_params, h_dims)
        self.bh_2 = NN.Linear(edge_tag_dims, h_dims)
        self.bh_all = NN.Linear(
            2 * h_dims + filters[-1] * NP.prod(final_pool_size), h_dims)
        self.G.register_message_func(self._b_to_h,
                                     edges=bh_edge_list,
                                     batched=True)

        # h -> h.  Just passes h itself
        self.G.register_message_func(self._h_to_h,
                                     edges=hh_edge_list,
                                     batched=True)

        # h -> b.  Concatenates h with edge tag and go through MLP.
        # Produces Δb
        self.hb = NN.Linear(h_dims + edge_tag_dims, self.glimpse.att_params)
        self.G.register_message_func(self._h_to_b,
                                     edges=hb_edge_list,
                                     batched=True)

        # h -> y.  Concatenates h with edge tag and go through MLP.
        # Produces Δy
        self.hy = NN.Linear(h_dims + edge_tag_dims, self.n_classes)
        self.G.register_message_func(self._h_to_y,
                                     edges=hy_edge_list,
                                     batched=True)

        # b update: just adds the original b by Δb
        self.G.register_update_func(self._update_b,
                                    nodes=b_nodes_list,
                                    batched=False)

        # y update: also adds y by Δy
        self.G.register_update_func(self._update_y,
                                    nodes=y_nodes_list,
                                    batched=False)

        # h update: simply adds h by the average messages and then passes it through ReLU
        self.G.register_update_func(self._update_h,
                                    nodes=h_nodes_list,
                                    batched=False)
def main():
    # Load the graph
    G = DiGraph("net5")

    # Get the painting object and set its properties.
    paint = G.painter()
    #paint.set_source_sink('ada', 'bob')
    paint.set_source_sink('s5', 's7')

    # load the graph dictionary
    das_ergebnis = joblib.load(
        "/Users/aditimiglani/Downloads/das_ergebnis.pkl")

    list_of_edges = das_ergebnis['edges']
    list_of_nodes = das_ergebnis['nodes']
    for edge in list_of_edges:
        source = str(edge['source'])
        target = str(edge['target'])
        load = float(edge['load'])
        G.add_edge(source, target, load)
        G.add_edge(target, source, load)

    #G.add_edge('ada', 'sue', 3)
    #G.add_edge('ada', 'john', 2)
    #G.add_edge('sue', 'bob', 1)
    #G.add_edge('a', 'b', 1)
    #G.add_edge('sue', 'john', 1)
    #G.add_edge('john', 'bob', 1)

    # Generate the graph using the painter we configured.
    G.export(False, paint)

    # Get 5 shortest paths from the graph.
    items = algorithms.ksp_yen(G, 's5', 's7', 5)
    all_paths = []
    #items = algorithms.ksp_yen(G, 'ada', 'bob', 5)
    for path in items:
        print "Cost:%s\t%s" % (path['cost'], "->".join(path['path']))
        all_paths.append(path['path'])

    print all_paths
    sub_das_ergebnis = {}
    sub_das_ergebnis['nodes'] = []
    sub_das_ergebnis['edges'] = []
    for sub_dict in list_of_nodes:
        for path in all_paths:
            for i in range(len(path)):
                if path[i] == sub_dict['id']:
                    if sub_dict not in sub_das_ergebnis['nodes']:
                        if i == 0 or i == len(path) - 1:
                            sub_dict['root'] = True
                            sub_das_ergebnis['nodes'].append(sub_dict)
                        else:
                            sub_dict['root'] = False
                            sub_das_ergebnis['nodes'].append(sub_dict)
    for sub_dict in list_of_edges:
        for path in all_paths:
            for i in range(len(path)):
                if i < len(path) - 1:
                    if (path[i] == sub_dict['source']
                            or path[i] == sub_dict['target']) and (
                                path[i + 1] == sub_dict['source']
                                or path[i + 1] == sub_dict['target']):
                        if sub_dict not in sub_das_ergebnis['edges']:
                            sub_das_ergebnis['edges'].append(sub_dict)

    joblib.dump(sub_das_ergebnis, 'sub_das_ergebnis.pkl')
    print sub_das_ergebnis
    print 'loading'
    loaded = joblib.load('sub_das_ergebnis.pkl')
    print loaded

    return 0
示例#19
0
def main(carData, roadData):


    start = datetime.now()

    with open('./answer.txt', 'w') as f:
        G = DiGraph("net5")

        # 5道,4道,3道,2道,1道
        channelCoefficient = [0.9, 0.8, 0.7, 0.6, 0.5]
        # 9, 8, 7, 6, 5, 4, 3, 2, 1
        speedCoefficient = [1.8, 1.7, 1.6, 1.5, 1.4, 1.3, 1.2, 1.1]

        finalPath = []

        for carNum in range(len(carData)):
            # items = algorithms.ksp_yen(G, '51', '3', 5)
            pathDict = G._data


            items = algorithms_transport.ksp_yen(G, str(carData[carNum][1]), str(carData[carNum][2]), 2)

            for path in items:
                print(str(carNum) + "Cost:%s\t%s" % (path['cost'], "->".join(path['path'])))
                carRoute = path['path']

                length = len(carRoute)
                carRoute.reverse()

                # for i in range(len(carRoute)):
                #     f.write(carRoute[i])
                #     if i != len(carRoute) - 1:
                #         f.write(',')
                # f.write('\n')

                carRouteTmp = []
                carRouteTmp.append(carData[carNum][0])
                carRouteTmp.append(carData[carNum][-1])
                for i in range(1, length):
                    for j in range(len(roadData)):
                        if ((roadData[j][-3] == int(carRoute[length - i]) and roadData[j][-2] == int(
                                carRoute[length - i - 1])) or
                                (roadData[j][-2] == int(carRoute[length - i]) and roadData[j][-3] == int(
                                    carRoute[length - i - 1]))):
                            carRouteTmp.append(roadData[j][0])
                finalPath.append(carRouteTmp)


                carRoute.reverse()

                for i in range(len(carRoute) - 1):

                    currentRoad = finalPath[carNum][i]
                    startPoint = carRoute[i]
                    endPoint = carRoute[i+1]

                    oldRoadLength = 0
                    speed = 0
                    channel = 0

                    for roadNum in range(len(roadData)):
                        if roadData[roadNum][0] == currentRoad:
                            oldRoadLength = roadData[roadNum][1]
                            speed = roadData[roadNum][2]
                            channel = roadData[roadNum][3]

                    newRoadLength = math.ceil(oldRoadLength * channelCoefficient[channel - 1] * speedCoefficient[speed - 1])



                    for p in G._data:
                        if p == startPoint:
                            pathDict[startPoint][endPoint] = newRoadLength

                print("debug")
                break


        f.write('#(carId,StartTime,RoadId...)')
        f.write('\n')

        for i in range(len(finalPath)):
            for j in range(len(finalPath[i])):
                if j == 0:
                    f.write('(')
                f.write(str(finalPath[i][j]))
                if j != len(finalPath[i]) - 1:
                    f.write(',')
                else:
                    f.write(')')
            if i != len(finalPath) - 1:
                f.write('\n')

        f.close()

    end = datetime.now()
    print((end - start).seconds)
    return 0
from helpers import get_links
from graph import DiGraph

import os

# from pprint import pprint

filename = "graph.txt"

graph = DiGraph()

if os.path.isfile(filename):
    graph.read_from_filename(filename)

if graph.nodes:
    to_traverse = graph.get_to_traverse()
else:
    root_pagename = "Kevin_Bacon"
    to_traverse = {root_pagename}
count = len(graph.nodes)

# traverse whole graph
with open(filename, "a+", encoding="utf-8") as file:
    while to_traverse:
        pagename = to_traverse.pop()
        graph.add_node(pagename)
        count += 1

        print(f"Traversing {pagename}")
        print(count)
示例#21
0
###
# Create vertices for interstation connections
###

# interstation cost (mins)
isc = 5
interrows = []
for stopA in fullStops:
    for stopB in fullStops:
        a, b = spsl(stopA)
        c, d = spsl(stopB)
        if (a == c and not (b == d)):
            interrows.append((stopA, stopB, isc))

GMaster = DiGraph('awesome')
for stop in fullStops:
    GMaster.add_node(stop)

for row in rows:
    GMaster.add_edge(row[0], row[1], row[2])

for irow in interrows:
    GMaster.add_edge(irow[0], irow[1], irow[2])

print "Calculate Routes..."

# Holds all routes in memory to aid filtering
fromToDict = {}

for stopFrom in fullStops:
def adjust_route(topology, remove_file, routes, link_order):
    # get remove_links
    f = open(remove_file)
    links = []
    line = f.readline()
    while line:
        s = line.rstrip().split(' ')
        s = [int(i) for i in s]

        ls = s[0]
        ld = s[1]
        if ld < ls:
            tmp = ls
            ls = ld
            ld = tmp

        links.append((ls, ld))
        line = f.readline()
    f.close()

    # get final topology
    g = DiGraph()
    f = open(topology)
    line = f.readline()
    line = f.readline()
    while line:
        s = line.rstrip().split(' ')
        lls = int(s[0])
        lld = int(s[1])

        #g.add_nodes([int(s[0]), int(s[1])])
        g.add_edge(lls, lld, link_order[lls, lld][1] / link_order[lls, lld][0])
        g.add_edge(lld, lls, link_order[lls, lld][1] / link_order[lls, lld][0])

        line = f.readline()
    f.close()

    def sort_path(paths):
        for i in range(len(paths)):
            for j in range(i, len(paths)):
                if paths[i][1] < paths[j][1]:
                    tmp = paths[i]
                    paths[i] = paths[j]
                    paths[j] = tmp

    def calculate_cf(paths, link_order):
        for p in paths:
            cf = 100000
            for i in range(len(p[0]) - 1):
                tmp_cf = 0
                s = p[0][i]
                d = p[0][i + 1]
                if s < d:
                    tmp_cf = link_order[s, d][0] / link_order[s, d][1]
                else:
                    tmp_cf = link_order[d, s][0] / link_order[d, s][1]
                if tmp_cf < cf:
                    cf = tmp_cf
            p[1] = cf
            sort_path(paths)

    for ls, ld in links:
        #print(ls,ld)
        for s, d in routes:
            route_paths = routes[s, d]

            remove_route = []
            remove_weight = 0
            for path, weight in route_paths:
                for i in range(len(path) - 1):
                    if (path[i] == ls
                            and path[i + 1] == ld) or (path[i] == ld
                                                       and path[i + 1] == ls):
                        remove_route.append([path, weight])
                        remove_weight += weight

                        # remove weight from all the link
                        for pi in range(len(path) - 1):
                            ps = path[pi]
                            pd = path[pi + 1]
                            if pd < ps:
                                tmp = pd
                                pd = ps
                                ps = tmp

                            link_order[ps, pd][1] -= weight

                        break
            if remove_weight != 0:
                # Find k-shortest-paths between s,d
                tmp_paths = algorithms.ksp_yen(g, s, d, 20)
                yen_paths = [[yen_path['path'], 0, 0]
                             for yen_path in tmp_paths]

                # make sure the proportion of every yen_path
                steps = 10.0
                gap = remove_weight / steps
                calculate_cf(yen_paths, link_order)
                for step in range(int(steps)):
                    for yi in range(len(yen_paths[0][0]) - 1):
                        ys = yen_paths[0][0][yi]
                        yd = yen_paths[0][0][yi + 1]
                        if yd < ys:
                            tmp = ys
                            ys = yd
                            yd = tmp
                        link_order[ys, yd][1] += gap
                    yen_paths[0][2] += 1 / steps
                    calculate_cf(yen_paths, link_order)

                # remain routes
                for rr in remove_route:
                    route_paths.remove(rr)

                # merge k-s-p into routes
                for yen_path, flows, proportion in yen_paths:
                    if proportion == 0:
                        continue

                    exist = False
                    for rp in route_paths:
                        if rp[0] == yen_path:
                            rp[1] += remove_weight * proportion
                            exist = True
                    if not exist:
                        route_paths.append(
                            [yen_path, remove_weight * proportion])

                routes[s, d] = route_paths

    return routes
示例#23
0
                route_paths = new_global_routes_fixed[s, d]
                path_len = 0
                for path, weight in route_paths:
                    path_len += (len(path) - 1) * weight
                path_len_routes[(alpha, s, d)] = path_len

            #print path_len_routes

            for s, d in new_global_routes_base_fixed:
                route_paths = new_global_routes_base_fixed[s, d]
                path_len = 0
                for path, weight in route_paths:
                    path_len += (len(path) - 1) * weight
                path_len_routes_base[(alpha, s, d)] = path_len

            g = DiGraph()
            f = open(final_topology)
            line = f.readline()
            length_graph = int(line.rstrip().split(' ')[0])
            line = f.readline()
            while line:
                s = line.rstrip().split(' ')
                lls = int(s[0])
                lld = int(s[1])

                g.add_edge(lls, lld, 1)
                g.add_edge(lld, lls, 1)

                line = f.readline()
            f.close()
示例#24
0
def main(carData, roadData):
    # Load the graph
    G = DiGraph("net5")

    # Get the painting object and set its properties.
    # paint = G.painter()
    # paint.set_source_sink("C", "H")
    # paint.set_source_sink("C", "H")
    # paint.set_rank_same(['C', 'D', 'F'])
    # paint.set_rank_same(['E', 'G', 'H'])

    # Generate the graph using the painter we configured.
    # G.export(False, paint)

    # Get 30 shortest paths from the graph.
    # items = algorithms.ksp_yen(G, "E", "H", 10)

    start = datetime.now()

    with open('./answer.txt', 'w') as f:

        for carNum in range(len(carData)):
            # items = algorithms.ksp_yen(G, '51', '3', 5)

            f.write(str(carData[carNum][0]))
            f.write('\n')

            items = algorithms.ksp_yen(G, str(carData[carNum][1]),
                                       str(carData[carNum][2]), 2)
            finalPath = []
            for path in items:
                print(
                    str(carNum) + "Cost:%s\t%s" %
                    (path['cost'], "->".join(path['path'])))
                carRoute = path['path']

                length = len(carRoute)
                carRoute.reverse()

                # for i in range(len(carRoute)):
                #     f.write(carRoute[i])
                #     if i != len(carRoute) - 1:
                #         f.write(',')
                # f.write('\n')

                carRouteTmp = []
                for i in range(1, length):
                    for j in range(len(roadData)):
                        if ((roadData[j][-3] == int(carRoute[length - i]) and
                             roadData[j][-2] == int(carRoute[length - i - 1]))
                                or
                            (roadData[j][-2] == int(carRoute[length - i])
                             and roadData[j][-3] == int(
                                 carRoute[length - i - 1]))):
                            carRouteTmp.append(roadData[j][0])
                finalPath.append(carRouteTmp)

            for i in range(len(finalPath)):
                for j in range(len(finalPath[i])):
                    f.write(str(finalPath[i][j]))
                    if j != len(finalPath[i]) - 1:
                        f.write(',')
                f.write('\n')

        f.close()

    end = datetime.now()
    print((end - start).seconds)
    return 0
def GenerateGraph(person1,person2):
	person1_found = 'N'
	person2_found = 'N'

	#u'phillip.k': u's2625'
	print person1
	print person2
	try:
		node1 =person_node_mapping[person1.lower()]
		person1_found = 'Y'
	except:
		print "Person 1 Not found"
		node1 =u's78' #Janel Guerrero

	try:
		node2 =person_node_mapping[person2.lower()]
		person2_found = 'Y'
	except:
		print "Person 2 Not found"
		node2 = u's30018'

	
	G = DiGraph("net5")
	
	# Get the painting object and set its properties.
	paint = G.painter()
	#paint.set_source_sink('ada', 'bob')
	paint.set_source_sink(node1, node2)
	
	# load the graph dictionary

	list_of_edges = das_ergebnis['edges']
	list_of_nodes = das_ergebnis['nodes']
	for edge in list_of_edges:
	    source = str(edge['source'])
	    target = str(edge['target'])
	    load = float(edge['load'])
	    G.add_edge(source, target, load)
	    G.add_edge(target, source, load)

	#G.add_edge('ada', 'sue', 3)
	#G.add_edge('ada', 'john', 2)
	#G.add_edge('sue', 'bob', 1)
	#G.add_edge('a', 'b', 1)
	#G.add_edge('sue', 'john', 1)
	#G.add_edge('john', 'bob', 1)

	# Generate the graph using the painter we configured.
	#G.export(False, paint)
	
	# Get 5 shortest paths from the graph.
	items = algorithms.ksp_yen(G, node1, node2, 5)
	all_paths = []
	#items = algorithms.ksp_yen(G, 'ada', 'bob', 5)
	for path in items:
	    print "Cost:%s\t%s" % (path['cost'], "->".join(path['path']))
	    all_paths.append(path['path'])

	print all_paths
	sub_das_ergebnis = {}
	sub_das_ergebnis['nodes'] = []
	sub_das_ergebnis['edges'] = []
	for sub_dict in list_of_nodes:
	    for path in all_paths:
	        for i in range(len(path)):
	            if path[i] == sub_dict['id']:
	                if sub_dict not in sub_das_ergebnis['nodes']:
	                    if i == 0 or i == len(path) - 1:
	                        sub_dict['root'] = True
	                        sub_das_ergebnis['nodes'].append(sub_dict)
	                    else:
	                        sub_dict['root'] = False
	                        sub_das_ergebnis['nodes'].append(sub_dict)
	for sub_dict in list_of_edges:
	    for path in all_paths:
	        for i in range(len(path)):
	            if i < len(path)-1:
	                if (path[i] == sub_dict['source'] or path[i] == sub_dict['target']) and (path[i+1] == sub_dict['source'] or path[i+1] == sub_dict['target']):
	                    if sub_dict not in sub_das_ergebnis['edges']:
	                        sub_das_ergebnis['edges'].append(sub_dict)
				


	#joblib.dump(sub_das_ergebnis, 'sub_das_ergebnis.pkl')   
	nodes =sub_das_ergebnis['nodes']
	edges =sub_das_ergebnis['edges']


	node_list = []

	count =0
	for node in nodes:
		if node['caption'] in enron_table:
			word = enron_table[node['caption']][1]
			org = enron_table[node['caption']][0]
			node_list.append([word,org,node['id']])
			count+=1
		elif node['caption'] in other_org_table:
			word = other_org_table[node['caption']][1]
			org = other_org_table[node['caption']][0]
			node_list.append([word,org,node['id']])
			count+=1
		else:
			continue

	edge_list = []
	connection_exist =[]
	for edge in edges:
		source = edge['source']
		target = edge['target']
		value = edge['load']
		value = value/4
		value1 = int(value*10)
		value2 = int(value*200)
		connection_exist.append(edge['source'])
		connection_exist.append(edge['target'])

		edge_list.append([edge['source'],edge['target'],value1,value2])

	F = open("WorldCup2014.js","w")
	F.write("var nodes = [") 

	nodes =[]
	for node in node_list:
		num = 1
		if node[0] ==person1:
			num = 2
		if node[0] ==person2:
			num = 3

		string = "{id: " + str(node[2][1:]) + ", label: '" + str(node[0].title()) + "', value: " + str(20) +", group:  " + str(num)+"},"
		nodes.append(string)
		#print string
		F.write(string)

	F.write("];")
	F.write("var edges = [")
	edges =[]

	for edge in edge_list:
		string = "{from: %s, to: %s, value: %s, title: 'Relationship score: %s'}," %(edge[0][1:],edge[1][1:],edge[2],edge[3])
		edges.append(string)
		F.write(string)

	F.write("];")
	F.close()



	return nodes,edges