From 77408618873c9ed5ad13aec074159b5cd02fefc2 Mon Sep 17 00:00:00 2001
From: Jacob Theisen <jacobth@stud.ntnu.no>
Date: Mon, 22 Nov 2021 13:05:25 +0100
Subject: [PATCH] sjo

---
 ov9/program1.py | 58 +++++++++++++++++++++++++------------------------
 1 file changed, 30 insertions(+), 28 deletions(-)

diff --git a/ov9/program1.py b/ov9/program1.py
index f399d77..98820fe 100644
--- a/ov9/program1.py
+++ b/ov9/program1.py
@@ -75,22 +75,25 @@ def init_table():
         res_arr.append(results(i, inf, False, None))
 
 def rec_func(node, counter, global_fra):
+    print('in recfunc')
     counter += node.time
+    print(f'node: {node.node}, node_time: {node.time}, time_count: {counter}')
     if node.node == global_fra:
         return counter
     else:
+        print(node.prev)
         node = [a for a in res_arr if a.node == node.prev][0]
         rec_func(node, counter, global_fra)
 
-def calculate_shortest_path(b, i, a, global_fra):
-    print('in calculate')
-    res_a = [a for a in res_arr if a == a.node]
-    print(f'curr_node: {b.node}, edge_time: {i}, curr_neig_node: {a.node}, curr_neig_prev: {a.prev}, global_fra: {global_fra}')
+def calculate_shortest_path(current_node, time, current_neighbour, slutt):
+    print('\nin calculate')
+    res_a = [c for c in res_arr if c.node == current_neighbour.node][0]
+    print(f'curr_node: {current_node.node}, edge_time: {time}, curr_neig_node: {current_neighbour.node}, curr_neig_prev: {current_neighbour.prev}, dist to curr_neig: {res_a.time}, global_fra: {slutt}')
     count_a = 0
-    count_a = rec_func(a, count_a, global_fra)
+    count_a = rec_func(current_neighbour, count_a, slutt)
     count_b = 0
-    count_b = rec_func(b, count_b, global_fra)
-    count_b += i
+    count_b = rec_func(current_node, count_b, slutt)
+    count_b += time
     if count_b < count_a:
         return True
     else: 
@@ -102,32 +105,31 @@ queue = []
 def dijkstras(slutt, fra, start):
     global round
     round +=1 
-    print('runde: ', round, ' queue: ', queue)
+    print('runde: ', round, ' queue: ', queue, 'start: ', start)
     current_node = [b for b in res_arr if b.node == fra][0]
     print(current_node.node)
     for tmp_edge in edge_arr:
         if tmp_edge.fra == fra:
             current_neighbour = [a for a in res_arr if a.node == tmp_edge.til][0]
-            if current_neighbour:
-                if current_neighbour.visited == False:
-                    print(f'fra: {fra}, a: {current_neighbour.visited}, til  {tmp_edge.til}')
-                    if start == fra: 
-                        if tmp_edge.time < current_neighbour.time:
-                            current_neighbour.time = tmp_edge.time
-                            current_neighbour.prev = fra 
-                    if tmp_edge.til not in queue:
-                        if tmp_edge.til == slutt:
-                            current_node.visited = True
-                            queue.pop(0)
-                            dijkstras(start, slutt, slutt)
-                        else:
-                            queue.append(tmp_edge.til)
-
-                elif current_neighbour.visited == True and current_neighbour.node != start:
-                    print(current_node.node, tmp_edge.time, current_neighbour.node, start)
-                    if calculate_shortest_path(current_node, tmp_edge.time, current_neighbour, start):
+            if current_neighbour.visited == False:
+                print(f'fra: {fra}, a: {current_neighbour.visited}, til  {tmp_edge.til}')
+                if start == fra: 
+                    if tmp_edge.time < current_neighbour.time:
                         current_neighbour.time = tmp_edge.time
-                        current_neighbour.prev = tmp_edge.fra
+                        current_neighbour.prev = fra 
+                if tmp_edge.til not in queue:
+                    if tmp_edge.til == slutt:
+                        current_node.visited = True
+                        queue.pop(0)
+                        dijkstras(slutt, slutt, start)
+                    else:
+                        queue.append(tmp_edge.til)
+
+            elif current_neighbour.visited == True and current_neighbour.node != start:
+                print(current_node.node, tmp_edge.time, current_neighbour.node, start)
+                if calculate_shortest_path(current_node, tmp_edge.time, current_neighbour, start):
+                    current_neighbour.time = tmp_edge.time
+                    current_neighbour.prev = tmp_edge.fra
 
     if fra == slutt: 
         #not finished
@@ -140,7 +142,7 @@ def dijkstras(slutt, fra, start):
     else:
         queue.pop(0)
         current_node.visited = True
-        dijkstras(start,queue[0],slutt)
+        dijkstras(slutt,queue[0],start)
              
 
 
-- 
GitLab