[英]Python: DFS Optimised Algorithm?
我剛剛在Python上實現了DFS; 但是,由於倒數第三行的for循環,我認為這不是最優化的代碼。 我知道此DFS可以工作; 但是,它是否經過優化? 圖表網址以及代碼附在下面。
graphx=[[1, 1, 0, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 1, 0], [0, 0, 1, 1, 0, 0, 0, 0], [0, 0, 1, 1, 1, 0, 1, 0], [0, 0, 0, 1, 1, 1, 0, 0], [0, 0, 0, 0, 1, 1, 1, 1], [0, 1, 0, 1, 0, 1, 1, 0], [0, 0, 0, 0, 0, 1, 0, 1]]
visited=[False]*len(graphx[0])
stack=[]
def dfs(graphx, a):
stack.append(a)
while len(stack)!=0:
v=stack.pop()
if visited[v]==False:
visited[v]=True
print(v)
for w in range(len(graphx[0])):
if graphx[v][w]!=0:
stack.append(w)
圖: http : //i.imgur.com/BvBUdhP.png
有人說這是O(n ^ 2)時間,這非常糟糕。 如何優化?
編輯:這是基於原始DFS答案的BFS corect嗎?
def bfs(G, start):
"""Perform bfs on adjacency list of graph
G: Adjacency list representation of graph.
start: Index of start node.
"""
visited = [False] * len(G)
queue = []
queue.append(start)
while queue:
v = queue.pop()
if not visited[v]:
visited[v] = True
print(v)
for neighbor in G[v]:
queue.insert(0, neighbor)
算法的關鍵問題是您將圖形表示為鄰接矩陣。 對於V個節點,這將導致O(V ^ 2)運行時,因為您必須訪問矩陣的所有V ^ 2條目。
使用鄰接表表示圖更有效(運行時和內存方面)。 運行時間為O(E),其中E為邊數。
# Run time is O(E) and not O(V^2)
# It visits each node and edge exactly once.
def dfs(G, start):
"""Perform dfs on adjacency list of graph
G: Adjacency list representation of graph.
start: Index of start node.
"""
visited = [False] * len(G)
stack = []
stack.append(start)
while stack:
v = stack.pop()
if not visited[v]:
visited[v] = True
print(v)
for neighbor in G[v]:
stack.append(neighbor)
# This is your code. Takes O(V^2) because you are visiting every entry in
# the adjacency matrix, which has V^2 entries.
def dfs_adjacency_mat(G, start):
visited=[False]*len(G[0])
stack=[]
stack.append(start)
while len(stack)!=0:
v=stack.pop()
if visited[v]==False:
visited[v]=True
print(v)
for w in range(len(G[0])):
if G[v][w]!=0:
stack.append(w)
def main():
# Represent graph as adjacency list, not adjacency matrix
G = [[1], # Node 0 (A) has node 1 (B) as neighbor
[0, 6], # Node 1 (B) has node 0 (A) and 6 (G) as neighbor
[3],
[2, 4, 6],
[3, 5],
[4, 6, 7],
[1, 3, 5],
[5]]
print("Using adjacency list")
dfs(G, 0) # Start dfs at node 0 (i.e., node A)
print('-' * 50)
print("Using adjacency matrix")
# Adjacency matrix
graphx = [[1, 1, 0, 0, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 0, 1, 0],
[0, 0, 1, 1, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 0, 1, 0],
[0, 0, 0, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 1, 1, 1, 1],
[0, 1, 0, 1, 0, 1, 1, 0],
[0, 0, 0, 0, 0, 1, 0, 1]]
dfs_adjacency_mat(graphx, 0)
if __name__ == '__main__':
main()
輸出 :
Using adjacency list
0
1
6
5
7
4
3
2
--------------------------------------------------
Using adjacency matrix
0
1
6
5
7
4
3
2
您的問題相當模糊且開放。 優化代碼可以達到用另一種語言實現的目的,因此您必須更加精確地了解實際想要了解的內容。 建議您用C重寫整個內容可能不是您要尋找的答案。
無論如何,由於您使用的是鄰接矩陣,因此在節點數上定位鄰居是線性的。 如果有鄰接表,則可以在邊數上實現線性關系。 如果圖形稀疏,那是一個重要的區別。
一些特定於Python的注釋:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.