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

  1. Used for finding shortest path
  2. Time Complexity is O(V+ E)

DFS

  1. Used for finding if path exists/possible
  2. Time Complexity is O(V+ E)

Check if cycle exist in graph

Directed
Use color array with value 0, 1 and 2

Undirected

  1. Use union find
  2. use parent pointer and visited array ( DFS or BFS)

Topological Sort (Directed Acyclic Graph)
https://www.interviewcake.com/concept/java/topological-sort

BFS

  1. InDegree Array and start with indegree 0
  2. Important to check for length of topo sort array with total number of vertices to detect cycle.
  3. Time Complexity is O(V+ E).
  4. It is easier to check cycles in BFS.

DFS

  1. Add current node at start of list only after processing all child nodes.
  2. No need for indegree nodes. Just do dfs until all nodes are visited
  3. Time Complexity is O(V+ E)

Single Source Shortest Path

1. Topological Sort

  1. Perform Topo Sort
  2. Traverse graph in topo sort order. ie. Pick start vertex from top sort and explore its neighbours and keep maintaining shortest distance.
  3. Time Complexity is O(V + E).

2. Dijkstra Algorithm

Bellman Ford

  • Used with negative weights also.
  • Able to find whether graph has negative cycle
  • Not preferred over Dijkstra as time complexity of bellman ford is O(VE)
  • Implemented Bellman ford for this problem to see its working code. https://leetcode.com/problems/network-delay-time
  • Run this algo one more time if a negative cycle check is required. If the shortest distance of a vertex is reduced, then the graph has a negative cycle.

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

  • Start with any vertex. Use Priority Queue to process the smallest edge.
  • Use visited array or distance array.
  • Difference between Prims and Dijkstra is “Don’t add current vertex distance to calculate neighbour distance”.
  • Example : u, v
  • Dijkstra - dis[v] = dis[u] + graph[u][v];
  • Prims - dis[v] = graph[u][v]
  • Time Complexity is O(ElogV)
  • Implementation: https://leetcode.com/problems/min-cost-to-connect-all-points

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.

Comments (7)