python cp9_p182 breadth-first search

# A complete solution to the breadth-first search, using a table of links as
# described in Chapter 6, is as follows:

import pymysql

#conn = pymysql.connect(host='127.0.0.1', unix_socket='/tmp/mysql.sock', user='root', passwd=None, db='mysql')
conn = pymysql.connect(host='127.0.0.1', port =3306, user='root', password='LlQ54951', db='mysql', charset='utf8')

cur = conn.cursor()
cur.execute('USE wikipedia')

#getUrl is a helper function that retrieves URLs from the database given a page ID.
def getUrl(pageId):
    cur.execute( 'SELECT url FROM pages WHERE id=%s', (int(pageId)) )
    return cur.fetchone()[0]

# getLinks takes a fromPageId representing the integer ID
# for the current page, and fetches a list of all integer IDs for pages it links to
def getLinks(fromPageId):
    cur.execute( 'SELECT toPageId FROM links WHERE fromPageId = %s', (int(fromPageId)) )

    if cur.rowcount ==0:
        return[]

    toPageIdNestedTuple=cur.fetchall()
    #print(toPageIdNestedTuple)
    return[x[0] for x in toPageIdNestedTuple]

# searchBreadth, works recursively to construct a list of
# all possible paths from the search page and stops when it finds a path that has
# reached the target page
def searchBreadth(targetPageId, pathNestedList=[ [1] ]):
    newPathNestedList = []
    for pathList in pathNestedList:
        linkList = getLinks(pathList[-1])
        for link in linkList:
            print(link)
            if link == targetPageId:
                return pathList + [link]
            else:
                newPathNestedList.append(pathList + [link])
    #print(newPathNestedList)
    return searchBreadth(targetPageId, newPathNestedList)


nodes = getLinks(1) # ID 1 (Kevin Bacon)
targetPageId = 28624 #Eric Idle (page ID 28624
pageIdList = searchBreadth(targetPageId)

print(pageIdList)
for pageId in pageIdList:
    print(getUrl(pageId))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值