Lecture 14 - Graphs and basic search

Date Pre-lecture slides Post-lecture scribbles Async video Lecture recording
October 16 2025 Lecture 14 - Graphs and basic search Lecture 14 - Graphs and basic search Lecture 14 - Graphs and basic search Lecture 14 - Graphs and basic search
 

Notes

I. Undirected graphs

Definition

An undirected (simple) graph G = (V, E) is a 2-tuple:

Notation and Convention

An edge in an undirected graphs is an unordered pair of nodes and hence it is a set. Conventionally we use uv for {u, v} when it is clear from the context that the graph is undirected.

Graph Representation I - Adjacency Matrix

Represent G = (V, E) with n vertices and m edges using a n x n adjacency matrix A where

Example:

text text text


Graph Representation II - Adjacency List

Represent G = (V, E) with n vertices and m edges using adjacency lists:

Example:

text text text


Concrete Representation

Assume vertices are numbered arbitrarily as {1, 2,…, n}.

text text
Connectivity

Given a graph G = (V, E):

text text text


Basic Graph Search in Undirected Graphs:
Given G = (V,E) and vertex u $\in$ V. Let n = V . Then:
Explore(G,u):
  Visited[1 . . n] = FALSE
  //ToExplore, S: Lists
  Add u to ToExplore and to S
  Visited[u] = TRUE
  while (ToExplore is non-empty) do
    Remove node x from ToExplore
    for each edge xy in Adj(x) do
      if (Visited[y] = FALSE)
        Visited[y] = TRUE
        Add y to ToExplore
        Add y to S
  Output S


Running time : O(n+m)

II. Directed graphs

Definition
Representation

Graph G = (V, E) with n vertices and m edges:

text text
Directed Connectivity

Given a graph G = (V, E):

Example of Asymmetricity: D can reach B but B cannot reach D.
text

Strong Connected Components
text text text


Basic Graph Search in Directed Graphs

Given G = (V,E) and vertex u $\in$ V. Let n = |V|. Then:

Explore(G,u):
  array Visited[1..n]
  Initialize: Set Visited[i] = FALSE for 1 <= i <= n
  List: ToExplore, S
  Add u to ToExplore and to S, Visited[u] = TRUE
  Make tree T with root as u
  while (ToExplore is non-empty) do
    Remove node x from ToExplore
    for each edge (x, y) in Adj(x) do
      if (Visited[y] = FALSE)
        Visited[y] = TRUE
        Add y to ToExplore
        Add y to S
        Add y to T with edge (x, y)
  Output S



Example - rch(B) :

text text text
Basic Graph Search Properties

Relevent LeetCode Practice (by Tristan Yang)

  1. LeetCode 323 — Number of Connected Components in an Undirected Graph (Medium)
    • Relevance: Directly matches the task of finding all connected components in an undirected graph using adjacency lists.
    • ECE 374 Process: Build the graph from the given edges, then run a DFS or BFS from each unvisited node, marking all reachable nodes as visited and counting each start as a new component. Runs in $O(n+m)$ for n nodes and m edges.
    • Resource: CP-Algorithms tutorial on Depth-First Search (for component counting).
    • Takeaway: The basic graph “Explore” pattern (DFS/BFS) can identify components — essentially just repeated graph searches for each component.
  2. LeetCode 200 — Number of Islands (Medium)
    • Relevance: Same connected components idea but on a grid (implicit graph). It reinforces how we model problems as graphs.
    • ECE 374 Process: Treat each cell containing ‘1’ as a vertex, with edges connecting adjacent (up/down/left/right) land cells. Then perform DFS/BFS “flood fill” from each unvisited land cell, marking the entire island as visited.
    • Resource: NeetCode video on Number of Islands.
    • Takeaway: Modeling matters — once you recognize a grid of land/water as a graph, the same search algorithms solve it just like any other component-finding problem.
  3. LeetCode 785 — Is Graph Bipartite? (Medium)
    • Relevance: Classic application of BFS/DFS with 2-coloring (checking bipartiteness via parity of levels).
    • ECE 374 Process: Use BFS or DFS to attempt to color the graph with two colors (e.g. +1/-1). Start at any node, color it, then color neighbors with the opposite color. If you ever find an edge connecting two nodes of the same color, the graph isn’t bipartite. Make sure to restart the search for disconnected components.
    • Resource: VisuAlgo visualizer – BFS Graph Traversal (demonstrates layering which underpins bipartite check).
    • Takeaway: A layered BFS naturally assigns alternating “parity” to levels, which can be used to test bipartiteness (no odd-length cycles present).
  4. LeetCode 207 — Course Schedule (Medium)
    • Relevance: Introduces directed graph reachability and cycle detection, which is a precursor to strongly connected components and topological sorting.
    • ECE 374 Process: Build a directed graph of course prerequisites. Then either: (a) perform DFS with 3-color marking (white/gray/black) to detect a back-edge (cycle) if one is found; or (b) perform Kahn’s algorithm (BFS using an in-degree=0 queue) to attempt a topological sort and see if you can pop all nodes. If a cycle exists, you won’t be able to complete all courses.
    • Resource: CP-Algorithms article on Topological Sorting.
    • Takeaway: Two approaches to detect cycles in directed graphs: DFS (find back-edges via a recursion stack) or BFS (Kahn’s algorithm) — both will indicate if the graph is acyclic or not.

Supplemental Problems

Additional Resources

Contributors

Sandhya Perumenki