Paths . . . . . . . . . . . . . . . . . . . . . . .
Connectivity in Undirected Graphs . . .
Connectivity in Directed Graphs . . . . .
Depth-First Search . . . . . . . . . . . . . .
Depth-First Search Running Time . . . .
Proof of Depth-First Search Algorithm
Depth-First Search and Connectivity . .
Graphs
Weighted Graphs
Weighted Graphs . . . . . . . . . .
Weighted Graph: Example 1 . .
Weighted Graph: Example 2 . .
Dijstra’s Algorithm: Part 1 . . .
Dijstra’s Algorithm: Part 2 . . .
Traveling Salesperson Problem .
Graphs
Definitions . . . . . . . . . . . . . . .
Example of an Undirected Graph
Example of a Directed Graph . . .
Some Terminology . . . . . . . . . .
Adjacency List 1 . . . . . . . . . . .
Adjacency List 2 . . . . . . . . . . .
Adjacency Matrix 1 . . . . . . . . .
Adjacency Matrix 2 . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
Paths and Connectivity
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
2
2
3
4
5
6
7
8
9
10
1
2
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
10
11
12
13
14
15
16
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
17
17
18
19
20
21
22
Graphs
2
Example of a Directed Graph
Definitions
FW
A graph is a set of vertices V and a set of edges E.
[Differs from book.] In an undirected graph, each edge is a two-element subset
of V . The book uses the term simple graph for this structure.
A
[Differs from book.] In a directed graph, each edge is an ordered pair from V .
That is, the edges are a relation on V . This is close to what the book calls a
simple directed graph, except that loops are allowed in a directed graph.
CS 2233 Discrete Mathematical Structures
D
EP
Graphs – 2
H
SA
Example of an Undirected Graph
FW
CC
D
E = {(A, D), (EP, A), (F W, A), (A, H), . . .}
CS 2233 Discrete Mathematical Structures
Graphs – 4
A
Some Terminology
Undirected graphs:
EP
H
SA
CC
Directed graphs:
E = {{A, D}, {A, EP }, {A, F W }, {A, H}, . . .}
CS 2233 Discrete Mathematical Structures
{u, v} is an edge between u and v.
The degree of v is how many edges v is in.
Sum of degrees = twice the number of edges.
An even number of vertices have an odd degree.
Graphs – 3
(u, v) is an edge from u to v.
The in-degree of v is how many edges to v.
The out-degree of v is how many edges from v.
Sum of in-degrees = sum of out-degrees = number of edges
CS 2233 Discrete Mathematical Structures
3
Graphs – 5
4
Adjacency List Representation, Part 1
Adjacency Matrix Representation, Part 2
Undirected Graphs
Directed Graphs, Relations
0 0 1 0 0
A
CC
0 0 0 0 0
0 0 0 0 1
D
EP
1 0 0 0 1
FW
1 0 1 1 0
0 1 0 0 0
H
0 0 0 1 0
SA
Vertex
A
CC
D
EP
FW
H
SA
Adjacent Vertices
D, EP, F W, H, SA
H, SA
A, F W
A, F W, SA
A, D, EP
A, CC, SA
A, CC, EP, H
1
0
0
0
0
1
1
1
1
0
0
0
1
1
CS 2233 Discrete Mathematical Structures
CS 2233 Discrete Mathematical Structures
Graphs – 9
Graphs – 6
Paths and Connectivity
Adjacency List Representation, Part 2
Directed Graphs, Relations
10
Paths
Initial Vertex Terminal Vertices
A
D, H, SA
CC
SA
D
FW
EP
A, F W
FW
A, D, EP
H
CC, H, SA
SA
EP, H, SA
Definition of path [differs from book]:
Basis: v is a vertex implies v is a path of length 0 from v to v.
Recursive: a path v0, . . . , vn of length n from v0 to vn and an edge from vn
to vn+1 imply v0 , . . . , vn, vn+1 is a path of length n + 1 from v0 to vn+1.
A simple path does not repeat vertices.
CS 2233 Discrete Mathematical Structures
Graphs – 7
A circuit or cycle begins and ends at the same vertex. A simple circuit does
not otherwise repeat vertices.
CS 2233 Discrete Mathematical Structures
Graphs – 10
Adjacency Matrix Representation, Part 1
Undirected Graphs
A
0 0 1
CC
0 0 0
1 0 0
D
EP
1 0 0
FW
1 0 1
1 1 0
H
SA
1 1 0
1
0
0
0
1
0
1
1
0
1
1
0
0
0
1
1
0
0
0
0
1
1
1
0
1
0
1
0
CS 2233 Discrete Mathematical Structures
Graphs – 8
5
6
Connectivity in Undirected Graphs
Depth-First Search
An undirected graph is connected if there is a path between every pair of
vertices.
Call DFS(u, G, {u}) to do DFS from vertex u.
A connected component of an undirected graph is a set of vertices C such that
1. u ∈ C and v ∈ C imply paths between u and v.
2. v 6∈ C implies no paths between v and C.
A vertex v (edge e) in an undirected graph is a cut vertex (cut edge) if
removing v (removing e) results in more components.
A set of vertices/edges C is a vertex/edge cut if removing C results in more
components.
CS 2233 Discrete Mathematical Structures
Graphs – 11
Connectivity in Directed Graphs
A directed graph is strongly connected if there is a path between every pair of
vertices.
A directed graph is weakly connected if the underlying undirected graph is
connected.
procedure DFS (u, G, S)
assert G = (V, E) is a digraph, S ⊆ V , and u ∈ S
S ′ := S
for each v ∈ V such that (u, v) ∈ E
assert S ′ − S has all vertices reachable from u
using vertices in S ′ − S
if v 6∈ S ′ then
S ′ := S ′ ∪ {v}
S ′ := DFS (v, G, S ′)
assert S ′ − S has all vertices reachable from u
using vertices in V − S
return S ′
CS 2233 Discrete Mathematical Structures
Graphs – 13
Depth-First Search Running Time
Using a suitable adjacency list representation, DFS(u, G, {u}) is O(|V | + |E|),
that is, in the number of vertices plus the number of edges.
A strongly connected component in a directed graph is a set of vertices S such
that:
1. u ∈ S and v ∈ S imply a path from u to v and a path from v to u.
2. v 6∈ S implies no path from v to S or no path from S to v.
Note DFS(v, G, S ′) is called at most once for v because it is only called only if
v is not in S ′ , and v is added to S ′ just before the call.
Each vertex in a directed graph belongs to exactly one SCC.
DFS(v, G, S ′) processes each edge from v once.
CS 2233 Discrete Mathematical Structures
Graphs – 12
If accessing the list for a vertex v is O(1), then the running time will be
O(|V | + |E|).
CS 2233 Discrete Mathematical Structures
7
Graphs – 14
8
Weighted Graphs
Proof of Depth-First Search Algorithm
17
DFS(u, G, {u}) has all vertices reachable from u.
Note DFS only adds vertices, never removes any.
Weighted Graphs
Basis: DFS(u, G, {u}) includes u and each vertex on an edge from u.
A weighted graph is a graph in which each edge (u, v) has a weight w(u, v).
Each weight is a real number. Weights can represent distance, cost, time,
capacity, etc.
Induction: Assume DFS(u, G, {u}) has all vertices v that are within k ≥ 1 edges from u to v.
Show DFS(v, G, {u}) includes all vertices x where there is a path of length k + 1 from u to x.
Proof: Consider a path of length k + 1 from u to x.
There must be a vertex v with a path of length k from u to v and with an edge (v, x).
For v to be in DFS(u, G, {u}), both S ′ := S ′ ∪ {v} and
S ′ := DFS(v, G, S ′) must have been executed.
DFS(v, G, S ′) will ensure that x is added.
CS 2233 Discrete Mathematical Structures
The length of a path in a weighted graph is the sum of the weights on the
edges.
Dijkstra’s Algorithm finds the shortest paths from a given vertex in the graph.
Graphs – 15
The traveling salesperson problem is to find the shortest circuit that visits each
vertex once.
Depth-First Search and Connectivity
CS 2233 Discrete Mathematical Structures
Let G = (V, E), u ∈ V , and G−1 = (V, E −1).
Recall E −1 = {(v, u) | (u, v) ∈ E}.
Weighted Graph: Example 1
G is strongly connected if and only if
DFS(u, G, {u}) = V and DFS(u, G−1, {u}) = V .
Proof: Strongly connected means paths from u to all vertices, and paths from
all vertices to v.
1
b
−1
DFS(u, G, {u}) ∩ DFS(u, G , {u}) is a strongly connected component.
Proof: This expression includes all vertices with paths to and from u. Any
additional vertex in the SCC would also have a path to and from u.
CS 2233 Discrete Mathematical Structures
Graphs – 17
Graphs – 16
a
2
4
h
6
4
c
i
3
8 g
CS 2233 Discrete Mathematical Structures
9
8
d
7
5
e
9
7
5
f
Graphs – 18
10
Weighted Graph: Example 2
FW
Dijstra’s Algorithm: Part 2
30
D
EP
582
1
20
A
16
3
78
604
1
19
0
59
while S 6= V
assert v ∈ S implies L(v) is s.p.l. from a to v
u := a vertex such that u 6∈ S and
(L(v) < L(u) implies v ∈ S)
S := S ∪ {u}
for each vertex v where (u, v) ∈ E
if v 6∈ S and L(u) + w(u, v) < L(v) then
L(v) := L(u) + w(u, v)
assert v ∈ V implies L(v) is s.p.l. from a to v
SA
201
H
L(v) contains the shortest path length to v using vertices in S.
O(E + V log V ) assuming adjacency list and O(log V ) to find u.
7
Graphs – 21
21
6
14
CS 2233 Discrete Mathematical Structures
Traveling Salesperson Problem
CC
CS 2233 Discrete Mathematical Structures
Find the minimum weight circuit in a weighted, complete, undirected graph
that visits each vertex exactly once.
Graphs – 19
If the graph is not complete, assign a high value to edges that don’t exist.
Dijstra’s Algorithm: Part 1
Determining whether there is a solution less than a given weight W is an
NP-complete problem, roughly problems in which testing solutions is easy, but
finding solutions that pass the test is hard.
procedure dijkstra(G, a)
assert G = (V, E) is weighted graph and a ∈ V
assert (u, v) ∈ E implies w(u, v) > 0
for each vertex v in G
L(v) := ∞
L(a) := 0
S := ∅
assert L(a) is shortest path length from a to a
CS 2233 Discrete Mathematical Structures
Brute force algorithm: consider all possible (n − 1)! circuits.
CS 2233 Discrete Mathematical Structures
Graphs – 22
Graphs – 20
11
12
© Copyright 2026 Paperzz