1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# Using this module to store commonly used, but annoying to create, data
# structures for my test inputs.
#
# Use like:
# from fixtures import graph_a
################################################################################
# Constants
################################################################################
edge_list = [
('a', 'b'),
('a', 'c'),
('a', 'e'),
('b', 'c'),
('b', 'd'),
('c', 'e'),
('d', 'f'),
('e', 'd'),
('e', 'f'),
]
unweighted_graph = {
'a': {'b', 'c', 'e'},
'b': {'c', 'd'},
'c': {'e'},
'd': {'f'},
'e': {'d', 'f'},
'f': set(),
}
adjacencies = {
'a': {
'a': False,
'b': False
},
'a': [],
'a': [],
'a': [],
'a': [],
'a': [],
'a': [],
}
weighted_graph = {
'a': {(4, 'b'), (2, 'c'), (4, 'e')},
'b': {(5, 'c'), (10, 'd')},
'c': {(3, 'e')},
'd': {(11, 'f')},
'e': {(4, 'd'), (5, 'f')},
'f': set(),
}
# This is `weighted_graph` with each of its weighted edges "expanded".
expanded_weights_graph = {
'a': ['b-1', 'c-1', 'e-1'],
'b-1': ['b-2'],
'b-2': ['b-3'],
'b-3': ['b'],
'c-1': ['c'],
'e-1': ['e-2'],
'e-2': ['e-3'],
'e-3': ['e'],
# and so on...
}
unweighted_digraph = {
'5': {'2', '0'},
'4': {'0', '1'},
'3': {'1'},
'2': {'3'},
'1': set(),
'0': set(),
}
################################################################################
# Functions
################################################################################
def vertices(xs):
result = set()
for a, b in xs:
result.add(a)
result.add(b)
return result
def edges_to_neighbors(xs):
result = {v: set() for v in vertices(xs)}
for a, b in xs:
result[a].add(b)
return result
def neighbors_to_edges(xs):
result = []
for k, ys in xs.items():
for y in ys:
result.append((k, y))
return result
def edges_to_adjacencies(xs):
return xs
# Skipping handling adjacencies because I cannot think of a reasonable use-case
# for it when the vertex labels are items other than integers. I can think of
# ways of handling this, but none excite me.
|