码迷,mamicode.com
首页 > 其他好文 > 详细

Word Ladder

时间:2017-07-12 19:57:02      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:interview   algo   i+1   ddn   nec   end   href   key   app   

https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Graphs/Word%20Ladder%20Example%20Problem.ipynb

 

BFS.

技术分享

 

技术分享

 

 

 

 技术分享

 

 

技术分享

 

 技术分享

 

 

 

 

 技术分享

 

 

 

 

技术分享

 

 

 

 

 

 

Word Ladder Example Code

Below is the Vertex and Graph class used for the Word Ladder example code:

 

class Vertex:
    def __init__(self,key):
        self.id = key
        self.connectedTo = {}

    def addNeighbor(self,nbr,weight=0):
        self.connectedTo[nbr] = weight

    def __str__(self):
        return str(self.id) +  connectedTo:  + str([x.id for x in self.connectedTo])

    def getConnections(self):
        return self.connectedTo.keys()

    def getId(self):
        return self.id

    def getWeight(self,nbr):
        return self.connectedTo[nbr]

 

 

class Graph:
    def __init__(self):
        self.vertList = {}
        self.numVertices = 0

    def addVertex(self,key):
        self.numVertices = self.numVertices + 1
        newVertex = Vertex(key)
        self.vertList[key] = newVertex
        return newVertex

    def getVertex(self,n):
        if n in self.vertList:
            return self.vertList[n]
        else:
            return None

    def __contains__(self,n):
        return n in self.vertList

    def addEdge(self,f,t,cost=0):
        if f not in self.vertList:
            nv = self.addVertex(f)
        if t not in self.vertList:
            nv = self.addVertex(t)
        self.vertList[f].addNeighbor(self.vertList[t], cost)

    def getVertices(self):
        return self.vertList.keys()

    def __iter__(self):
        return iter(self.vertList.values())

 

 


Code for buildGraph function:

 

def buildGraph(wordFile):
    d = {}
    g = Graph()
    
    wfile = open(wordFile,r)
    # create buckets of words that differ by one letter
    for line in wfile:
        print line
        word = line[:-1]
        print word
        for i in range(len(word)):
            bucket = word[:i] + _ + word[i+1:]
            if bucket in d:
                d[bucket].append(word)
            else:
                d[bucket] = [word]
    # add vertices and edges for words in the same bucket
    for bucket in d.keys():
        for word1 in d[bucket]:
            for word2 in d[bucket]:
                if word1 != word2:
                    g.addEdge(word1,word2)
    return g

 

Word Ladder

标签:interview   algo   i+1   ddn   nec   end   href   key   app   

原文地址:http://www.cnblogs.com/prmlab/p/7156868.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!