This article describes the example data structures of FIG python breadth-first and depth-first usage. Share to you for your reference. details as follows:
First, there is a concept: back
Backtracking (exploration and backtracking) is an optimal selection search, search forward Press to excellent condition, in order to achieve our goals. But when a step to explore, discover original choice is not superior or to meet its target, it is a step back to re-select, this dead end on the return walk technology for backtracking, and meet back at some point state conditions called "backtrack point."
Depth-first algorithm:
(1) an initial access tag vertices and vertex v v visited.
(2) finds the first adjacent vertices of vertex v w.
(3) If the adjacent vertices w vertex v exists, it continues; otherwise, back to v, the other unvisited neighbors find the v.
(4) If the vertex w yet been accessed, the access vertices w and w vertex marked as visited.
(5) continues to find the next adjacent vertices vertices w, wi, wi if the value of v to step (3). FIG communication until all vertices of all visited so far.
Breadth-first algorithm:
(1) queues the vertex v.
(2) when the queue is not empty then continue, otherwise the algorithm ends.
(3) the queue head to obtain team points v; v visits vertices and vertex v mark has been accessed.
(4) finds the first adjacent vertices of vertex v col.
(5) If the adjacent vertices v col not been accessed, the queues col.
(6) continue to look for another new vertex v adjacent vertices col, go to step (5). Until all the neighbors are not visited vertex v processed. Go to step (2).
Code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
class Graph(object):
def __init__(self,*args,**kwargs):
self.node_neighbors = {}
self.visited = {}
def add_nodes(self,nodelist):
for node in nodelist:
self.add_node(node)
def add_node(self,node):
if not node in self.nodes():
self.node_neighbors[node] = []
def add_edge(self,edge):
u,v = edge
if(v not in self.node_neighbors[u]) and ( u not in self.node_neighbors[v]):
self.node_neighbors[u].append(v)
if(u!=v):
self.node_neighbors[v].append(u)
def nodes(self):
return self.node_neighbors.keys()
def depth_first_search(self,root=None):
order = []
def dfs(node):
self.visited[node] = True
order.append(node)
for n in self.node_neighbors[node]:
if not n in self.visited:
dfs(n)
if root:
dfs(root)
for node in self.nodes():
if not node in self.visited:
dfs(node)
print order
return order
def breadth_first_search(self,root=None):
queue = []
order = []
def bfs():
while len(queue)> 0:
node = queue.pop(0)
self.visited[node] = True
for n in self.node_neighbors[node]:
if (not n in self.visited) and (not n in queue):
queue.append(n)
order.append(n)
if root:
queue.append(root)
order.append(root)
bfs()
for node in self.nodes():
if not node in self.visited:
queue.append(node)
order.append(node)
bfs()
print order
return order
if __name__ == '__main__':
g = Graph()
g.add_nodes([i+1 for i in range(8)])
g.add_edge((1, 2))
g.add_edge((1, 3))
g.add_edge((2, 4))
g.add_edge((2, 5))
g.add_edge((4, 8))
g.add_edge((5, 8))
g.add_edge((3, 6))
g.add_edge((3, 7))
g.add_edge((6, 7))
print "nodes:", g.nodes()
order = g.breadth_first_search(1)
order = g.depth_first_search(1)
Results:
Nodes: [1, 2, 3, 4, 5, 6, 7, 8]
breadth Priority:
[1, 2, 3, 4, 5, 6, 7, 8]
Depth Priority:
[1, 2, 4 , 8, 5, 3, 6, 7]
Finally, we recommend a very wide python learning resource gathering, [click to enter] , here are my collection before learning experience, study notes, there is a chance of business experience, and calmed down to zero on the basis of the actual project data, we can at the bottom, leave a message, do not know to put forward, we will study together progress