Chapter 6Graphs and Treesnotes.en.pdf:82

Undirected & Directed Graphs

Vertices and edges — ordered or not

Def 6.1.1Undirected graphDef 6.1.2Directed graph
Concept

A graph is one of the simplest yet most powerful structures in mathematics.
It consists of two pieces of data: a set of vertices (the "dots") and a set of
edges (the "lines" between them). The same idea shows up everywhere in AI:
knowledge graphs link entities, search spaces link states, and neural networks link
neurons.

According to Definition 6.1.1 (notes p. 82), an undirected graph is a pair
V,E\langle V, E \rangle where VV is a set of vertices and the edges come from a
specific kind of set:

E{{v,v}v,vVvv}E \subseteq \{\{v, v'\} \mid v, v' \in V \land v \neq v'\}

Because edges are sets of two, the edge {a,b}\{a, b\} is the same as {b,a}\{b, a\}
direction does not matter. Picture a friendship network: if Alice is friends with
Bob, then Bob is also friends with Alice.

In contrast, Definition 6.1.2 gives us a directed graph (digraph): the same
pair V,E\langle V, E \rangle, but with ordered pairs:

EV×VE \subseteq V \times V

Now the edge (a,b)(a, b) is distinct from (b,a)(b, a): it points from aa to bb. Picture
a Twitter following: Alice can follow Bob without Bob following her back.

Why care? In AI:
- A knowledge graph (Google, Wikipedia, chatbots) is a directed graph whose
nodes are concepts and edges are relations ("Paris", "capital-of", "France").
- The state space of a search problem is a graph: nodes are configurations,
edges are moves.
- A neural network is a layered directed graph of neurons with weighted edges.

The same definition (vertices + edges) is flexible enough to model all of them.

Animation — graph basics
Transcript — click a line to jump7 cues
  1. 0.0sGraphs: vertices and edges
  2. 1.1sFive cyan dots a, b, c, d, e appear in a line
  3. 2.4sThree grey undirected edges draw: a-b, b-c, c-d
  4. 4.7sUndirected: edges are pairs {a,b}
  5. 5.4sUndirected edges fade out
  6. 6.0sThree cyan directed arrows replace them
  7. 7.4sDirected: edges are ordered pairs (a,b)
Adjacency-list representation

An adjacency list represents a graph G=V,EG = \langle V, E \rangle as a function from each vertex to its neighbour set (or list).

In SML:


type graph = int list list;
(* graph[i] = list of vertices adjacent to i *)

Example. The graph with vertices {1,2,3,4}\{1, 2, 3, 4\} and edges {(1,2),(2,3),(3,4),(4,1)}\{(1,2), (2,3), (3,4), (4,1)\} (a 4-cycle) has adjacency list:


val G = [[2,4], [1,3], [2,4], [3,1]] : graph;

Operations on adjacency lists:
- isTree G: a graph is a tree iff it's connected and has exactly n - 1 edges.
- size G: V=|V| = length G.
- depth v G: longest path from vv in GG.
- getNeighbors v G: nth v G.

Worked example. Size of the 4-cycle above: length G = 4. ✓

Used in assignment 6.6.

Practice — score 100% to advance
Multiple choice
Q1
What are the two components of a graph G=V,EG = \langle V, E \rangle?
Q2
In an undirected graph, {a,b}\{a, b\} and {b,a}\{b, a\} are…
Q3
In a directed graph, (a,b)(a, b) and (b,a)(b, a) are…
Q4
Which is a typical AI use of graphs?
Q5
An edge of an undirected graph is mathematically a…
Q6
An adjacency list [[2,4],[1,3],[2,4],[3,1]] represents a graph with how many vertices?
Fill in the blank
Q1
A graph is a pair V,\langle V, \rangle where VV is the vertex set.
Q2
In an undirected graph, edges are 2-element ___ of vertices.
Q3
In a directed graph, edges are ___ pairs in V×VV \times V.
Match definitions
Match each concept on the left to its definition on the right.
Loading…