Chapter 2
■
the BasiCs
27
The adjacency dict version can be used just like the others, with the additional edge weight functionality:
>>> b in N[a] #
Neighborhood membership
True
>>> len(N[f]) # Degree
3
>>> N[a][b] # Edge weight for (a, b)
2
If you want, you can use
adjacency dicts even if you don’t have any useful edge weights or the like, of course
(using, perhaps, None, or some other placeholder instead). This would give you the main
advantages of the adjacency
sets, but it would also work with very,
very old versions of Python, which don’t have the set type.
14
Until now, the main collection containing our adjacency structures—be they lists, sets, or dicts—has
been a list,
indexed by the node number. A more flexible approach,
allowing us to use arbitrary, hashable,
node labels, is to use a
dict as this main structure.
15
Listing 2-4 shows what a dict containing adjacency sets would look like.
Note that nodes
are now represented by characters.
Do'stlaringiz bilan baham: