Graph & Search
Breadth-first Search
https://en.wikipedia.org/wiki/Breadth-first_search
BFS - Iterative using queue
Source: https://www.geeksforgeeks.org/breadth-first-search-or-bfs-for-a-graph/
Breadth First Search or BFS for a Graph
Applications of Breadth First Traversal
https://www.geeksforgeeks.org/applications-of-breadth-first-traversal/
1) Shortest Path and Minimum Spanning Tree for unweighted graph
In an unweighted graph, the shortest path is the path with least number of edges.
2) Peer to Peer Networks.
In Peer to Peer Networks like BitTorrent, Breadth First Search is used to find all neighbor nodes.
3) Crawlers in Search Engines:
Crawlers build index using Breadth First. The idea is to start from source page and follow all links from source and keep doing same. Depth First Traversal can also be used for crawlers, but the advantage with Breadth First Traversal is, depth or levels of the built tree can be limited.
4) Social Networking Websites:
In social networks, we can find people within a given distance ‘k’ from a person using Breadth First Search till ‘k’ levels.
5) GPS Navigation systems:
Breadth First Search is used to find all neighboring locations.
6) Broadcasting in Network:
In networks, a broadcasted packet follows Breadth First Search to reach all nodes.
7) In Garbage Collection:
Breadth First Search is used in copying garbage collection using Cheney’s algorithm. Refer this and for details. Breadth First Search is preferred over Depth First Search because of better locality of reference:
8) Cycle detection in undirected graph:
In undirected graphs, either Breadth First Search or Depth First Search can be used to detect cycle. In directed graph, only depth first search can be used.
In Ford-Fulkerson algorithm, we can either use Breadth First or Depth First Traversal to find the maximum flow. Breadth First Traversal is preferred as it reduces worst case time complexity to O(VE2).
10)To test if a graph is Bipartite
We can either use Breadth First or Depth First Traversal.
11) Path Finding
We can either use Breadth First or Depth First Traversal to find if there is a path between two vertices.
12) Finding all nodes within one connected component:
We can either use Breadth First or Depth First Traversal to find all nodes reachable from a given node.
Many algorithms like Prim’s Minimum Spanning Tree and Dijkstra’s Single Source Shortest Path use structure similar to Breadth First Search.
Depth-first Search
DFS - Iterative using stack
DFS - Recursive
以下DFS总结的内容来源:http://chen-tao.github.io/2017/01/26/about-dfs/
深度优先搜索(Depth First Search)
假设初始状态是图中所有顶点均未被访问,则从某个顶点v出发,首先访问该顶点,然后依次从它的各个未被访问的邻接点出发深度优先搜索遍历图,直至图中所有和v有路径相通的顶点都被访问到。 若此时尚有其他顶点未被访问到,则另选一个未被访问的顶点作起始点,重复上述过程,直至图中所有顶点都被访问到为止。
DFS Implementation
Graph Node
为了防止重复,使用一个HashSet来保存已经遍历过的节点(或者使用1维或2维数组,例如节点是数字类型,或者图本身是2维矩阵)
Recursive DFS
每到一个节点,标记已经被访问过,对邻居里没有访问的节点进行DFS
Non-Recursive DFS
非递归版本,相比递归版本效率高,且不会导致栈溢出
DFS with depth (for Tree)
深度depth在搜索中记录,递归版本加一个depth参数++就可以了,非递归版本用一个和s平行的栈记录深度
DFS for binary tree–PreOrder traversal
DFS对于二叉树而言,其遍历序列就是其前序遍历 Pre-order Traversal。
DFS 解题框架模板
修改自:http://chen-tao.github.io/2017/01/26/about-dfs/
Cycle Detection
对DFS稍作修改,可以判断一个有向图是否有回路
在递归版本里,我们队每一个点改为三种标记:
未访问过(0)
正在访问其邻居节点(1)
已经访问完毕该节点以及所有该节点可以到达的节点(2)
什么时候会出现回路?就是当前节点v的一个邻居u的状态为1的时候。
因为该节点状态为1,即还没有把它以后的节点全部遍历,所以当前节点v肯定可以从u到达,而现在又可以从v到达u,所以回路构成。
Topological Sort
拓扑排序是一个dfs的应用, 所谓拓扑排序是指在一个DAG(有向无回路图)里给每个节点定义一个顺序(v1…vn), 使得按照这个顺序遍历的节点, 每一个节点vi都是之前遍历过的的节点(v1 ~ vi-1)所指向的(或没有任何其他节点指向的).
See:
Shortest Path and Dijkstra's Algorithm
StackOverflow: What is difference between BFS and Dijkstra's algorithms when looking for shortest path?
Breadth-first search is just Dijkstra's algorithm with all edge weights equal to 1.
Dijkstra's algorithm is conceptually breadth-first search that respects edge costs.
The process for exploring the graph is structurally the same in both cases.
Aos Dabbagh: Understanding Dijkstra's Algorithm
One of Dijkstra’s algorithm modifications on breadth-first search is its use of a priority queue instead of a normal queue. With a priority queue, each task added to the queue has a “priority” and will slot in accordingly into the queue based on its priority level.
UIUC CS473: Breadth First Search, Dijkstra’s Algorithm for Shortest Paths (PDF)
无权图可以用BFS,到达目标点遍历的层数就是最短路径。
有权图则需要用Dijkstra's Algorithm。
Reference
Dijkstra's Algorithm
Last updated