Exemple #1
0
    for i in range(0, 5):
        a = rd.randint(1, n)
        b = a
        while b == a:
            b = rd.randint(1, n)

        x = bfs(G, a, b)
        if x > av_dist:
            av_dist = x

    return av_dist


print()
G6 = graph6.Graph()
print(
    'The diameter of G6 (i.e. the maximum distance between two vertices) is:',
    max_distance(G6))
G6 = graph6.Graph()  # we initialize again the attributes of the graph G6
print('I found the distance between two random vertices in G6 to be:',
      random_distance(G6))
print()

G7 = graph7.Graph()
print(
    'The diameter of G7 (i.e. the maximum distance between two vertices) is:',
    max_distance(G7))
G7 = graph7.Graph()  # we initialize again the attributes of the graph G7
print('I found the distance between two random vertices in G7 to be:',
      random_distance(G7))
Exemple #2
0
G.add_edge(1, 3)
G.add_edge(1, 4)
G.add_edge(1, 5)
G.add_edge(2, 6)
G.add_edge(2, 7)
G.add_edge(3, 7)
G.add_edge(4, 7)
G.add_edge(5, 6)
G.add_nodes_from(G.nodes(), colour='never coloured')
G.add_nodes_from(G.nodes(), label=-1)
G.add_nodes_from(G.nodes(), visited='no')
dfs(G, 1)
quit()

print()
G6 = graph6.Graph()
visited_counter = 0
print('The nodes of G6 are visited by depth-first-search in this order:')
dfs(G6, 1)
print()

G7 = graph7.Graph()
visited_counter = 0
print('The nodes of G7 are visited by depth-first-search in this order:')
dfs(G7, 1)
print()

G8 = graph8.Graph()
visited_counter = 0
print('The nodes of G8 are visited by depth-first-search in this order:')
dfs(G8, 1)
Exemple #3
0
    n = len(G.nodes())
    global kmax
    kmax = 1
    global visited_counter
    visited_counter = 1
    colour = find_smallest_colour(G, 1)
    G.nodes[1]['colour'] = colour
    G.nodes[1]['visited'] = 'yes'
    while visited_counter <= n - 1:
        visited_counter += 1
        i = find_next_vertex(G)
        print(i)
        G.nodes[i]['visited'] = 'yes'
        colour = find_smallest_colour(G, i)
        G.nodes[i]['colour'] = colour
        if colour > kmax:
            kmax = colour

    print()
    for i in G.nodes():
        print('vertex', i, ': colour', G.nodes[i]['colour'])
    print()
    print('The number of colours that Greedy computed is:', kmax)
    print()


print('Graph G6:')
G = graph6.Graph()
G.add_nodes_from(G.nodes(), visited='no')
greedy(G)