diff --git a/SZI1/__pycache__/__init__.cpython-37.pyc b/SZI1/__pycache__/__init__.cpython-37.pyc index 290e05d..796498f 100644 Binary files a/SZI1/__pycache__/__init__.cpython-37.pyc and b/SZI1/__pycache__/__init__.cpython-37.pyc differ diff --git a/SZI1/__pycache__/settings.cpython-37.pyc b/SZI1/__pycache__/settings.cpython-37.pyc index b75e1d9..6290847 100644 Binary files a/SZI1/__pycache__/settings.cpython-37.pyc and b/SZI1/__pycache__/settings.cpython-37.pyc differ diff --git a/SZI1/__pycache__/urls.cpython-37.pyc b/SZI1/__pycache__/urls.cpython-37.pyc index fcabce6..29f6986 100644 Binary files a/SZI1/__pycache__/urls.cpython-37.pyc and b/SZI1/__pycache__/urls.cpython-37.pyc differ diff --git a/SZI1/__pycache__/wsgi.cpython-37.pyc b/SZI1/__pycache__/wsgi.cpython-37.pyc index 07660ed..2100faa 100644 Binary files a/SZI1/__pycache__/wsgi.cpython-37.pyc and b/SZI1/__pycache__/wsgi.cpython-37.pyc differ diff --git a/magazine/__pycache__/__init__.cpython-37.pyc b/magazine/__pycache__/__init__.cpython-37.pyc index 9cfab94..bb759d9 100644 Binary files a/magazine/__pycache__/__init__.cpython-37.pyc and b/magazine/__pycache__/__init__.cpython-37.pyc differ diff --git a/magazine/__pycache__/admin.cpython-37.pyc b/magazine/__pycache__/admin.cpython-37.pyc index 576760e..83567c8 100644 Binary files a/magazine/__pycache__/admin.cpython-37.pyc and b/magazine/__pycache__/admin.cpython-37.pyc differ diff --git a/magazine/__pycache__/urls.cpython-37.pyc b/magazine/__pycache__/urls.cpython-37.pyc index 49513de..c6cc7f9 100644 Binary files a/magazine/__pycache__/urls.cpython-37.pyc and b/magazine/__pycache__/urls.cpython-37.pyc differ diff --git a/magazine/__pycache__/views.cpython-37.pyc b/magazine/__pycache__/views.cpython-37.pyc index d8cabd2..b0ebbdf 100644 Binary files a/magazine/__pycache__/views.cpython-37.pyc and b/magazine/__pycache__/views.cpython-37.pyc differ diff --git a/magazine/migrations/__pycache__/__init__.cpython-37.pyc b/magazine/migrations/__pycache__/__init__.cpython-37.pyc index 8dff860..f510028 100644 Binary files a/magazine/migrations/__pycache__/__init__.cpython-37.pyc and b/magazine/migrations/__pycache__/__init__.cpython-37.pyc differ diff --git a/magazine/urls.py b/magazine/urls.py index 5da255f..543b64a 100644 --- a/magazine/urls.py +++ b/magazine/urls.py @@ -5,4 +5,5 @@ from . import views urlpatterns = [ path('', TemplateView.as_view(template_name='magazine/index.html')), path('classify', views.classify, name='classify'), + path('shortestPath', views.shortestPath, name='shortestPath'), ] diff --git a/magazine/views.py b/magazine/views.py index 5961bd5..d756395 100644 --- a/magazine/views.py +++ b/magazine/views.py @@ -14,3 +14,41 @@ def index(request): @csrf_exempt def classify(request): return HttpResponse(json.load(request)) + +def shortestPath(request): + graph = json.load(request)["graph"] + start_node = json.load(request)["start_node"] + dest_node = json.load(request)["dest_node"] + + distance = {} + predecessor = {} + path = {} + unseen_nodes = graph.copy() + + for node in graph: + distance[node] = math.inf + distance[start_node] = 0 + + while unseen_nodes: + min_node = None + for node in unseen_nodes: + if min_node is None: + min_node = node + elif distance[node] < distance[min_node]: + min_node = node + + for childNode, weight in graph[min_node].items(): + if distance[min_node] + weight < distance[childNode]: + distance[childNode] = distance[min_node] + weight + predecessor[childNode] = min_node + unseen_nodes.pop(min_node) + + for node in graph: + current = node + p = [current + 1] + while current != start_node: + p.append(predecessor[current] + 1) + current = predecessor[current] + path[node] = p[::-1] + + return HttpResponse(path[dest_node]) \ No newline at end of file