Exemple #1
0
def main():
    '''
    1. We first read in the file.  The challenge said that each new line is a single Venmo payment written in JSON format
    so we import
    '''
    f = open("venmo_input/venmo-trans_short.txt")
    graph= Graph()

    # this holds a list of median degrees
    rollingMedianDegree = []
    storeFile =[]
    for line in f:
        newStr = line.strip('\n')
        storeFile.append(newStr)

    for line in storeFile:
        #find the current max time of the graph
        max = findMaxTime(graph)

        newStr = line.strip('\n')
        data = json.loads(newStr)
        if add_to_graph(data['created_time'], max):
            node = Node(data['actor'])
            neighbor = Node(data['target'])
            graph.add_node(node)
            graph.add_node(neighbor)
            graph.add_edge(node, neighbor)
            key = data['actor'] + "->" + data['target']
            metadata = data['created_time']
            graph_node = graph.get_node(node.id)
            graph_node.add_metadata(key, metadata)
        else:
            continue
        #get new max
        max = findMaxTime(graph)
        # set the weight in this case it is the number of neighbors the node has
        setWeight(graph)

        #find expired edges
        expiredNode = findExpiredNode(graph, 60, max)

        #remove those relationships
        for actor, target in expiredNode.items():
            graph.get_node(actor).remove_metadata(actor + "->" + target)
            graph.remove_edge(actor, target)

        #find the median degree
        degree = getMedianDegree(graph)

        #add to rolling median degree records.  this is what will be saved in the output.
        rollingMedianDegree.append(degree)
        

    
    print(graph)
    print(rollingMedianDegree)
def init_graph(fname):
    with open(fname) as f:
        lines = f.readlines()

    graph = Graph()

    for line in lines:
        [parent, child] = line.strip().split(',')
        graph.add_edge(parent, child)

    graph.sort_nodes()

    return graph
Exemple #3
0
    def get_graph_from_file(file_name):
        graph = None
        with open(file_name) as file:
            lines = file.readlines()
            for line in lines:
                words = line.split()
                if words[0] is preamble_symbol:
                    graph = Graph(int(words[2]))
                elif words[0] is edge_symbol:
                    graph.add_edge(int(words[1]) - 1, int(words[2]) - 1)

        graph.set_max_degree()
        graph.set_colors()
        graph.make_triangular()
        print(graph.max_degree, graph.number_vertices)
        return graph