示例#1
0
def create_graph_without_paths(net_name):
    start = time.time()
    g = snap.TUNGraph.New()
    crf.read_file(g, net_name)
    source = g.BegNI()
    title_row = []  # for matrix writing to csv
    while source < g.EndNI():
        s_id = source.GetId()
        title_row.append(str(s_id))
        source.Next()
    with open(net_name.replace('input/', 'output/matrix-'), 'wb') as writefile:
        matrix_writer = csv.writer(writefile, delimiter=',')
        matrix_writer.writerow(['#'] + title_row)
        for i in range(0, len(title_row)):
            this_row = []
            for j in range(0, len(title_row)):
                if i == j:
                    this_row.append("0")
                    continue
                j_id = int(title_row[j])
                i_id = int(title_row[i])
                this_row.append(str(snap.GetShortPath(g, i_id, j_id)))
            matrix_writer.writerow([title_row[i]] + this_row)
    writefile.close()
    print str(((time.time() - start) / 20) * 10000).replace('.', ',')
示例#2
0
def create_graph_with_matrix_class(net_name):
    start = time.time()
    g = snap.TUNGraph.New()
    crf.read_file(g, net_name)
    matrix = Matrix()
    title = []
    node = g.BegNI()
    end = g.EndNI()
    while node < end:
        n = g.GetNI(node.GetId())
        title.append(node.GetId())
        while n < end:
            matrix.add_path(node.GetId(), n.GetId(), snap.GetShortPath(g, node.GetId(), n.GetId()))
            n.Next()
        node.Next()
    with open(net_name.replace('input/', 'output/matrix-'), 'wb') as writefile:
        matrix_writer = csv.writer(writefile, delimiter=',')
        matrix_writer.writerow(['#'] + title)
        for i in range(0, len(title)):
            this_row = []
            for j in range(0, len(title)):
                if i == j:
                    this_row.append(0)
                else:
                    this_row.append(matrix.get_ij(title[i], title[j]))
            matrix_writer.writerow([title[i]] + this_row)
        writefile.close()
    print str(((time.time() - start) * 20) * 10000).replace('.', ',')
示例#3
0
def create_graph_using_paths(net_name):
    real_start = time.time()
    g = snap.TUNGraph.New()
    crf.read_file(g, net_name)
    print "reading and creating graph: " + str((time.time() - real_start))
    start = time.time()
    paths = {}  # match each node to all in graph and saves the shortest path
    source = g.BegNI()
    title_row = []  # for matrix writing to csv
    while source < g.EndNI():
        s_id = source.GetId()
        dest = g.GetNI(s_id)
        dest.Next()
        title_row.append(s_id)
        while dest < g.EndNI():
            d_id = dest.GetId()
            if s_id not in paths:
                if d_id not in paths:
                    paths[s_id] = {}
                    paths[s_id][d_id] = snap.GetShortPath(g, s_id, d_id)
                else:
                    paths[d_id][s_id] = snap.GetShortPath(g, s_id, d_id)
            else:
                paths[s_id][d_id] = snap.GetShortPath(g, s_id, d_id)
            dest.Next()
        source.Next()
    print "creating paths: %f" % (time.time() - start)
    start = time.time()
    with open(net_name.replace('input/', 'output/matrix-'), 'wb') as writefile:
        matrix_writer = csv.writer(writefile, delimiter=',')
        matrix_writer.writerow(['#'] + title_row)
        for i in range(0, len(title_row)):
            this_row = []
            for j in range(0, len(title_row)):
                if i == j:
                    this_row.append("0")
                    continue
                j_id = title_row[j]
                i_id = title_row[i]
                if i_id in paths:
                    if j_id in paths[i_id]:
                        this_row.append(str(paths[i_id][j_id]))
                    else:
                        this_row.append(str(paths[j_id][i_id]))
                else:
                    this_row.append(str(paths[j_id][i_id]))
            matrix_writer.writerow([title_row[i]] + this_row)
    writefile.close()
    print "writing: %f" % (time.time() - start)
    print str((time.time() - real_start)).replace('.', ',')