Last active
February 16, 2021 09:38
-
-
Save dabasajay/e9949dec63f348da0dded4c3e150e2ac to your computer and use it in GitHub Desktop.
dsa/graphs/traversal/ | desc: Breadth First Search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vector<vector<int>> adj; // adjacency list representation | |
int n; // number of nodes | |
int src; // source vertex | |
queue<int> q; | |
vector<bool> vis(n); | |
vector<int> dist(n), par(n); | |
q.push(src); | |
vis[src] = true; | |
par[src] = -1; | |
while (!q.empty()) { | |
int u = q.front(); | |
q.pop(); | |
for (int v : adj[u]) { | |
if (!vis[v]) { | |
vis[v] = true; | |
q.push(v); | |
dist[v] = dist[u] + 1; | |
par[v] = u; | |
} | |
} | |
} |
Author
dabasajay
commented
Feb 14, 2021
•
- CP-Algorithms
- DFS vs BFS
- Shortest cycle in an undirected unweighted graph
- Multi-source BFS(Dijkstra & BFS)
- Clone an Undirected Graph
- Clone a Binary Tree with Random Pointers
- Check if there is a cycle with odd weight sum in an undirected graph
- Concept/Trick: number of odd weighted edges needs to be odd in a cycle for it to be an oddly weighted cycle.
- Number of shortest paths in an unweighted and directed graph
- Find minimum weight cycle in an undirected graph
- Shortest Path with even number of Edges from Source to Destination - Weighted(Dijkstra)/Unweighted(BFS)
Practice
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment