Graph Theory
Graph Theory
1
Definition Let x, y be (not necessarily distinct) vertices in an undirected
graph G = (V, E). An x−y walk in G is a loop-free finite alternating sequence
2
a) If no edge in the x − y walk is repeated, then the walk is an x − y trail.
A closed x − x trail is called a circuit.
b) If no vertex of the x − y walk occurs more than once, then the walk is
called an x − y path.
When x = y, the term cycle is used to describe a closed path.
Definition Let G = (V, E) be an undirected graph. We call G connected if
there is a path between any two distinct vertices of G.
A graph that is not connected is called disconnected.
Example Given undirected graph G on V = {a, b, c, d, e, f, g}, if G is com-
posed of pieces (with vertex sets V1 = {a, b, c, d}, V2 = {e, f, g} and edge sets
E1 = {{a, b}, {a, c}, {a, d}, {b, d}, E2 = {{e, f }, {f, g}} that are themselves
connected, and these pieces are called the (connected) components of the
graph.
3
A graph is connected if and only if it has only one component.
Definition For any graph G = (V, E), the number of components of G is
denoted by κ(G).
Definition Let V be a finite non-empty set. We say that the pair (V, E) de-
termines a multigraph G with vertex set V and edge E if for some x, y ∈ V ,
there are two or more edges in E of the form
(a) (x, y) (for directed multigraph) or
(b) {x, y} (for an undirected multigraph).
If G = (V, E) is a graph (directed or undirected), then G1 = (V1 , E1 ) is called
a subgraph of G if ∅ =
̸ V1 ⊆ V and E1 ⊆ E, where each edge in E1 is incident
with vertices in V1 .
Definition Given a (directed or undirected) graph G = (V, E), let G1 =
(V1 , E1 ) be a subgraph of G. If V1 = V , then G1 is called a spanning sub-
graph of G.
Definition Let G = (V, E) be a graph (directed or undirected). If ∅ ̸= U ⊆
V , the subgraph of G induced by U is the subgraph whose vertex is U and
which contains all edges (from G) of either of the form
a) (x, y)∀x, y ∈ U (when G is directed)
or b) {x, y} for x, y ∈ U (when G is undirected).
We denote this subgraph by < U >.
A subgraph G′ of a graph G = (V, E) is called an induced subgragh if there
̸ U ⊆ V , where G′ =< U >.
exists ∅ =
4
Definition Let V be a set of n vertices. The complete graph on V ,
denoted Kn , is a loop-free undirected graph, where for all a, b ∈ V, a ̸= b,
there is an edge {a, b}.
Definition Let G1 = (V1 , E1 ) and G2 = (V2 , E2 ) be two undirected graphs.
A function f : V1 → V2 is called a graph isomorphism if
a) f is one-to-one and onto and
b) for all a, b ∈ V1 , {a, b} ∈ E1 if and only if {f (a), f (b)} ∈ E2 . When such
a function exists, G1 and G2 are called isomorphic graphs.
5
a11 a12 a13 · · · a1n
a21
a22 a23 · · · a2n
. ..
.. ··· ··· .
a31 a32 a33 · · · a3n
1; if there is an edge from′ vi′ to′ vj′ ,
where aij =
0
otherwise
6
0 1 1 0 0 1
1 0 1 0 0 0
1 1 0 1 0 0
The adjacency matrix is
0 0 1 0 1 0
0 0 0 1 0 0
1 0 0 0 0 0
and ii)
7
Consider the gragh G
The incidence matrix of the graph G is of order (5×7). The incidence matrix
relative to the ordering v1 , v2 , v3 , v4 , v5 and e1 , e2 , e3 , e4 , e5 , e6 , e7 is given
below:
1 0 0 0 1 0 1
1 1 0 0 0 1 0
I= 0 1 1 0 0 0 0
0 0 1 1 1 1 0
0 0 0 1 0 0 1
Path Matrix
Suppose that G is a simple graph with n-vertices. Then the (n × n) matrix
1; if there is a path fromvi to vj
P = [pij ](n×n) defined by: Pij =
0; otherwise
8
v3 , v4 , v5 is given as
0 1 0 1 1
1 0 0 1 1
P = 0 0 0 0 0
1 1 0 0 1
1 1 0 1 0
9
G1 is strongly connected, G2 is weakly connected, G3 is connected, and G4
is disconnected.
Planar Graphs A graph (or multigraph) G is called planar if G can be
drawn in the plane with its edges intersecting only at the vertices of G. Such
a drawing of G is called an embedding of G in the plane.
Look at the graphs below:
10
In (b) we have a nonplanar graph; the edges {x, z} and {w, y} overlap at a
point other than a vertex. However, we can redraw the graph as shown in
(c).
k5 is nonplanar.
Theorem If a, b are distinct vertices in a tree T = (V, E), then there is a
unique path that connects these vertices.
Proof Since T is connected, there is at least one path in T that connects
a and b. If there were more, from two such paths some of the edges would
form a cycle. But T has no cycles.
11
Suppose we are given a connected, undirected, weighted graph. This is a
graph G = (V, E) together with a function w : ER that assigns a weight to
each edge e.
The minimum (weight) spanning tree (MST) problem is given a connected,
undirected, weighted graph G = (V, E, w), to find spanning tree of minimum
weight, where the weight of a tree T is defined as:
P
w(T ) = e∈E(T ) w(e)
Kruskal’s algorithm
- It works with edges
- Two steps
* Sort edges by increasing edge weight
* Select the first |V | − 1 edges that do not generate a cycle.
Example Use Kruskal’s algorithm to find a minimum spanning tree (MST)
12
Solution
1. Sort edges by increasing edge weight:
2. Select the first |V | − 1 edges which do not generate a cycle.
13
Prim’s algorithm
- It works with nodes (instead of edges)
- Two steps
* Select node with minimum distance
* Update distances of adjacent, unselected nodes
Example Use Prim’s algorithm to find a minimum spanning tree (MST) for
this graph:
Solution
1. Start with any node, say D
2. Update distances of adjacent, unselected nodes
3. Select node with minimum distance
Repeat these steps until all nodes are included
K: Whether in the tree
dv : distance to the tree
pv : Closest node that is in the tree
14
K dv pv
A
B
C F 3 D
D T 0 -
E F 25 D
F F 18 D
G F 2 D
H
K dv pv
A
B
C F 3 D
D T 0 -
E F 7 G
F F 18 D
G T 2 D
H F 3 G
Next we include C and update the table for the unselected nodes.
15
K dv pv
A
B F 4 C
C F 3 D
D T 0 -
E F 7 G
F F 3 C
G T 2 D
H F 3 G
Suppose we are given a directed weighted graph G = (V, E, w) with node set
V , edge set E, and weight set w. We are given starting node s ∈ V .
The shortest path problem is the problem of determining the shortest path
from node s to all other nodes in the graph.
The weights on the edges are also called costs.
16
Dijkstra’s algorithm
Dijkstra’s algorithm solves shortest path problems for graphs with non-
negative costs, i.e wij ≥ 0 for all (i, j) ∈ E.
Importance of Dijkstra’s algorithm
- Is applied to automatically find directions between physical locations, such
as driving directions on websites like Google Maps.
Steps to implement
Step 1. Initialization
* Assign distance 0 to node s, and label as permanent [The state of node
s is (0, p)]
* Assign to every node a distance value of ∞ and label them as temporary
* Designate the node s as the current node
Step 2. Distance value update and current node designation update
Let i be the index of the current node
(1) Find the set J of nodes with temporary labels that can be reached from
the current node i by an edge (i, j). Update the distance values of these
nodes.
* For each j ∈ J, the distance value dj of node j is updated as follows
new dj = min{dj , dj + wij } where wij is the weight of edge (i, j)
(2) Determine a node j that has the smallest distance value dj among all
nodes j ∈ J, find j ∗ such that minj∈J dj = dj ∗
Step 3. Termination Criterion
If all nodes that can be reached from node s have been permanently labeled,
17
then stop - we are doen.
If we can’t reach any temporary labeled node from the current node, then
all the temporary labels become permanent - we are done.
Otherwise, go to step 2.
Example Use Dijkstra’s algorithm to compute the distance to all nodes from
node 1.
Step 1. Initialization
* Node 1 is designated as the current node
* The state of node 1 is (0, p)
* Every other node has state (∞, t)
Step 2.
* Nodes 2, 3, and 6 can be reached from the current node 1
* Update distance values for these nodes
d2 = min{∞, 0 + 7} = 7
d3 = min{∞, 0 + 9} = 9
18
d6 = min{∞, 0 + 14} = 14
Now among the nodes 2, 3, and 6, node 2 has the smallest distance value.
The status label of node 2 changes to permanent, so its state is (7, p) while
the status of 3 and 6 remains temporary.
* Node 2 becomes the current node.
Step 3
Not all nodes have been reached from node 1, hence we perform another
iteration (back to step 2).
Another implementation of step 2
* Nodes 3 and 4 can be reached from the current node 2
* Update distance values for these nodes
d3 = min{9, 7 + 10} = 9
d4 = min{∞, 7 + 15} = 22
* Node 3 has the smallest distance value
19
* The status label of node 3 changes to permanent, while the status of node
4 remains temporary
* Node 3 becomes the current node
We are not done, so we perform another step 2.
Another step 2
* Nodes 6 and 4 can be reached from the current node 3
* Update distance values for them
d4 = min{22, 9 + 11} = 20
d6 = min{14, 9 + 2} = 11
Node 6 has the smallest distance value. The status label of node 6 changes
to permanent, while the status of 4 remains temporary.
* Node 6 becomes the current node.
We are not done, so we perform another step 2.
Another step 2
* Node 5 can be reached from the current node 6
20
* Update distance value for node 5
d5 = min{∞, 11 + 9} = 20
* Node 5 is the only candidate, so its status changes to permanent and be-
comes the current node.
From node 5 we can’t reach any other node. Hence, node 4 gets permanently
labeled and we are done.
21