Lecture 14 - Graphs and basic search

Date Pre-lecture slides Post-lecture scribbles Async video Lecture recording
October 12 2023 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

Additional Resources

Contributors

Sandhya Perumenki