Algorithm

Depth-First Search

  1. def DFS(G):
  2. global count
  3. mark = [0] * len(G) //create an empty stack
  4. count = 0
  5. for v in range(len(G)):
  6. if mark[v] == 0:
  7. DFSExplore(v, G, mark)
  8. #print(mark)
  9. def DFSExplore(v, G, mark):
  10. global count
  11. count = count + 1
  12. mark[v] = count
  13. for w in range(len(G)):
  14. if G[v][w] != 0:
  15. if mark[w] == 0:
  16. DFSExplore(w, G, mark)
  17. def test():
  18. G = [[0, 1, 0, 1],
  19. [1, 0, 0, 1],
  20. [0, 0, 0, 1],
  21. [1, 1, 1, 0]]
  22. DFS(G)

Breadth-First Search

  1. from queue import Queue
  2. def BFS(G):
  3. mark = [0] * len(G) //create an empty queue
  4. count = 0
  5. Q = Queue()
  6. for v in range(len(G)):
  7. if mark[v] == 0:
  8. count = count + 1
  9. mark[v] = count
  10. Q.put(v) //queue containing just v
  11. while not Q.empty():
  12. u = Q.get() //dequeues u
  13. for w in range(len(G)):
  14. if G[u][w] != 0:
  15. if mark[w] == 0:
  16. count = count + 1
  17. mark[w] = count
  18. Q.put(w) //enqueues w
  19. def test():
  20. G = [[0, 1, 0, 1],
  21. [1, 0, 0, 1],
  22. [0, 0, 0, 1],
  23. [1, 1, 1, 0]]
  24. BFS(G)

Review

Tip

使用 webpack 搭建一个简单的 React 脚手架

SQL keywords

C