Пример #1
0
def create_combo_lock(nodes):
    G = {}
    prev = first = nodes[0]
    for node in nodes[1:]:
        make_link(G, first, node)
        if first != prev:
            make_link(G, prev, node)
        prev = node
    # your code here
    return G
Пример #2
0
def test_graph():
    flights = [("ORD", "SEA"), ("ORD", "LAX"), ('ORD', 'DFW'), ('ORD', 'PIT'),
        ('SEA', 'LAX'), ('LAX', 'DFW'), ('ATL', 'PIT'), ('ATL', 'RDU'),
        ('RDU', 'PHL'), ('PIT', 'PHL'), ('PHL', 'PVD')]

    G = {}
    for (x,y) in flights: graph.make_link(G,x,y)
    marvel_G = graph.read_graph("resources/marvel_graph.tsv")


    assert graph.clustering_coefficient(G) == 2.0/9.0
    assert len(marvel_G) == 19255
    assert graph.path(marvel_G, 'A', 'ZZZAX') == ['A', 'W2 159', 'WOLVERINE/LOGAN ', 'W2 41', 'SUMMERS, NATHAN CHRI', 'C2 59', 'ZZZAX']
    assert 5.11 > graph.centrality(marvel_G, 'A') > 5.1
Пример #3
0
def test_graph():
    flights = [("ORD", "SEA"), ("ORD", "LAX"), ('ORD', 'DFW'), ('ORD', 'PIT'),
               ('SEA', 'LAX'), ('LAX', 'DFW'), ('ATL', 'PIT'), ('ATL', 'RDU'),
               ('RDU', 'PHL'), ('PIT', 'PHL'), ('PHL', 'PVD')]

    G = {}
    for (x, y) in flights:
        graph.make_link(G, x, y)
    marvel_G = graph.read_graph("resources/marvel_graph.tsv")

    assert graph.clustering_coefficient(G) == 2.0 / 9.0
    assert len(marvel_G) == 19255
    assert graph.path(marvel_G, 'A', 'ZZZAX') == [
        'A', 'W2 159', 'WOLVERINE/LOGAN ', 'W2 41', 'SUMMERS, NATHAN CHRI',
        'C2 59', 'ZZZAX'
    ]
    assert 5.11 > graph.centrality(marvel_G, 'A') > 5.1
Пример #4
0
def test():
    # shortcuts
    (a,b,c,d,e,f,g) = ('A', 'B', 'C', 'D', 'E', 'F', 'G')
    triples = ((a,c,3),(c,b,10),(a,b,15),(d,b,9),(a,d,4),(d,f,7),(d,e,3),
               (e,g,1),(e,f,5),(f,g,2),(b,f,1))
    G = {}
    for (i,j,k) in triples:
        make_link(G, i, j, k)

    start = time.clock()
    dist = dijkstra(G, a)
    print str(time.clock() - start)
    start = time.clock()
    dist = dijkstra_mine(G, a)
    print str(time.clock() - start)
    print dist
    assert dist[g] == 8 #(a -> d -> e -> g)
    assert dist[b] == 11 #(a -> d -> e -> g -> f -> b)
Пример #5
0
def is_combo_lock(graph, nodes):
    # first see if we have a star
    center = None
    degree = None
    for node, edges in graph.iteritems():
        if len(edges) > degree:
            center = node
            degree = len(edges)
    if not degree == len(nodes) - 1:
        return False
        # make a graph out of all the edges
    # not connected to the center
    chain = {}
    for node, edges in graph.iteritems():
        if node == center:
            continue
        for e in edges:
            if e == center:
                continue
            make_link(chain, node, e)
    return is_chain(chain, [n for n in nodes if n != center])
Пример #6
0
        for neighbor in G[current].keys():
            if neighbor not in path_from_start:
                x = path_from_start[neighbor] = path_from_start[current] + 1
                s += x
                n += 1
                if s / n > mu:
                    return s / n
                open_list.append(neighbor)
    return s / n

tsv = csv.reader(open("resources/imdb-1.tsv"), delimiter='\t')
G, actors, movies = {}, set(), set()
for (actor, movie, year) in tsv:
    actors.add(actor)
    movies.add(movie)
    graph.make_link(G, actor, movie)

actors = list(actors)
movies = list(movies)

times2 = []
T20 = []
for actor in actors[:20]:
    T20.append((centrality2(G, actor), actor))

T20.sort()
for actor in actors[20:]:
    mu = centrality2(G, actor, T20[19][0])
    if mu < T20[19][0]:
        T20.append((mu, actor))
        T20.sort()