廣度/深度優先搜尋演算法 - python實現

來源:互聯網
上載者:User

標籤:鄰居   images   root   wrong   路徑   python2   ons   依次   obj   

1. 圖

定義:圖(Graph)是由頂點的有窮非空集合和頂點之間邊的集合組成,通常表示為:G(V,E),其中,G表示一個圖,V是圖G中頂點的集合,E是圖G中邊的集合.


簡單點的說:圖由節點和邊組成。一個節點可能與眾多節點直接相連,這些節點被稱為鄰居。

如二叉樹就為一個簡單的圖:

更加詳細的資訊可參見:https://www.cnblogs.com/polly333/p/4760275.html

2. 演算法1). 廣度優先搜尋:

廣度優先搜尋演算法(Breadth First Search,BSF),思想是:

  • 1.某頂點v出發,首先訪問定點v
  • 2.在訪問了v之後依次訪問v的各個未曾訪問過的鄰接點;
  • 3.然後分別從這些鄰接點出發依次訪問它們的鄰接點,並使得“先被訪問的頂點的鄰接點先於後被訪問的頂點的鄰接點被訪問;
  • 4.直至圖中所有已被訪問的頂點的鄰接點都被訪問到;
  • 5.如果此時圖中尚有頂點未被訪問,則需要另選一個未曾被訪問過的頂點作為新的起始點,重複上述過程,直至圖中所有頂點都被訪問到為止。

換句話說,廣度優先搜尋遍曆圖的過程是以v為起點,由近至遠,依次訪問和v有路徑相通且路 徑長度為1,2...的頂點。


如的BFS訪問順序為:

A->B->C->D->E->F

2). 深度優先搜尋:

圖的深度優先搜尋(Depth First Search, DFS),和樹的前序走訪非常類似。

它的思想:

  • 1.從頂點v出發,首先訪問該頂點;
  • 2.然後依次從它的各個未被訪問的鄰接點出發深度優先搜尋遍曆圖;
  • 3.直至圖中所有和v有路徑相通的頂點都被訪問到。
  • 4.若此時尚有其他頂點未被訪問到,則另選一個未被訪問的頂點作起始點,重複上述過程,直至圖中所有頂點都被訪問到為止

如的BFS訪問順序為:

A->B->D->E->C->F

3. 代碼
# -*- coding: utf-8 -*-#/usr/bin/pythonfrom collections import dequeimport sysclass Graph(object):    def __init__(self, *args, **kwargs):        self.order = []     #visited order         self.neighbor = {}    def add_node(self, node):        key,val = node        if not isinstance(val, list):            print(‘node value should be a list‘)                    #sys.exit(‘failed for wrong input‘)                self.neighbor[key] = val    def broadth_first(self, root):        if root != None:            search_queue = deque()            search_queue.append(root)            visited = []        else:            print(‘root is None‘)            return -1        while search_queue:            person = search_queue.popleft()            self.order.append(person)            if (not person in visited) and (person in self.neighbor.keys()):                search_queue += self.neighbor[person]                visited.append(person)    def depth_first(self, root):        if root != None:            search_queue = deque()            search_queue.append(root)            visited = []        else:            print(‘root is None‘)            return -1        while search_queue:            person = search_queue.popleft()            self.order.append(person)            if (not person in visited) and (person in self.neighbor.keys()):                tmp = self.neighbor[person]                tmp.reverse()                for index in tmp:                    search_queue.appendleft(index)                visited.append(person)                #self.order.append(person)    def clear(self):        self.order = []    def node_print(self):        for index in self.order:            print(index, end=‘  ‘) if __name__ == ‘__main__‘:    g = Graph()    g.add_node((‘1‘,[‘one‘, ‘two‘,‘three‘]))    g.add_node((‘one‘,[‘first‘,‘second‘,‘third‘]))    g.add_node((‘two‘,[‘1‘,‘2‘,‘3‘]))    g.broadth_first(‘1‘)     print(‘broadth search first:‘)     print(‘  ‘, end=‘  ‘)    g.node_print()        g.clear()    print(‘\n\ndepth search first:‘)     print(‘  ‘, end=‘  ‘)    g.depth_first(‘1‘)    g.node_print()    print() 

ps: 以上代碼需要用python3.x運行,python2.x不支援print的關鍵字參數

PS2: 以上代碼有些許不完善,如果你有改進的方法,請留言,萬分感謝!

廣度/深度優先搜尋演算法 - python實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.