Skip to content

Commit 84b6600

Browse files
committed
Renamed directory Pathing to Pathfinding
1 parent 6e48862 commit 84b6600

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

pygorithm/pathfinding/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""
2+
Collection of pathfinding examples
3+
"""
4+
from . import dijkstra
5+
6+
__all__ = [
7+
'dijkstra'
8+
]

pygorithm/pathfinding/dijkstra.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import heapq
2+
class Dijkstra(object):
3+
"""Dijkstra object
4+
Finds the optimal path between two nodes on
5+
a graph."""
6+
7+
def __init__(self):
8+
pass
9+
10+
def reverse_path(self, node):
11+
result = []
12+
while node is not None:
13+
result.insert(0, node['vertex'])
14+
node = node['parent']
15+
return result
16+
17+
def find_path(self, graph, start, end):
18+
"""
19+
Calculates the optimal path from start to end
20+
on the graph. Weights are ignored.
21+
22+
:param graph: object contains `graphs` as per pygorithm.data_structures.WeightedUndirectedGraph
23+
weights are ignored.
24+
:param start: the start vertex (which is the same type of the verticies in the graph)
25+
:param end: the end vertex (which is the same type of the vertices in the graph)
26+
:return: a list starting with `start` and ending with `end`, or None if no path is possible.
27+
"""
28+
29+
open = []
30+
closed = set()
31+
32+
# the first element in the tuple is the distance from the source. This is used as the primary
33+
# key for sorting. The second element in the tuple is just a counter and is used to avoid having
34+
# to hash the dictionary when the distance from the source is not unique.
35+
36+
counter = 0
37+
heapq.heappush(open, (0, counter, { 'vertex': start, 'parent': None }))
38+
counter += 1
39+
40+
while len(open) > 0:
41+
current = heapq.heappop(open)
42+
closed.update(current[2]['vertex'])
43+
44+
if current[2]['vertex'] == end:
45+
return self.reverse_path(current[2])
46+
47+
neighbors = graph.graph[current[2]['vertex']]
48+
for neighbor in neighbors:
49+
if neighbor not in closed:
50+
heapq.heappush(open, (current[0] + 1, counter, { 'vertex': neighbor, 'parent': current[2] }))
51+
counter += 1
52+
53+
54+
return None
55+
56+
@staticmethod
57+
def get_code(self):
58+
"""
59+
returns the code for the current class
60+
"""
61+
return inspect.getsource(Dijkstra)

pygorithm/pathfinding/modules.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import pkgutil
2+
3+
4+
def modules():
5+
"""
6+
Find all functions in pygorithm.pathfinding
7+
"""
8+
import pygorithm.pathfinding
9+
package = pygorithm.pathfinding
10+
modules = []
11+
for importer, modname, ispkg in pkgutil.iter_modules(package.__path__):
12+
modules.append(modname)
13+
modules.remove('modules')
14+
modules.sort()
15+
return modules

0 commit comments

Comments
 (0)