Hi All,
I have created this post to share my notes on graph. I always faced problem in understanding graph problem and even if I understand the problem then next worry is which data strucutre and algorithm is used to solve this problem.
This post does not have any shortcut to remember templates or code. This post has important graph algorithms and resources to learn these algorithms.
After solving problem related to each topic, I am confident to implement the required algorithm quickly. These building blocks also help me to revise the concept of graph just before the interview.
Please upvote the posts so that it will reach max folks and everyone will get benefit out of it.
How to declare data structure for storing graph?
Unweighted Graph
List[] graph = new List[n];
Weighted Graph
List<int[]>[] graph = new List[n];
Tree vs Graph
Visited array is used in graph to avoid processing of the same node again.
Algorithm used to traverse/search in graph
BFS
DFS
Check if cycle exist in graph
Directed
Use color array with value 0, 1 and 2
Undirected
Topological Sort (Directed Acyclic Graph)
https://www.interviewcake.com/concept/java/topological-sort
BFS
DFS
Single Source Shortest Path
1. Topological Sort
2. Dijkstra Algorithm
Bellman Ford
All Pair Shortest Path Algorithm
Floyd Warshall Algorithm
Strongly Connected Components(SCC)
Simple DFS and visited array is used to find scc in an undirected graph.
Tarjan algo and kosaraju algo are used to find scc in directed graphs.
Tarjan’s Algorithm
Kosaraju’s Algorithm
Minimum Spanning Tree
Prims Algo
Kruskal Algo
Travelling Salesman Problem (TSP)
Hamiltonian Path or TSP
Euler Path
Graph beginner problems:
https://leetcode.com/discuss/general-discussion/655708/graph-for-beginners-problems-pattern-sample-solutions/
If I missed anything or written wrongly, please let me know. I will keep updating the post.
Next: I will be writing the same for Dynamic Programming.