Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from sys import stdin, stderr
- from collections import deque
- from copy import copy
- def beste_sti(nm, prob):
- cProb = [0]*len(nm) #Current probability at each node
- done = [] # Done nodes
- pi=[-1]*len(nm) #Predecessor array
- cProb[0] = prob[0]
- Q = [0] #Available nodes.
- while Q:
- print "Current Probability: ",cProb
- print "Parent: ",pi
- print "Q: ",Q
- print "Done: ", done
- #Finds the node with max probability
- # Picks highest node from Q that is not finished.
- maxNode = -1
- for i in range(len(Q)):
- u = Q[i]
- if cProb[u] > cProb[maxNode]:
- maxNode = u
- if maxNode == -1:
- return
- print "maxNode: ", maxNode
- print ""
- Q.remove(maxNode)
- done.append(maxNode)
- for nabo in range(len(nm)):
- isNabo = nm[maxNode][nabo]
- #Just adds the nabo to Q if it isnt done
- if isNabo == 1:
- if not nabo in Q and not nabo in done:
- Q.append(nabo)
- #Gives new probability if possible
- if cProb[maxNode]*prob[nabo] > cProb[nabo]:
- cProb[nabo] = cProb[maxNode]*prob[nabo]
- pi[nabo] = maxNode
- streng = ""
- #Find the path from start to stop.
- curr = len(nm)-1
- streng+=str(curr)
- #print "Starting backtracking"
- while not pi[curr] == -1:
- #print "Path: ",streng
- # print "Current: ",curr
- if prob[curr] == 0:
- return 0
- streng+="-"+str(pi[curr])
- curr = pi[curr]
- #print streng
- streng = streng[::-1]
- #print streng
- return streng
- n = int(stdin.readline())
- sansynligheter = [float(x) for x in stdin.readline().split()]
- nabomatrise = []
- for linje in stdin:
- naborad = [0] * n
- naboer = [int(nabo) for nabo in linje.split()]
- for nabo in naboer:
- naborad[nabo] = 1
- nabomatrise.append(naborad)
- print beste_sti(nabomatrise, sansynligheter)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement