Просмотр исходного кода

moving db logs to file, printing SSSP to show closer to spec

tarfeef101 6 лет назад
Родитель
Сommit
6d8b903cd1
1 измененных файлов с 10 добавлено и 5 удалено
  1. 10 5
      a3/router.py

+ 10 - 5
a3/router.py

@@ -65,7 +65,7 @@ class connection:
         this.cost = cost
         
     def show(this):
-        print("Link: " + str(this.sender) + " " + str(this.src) + " " + str(this.link) + " " + str(this.cost))
+        log.write("Link: " + str(this.sender) + " " + str(this.src) + " " + str(this.link) + " " + str(this.cost))
         
     # infers what this connection's (link's) destination is
     def infer(this, db):
@@ -89,6 +89,7 @@ class db:
         this.entries = [ [] for i in range(routers) ]
         
     def show(this):
+        log.write("Topology DB: " + '\n')
         for i in this.entries:
             for j in i:
                 j.show()
@@ -120,7 +121,7 @@ class db:
 class graph:
     def __init__(this, routers):
         # stores shortest paths
-        this.sssp = [inf] * routers
+        this.sssp = [(inf, 0)] * routers
         # stores adjacency list (Graph)
         this.alist = [ [-1, -1, -1, -1, -1] for i in range(routers) ]
         
@@ -140,8 +141,8 @@ class graph:
         this.alist[dest - 1][src - 1] = weight
             
     def rebuild(this):
-        this.sssp = [inf] * 5
-        this.sssp[rid - 1] = 0
+        this.sssp = [(inf, 0)] * 5
+        this.sssp[rid - 1] = (0, rid - 1)
         
         curnode = rid - 1
         unvisited = [1] * 5
@@ -158,7 +159,11 @@ class graph:
                 # visited already
                 if (unvisited[i] == 0):
                     continue
-                this.sssp[i] = min((this.sssp[curnode] + this.alist[curnode][i]), this.sssp[i])
+                result = min((this.sssp[curnode] + this.alist[curnode][i]), this.sssp[i])
+                if (result != this.sssp[i][0]):
+                    this.sssp[i] = (result, curnode)
+                else:
+                    this.sssp[i] = (result, this.sssp[i][1]
                 
             unvisited[curnode] = 0
             # if we visited all, we're done