99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀Python數(shù)據(jù)結構與算法之圖的廣度優(yōu)先與深度優(yōu)先搜索算法示例
Python數(shù)據(jù)結構與算法之圖的廣度優(yōu)先與深度優(yōu)先搜索算法示例
2018-06-20
收藏

Python數(shù)據(jù)結構與算法之圖的廣度優(yōu)先與深度優(yōu)先搜索算法示例

本文實例講述了Python數(shù)據(jù)結構與算法之圖的廣度優(yōu)先與深度優(yōu)先搜索算法。分享給大家供大家參考,具體如下:
根據(jù)維基百科的偽代碼實現(xiàn):
廣度優(yōu)先BFS:
使用隊列,集合
標記初始結點已被發(fā)現(xiàn),放入隊列
每次循環(huán)從隊列彈出一個結點
將該節(jié)點的所有相連結點放入隊列,并標記已被發(fā)現(xiàn)
通過隊列,將迷宮路口所有的門打開,從一個門進去繼續(xù)打開里面的門,然后返回前一個門處    
"""
 procedure BFS(G,v) is
   let Q be a queue
   Q.enqueue(v)
   label v as discovered
   while Q is not empty
    v ← Q.dequeue()
    procedure(v)
    for all edges from v to w in G.adjacentEdges(v) do
      if w is not labeled as discovered
        Q.enqueue(w)
        label w as discovered
"""
def procedure(v):
  pass
def BFS(G,v0):
  """ 廣度優(yōu)先搜索 """
  q, s = [], set()
  q.extend(v0)
  s.add(v0)
  while q:  # 當隊列q非空
    v = q.pop(0)
    procedure(v)
    for w in G[v]:   # 對圖G中頂點v的所有鄰近點w
      if w not in s: # 如果頂點 w 沒被發(fā)現(xiàn)
        q.extend(w)
        s.add(w)  # 記錄w已被發(fā)現(xiàn)
深度優(yōu)先DFS
使用 棧,集合
初始結點入棧
每輪循環(huán)從棧中彈出一個結點,并標記已被發(fā)現(xiàn)
對每個彈出的結點,將其連接的所有結點放到隊列中
通過棧的結構,一步步深入挖掘    
""""
Pseudocode[edit]
Input: A graph G and a vertex v of G
Output: All vertices reachable from v labeled as discovered
A recursive implementation of DFS:[5]
1 procedure DFS(G,v):
2   label v as discovered
3   for all edges from v to w in G.adjacentEdges(v) do
4     if vertex w is not labeled as discovered then
5       recursively call DFS(G,w)
A non-recursive implementation of DFS:[6]
1 procedure DFS-iterative(G,v):
2   let S be a stack
3   S.push(v)
4   while S is not empty
5      v = S.pop()
6      if v is not labeled as discovered:
7        label v as discovered
8        for all edges from v to w in G.adjacentEdges(v) do
9          S.push(w)
"""
def DFS(G,v0):
  S = []
  S.append(v0)
  label = set()
  while S:
    v = S.pop()
    if v not in label:
      label.add(v)
      procedure(v)
      for w in G[v]:
        S.append(w)

數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師考試動態(tài)
數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調用 initGeetest 進行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調,回調的第一個參數(shù)驗證碼對象,之后可以使用它調用相應的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }