Я использовал BFS / DFS на графиках.Рассмотрим следующий график
v1-------------> v2-
| | -
| | -
| | -
|> |> - >
v4------------> v3----------> v6
- |
- |
- |
- |>
-------- > v5
В BFS вы начинаете с посещения начальной вершины v1.После посещения начальной вершины вы затем посещаете вершины, смежные с начальной вершиной.
Let us start with vertex v1.
Traverse v1.
Vertices(unvisited) adjacent to v1 are v2, v4.
Traverse v2, v4.
Vertices(unvisited) adjacent to v2 are v3, v6.
Traverse v3, v6.
Vertices(unvisited) adjacent to v4 are v5.
Traverse v5.
Vertices(unvisited) adjacent to v3 is nill.
Vertices(unvisited) adjacent to v6 is nill.
Vertices(unvisited) adjacent to v5 is nill. End.
Реализация с использованием очереди .
Algorithm: BFS(v)
1. Visit the starting vertex, v and insert it into the queue.
2. Repeat step 3 until the queue becomes empty.
3. Delete the front element from the queue. Visit all it's unvisited adjacent vertices and insert them into the queue.
If the graph is not connected then it's not possible to traverse the whole graph from a single vertex.
So we need to execute the preceding algorithm repeatedly for all unvisited vertices in the graph.
1. Repeat step 2 for each vertex, v in the graph
2. If v is not visited:
a. Call BFS(v)
Queue representation Vertices visited
--------------------------------------------------------
v1 v1
--------------------------------------------------------
v2 v4 v1, v2, v4
--------------------------------------------------------
v4 v3 v6 v1, v2, v4, v3, v6
--------------------------------------------------------
v3 v6 v5 v1, v2, v4, v3, v6, v5
--------------------------------------------------------
v6 v5 v1, v2, v4, v3, v6, v5
--------------------------------------------------------
v5 v1, v2, v4, v3, v6, v5
--------------------------------------------------------
empty v1, v2, v4, v3, v6, v5
--------------------------------------------------------
В DFS, вы начинаете обход, начиная с любого вершины v в качестве начальной вершины.После прохождения начальной вершины вы затем проходите любую из вершин, смежных с начальной вершиной.Затем вы перемещаетесь по любой из вершин, смежных с последней посещенной вершиной.Этот процесс продолжается до тех пор, пока вы не достигнете вершины, для которой нет не посещенных смежных вершин.На этом этапе вам необходимо вернуться к последней посещенной вершине, у которой есть невидимая смежная вершина, и инициировать оттуда DFS.
Let us first traverse v1.
Vertices adjacent to v1 which are unvisited are v2 and v4. Let us choose v2.
Traverse v2.
Vertices adjacent to v2 which are unvisited are v3 and v6. Let us choose v3.
Traverse v3.
Vertices adjacent to v3 which are unvisited are v6 and v5. Let us choose v5.
Traverse v5.
Vertices adjacent to v5 which are unvisited are nill. So backtrack.
Vertices adjacent to v3 which are unvisited is v6. Choose v6.
Traverse v6.
Vertices adjacent to v6 which are unvisited are nill. So backtrack.
Vertices adjacent to v3 which are unvisited are nill. So backtrack.
Vertices adjacent to v2 which are unvisited are nill. So backtrack.
Vertices adjacent to v1 which are unvisited v4. Choose v4.
Traverse v4.
Vertices adjacent to v4 which are unvisited are nill. So backtrack.
Vertices adjacent to v1 which are unvisited are nill. End.
Реализация с использованием stack
Algorithm: DFS(v)
1. Push the starting vertex, v into the stack
2. Repeat until the stack becomes empty.
a. Pop a vertex from stack.
b. Visit the popped vertex.
c. Push all the unvisited vertices adjacent to the popped vertex into the stack.
If the graph is not connected then its not possible to traverse the whole graph from a single vertex.
So we need to execute the preceding algorithm repeatedly for all unvisited vertices in the graph.
1. Repeat step 2 for each vertex, v in the graph
2. If v is not visited:
a. Call DFS(v)
Stack representation Vertices visited
--------------------------------------------------------
v1
--------------------------------------------------------
v2 v1
v4
--------------------------------------------------------
v3 v1, v2
v6
v4
--------------------------------------------------------
v5 v1, v2, v3
v6
v4
--------------------------------------------------------
v6 v1, v2, v3, v5
v4
--------------------------------------------------------
v4 v1, v2, v3, v5, v6
--------------------------------------------------------
empty v1, v2, v3, v5, v6, v4
--------------------------------------------------------