Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.23 KB | None | 0 0
  1. from sys import stdin, stderr
  2. from collections import deque
  3. from copy import copy
  4.  
  5.  
  6.  
  7. def beste_sti(nm, prob):
  8.        
  9.     cProb = [0]*len(nm)   #Current probability at each node
  10.     done = []             # Done nodes
  11.     pi=[-1]*len(nm)     #Predecessor array  
  12.    
  13.     cProb[0] = prob[0]
  14.      
  15.    
  16.     Q = [0]  #Available nodes.
  17.        
  18.          
  19.     while Q:
  20.       print "Current Probability: ",cProb
  21.       print "Parent: ",pi
  22.       print "Q: ",Q
  23.       print "Done: ", done
  24.      
  25.       #Finds the node with max probability
  26.       # Picks highest node from Q that is not finished.
  27.      
  28.       maxNode = -1      
  29.       for i in range(len(Q)):
  30.         u = Q[i]
  31.         if cProb[u] > cProb[maxNode]:
  32.           maxNode = u
  33.      
  34.       if maxNode == -1:
  35.         return
  36.      
  37.       print "maxNode: ", maxNode
  38.       print ""
  39.        
  40.       Q.remove(maxNode)
  41.       done.append(maxNode)
  42.      
  43.       for nabo in range(len(nm)):
  44.         isNabo = nm[maxNode][nabo]
  45.        
  46.         #Just adds the nabo to Q if it isnt done
  47.         if isNabo == 1:
  48.           if not nabo in Q and not nabo in done:
  49.             Q.append(nabo)
  50.         #Gives new probability if possible
  51.           if cProb[maxNode]*prob[nabo] > cProb[nabo]:
  52.             cProb[nabo] = cProb[maxNode]*prob[nabo]
  53.             pi[nabo] = maxNode
  54.    
  55.     streng = ""              
  56.    
  57.     #Find the path from start to stop.
  58.    
  59.     curr = len(nm)-1
  60.     streng+=str(curr)
  61.     #print "Starting backtracking"
  62.     while not pi[curr] == -1:
  63.       #print "Path: ",streng
  64.       # print "Current: ",curr
  65.       if prob[curr] == 0:
  66.         return 0
  67.       streng+="-"+str(pi[curr])
  68.       curr = pi[curr]
  69.    
  70.    
  71.     #print streng
  72.     streng = streng[::-1]
  73.     #print streng
  74.            
  75.      
  76.     return streng
  77.      
  78.      
  79.      
  80.        
  81.            
  82.    
  83.    
  84.    
  85.      
  86.        
  87.        
  88.    
  89.    
  90.    
  91.  
  92. n = int(stdin.readline())
  93. sansynligheter = [float(x) for x in stdin.readline().split()]
  94. nabomatrise = []
  95. for linje in stdin:
  96.     naborad = [0] * n
  97.     naboer = [int(nabo) for nabo in linje.split()]
  98.     for nabo in naboer:
  99.         naborad[nabo] = 1
  100.     nabomatrise.append(naborad)
  101. print beste_sti(nabomatrise, sansynligheter)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement