Reputation: 62393
networkx
G
edges
to G
len(edges)
is 119G.edges()
doesn't match edges
len(G.edges())
is 112, but my expectation is it should be 119edges
, but my expectation is they should matchimport networkx as nx
edges = [(1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (1, 10), (1, 11), (1, 12), (1, 13), (1, 14), (1, 15), (1, 16), (1, 17), (1, 18), (1, 19), (1, 20), (1, 21), (1, 22), (1, 23), (1, 24), (1, 25), (1, 26), (1, 27), (1, 28), (1, 29), (1, 30), (1, 31), (1, 32), (1, 33), (1, 34), (1, 35), (1, 36), (1, 37), (1, 38), (1, 39), (1, 40), (1, 41), (1, 42), (1, 43), (1, 44), (1, 45), (1, 46), (1, 47), (1, 48), (1, 49), (16, 18), (16, 35), (16, 36), (16, 48), (18, 16), (18, 24), (18, 35), (18, 36), (19, 5), (19, 8), (19, 11), (19, 13), (19, 15), (19, 17), (19, 20), (19, 21), (19, 24), (19, 30), (19, 31), (19, 35), (19, 36), (19, 37), (19, 48), (28, 1), (28, 5), (28, 7), (28, 8), (28, 11), (28, 14), (28, 15), (28, 17), (28, 20), (28, 21), (28, 24), (28, 25), (28, 27), (28, 29), (28, 30), (28, 31), (28, 35), (28, 36), (28, 37), (28, 44), (28, 48), (28, 49), (36, 5), (36, 24), (36, 35), (36, 37), (37, 24), (37, 35), (37, 36), (39, 1), (39, 24), (39, 33), (39, 35), (39, 36), (39, 38), (39, 40), (39, 41), (39, 45), (42, 1), (43, 24), (43, 29), (43, 35), (43, 36), (43, 37), (43, 47), (43, 48), (45, 1), (45, 39), (45, 41)]
print(len(edges))
>>> 119
# unique index 0 values from each edges tuple
print(set([x[0] for x in edges]))
>>> {1, 16, 18, 19, 28, 36, 37, 39, 42, 43, 45}
# create empty graph
G = nx.Graph()
# add edges
G.add_edges_from(edges)
# get graph edges
ge = G.edges()
# length of ge
print(len(ge))
>>> 112
# unique index 0 values from each ge tuple
print(set([x[0] for x in ge]))
>>>{1, 5, 7, 8, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 24, 25, 27, 28, 29, 33, 35, 36, 37, 38, 39, 41, 43}
# output the edges of G
print(ge)
>>> EdgeView([(1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (1, 10), (1, 11), (1, 12), (1, 13), (1, 14), (1, 15), (1, 16), (1, 17), (1, 18), (1, 19), (1, 20), (1, 21), (1, 22), (1, 23), (1, 24), (1, 25), (1, 26), (1, 27), (1, 28), (1, 29), (1, 30), (1, 31), (1, 32), (1, 33), (1, 34), (1, 35), (1, 36), (1, 37), (1, 38), (1, 39), (1, 40), (1, 41), (1, 42), (1, 43), (1, 44), (1, 45), (1, 46), (1, 47), (1, 48), (1, 49), (5, 19), (5, 28), (5, 36), (7, 28), (8, 19), (8, 28), (11, 19), (11, 28), (13, 19), (14, 28), (15, 19), (15, 28), (16, 18), (16, 35), (16, 36), (16, 48), (17, 19), (17, 28), (18, 24), (18, 35), (18, 36), (19, 20), (19, 21), (19, 24), (19, 30), (19, 31), (19, 35), (19, 36), (19, 37), (19, 48), (20, 28), (21, 28), (24, 28), (24, 36), (24, 37), (24, 39), (24, 43), (25, 28), (27, 28), (28, 29), (28, 30), (28, 31), (28, 35), (28, 36), (28, 37), (28, 44), (28, 48), (28, 49), (29, 43), (33, 39), (35, 36), (35, 37), (35, 39), (35, 43), (36, 37), (36, 39), (36, 43), (37, 43), (38, 39), (39, 40), (39, 41), (39, 45), (41, 45), (43, 47), (43, 48)])
edges
and ge
should have the same length
edges
and ge
should have the same node combinations
ge
and edges
match?Upvotes: 3
Views: 466
Reputation: 88236
This is because you're generating an undirected graph, and some of the edges in the edge list are the same just in reversed order. You can check this by sorting the sublists, and building a set
from the result:
len(set(tuple(sorted(i)) for i in edges))
# 112
As you can see you have the same amount of unique combinations of nodes as edges in the graph.
If you generated a directed graph instead, you'd get as you expect, since in this case order does matter:
G = nx.DiGraph()
G.add_edges_from(edges)
len(G.edges())
# 119
Upvotes: 4