multi-weighted-graph 0.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- multi_weighted_graph-0.3.0/.gitignore +44 -0
- multi_weighted_graph-0.3.0/PKG-INFO +96 -0
- multi_weighted_graph-0.3.0/README.md +76 -0
- multi_weighted_graph-0.3.0/license +202 -0
- multi_weighted_graph-0.3.0/pyproject.toml +39 -0
- multi_weighted_graph-0.3.0/src/multi_weighted_graph/__init__.py +3 -0
- multi_weighted_graph-0.3.0/src/multi_weighted_graph/_core.py +223 -0
- multi_weighted_graph-0.3.0/src/multi_weighted_graph/_utils.py +0 -0
- multi_weighted_graph-0.3.0/src/multi_weighted_graph/py.typed +0 -0
- multi_weighted_graph-0.3.0/tests/test_core.py +79 -0
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# Python bytecode
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# Virtual environments
|
|
7
|
+
.venv/
|
|
8
|
+
venv/
|
|
9
|
+
env/
|
|
10
|
+
ENV/
|
|
11
|
+
|
|
12
|
+
# Distribution / packaging
|
|
13
|
+
build/
|
|
14
|
+
dist/
|
|
15
|
+
*.egg-info/
|
|
16
|
+
|
|
17
|
+
# Test / coverage
|
|
18
|
+
.pytest_cache/
|
|
19
|
+
.coverage
|
|
20
|
+
.coverage.*
|
|
21
|
+
htmlcov/
|
|
22
|
+
.tox/
|
|
23
|
+
.nox/
|
|
24
|
+
|
|
25
|
+
# Type checkers
|
|
26
|
+
.mypy_cache/
|
|
27
|
+
.pyright/
|
|
28
|
+
.ruff_cache/
|
|
29
|
+
|
|
30
|
+
# Jupyter
|
|
31
|
+
.ipynb_checkpoints/
|
|
32
|
+
|
|
33
|
+
# IDEs / editors
|
|
34
|
+
.vscode/
|
|
35
|
+
.idea/
|
|
36
|
+
|
|
37
|
+
# OS files
|
|
38
|
+
.DS_Store
|
|
39
|
+
Thumbs.db
|
|
40
|
+
|
|
41
|
+
# Local environment / secrets
|
|
42
|
+
.env
|
|
43
|
+
.env.*
|
|
44
|
+
!.env.example
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: multi_weighted_graph
|
|
3
|
+
Version: 0.3.0
|
|
4
|
+
Summary: A data type representing a graph in which edges can be associated to more weights.
|
|
5
|
+
Project-URL: Homepage, https://github.com/Gabri432/multi_weighted_graph
|
|
6
|
+
Project-URL: Issues, https://github.com/Gabri432/multi_weighted_graph/issues
|
|
7
|
+
Author-email: Gabriele Gatti <gabrielegatti432@gmail.com>
|
|
8
|
+
Maintainer-email: Gabriele Gatti <gabrielegatti432@gmail.com>
|
|
9
|
+
License: Apache License
|
|
10
|
+
License-File: license
|
|
11
|
+
Classifier: Intended Audience :: Education
|
|
12
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
15
|
+
Classifier: Topic :: Education
|
|
16
|
+
Requires-Python: >=3.14.3
|
|
17
|
+
Provides-Extra: dev
|
|
18
|
+
Requires-Dist: pytest>=9.1.1; extra == 'dev'
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
|
|
21
|
+
# MultiWeightedGraph
|
|
22
|
+
|
|
23
|
+
- A class representing graphs having multiple weights associated to each edge.
|
|
24
|
+
|
|
25
|
+
## Project structure
|
|
26
|
+
```
|
|
27
|
+
- multi_weighted_graph (root)
|
|
28
|
+
- license
|
|
29
|
+
- README.md
|
|
30
|
+
- .gitignore
|
|
31
|
+
- pyproject.toml
|
|
32
|
+
- src/multi_weighted_graph (folder)
|
|
33
|
+
- __init__.py
|
|
34
|
+
- _core.py
|
|
35
|
+
- _utils.py
|
|
36
|
+
- py.typed
|
|
37
|
+
- tests (folder)
|
|
38
|
+
- test_core.py
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Tech stack
|
|
42
|
+
|
|
43
|
+
- Python 3.14.3
|
|
44
|
+
- pytest 9.1.1
|
|
45
|
+
|
|
46
|
+
## How to use it
|
|
47
|
+
- To run tests run the command:
|
|
48
|
+
```
|
|
49
|
+
pytest
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
- Import the class and start using it:
|
|
53
|
+
```py
|
|
54
|
+
from multi_weighted_graph import MultiWeightedGraph
|
|
55
|
+
|
|
56
|
+
my_graph = MultiWeightedGraph("My New Graph", 2)
|
|
57
|
+
my_graph.add_edge("a", "b", [4, 2])
|
|
58
|
+
my_graph.add_edge("b", "c", [3, 2])
|
|
59
|
+
my_graph.add_edge("c", "d", [2, 2])
|
|
60
|
+
my_graph.add_edge("a", "d", [1, 3])
|
|
61
|
+
my_graph.add_edge("e", "f", [3, 5])
|
|
62
|
+
my_graph.remove_edge("e", "f")
|
|
63
|
+
|
|
64
|
+
print(my_graph.minimal_spanning_tree())
|
|
65
|
+
print(my_graph.matrix_of_distances())
|
|
66
|
+
print(my_graph.matrix_of_distances_from_source("a"))
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
- Calculate the minimal spanning tree:
|
|
70
|
+
```py
|
|
71
|
+
>>> print(my_graph.minimal_spanning_tree())
|
|
72
|
+
[GraphEdge('a', 'd', [1, 2]), GraphEdge('c', 'd', [2, 2]), GraphEdge('b', 'c', [3, 2])]
|
|
73
|
+
|
|
74
|
+
>>> print(my_graph.minimal_spanning_tree(1)) # building the minimal spanning tree using the second weight associated to edges
|
|
75
|
+
[GraphEdge('a', 'b', [4, 2]), GraphEdge('b', 'c', [3, 2]), GraphEdge('c', 'd', [2, 2])]
|
|
76
|
+
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
- Calculate the matrix of distances between each vertex:
|
|
80
|
+
```py
|
|
81
|
+
>>> print(my_graph.matrix_of_distances())
|
|
82
|
+
[[0, 4, 3, 1], [4, 0, 3, 5], [3, 3, 0, 2], [1, 5, 2, 0]]
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
- Calculate the matrix of distances from a vertex to all the others
|
|
86
|
+
```py
|
|
87
|
+
>>> print(my_graph.matrix_of_distances_from_source("a", 0)) # distance from vertex "a" to all the others, using first weight associated to edges
|
|
88
|
+
[0, 4, 3, 1]
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
## Notes
|
|
93
|
+
|
|
94
|
+
- The intended use of this project is for personal and/or educational purposes.
|
|
95
|
+
- The project is under the Apache License, that you can see from [here](https://github.com/Gabri432/multi_weighted_graph/blob/main/license)
|
|
96
|
+
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# MultiWeightedGraph
|
|
2
|
+
|
|
3
|
+
- A class representing graphs having multiple weights associated to each edge.
|
|
4
|
+
|
|
5
|
+
## Project structure
|
|
6
|
+
```
|
|
7
|
+
- multi_weighted_graph (root)
|
|
8
|
+
- license
|
|
9
|
+
- README.md
|
|
10
|
+
- .gitignore
|
|
11
|
+
- pyproject.toml
|
|
12
|
+
- src/multi_weighted_graph (folder)
|
|
13
|
+
- __init__.py
|
|
14
|
+
- _core.py
|
|
15
|
+
- _utils.py
|
|
16
|
+
- py.typed
|
|
17
|
+
- tests (folder)
|
|
18
|
+
- test_core.py
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Tech stack
|
|
22
|
+
|
|
23
|
+
- Python 3.14.3
|
|
24
|
+
- pytest 9.1.1
|
|
25
|
+
|
|
26
|
+
## How to use it
|
|
27
|
+
- To run tests run the command:
|
|
28
|
+
```
|
|
29
|
+
pytest
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
- Import the class and start using it:
|
|
33
|
+
```py
|
|
34
|
+
from multi_weighted_graph import MultiWeightedGraph
|
|
35
|
+
|
|
36
|
+
my_graph = MultiWeightedGraph("My New Graph", 2)
|
|
37
|
+
my_graph.add_edge("a", "b", [4, 2])
|
|
38
|
+
my_graph.add_edge("b", "c", [3, 2])
|
|
39
|
+
my_graph.add_edge("c", "d", [2, 2])
|
|
40
|
+
my_graph.add_edge("a", "d", [1, 3])
|
|
41
|
+
my_graph.add_edge("e", "f", [3, 5])
|
|
42
|
+
my_graph.remove_edge("e", "f")
|
|
43
|
+
|
|
44
|
+
print(my_graph.minimal_spanning_tree())
|
|
45
|
+
print(my_graph.matrix_of_distances())
|
|
46
|
+
print(my_graph.matrix_of_distances_from_source("a"))
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
- Calculate the minimal spanning tree:
|
|
50
|
+
```py
|
|
51
|
+
>>> print(my_graph.minimal_spanning_tree())
|
|
52
|
+
[GraphEdge('a', 'd', [1, 2]), GraphEdge('c', 'd', [2, 2]), GraphEdge('b', 'c', [3, 2])]
|
|
53
|
+
|
|
54
|
+
>>> print(my_graph.minimal_spanning_tree(1)) # building the minimal spanning tree using the second weight associated to edges
|
|
55
|
+
[GraphEdge('a', 'b', [4, 2]), GraphEdge('b', 'c', [3, 2]), GraphEdge('c', 'd', [2, 2])]
|
|
56
|
+
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
- Calculate the matrix of distances between each vertex:
|
|
60
|
+
```py
|
|
61
|
+
>>> print(my_graph.matrix_of_distances())
|
|
62
|
+
[[0, 4, 3, 1], [4, 0, 3, 5], [3, 3, 0, 2], [1, 5, 2, 0]]
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
- Calculate the matrix of distances from a vertex to all the others
|
|
66
|
+
```py
|
|
67
|
+
>>> print(my_graph.matrix_of_distances_from_source("a", 0)) # distance from vertex "a" to all the others, using first weight associated to edges
|
|
68
|
+
[0, 4, 3, 1]
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
## Notes
|
|
73
|
+
|
|
74
|
+
- The intended use of this project is for personal and/or educational purposes.
|
|
75
|
+
- The project is under the Apache License, that you can see from [here](https://github.com/Gabri432/multi_weighted_graph/blob/main/license)
|
|
76
|
+
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2026 Gabriele Gatti
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling >= 1.26"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "multi_weighted_graph"
|
|
7
|
+
version = "0.3.0"
|
|
8
|
+
description = "A data type representing a graph in which edges can be associated to more weights."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.14.3"
|
|
11
|
+
license = {text = "Apache License"}
|
|
12
|
+
authors = [
|
|
13
|
+
{name = "Gabriele Gatti", email = "gabrielegatti432@gmail.com"},
|
|
14
|
+
]
|
|
15
|
+
maintainers = [
|
|
16
|
+
{ name="Gabriele Gatti", email="gabrielegatti432@gmail.com" },
|
|
17
|
+
]
|
|
18
|
+
classifiers = [
|
|
19
|
+
"Operating System :: OS Independent",
|
|
20
|
+
"License :: OSI Approved :: Apache Software License",
|
|
21
|
+
"Programming Language :: Python :: 3.14",
|
|
22
|
+
"Intended Audience :: Education",
|
|
23
|
+
"Topic :: Education"
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
[project.urls]
|
|
27
|
+
Homepage = "https://github.com/Gabri432/multi_weighted_graph"
|
|
28
|
+
Issues = "https://github.com/Gabri432/multi_weighted_graph/issues"
|
|
29
|
+
|
|
30
|
+
[project.optional-dependencies]
|
|
31
|
+
dev = [
|
|
32
|
+
"pytest>=9.1.1",
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
[tool.hatch.build.targets.wheel]
|
|
36
|
+
where = ["src/multi_weighted_graph"]
|
|
37
|
+
|
|
38
|
+
[tool.pytest.ini_options]
|
|
39
|
+
testpaths = ["tests"]
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
class MultiWeightedGraph:
|
|
2
|
+
'''An instance of class MultiWeightedGraph represents a graph in which each edge is associated to one or more weights.'''
|
|
3
|
+
class GraphEdge:
|
|
4
|
+
def __init__(self, vertex_a: str, vertex_b: str, weights: list[float]) -> None:
|
|
5
|
+
self.vertex_a = vertex_a
|
|
6
|
+
self.vertex_b = vertex_b
|
|
7
|
+
self.weights = weights
|
|
8
|
+
|
|
9
|
+
def __str__(self) -> str:
|
|
10
|
+
return "(" + self.vertex_a + ")- "+ str(self.weights) + " -(" + self.vertex_b +")"
|
|
11
|
+
|
|
12
|
+
def __repr__(self) -> str:
|
|
13
|
+
return f"GraphEdge({self.vertex_a!r}, {self.vertex_b!r}, {self.weights!r})"
|
|
14
|
+
|
|
15
|
+
class __Partition__:
|
|
16
|
+
def __init__(self, name: str) -> None:
|
|
17
|
+
self.name = name
|
|
18
|
+
self.elements = set(name)
|
|
19
|
+
|
|
20
|
+
def __find__(self, name: str) -> bool:
|
|
21
|
+
return self.elements.__contains__(name)
|
|
22
|
+
|
|
23
|
+
def __str__(self) -> str:
|
|
24
|
+
return "{"+ self.elements.__str__() +"}"
|
|
25
|
+
|
|
26
|
+
def __repr__(self) -> str:
|
|
27
|
+
return f"Partition(name={self.name}, elements={self.elements})"
|
|
28
|
+
|
|
29
|
+
def __union__(self, elements: set[str], new_name: str) -> None:
|
|
30
|
+
self.elements = self.elements.union(elements)
|
|
31
|
+
self.name = new_name
|
|
32
|
+
|
|
33
|
+
def __eq__(self, value: object) -> bool:
|
|
34
|
+
if not isinstance(value, MultiWeightedGraph.__Partition__):
|
|
35
|
+
return False
|
|
36
|
+
|
|
37
|
+
return value.name == self.name
|
|
38
|
+
|
|
39
|
+
def __hash__(self) -> int:
|
|
40
|
+
return hash(self.name)
|
|
41
|
+
|
|
42
|
+
class __Vertex__:
|
|
43
|
+
def __init__(self, vertex: str, distance_from_source: float) -> None:
|
|
44
|
+
self.vertex = vertex
|
|
45
|
+
self.distance_from_source = distance_from_source
|
|
46
|
+
|
|
47
|
+
def __str__(self) -> str:
|
|
48
|
+
return "{"+ self.vertex +" : " + str(self.distance_from_source)+"}"
|
|
49
|
+
|
|
50
|
+
def __repr__(self) -> str:
|
|
51
|
+
return f"Vertex(name={self.vertex}, distance_from_source={self.distance_from_source})"
|
|
52
|
+
|
|
53
|
+
class __VertexNeighbours__:
|
|
54
|
+
def __init__(self, vertex: str, neighbours: list[MultiWeightedGraph.__Vertex__]):
|
|
55
|
+
self.vertex = vertex
|
|
56
|
+
self.neighbourhood = neighbours
|
|
57
|
+
|
|
58
|
+
def __str__(self) -> str:
|
|
59
|
+
return "{"+ self.vertex +" : " + str(self.neighbourhood)+"}"
|
|
60
|
+
|
|
61
|
+
def __repr__(self) -> str:
|
|
62
|
+
return f"Vertex(name={self.vertex}, neighbours={self.neighbourhood})"
|
|
63
|
+
|
|
64
|
+
def __init__(self, name: str = "My new multi-weighted graph", number_of_weights_per_edge: int = 1) -> None:
|
|
65
|
+
self.name = name
|
|
66
|
+
self.weights_per_edge = number_of_weights_per_edge
|
|
67
|
+
self.edges: list[MultiWeightedGraph.GraphEdge] = []
|
|
68
|
+
self.vertices: list[str] = []
|
|
69
|
+
|
|
70
|
+
def __str__(self) -> str:
|
|
71
|
+
return self.name
|
|
72
|
+
|
|
73
|
+
def __repr__(self) -> str:
|
|
74
|
+
return f"MultiWeightedGraph({self.name!r}, {self.weights_per_edge})"
|
|
75
|
+
|
|
76
|
+
def add_edge(self, first_vertex_name: str, second_vertex_name: str, weights: list[float]) -> None:
|
|
77
|
+
'''Adds an edge to the graph by specifying the name of the vertices, as well as a list of weights to associate to such edge'''
|
|
78
|
+
edge = self.GraphEdge(first_vertex_name, second_vertex_name, weights)
|
|
79
|
+
self.edges.append(edge)
|
|
80
|
+
if (self.vertices.__contains__(first_vertex_name) == False):
|
|
81
|
+
self.vertices.append(first_vertex_name)
|
|
82
|
+
if (self.vertices.__contains__(second_vertex_name) == False):
|
|
83
|
+
self.vertices.append(second_vertex_name)
|
|
84
|
+
|
|
85
|
+
def remove_edge(self, first_vertex_name: str, second_vertex_name: str) -> None:
|
|
86
|
+
'''Removes an edge from the graph by specifying its two vertices.'''
|
|
87
|
+
for edge in self.edges:
|
|
88
|
+
condition1 = edge.vertex_a == first_vertex_name and edge.vertex_b == second_vertex_name
|
|
89
|
+
condition2 = edge.vertex_a == second_vertex_name and edge.vertex_b == first_vertex_name
|
|
90
|
+
if condition1:
|
|
91
|
+
self.edges.remove(edge)
|
|
92
|
+
elif condition2:
|
|
93
|
+
self.edges.remove(edge)
|
|
94
|
+
|
|
95
|
+
def minimal_spanning_tree(self, weights_to_select: int) -> list[MultiWeightedGraph.GraphEdge]:
|
|
96
|
+
'''Calculate the minimal spanning tree based on the weight selected for each edge.'''
|
|
97
|
+
ordered_edges = sorted(self.edges, key=lambda edge:(edge.weights[weights_to_select]))
|
|
98
|
+
current_tree: list[MultiWeightedGraph.GraphEdge] = []
|
|
99
|
+
partition: list[MultiWeightedGraph.__Partition__] = []
|
|
100
|
+
for edge in ordered_edges:
|
|
101
|
+
part_a = self.__Partition__(edge.vertex_a)
|
|
102
|
+
part_b = self.__Partition__(edge.vertex_b)
|
|
103
|
+
if partition.__contains__(part_a) == False:
|
|
104
|
+
partition.append(part_a)
|
|
105
|
+
if partition.__contains__(part_b) == False:
|
|
106
|
+
partition.append(part_b)
|
|
107
|
+
if not self.__same_partition__(partition, edge.vertex_a, edge.vertex_b):
|
|
108
|
+
partition_of_a = self.__find_partition__(partition, edge.vertex_a)
|
|
109
|
+
partition_of_b = self.__find_partition__(partition, edge.vertex_b)
|
|
110
|
+
partition.remove(partition_of_a)
|
|
111
|
+
partition.remove(partition_of_b)
|
|
112
|
+
partition_of_a.__union__(partition_of_b.elements, part_a.name)
|
|
113
|
+
partition.append(partition_of_a)
|
|
114
|
+
current_tree.append(edge)
|
|
115
|
+
return current_tree
|
|
116
|
+
|
|
117
|
+
def __same_partition__(self, partition: list[MultiWeightedGraph.__Partition__], vertex_a: str, vertex_b) -> bool:
|
|
118
|
+
for sub_partition in partition:
|
|
119
|
+
if sub_partition.__find__(vertex_a) and sub_partition.__find__(vertex_b):
|
|
120
|
+
return True
|
|
121
|
+
return False
|
|
122
|
+
|
|
123
|
+
def __find_partition__(self, partition: list[MultiWeightedGraph.__Partition__], name: str) -> MultiWeightedGraph.__Partition__:
|
|
124
|
+
for sub_partition in partition:
|
|
125
|
+
if sub_partition.name == name or sub_partition.elements.__contains__(name):
|
|
126
|
+
return sub_partition
|
|
127
|
+
return MultiWeightedGraph.__Partition__("")
|
|
128
|
+
|
|
129
|
+
def matrix_of_distances(self, weight_to_select: int = 0) -> list[list[float]]:
|
|
130
|
+
'''Calculates the matrix of distances between vertices based on the weight selected for each edge.'''
|
|
131
|
+
n = len(self.vertices)
|
|
132
|
+
v_to_idx = {v: i for i, v in enumerate(self.vertices)}
|
|
133
|
+
inf = float("inf")
|
|
134
|
+
dist = [[inf] * n for _ in range(n)]
|
|
135
|
+
for i in range(n):
|
|
136
|
+
dist[i][i] = 0
|
|
137
|
+
|
|
138
|
+
for edge in self.edges:
|
|
139
|
+
dist[v_to_idx[edge.vertex_a]][v_to_idx[edge.vertex_b]] = edge.weights[weight_to_select]
|
|
140
|
+
dist[v_to_idx[edge.vertex_b]][v_to_idx[edge.vertex_a]] = edge.weights[weight_to_select]
|
|
141
|
+
|
|
142
|
+
for k in range(n):
|
|
143
|
+
for i in range(n):
|
|
144
|
+
for j in range(n):
|
|
145
|
+
if dist[i][j] > dist[i][k] + dist[k][j]:
|
|
146
|
+
dist[i][j] = dist[i][k] + dist[k][j]
|
|
147
|
+
return dist
|
|
148
|
+
|
|
149
|
+
def matrix_of_distances_from_source(self, source: str, weight_to_select: int = 0) -> list[float]:
|
|
150
|
+
'''Calculates the matrix of distances between vertices from a specific vertex (source) based on the weight selected for each edge.'''
|
|
151
|
+
graph = self.__create_adjacency_lists__(weight_to_select)
|
|
152
|
+
n = len(self.vertices)
|
|
153
|
+
explored = [False for _ in range(n)]
|
|
154
|
+
dist_from_source = self.__initialize_distance_vector__(source, weight_to_select)
|
|
155
|
+
v_to_idx = {v: i for i, v in enumerate(self.vertices)}
|
|
156
|
+
idx_to_v = {i: v for i, v in enumerate(self.vertices)}
|
|
157
|
+
a = (v_to_idx.get(source))
|
|
158
|
+
if a != None:
|
|
159
|
+
explored[a] = True
|
|
160
|
+
for _, v in enumerate(self.vertices):
|
|
161
|
+
if v != source:
|
|
162
|
+
index_min = self.__get_min_distance_vertex_index__(explored, dist_from_source)
|
|
163
|
+
adjacent_list: list[MultiWeightedGraph.__Vertex__] = []
|
|
164
|
+
vertex_of_min_distance = idx_to_v.get(index_min)
|
|
165
|
+
if vertex_of_min_distance != None:
|
|
166
|
+
adjacent_list = self.__get_specific_adjacent_list__(vertex_of_min_distance, graph)
|
|
167
|
+
for vert in adjacent_list:
|
|
168
|
+
index_of_adj_vertex = v_to_idx.get(vert.vertex)
|
|
169
|
+
if index_of_adj_vertex != None and explored[index_of_adj_vertex]:
|
|
170
|
+
continue
|
|
171
|
+
if index_of_adj_vertex != None and dist_from_source[index_of_adj_vertex] > dist_from_source[index_min] + vert.distance_from_source:
|
|
172
|
+
dist_from_source[index_of_adj_vertex] = dist_from_source[index_min] + vert.distance_from_source
|
|
173
|
+
explored[index_min] = True
|
|
174
|
+
return dist_from_source
|
|
175
|
+
|
|
176
|
+
def __create_adjacency_lists__(self, weight_to_select: int = 0) -> list[MultiWeightedGraph.__VertexNeighbours__]:
|
|
177
|
+
main_list: list[MultiWeightedGraph.__VertexNeighbours__] = []
|
|
178
|
+
for vertex in self.vertices:
|
|
179
|
+
adj: list[MultiWeightedGraph.__Vertex__] = []
|
|
180
|
+
for edge in self.edges:
|
|
181
|
+
if edge.vertex_a == vertex:
|
|
182
|
+
new_vertex = MultiWeightedGraph.__Vertex__(edge.vertex_b, edge.weights[weight_to_select])
|
|
183
|
+
adj.append(new_vertex)
|
|
184
|
+
elif edge.vertex_b == vertex:
|
|
185
|
+
new_vertex = MultiWeightedGraph.__Vertex__(edge.vertex_a, edge.weights[weight_to_select])
|
|
186
|
+
adj.append(new_vertex)
|
|
187
|
+
vertex_neighbourhood = MultiWeightedGraph.__VertexNeighbours__(vertex, adj)
|
|
188
|
+
main_list.append(vertex_neighbourhood)
|
|
189
|
+
return main_list
|
|
190
|
+
|
|
191
|
+
def __get_specific_adjacent_list__(self, vertex: str, graph: list[MultiWeightedGraph.__VertexNeighbours__]) -> list[MultiWeightedGraph.__Vertex__]:
|
|
192
|
+
for v_ne in graph:
|
|
193
|
+
if v_ne.vertex == vertex:
|
|
194
|
+
return v_ne.neighbourhood
|
|
195
|
+
return []
|
|
196
|
+
|
|
197
|
+
def __initialize_distance_vector__(self, vertex: str, weight_to_select: int = 0) -> list[float]:
|
|
198
|
+
n = len(self.vertices)
|
|
199
|
+
v_to_idx = {v: i for i, v in enumerate(self.vertices)}
|
|
200
|
+
inf = float("inf")
|
|
201
|
+
dist_from_source = [inf * n for _ in range(n)]
|
|
202
|
+
for i in range(n):
|
|
203
|
+
if i == v_to_idx.get(vertex):
|
|
204
|
+
dist_from_source[i] = 0
|
|
205
|
+
else:
|
|
206
|
+
dist_from_source[i] = inf
|
|
207
|
+
for edge in self.edges:
|
|
208
|
+
a = v_to_idx.get(edge.vertex_a)
|
|
209
|
+
b = v_to_idx.get(edge.vertex_b)
|
|
210
|
+
if edge.vertex_a == vertex and b != None:
|
|
211
|
+
dist_from_source[b] = edge.weights[weight_to_select]
|
|
212
|
+
elif edge.vertex_b == vertex and a != None:
|
|
213
|
+
dist_from_source[a] = edge.weights[weight_to_select]
|
|
214
|
+
return dist_from_source
|
|
215
|
+
|
|
216
|
+
def __get_min_distance_vertex_index__(self, explored_vertices: list, distances_from_source: list[float]) -> int:
|
|
217
|
+
min_dist = float("inf")
|
|
218
|
+
index = 0
|
|
219
|
+
for i, dist in enumerate(distances_from_source):
|
|
220
|
+
if dist < min_dist and explored_vertices[i] == False:
|
|
221
|
+
min_dist = dist
|
|
222
|
+
index = i
|
|
223
|
+
return index
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
from multi_weighted_graph import MultiWeightedGraph
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def test_add_edges():
|
|
5
|
+
my_graph = MultiWeightedGraph()
|
|
6
|
+
my_graph.add_edge("a", "b", [2])
|
|
7
|
+
assert len(my_graph.edges) == 1
|
|
8
|
+
|
|
9
|
+
def test_remove_edges():
|
|
10
|
+
my_graph = MultiWeightedGraph()
|
|
11
|
+
my_graph.add_edge("a", "b", [2])
|
|
12
|
+
my_graph.add_edge("c", "d", [2])
|
|
13
|
+
my_graph.remove_edge("a", "b")
|
|
14
|
+
assert len(my_graph.edges) == 1
|
|
15
|
+
|
|
16
|
+
def test_minimal_spanning_tree_1():
|
|
17
|
+
my_graph = MultiWeightedGraph()
|
|
18
|
+
my_graph.add_edge("a", "b", [2])
|
|
19
|
+
my_graph.add_edge("c", "d", [2])
|
|
20
|
+
my_graph.add_edge("a", "d", [2])
|
|
21
|
+
my_graph.add_edge("a", "c", [3])
|
|
22
|
+
spanning_tree = my_graph.minimal_spanning_tree(0)
|
|
23
|
+
total_weight = 0
|
|
24
|
+
for edge in spanning_tree:
|
|
25
|
+
total_weight += edge.weights[0]
|
|
26
|
+
assert total_weight == 6
|
|
27
|
+
|
|
28
|
+
def test_minimal_spanning_tree_2():
|
|
29
|
+
my_graph = MultiWeightedGraph("", 2)
|
|
30
|
+
my_graph.add_edge("a", "b", [4, 2])
|
|
31
|
+
my_graph.add_edge("b", "c", [3, 2])
|
|
32
|
+
my_graph.add_edge("c", "d", [2, 2])
|
|
33
|
+
my_graph.add_edge("a", "d", [1, 2])
|
|
34
|
+
spanning_tree_first_weight = my_graph.minimal_spanning_tree(0)
|
|
35
|
+
spanning_tree_second_weight = my_graph.minimal_spanning_tree(1)
|
|
36
|
+
total_weight_1 = 0
|
|
37
|
+
total_weight_2 = 0
|
|
38
|
+
for edge in spanning_tree_first_weight:
|
|
39
|
+
total_weight_1 += edge.weights[0]
|
|
40
|
+
for edge in spanning_tree_second_weight:
|
|
41
|
+
total_weight_2 += edge.weights[1]
|
|
42
|
+
assert total_weight_1 == total_weight_2
|
|
43
|
+
|
|
44
|
+
def test_matrix_of_distances():
|
|
45
|
+
my_graph = MultiWeightedGraph("", 2)
|
|
46
|
+
my_graph.add_edge("a", "b", [4, 2])
|
|
47
|
+
my_graph.add_edge("b", "c", [3, 2])
|
|
48
|
+
my_graph.add_edge("c", "d", [2, 2])
|
|
49
|
+
my_graph.add_edge("a", "d", [1, 2])
|
|
50
|
+
v_to_idx = {v: i for i, v in enumerate(my_graph.vertices)}
|
|
51
|
+
matrix_0 = my_graph.matrix_of_distances(0)
|
|
52
|
+
matrix_1 = my_graph.matrix_of_distances(1)
|
|
53
|
+
b = v_to_idx.get("b")
|
|
54
|
+
d = v_to_idx.get("d")
|
|
55
|
+
if b != None and d != None:
|
|
56
|
+
assert matrix_0[b][d] == 5
|
|
57
|
+
assert matrix_1[d][b] == 4
|
|
58
|
+
|
|
59
|
+
def test_matrix_of_distances_from_source():
|
|
60
|
+
my_graph = MultiWeightedGraph("", 2)
|
|
61
|
+
my_graph.add_edge("a", "b", [4, 2])
|
|
62
|
+
my_graph.add_edge("b", "c", [3, 2])
|
|
63
|
+
my_graph.add_edge("c", "d", [2, 2])
|
|
64
|
+
my_graph.add_edge("a", "d", [1, 2])
|
|
65
|
+
v_to_idx = {v: i for i, v in enumerate(my_graph.vertices)}
|
|
66
|
+
vector_a = my_graph.matrix_of_distances_from_source("a", 0)
|
|
67
|
+
vector_c = my_graph.matrix_of_distances_from_source("c", 1)
|
|
68
|
+
vector_b = my_graph.matrix_of_distances_from_source("b", 0)
|
|
69
|
+
vector_d = my_graph.matrix_of_distances_from_source("d", 0)
|
|
70
|
+
a = v_to_idx.get("a")
|
|
71
|
+
b = v_to_idx.get("b")
|
|
72
|
+
c = v_to_idx.get("c")
|
|
73
|
+
d = v_to_idx.get("d")
|
|
74
|
+
print(v_to_idx)
|
|
75
|
+
if a != None and c != None and b != None and d != None:
|
|
76
|
+
assert vector_a[c] == 3
|
|
77
|
+
assert vector_c[a] == 4
|
|
78
|
+
assert vector_b[d] == 5
|
|
79
|
+
assert vector_d[b] == 5
|