Graphs are used to model a wide variety of real-world problems, such as transport networks, social networks, disease networks, communication networks, website networks, e-commerce networks and more
Graphs are generalised versions of trees, where edges can be directed or undirected, edges can have weights and there can be cycles
DFS and BFS are the most common graph traversal algorithms, where we typically track visited nodes to avoid infinite loops
Graphs are used to model a wide variety of real-world problems, such as transport networks, social networks, disease networks, communication networks, website networks, e-commerce networks and more. Graph theory can get complex and is the subject of entire university courses; in this submodule we will introduce the basics that are relevant for algorithm interviews.
The following video showcases the types of problems we can model and solve with graphs. We hope this excites you about the potential of graphs!
Graphs are generalised versions of trees, where edges can be directed or undirected, edges can have weights and there can be cycles. All trees are graphs but not all graphs are trees.
Directed edges are like the edges in trees, where they only point in 1 direction. These can be used to represent directed relationships such as follow requests on Instagram or Twitter.
Undirected edges are edges that connect nodes regardless of direction. These can be used to represent undirected relationships such as Facebook friendships or 2-way road networks.
Weighted edges are edges with a number associated with them. These can be used to represent metadata about connections between nodes, such as friendship scores between Facebook friends or road distances.
Graph cycles are loops within graphs. Trees have no cycles by definition, and are part of a common subset of graphs called "directed acyclic graphs", meaning graphs with directed edges and no loops.
The following video introduces the above concepts and 3 common ways of representing graphs in code: adjacency matrix, adjacency list and edge list.
DFS and BFS are the most common graph traversal algorithms. Unlike trees, which are a type of graph with no cycles (called "acyclic" graphs), for graphs with cycles we will need to track visited nodes to avoid infinite loops.
Not all graph problems require DFS or BFS-like traversal algorithms, although many will. Some simpler graph problems can be solved by analysing edges without DFS or BFS.
DFS is generally a "simpler" algorithm because it involves recursion with no additional data structures beyond our graph. BFS requires the use of a queue to visit nodes in increasing distance order from the source node.
We would typically use DFS when we need to visit all nodes in the graph, and BFS when we wish to find the shortest path between nodes in the most efficient manner.
The following video explains the logic for DFS in graphs. Notice the use of a hash table to track which nodes have been visited to avoid cycles.
The following is DFS code for graphs in JavaScript. Notice it is similar to DFS for trees except we track visited nodes, and we reference a hash table that represents our graph to find neighbours.
Visit each node in graph in increasing distance from source node. This is similar to the level-order tree traversal algorithm. Notice the use of a hash table to track which nodes have been visited to avoid cycles.
The following is BFS code for graphs in JavaScript. Notice it is similar to BFS for N-ary trees except we track visited nodes, and we reference a hash table that represents our graph to find neighbours.
Find Center of Star Graph (LeetCode)
Hint: This does not require graph traversal
Find the Town Judge (LeetCode)
Hint: What additional data structures might be helpful for us to solve this problem?
Hint: This does not require graph traversal
Rocket solution code (Python)
Rocket solution video (Python)
Find if Path Exists in Graph (LeetCode)
Hint: This requires graph traversal
Keys and Rooms (LeetCode) (Medium)
All Paths from Source to Target (LeetCode) (Medium)
Hint: Would recursion be helpful?
Rocket solution code (Python)
Rocket solution video (Python)
Redundant Connection (LeetCode) (Medium)
Minimum Number of Vertices to Reach All Nodes (LeetCode) (Medium)
Number of Islands (LeetCode) (Medium)
BFS vs DFS written explanation in blog post
Graph Algorithms for Technical Interviews video by freeCodeCamp