Lecture 9 - Graph Algorithms and Spanning trees
Lecture 9 - Graph Algorithms and Spanning trees
Spanning trees
What’s a Graph?
• A bunch of vertices connected by edges.
vertex
2
• Why Graph Algorithms?
• They’re fun. 1 3
• They’re interesting.
4
• They have surprisingly many applications. edge
Graph Types
• A directed graph edges’ allow travel in one direction
• An undirected graph edges’ allow travel in either direction
Basic Concepts
• A graph is an ordered pair (V, E).
• V is the set of vertices. (You can think of them as integers 1, 2, …, n.)
• E is the set of edges.
• An edge is a pair of vertices: (u, v).
• Note:
• Since E is a set, there is at most one edge between two vertices.
• Edges can be labeled with a weight: 10
Concepts: Directedness
• In a directed graph, the edges are “one-way.”
• So an edge (u, v) means you can go from u to v, but not vice versa.
a self-loop
u is adjacent to v. u is adjacent to v.
v is adjacent to u and w. v is adjacent to w.
w is adjacent to v.
Concepts: Degree
• Undirected graph: The degree of a vertex is the number of edges
touching it. degree 4
in-degree 1, out-degree 2
Concepts: Path
• A path is a sequence of adjacent vertices.
• The length of a path is the number of
edges it contains. 2 Is there a path from 1 to 4?
• i.e. one less than the number of What is its length?
vertices. What about from 4 to 1?
1 3
• We write u v if there is path from u
How many paths are there
to v. (The correct symbol, a wiggly from 2 to 3? From 2 to 2?
arrow, is not available in standard fonts.) 4 From 1 to 1?
We say v is reachable from u.
More Terminology
• Simple path: no repeated vertices a b
c bec
d e
• Cycle: simple path, except that the last vertex is the same as the first
vertex a b
acda
c
d e
Concepts: Cycle
• A cycle is a path of length at least 1 from a vertex to itself.
• A graph with no cycles is acyclic.
• A path with no cycles is a simple path.
2
1 3
4
• The path <2, 3, 4, 2> is a cycle.
Concepts: Connectedness
15
Spanning trees
• Suppose you have a connected undirected graph
• Connected: every node is reachable from every other node
• Undirected: edges do not have an associated direction
• Then a spanning tree of the graph is a connected sub-graph in which there are no
cycles.
• A spanning tree for a connected, undirected graph G is a graph S consisting of the
nodes of G together with enough edges of G such that:
• S is connected, and
• S is acyclic.
Spanning trees