2. What is a graph?
Seattle
New York
L.A.
Tokyo
Sydney
Seoul 128
140
181
30
16
56
3. Graphs
graph: A data structure containing:
a set of vertices V, (sometimes called nodes)
a set of edges E, where an edge
represents a connection between 2 vertices.
Graph G = (V, E)
an edge is a pair (v, w) where v, w are in V
the graph at right:
V = {a, b, c, d}
E = {(a, c), (b, c), (b, d), (c, d)}
degree: number of edges touching a given vertex.
at right: a=1, b=2, c=3, d=2
a
c
b
d
4. Graph examples
For each, what are the vertices and what are the edges?
Web pages with links
Methods in a program that call each other
Road maps (e.g., Google maps)
Airline routes
Facebook friends
Course pre-requisites
Family trees
Paths through a maze
5. Paths
path: A path from vertex a to b is a sequence of edges that can be
followed starting from a to reach b.
can be represented as vertices visited, or edges taken
example, one path from V to Z: {b, h} or {V, X, Z}
What are two paths from U to Y?
path length: Number of vertices
or edges contained in the path.
neighbor or adjacent: Two vertices
connected directly by an edge.
example: V and X
X
U
V
W
Z
Y
a
c
b
e
d
f
g
h
6. Weighted graphs
weight: Cost associated with a given edge.
Some graphs have weighted edges, and some are unweighted.
Edges in an unweighted graph can be thought of as having equal weight (e.g. all 0, or all 1,
etc.)
Most graphs do not allow negative weights.
example: graph of airline flights, weighted by miles between cities:
ORD
PVD
MIA
DFW
SFO
LAX
LGA
HNL
7. Directed graphs
directed graph ("digraph"): One where edges are one-way
connections between vertices.
If graph is directed, a vertex has a separate in/out degree.
A digraph can be weighted or unweighted.
Is the graph below connected? Why or why not?
a
d
b
e
g
f
c
8. 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
...thus in the Mathematical field of graph theory, a spanning tree of the graph is
a connected sub-graph in which there are no cycles.
A Spanning tree for a graph has no cycles but still connects to every house.
If G is a connected graph with n vertices and m edges, spanning tree of G must
have n-1 edges, and no. of edges deleted from G to get a spanning tree must be
m-(n-1)=m-n+1.
A Graph may have many spanning trees; for instance the complete graph on
four vertices.
A connected,
undirected graph
Four of the spanning trees of the graph
8
9. Finding Spanning Trees
Spanning trees can be found in linear time by simply
performing breadth-first search or depth-first search.
These graph search algorithms are only dependent on
the number of vertices in the graph, so they are quite
fast.
Breadth-first search will use a queue to hold vertices to
explore later.
depth-first search will use a stack.
In either case, a spanning tree can be constructed by
connecting each vertex with the vertex that was used to
discover it.
10. Unfortunately, search algorithms are not well suited for
parallel or distributed computing, an area in which
spanning trees are popular.
There are, however, algorithms that are designed to find
spanning trees in a parallel setting.
11. 4
Finding a spanning tree
To find a spanning tree of a graph,
Pick an initial node and call it part of the spanning tree
do a search from the initial node:
each time you find a node that is not in the spanning tree, add to the
spanning tree both the new node and the edge you followed to get to it.
An undirected graph One possible
result of a BFS
starting from top
One possible
result of a DFS
starting from top
12. Properties of a spanning tree
There are a few general properties of spanning trees.
A connected graph can have more than one spanning tree.
All possible spanning trees for a graph G have the same number of edges and
vertices.
Spanning trees do not have any cycles.
Spanning trees are all minimally connected. That is, if any one edge is
removed, the spanning tree will no longer be connected.
Adding any edge to the spanning tree will create a cycle. So, a spanning tree is
maximally acyclic.
Spanning trees have |n|-1 edges, where |n| is the number of vertices