Python data structure and algorithm graph breadth-first and depth-first search algorithm examples

@ This article comes from the public number: csdn2299, like to pay attention to the public number programmer academy
This article describes the breadth-first and depth-first search algorithm of the Python data structure and algorithm graph. Share with you for your reference, as follows:

According to Wikipedia's pseudo-code implementation:

Breadth First BFS:

Use queue, collection

Mark the initial node has been found and put it in the queue

Each loop pops a node from the queue

Put all the connection points of the node into the queue and mark it as found

Through the queue, open all the doors at the intersection of the maze, enter from one door, continue to open the inside door, and then return to the previous door

"""
 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):
  """ 广度优先搜索 """
  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 没被发现
        q.extend(w)
        s.add(w)  # 记录w已被发现

Depth-first DFS

Use stack, collection

The initial node is pushed onto the stack

Each round of loops pops a node from the stack and marks that it has been found

For each node that pops up, put all the nodes it connects to the queue

Through the structure of the stack, dig deeper step by step

""""
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)

Thank you very much for reading
. When I chose to study python at university, I found that I ate a bad computer foundation. I did n’t have an academic qualification. This is
nothing to do. I can only make up for it, so I started my own counterattack outside of coding. The road, continue to learn the core knowledge of python, in-depth study of computer basics, sorted out, if you are not willing to be mediocre, then join me in coding, and continue to grow!
In fact, there are not only technology here, but also things beyond those technologies. For example, how to be an exquisite programmer, rather than "cock silk", the programmer itself is a noble existence, isn't it? [Click to join] want to be yourself, want to be noble, come on

Published 50 original articles · 21 praises · 20,000+ views

Guess you like

Origin blog.csdn.net/chengxun03/article/details/105545278