1
Graphs
Outline and Required Reading:
• The Graph ADT (§ 14.1)
CSE 2011, Winter 2017
Instructor: N. Vlajic
2
Graphs
Graph – most general data structure - consists of set of vertices (V)
and a set of edges (E) that connect vertices - G = {V, E}
• vertices can also be referred to as nodes
• in Graph ADT, both vertices and edges are Positions, i.e
they store elements (e.g. v.element(), e.element())
B
vertex =
city
computer
Web-page
task
E
A
D
C
edge =
road
cable
hyperlink
transition state
F
Application – road maps, computer networks, WWW, activity charts
(Are two points connected? What is the shortest path between them? etc. )
3
Graphs: Types of Edges
Edge – connects two vertices (u and v), can be undirected or directed
(1) undirected edge {u,v} does not indicated direction – we can
“travel” in either direction along the edge
(2) directed edge (u,v) has a direction – we can “travel” only in
one direction along the edge
Self-Loop – edge (undirected or directed) that originates and ends
at the same vertex
Parallel Edges – two undirected edges with the same end vertices, or
two directed edges with the same origin & destination
A
B
undirected edge
C
D
directed edge
E
self-edge
F
G
parallel edges
4
Graphs: Types of Graphs
Undirected Graph – graph whose edges are all undirected
Directed Graph – graph whose edges are all directed
Mixed Graph – graph that has both directed and undirected edges
• undirected and mixed graphs can be converted into a
directed graph by replacing each undirected edge {u,v}
by the pair of directed edges (u,v) and (v,u)
Simple Graph – graph without parallel edges and self-loops
B
B
F
E
A
C
F
E
A
D
C
D
Graphs: Types of Vertices
5
End Vertices (Endpoints) – two vertices joined by the edge
of an Edge
• in a directed edge, the first endpoint
is origin and the other is destination
Adjacent Vertices – endpoints of the same edge
Edge Incident on Vertex – vertex is one of the edge’s endpoints
Outgoing Edge of a Vertex – directed edge that originates at the vertex
Incoming Edge of a Vertex – directed edge that terminates at the vertex
Degree of a Vertex – deg(v) = number of incoming & outgoing edges of v
= number of edges incident on v
w
B
y
F
E
A
p
s
o
q
C
r
D
6
Graph: Basic Properties
Property 1: If G is a directed graph with m edges then
∑ deg(v) = 2m
v∈G
Proof
On the left-hand side, every directed edge (u,v) is counted twice – once
by its endpoint u and once by its endpoint v.
∑ deg(v) = deg(v ) + deg(v
1
2
) + ... + deg(v n )
v∈G
Property 2: If G is a directed graph, then
∑ indeg(v) = ∑ outdeg(v) = m
v∈G
Proof
v∈G
Each directed edge (u,v) contributes once to indeg sum of its origin, and
once to outdeg sum of its destination
w
B
y
F
E
A
p
s
o
q
C
r
D
7
Graphs: Basic Properties (cont.)
Property 3: In a simple undirected graph with n vertices and m edges
(no self-loops and parallel edges) the following must hold
n(n − 1)
m≤
2
Proof
max possible
number of edges in
a graph with n nodes
Assume all n vertexes are mutually connected (case with max # of edges).
Such a graph is called complete.
m = deg(v 1in G ) + deg(v 2in G- {v1 } ) + deg(v 3 in G- {v1,v 2 } ) + .. + deg(v nin G- {v1,v 2 ,..,v n-1 } )
m = (n - 1) + (n - 2) + ... + 1 =
B
B
A
E
C
n(n - 1)
2
D
B
A
E
C
D
A
E
C
D
8
Graphs: Paths and Cycles
Path – sequence of alternating vertices and edges – starts at a vertex
and ends at a vertex
Simple Path – each vertex in the path is distinct
Cycle – path with identical start and end vertex
Simple Cycle – each vertex in the cycle is distinct (except the 1st and
last)
w
w
B
y
p
E
A
o
q
C
F
s
t
r
D
B
y
E
A
o
q
C
F
p
s
t
r
D
Path = (F, s, D, t, E, p, F, w, B)
Cycle = (E, p, F, w, B, y, A, o, E, t, D, s, F, p, E)
Simple Path = (C, q, A, o, E)
Simple Cycle = (E, t, D, r, C, q, A, o, E)
9
Graphs: Subgraphs
Subgraph – graph in G whose vertices and edges are subset of vertices
and edges of G
Spanning Subgraph – subgraph in G that contains all the vertices of G
Connected Graph – for any two vertices there is a path between them
Connected Components of a Graph – maximal connected subgraphs
w
B
y
p
E
A
o
q
C
F
s
t
r
Graph G
D
y
B
F
E
A
q
C
r
y
B
E
A
D
Subgraph =
{A, B, C, D} U {m, q, r}
F
p
s
t
C
r
D
Spanning Subgraph =
= {A, B, C, D, E, F} U {m, o, p, s, t, r}
Graphs: Subgraphs (cont.)
Example 1 [ connected / unconnected directional graph ]
s
y
B
F
E
A
p
t
C
D
r
unconnected directional graph
s
y
B
F
E
A
t
C
r
p
D
connected directional graph
10
11
Graphs: Forests and Trees
Forest – undirected graph without cycles, possibly disconnected
Free Tree – connected forest (free = has no root)
• undirected connected graph without cycles
• connected components of a forest are free trees
Spanning Tree – subgraph of G that contains all of G’s vertices and
enough of its edges to form a tree
• in a graph with cycles there is more than one spanning tree
y
A
q
B
F
E
s
o
D
C
Forest
y
A
q
B
F
E
p
s
o
C
D
Spanning Tree
12
Graphs: Forests and Trees (cont.)
Example 2 [ spanning trees ]
w
B
y
E
A
o
q
F
p
s
t
C
r
B
D
E
C
Spanning Tree 3
D
B
F
E
C
Spanning Tree 4
D
Spanning Tree 2
B
A
E
C
Spanning Tree 1
F
F
A
D
C
B
A
E
A
Graph G
B
F
F
E
A
D
C
D
Spanning Tree 5
Are there any other spanning threes besides these? Find them!
13
Graphs: Forests and Trees (cont.)
Property 4: Let G be an undirected graph with n vertices and m edges
(1) If G is a (spanning) tree
m = n −1
(2) If G is a forest
m ≤ n −1
(3) If G is connected
m ≥ n −1
B
(connects all vertices +
+ no cycles)
(not necessarily connected +
no cycles)
(connects all vertices +
+ possibly with cycles)
F
B
F
E
A
E
A
C
D
Add one edge ⇒ graph gets cycle!
C
D
Remove one edge ⇒ graph gets disconnected!
Graphs: Forests and Trees (cont.)
Proof of Property 4.1
Consider a spanning tree T. Set m = 0, n = 0.
Consider any leaf of T. This vertex is adjacent to exactly one edge.
Remove this vertex and edge contributing 1 each to the number of
vertices and edges. (m++, n++).
Continue removing leaf / edge pairs until we are left with just a single
edge.
A graph with a single edge has one more vertex than edge, hence the
overall number of edges is one less than the total number of vertices.
(n=m+1)
B
F
E
A
C
D
14
© Copyright 2026 Paperzz