graphs-shaddad 0.0.1__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.
- graphs_shaddad-0.0.1/.gitignore +8 -0
- graphs_shaddad-0.0.1/LICENSE +19 -0
- graphs_shaddad-0.0.1/PKG-INFO +68 -0
- graphs_shaddad-0.0.1/README.md +54 -0
- graphs_shaddad-0.0.1/pyproject.toml +26 -0
- graphs_shaddad-0.0.1/src/graphs_shaddad/__init__.py +0 -0
- graphs_shaddad-0.0.1/src/graphs_shaddad/sp.py +84 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright (c) 2026 The Python Packaging Authority
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
|
11
|
+
copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
+
SOFTWARE.
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: graphs_shaddad
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: A graphing algorithm package
|
|
5
|
+
Project-URL: Homepage, https://github.com/samhaddad2005/Open-Source-Project
|
|
6
|
+
Project-URL: Issues, https://github.com/samhaddad2005/Open-Source-Project/issues
|
|
7
|
+
Author-email: Sam Haddad <samhaddad@gmail.com>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Requires-Python: >=3.9
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
|
|
15
|
+
# Open-Source-Project
|
|
16
|
+
This repository aims to act as a open source software that contains many graphing algorithms. Among said graphs, Dijkstra's algorithm — an algorithm that aims to solve the problem of finding the shortest paths between nodes in a graph. — is found under src/graphs_shaddad.
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
```
|
|
20
|
+
pip install graphs_shaddad
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Dijkstra's Algorithm
|
|
24
|
+
The graph is a dictionary mapping each node to a dictionary of `{neighbor: weight}`.
|
|
25
|
+
```python
|
|
26
|
+
from graphs_shaddad.sp import dijkstra
|
|
27
|
+
|
|
28
|
+
graph = {
|
|
29
|
+
0: {1: 4, 2: 8},
|
|
30
|
+
1: {0: 4, 4: 6, 2: 3},
|
|
31
|
+
2: {0: 8, 3: 2, 1: 3},
|
|
32
|
+
3: {2: 2, 4: 10},
|
|
33
|
+
4: {1: 6, 3: 10},
|
|
34
|
+
}
|
|
35
|
+
dist, path = dijkstra(graph, 0)
|
|
36
|
+
print(dist) # {0: 0, 1: 4, 2: 7, 3: 9, 4: 10}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
<img width="400" height="324" alt="image" src="https://github.com/user-attachments/assets/82cb811e-dde9-4aed-b81e-7de8bbef569f" />
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
Explanation of the shortest paths:
|
|
44
|
+
- 0 -> 0 = 0: Source node itself, so distance is 0.
|
|
45
|
+
- 0 -> 1 = 4: Direct edge from node 0 to 1 gives shortest distance 4.
|
|
46
|
+
- 0 -> 2 = 7: Path 0 → 1 → 2 gives total cost 4 + 3 = 7, which is smaller than direct edge 8.
|
|
47
|
+
- 0 -> 3 = 9: Path 0 → 1 → 2 → 3 gives total cost 4 + 3 + 2 = 9.
|
|
48
|
+
- 0 -> 4 = 10: Path 0 → 1 → 4 gives total cost 4 + 6 = 10.
|
|
49
|
+
|
|
50
|
+
## Prim's Algorithm for Minimum Spanning Tree (MST)
|
|
51
|
+
```python
|
|
52
|
+
from graphs_shaddad.sp import Graph
|
|
53
|
+
|
|
54
|
+
g = Graph(5)
|
|
55
|
+
g.graph = [[0, 2, 0, 6, 0],
|
|
56
|
+
[2, 0, 3, 8, 5],
|
|
57
|
+
[0, 3, 0, 0, 7],
|
|
58
|
+
[6, 8, 0, 0, 9],
|
|
59
|
+
[0, 5, 7, 9, 0]]
|
|
60
|
+
g.primMST() # prints each MST edge and its weight
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Prim’s algorithm is a Greedy algorithm like Kruskal's algorithm. This algorithm always starts with a single node and moves through several adjacent nodes, in order to explore all of the connected edges along the way.
|
|
64
|
+
|
|
65
|
+
The algorithm starts with an empty spanning tree.
|
|
66
|
+
The idea is to maintain two sets of vertices. The first set contains the vertices already included in the MST, and the other set contains the vertices not yet included.
|
|
67
|
+
At every step, it considers all the edges that connect the two sets and picks the minimum weight edge from these edges. After picking the edge, it moves the other endpoint of the edge to the set containing MST.
|
|
68
|
+
<img width="762" height="442" alt="image" src="https://github.com/user-attachments/assets/153ffc51-beac-4c83-bd24-c87062116569" />
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# Open-Source-Project
|
|
2
|
+
This repository aims to act as a open source software that contains many graphing algorithms. Among said graphs, Dijkstra's algorithm — an algorithm that aims to solve the problem of finding the shortest paths between nodes in a graph. — is found under src/graphs_shaddad.
|
|
3
|
+
|
|
4
|
+
## Installation
|
|
5
|
+
```
|
|
6
|
+
pip install graphs_shaddad
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## Dijkstra's Algorithm
|
|
10
|
+
The graph is a dictionary mapping each node to a dictionary of `{neighbor: weight}`.
|
|
11
|
+
```python
|
|
12
|
+
from graphs_shaddad.sp import dijkstra
|
|
13
|
+
|
|
14
|
+
graph = {
|
|
15
|
+
0: {1: 4, 2: 8},
|
|
16
|
+
1: {0: 4, 4: 6, 2: 3},
|
|
17
|
+
2: {0: 8, 3: 2, 1: 3},
|
|
18
|
+
3: {2: 2, 4: 10},
|
|
19
|
+
4: {1: 6, 3: 10},
|
|
20
|
+
}
|
|
21
|
+
dist, path = dijkstra(graph, 0)
|
|
22
|
+
print(dist) # {0: 0, 1: 4, 2: 7, 3: 9, 4: 10}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
<img width="400" height="324" alt="image" src="https://github.com/user-attachments/assets/82cb811e-dde9-4aed-b81e-7de8bbef569f" />
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
Explanation of the shortest paths:
|
|
30
|
+
- 0 -> 0 = 0: Source node itself, so distance is 0.
|
|
31
|
+
- 0 -> 1 = 4: Direct edge from node 0 to 1 gives shortest distance 4.
|
|
32
|
+
- 0 -> 2 = 7: Path 0 → 1 → 2 gives total cost 4 + 3 = 7, which is smaller than direct edge 8.
|
|
33
|
+
- 0 -> 3 = 9: Path 0 → 1 → 2 → 3 gives total cost 4 + 3 + 2 = 9.
|
|
34
|
+
- 0 -> 4 = 10: Path 0 → 1 → 4 gives total cost 4 + 6 = 10.
|
|
35
|
+
|
|
36
|
+
## Prim's Algorithm for Minimum Spanning Tree (MST)
|
|
37
|
+
```python
|
|
38
|
+
from graphs_shaddad.sp import Graph
|
|
39
|
+
|
|
40
|
+
g = Graph(5)
|
|
41
|
+
g.graph = [[0, 2, 0, 6, 0],
|
|
42
|
+
[2, 0, 3, 8, 5],
|
|
43
|
+
[0, 3, 0, 0, 7],
|
|
44
|
+
[6, 8, 0, 0, 9],
|
|
45
|
+
[0, 5, 7, 9, 0]]
|
|
46
|
+
g.primMST() # prints each MST edge and its weight
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Prim’s algorithm is a Greedy algorithm like Kruskal's algorithm. This algorithm always starts with a single node and moves through several adjacent nodes, in order to explore all of the connected edges along the way.
|
|
50
|
+
|
|
51
|
+
The algorithm starts with an empty spanning tree.
|
|
52
|
+
The idea is to maintain two sets of vertices. The first set contains the vertices already included in the MST, and the other set contains the vertices not yet included.
|
|
53
|
+
At every step, it considers all the edges that connect the two sets and picks the minimum weight edge from these edges. After picking the edge, it moves the other endpoint of the edge to the set containing MST.
|
|
54
|
+
<img width="762" height="442" alt="image" src="https://github.com/user-attachments/assets/153ffc51-beac-4c83-bd24-c87062116569" />
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling >= 1.26"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "graphs_shaddad"
|
|
7
|
+
version = "0.0.1"
|
|
8
|
+
authors = [
|
|
9
|
+
{ name="Sam Haddad", email="samhaddad@gmail.com" },
|
|
10
|
+
]
|
|
11
|
+
description = "A graphing algorithm package"
|
|
12
|
+
readme = "README.md"
|
|
13
|
+
requires-python = ">=3.9"
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Programming Language :: Python :: 3",
|
|
16
|
+
"Operating System :: OS Independent",
|
|
17
|
+
]
|
|
18
|
+
license = "MIT"
|
|
19
|
+
license-files = ["LICEN[CS]E*"]
|
|
20
|
+
|
|
21
|
+
[project.urls]
|
|
22
|
+
Homepage = "https://github.com/samhaddad2005/Open-Source-Project"
|
|
23
|
+
Issues = "https://github.com/samhaddad2005/Open-Source-Project/issues"
|
|
24
|
+
|
|
25
|
+
[tool.hatch.build.targets.sdist]
|
|
26
|
+
include = ["src/", "README.md", "LICENSE"]
|
|
File without changes
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
from heapq import heappush, heappop
|
|
3
|
+
|
|
4
|
+
def dijkstra(graph, source):
|
|
5
|
+
dist = {node: sys.maxsize for node in graph}
|
|
6
|
+
dist[source] = 0
|
|
7
|
+
heap = []
|
|
8
|
+
heappush(heap, (0, source))
|
|
9
|
+
path = {source: []}
|
|
10
|
+
|
|
11
|
+
while heap:
|
|
12
|
+
w, u = heappop(heap)
|
|
13
|
+
for v in graph[u]:
|
|
14
|
+
if w + graph[u][v] < dist[v]:
|
|
15
|
+
dist[v] = w + graph[u][v]
|
|
16
|
+
heappush(heap, (dist[v], v))
|
|
17
|
+
path[v] = path[u] + [u]
|
|
18
|
+
|
|
19
|
+
return dist, path
|
|
20
|
+
class Graph:
|
|
21
|
+
def __init__(self, vertices):
|
|
22
|
+
self.V = vertices
|
|
23
|
+
self.graph = [[0 for column in range(vertices)] for row in range(vertices)]
|
|
24
|
+
|
|
25
|
+
def printMST(self, parent):
|
|
26
|
+
print("Edge \tWeight")
|
|
27
|
+
for i in range(1, self.V):
|
|
28
|
+
print(parent[i], "-", i, "\t", self.graph[parent[i]][i])
|
|
29
|
+
|
|
30
|
+
# A utility function to find the vertex with
|
|
31
|
+
# minimum distance value, from the set of vertices
|
|
32
|
+
# not yet included in shortest path tree
|
|
33
|
+
def minKey(self, key, mstSet):
|
|
34
|
+
|
|
35
|
+
# Initialize min value
|
|
36
|
+
min = sys.maxsize
|
|
37
|
+
|
|
38
|
+
for v in range(self.V):
|
|
39
|
+
if key[v] < min and mstSet[v] == False:
|
|
40
|
+
min = key[v]
|
|
41
|
+
min_index = v
|
|
42
|
+
|
|
43
|
+
return min_index
|
|
44
|
+
|
|
45
|
+
# Function to construct and print MST for a graph
|
|
46
|
+
# represented using adjacency matrix representation
|
|
47
|
+
def primMST(self):
|
|
48
|
+
|
|
49
|
+
# Key values used to pick minimum weight edge in cut
|
|
50
|
+
key = [sys.maxsize] * self.V
|
|
51
|
+
|
|
52
|
+
parent = [None] * self.V # Array to store constructed MST
|
|
53
|
+
|
|
54
|
+
# Make key 0 so that this vertex is picked as first vertex
|
|
55
|
+
key[0] = 0
|
|
56
|
+
|
|
57
|
+
mstSet = [False] * self.V
|
|
58
|
+
|
|
59
|
+
parent[0] = -1 # First node is always the root of
|
|
60
|
+
|
|
61
|
+
for cout in range(self.V):
|
|
62
|
+
u = self.minKey(key, mstSet)
|
|
63
|
+
|
|
64
|
+
mstSet[u] = True
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
for v in range(self.V):
|
|
68
|
+
if self.graph[u][v] > 0 and mstSet[v] == False \
|
|
69
|
+
and key[v] > self.graph[u][v]:
|
|
70
|
+
key[v] = self.graph[u][v]
|
|
71
|
+
parent[v] = u
|
|
72
|
+
|
|
73
|
+
self.printMST(parent)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
if __name__ == '__main__':
|
|
77
|
+
g = Graph(5)
|
|
78
|
+
g.graph = [[0, 2, 0, 6, 0],
|
|
79
|
+
[2, 0, 3, 8, 5],
|
|
80
|
+
[0, 3, 0, 0, 7],
|
|
81
|
+
[6, 8, 0, 0, 9],
|
|
82
|
+
[0, 5, 7, 9, 0]]
|
|
83
|
+
|
|
84
|
+
g.primMST()
|