fix shortestPath function

shortestPath function now converts all dict keys to ints
This commit is contained in:
Jacob 2019-03-26 18:14:22 +01:00
parent 9422d6f45f
commit 0521035af3
10 changed files with 16 additions and 6 deletions

Binary file not shown.

Binary file not shown.

View File

@ -3,6 +3,7 @@ from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt from django.views.decorators.csrf import csrf_exempt
import json import json
import math
# Create your views here. # Create your views here.
@ -15,11 +16,20 @@ def index(request):
def classify(request): def classify(request):
return HttpResponse(json.load(request)) return HttpResponse(json.load(request))
@csrf_exempt
def shortestPath(request): def shortestPath(request):
graph = json.load(request)["graph"] loaded_request = json.load(request)
start_node = json.load(request)["start_node"]
dest_node = json.load(request)["dest_node"] graph = loaded_request["graph"]
graph = {int(k): v for k, v in graph.items()}
for node in graph:
graph[node] = {
int(k): v for k, v in graph[node].items()}
start_node = loaded_request["start_node"]
dest_node = loaded_request["dest_node"]
distance = {} distance = {}
predecessor = {} predecessor = {}
path = {} path = {}
@ -47,8 +57,8 @@ def shortestPath(request):
current = node current = node
p = [current + 1] p = [current + 1]
while current != start_node: while current != start_node:
p.append(predecessor[current] + 1) p.append(predecessor[current])
current = predecessor[current] current = predecessor[current]
path[node] = p[::-1] path[node] = p[::-1]
return HttpResponse(path[dest_node]) return HttpResponse(path[dest_node])