A.1.7: Graphs

Learning Objectives

  1. 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

  2. Graphs are generalised versions of trees, where edges can be directed or undirected, edges can have weights and there can be cycles

  3. DFS and BFS are the most common graph traversal algorithms, where we typically track visited nodes to avoid infinite loops

Introduction

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 for Graphs

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.

DFS

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.

// Represent graph in hash table with nodes as keys and neighbouring nodes as values
const graph = {
  a: ["b", "c"],
  b: ["d"],
  c: ["e"],
  d: ["f"],
  e: [],
  f: [],
};

// Track nodes we have visited in hash table
const visited = {};

const dfs = (currNode) => {
  // End this path if we reach visited node
  if (visited[currNode]) {
    return;
  }

  // Mark current node visited
  visited[currNode] = true;

  // Perform DFS on each neighbour
  for (const neighbour of graph[currNode]) {
    dfs(neighbour);
  }
};

// Start DFS on source node
dfs("a");

BFS

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.

// Represent graph in hash table with nodes as keys and neighbouring nodes as values
const graph = {
  a: ["b", "c"],
  b: ["d"],
  c: ["e"],
  d: ["f"],
  e: [],
  f: [],
};

// Track nodes we have visited in hash table
const visited = {};
// Track upcoming nodes to visit in a queue (represented by array for simplicity)
const queue = [];

const bfs = (sourceNode) => {
  // Mark source node as visited
  visited[sourceNode] = true;
  // Initialise BFS by adding source node to queue
  queue.push(sourceNode);

  while (queue.length > 0) {
    // Visit next node in queue
    const currNode = queue.shift(0);
    for (const neighbour in graph[currNode]) {
      // Only add neighbour to queue if not visited yet
      if (!visited[neighbour]) {
        // Mark neighbour as visited
        visited[neighbour] = true
        // Add neighbour to queue
        queue.push(neighbour);
      }
    }
  }
};

// Start BFS on source node
bfs("a");

Exercises

Pre-Class

  1. Find Center of Star Graph (LeetCode)

    1. Hint: This does not require graph traversal

Part 1

  1. Find the Town Judge (LeetCode)

    1. Hint: What additional data structures might be helpful for us to solve this problem?

    2. Hint: This does not require graph traversal

Part 2

  1. Find if Path Exists in Graph (LeetCode)

    1. Hint: This requires graph traversal

Part 3

  1. Keys and Rooms (LeetCode) (Medium)

More Comfortable

  1. All Paths from Source to Target (LeetCode) (Medium)

    1. Hint: Would recursion be helpful?

  2. Redundant Connection (LeetCode) (Medium)

  3. Minimum Number of Vertices to Reach All Nodes (LeetCode) (Medium)

  4. Number of Islands (LeetCode) (Medium)

Additional Resources

Last updated