Lecture 25: Graphs, Part II

Announcements

  • Gradescope links up soon
  • Final exam
    • released: Thursday, May 19th
    • due: Tuesday, May 24th by 4:00pm!
  • Same format as midterms
    • open book/note
    • no collaboration
  • All material from lectures is fair game
    • more focus on material since second midterm

Overview

  1. Depth-first Search (DFS)
  2. Breadth-first Search (BFS)
  3. Small Worlds

Last Time: Graphs

A graph $G = (V, E)$ consists of

  • a set $V$ of vertices (a.k.a. nodes) $V = {v_1, v_2,\ldots,v_n}$
  • a set $E$ of edges, where each edge is a pair of vertices

Example. $V = {1, 2, 3, 4, 5, 6}$, $E = {(1, 2), (1, 4), (2, 3), (2, 4), (2, 7), (3, 4), (3, 5), (4, 5), (5, 6)}$

Adjacency List Representation

Map<E, List<E>> gives List of neighbors for each vertex

Graph ADT and Operations

How to build & use graphs?

  • boolean adjacent(u, v) return true if u and v are adjacent (i.e., (u, v) is an edge)
  • neighbors(u) return a List of vertices adjacent to u
  • addVertex(u) add a vertex to set of vertices, if not already present
  • removeVertex(u) remove u and edges containing u from graph
  • addEdge(u, v) add an edge from u to v if not already present
  • removeEdge(u, v) remove edge from u to v if present

(Can have others too…)

Searching Graphs

Problem. Given a starting vertex $v$, determine if there is a path from $v$ to a given vertex $u$.

DFS Illustrated

Implementing DFS

What information do we need to maintain to perform DFS?

What ADTs/DSs could we use to maintain that information?

DFS Implementation

  • Set<E> visited of visited nodes
  • Stack<E> active of active nodes
boolean dfsFind(E v, E u) {
  visited.add(v);
  active.push(v)
  
  while (active.size() > 0) {
  
    E cur = active.peek();	
    if (cur.equals(u)) return true;
	  
    for (x : cur.neighbors()) {
      if (!visited.contains(x)) {
        visited.add(x);
        active.push(x);
        break;
      }
    }
  }
  return false;
}

Question 1

How could we modify dfsFind to return the path from v to u (if found)?

Question 2

What might be undesireable about the path found by DFS?

Question 3

How could we find the shortest path from v to u (if any)?

BFS Illustrated

Implementing BFS

What information do we need to maintain to perform BFS?

What ADTs/DSs could we use to maintain that information?

BFS Implementation

  • Set<E> visited of visited nodes
  • Queue<E> active of active nodes
boolean bfsFind(E v, E u) {
  visited.add(v);
  active.push(v)
  
  while (active.size() > 0) {
  
    E cur = active.remove();	
    if (cur.equals(u)) return true;
	  
    for (x : cur.neighbors()) {
      if (!visited.contains(x)) {
        visited.add(x)
        active.add(x);
      }
    }
  }
  return false;
}

Question 1

Why does BFS find u along the shortest path from v to u?

Question 2

How could we modify bfsFind to return the shortest path from v to u?

Application of BFS: Small Worlds

Properties of Social Networks:

  1. vertices in well-connected clusters

  2. max distance between nodes is small

    • Milgram’s experiment (1967)
    • 6 degrees of separation

Watts-Strogatz Model: Clusters

Watts-Strogatz Model: Shortcuts

Question

What is the “typical” distance between nodes in the network

  • How does distance scale with re-wiring parameter $p$?

Empirical investigation!

  • Pick random pair of nodes
  • Use BFS to find distance between them
  • (repeat)

Small World Parameters

  • size of network: $n = 1,000,000$
  • number of “out” edges: $k = 8$
    • by default, vertex $i $ forms edge with $i + 1, i + 2, \ldots, i + k$
  • probability of rewiring: $p $
    • for each out edge, with probability $p$ pick a random vertex $j$ from $0$ to $n - 1$ and add edge from $i $ to $j $

Average Distance to Random Node