Module9 Graphs
Module9 Graphs
26/12/2021
College of Computing and
Informatics
BS in Computer Science
CS243
Discrete Mathematics in Computer Science
Discrete Mathematics in Computer Science
Week 10
Graphs
Required Reading
1. Chapter 10; 10.1, 10.6 – 10.8 “Discrete Mathematics and Its
Application”
2. Chapter 10 Key Terms and Results
Recommended Reading
Difference between graph and tree
https://www.geeksforgeeks.org/difference-between-graph-and-tree/
Recommended videos
Discrete Mathematics 1: Introduction to Graphs
https://www.youtube.com/watch?v=PLU1sLuVnKI
This Presentation is mainly dependent on the textbook: Discrete Mathematics and Its Application by Kenneth H. Rosen
10.1 Graphs and Graph Models
• Definition 1: A graph G = (V, E) consists of V, a nonempty set of vertices (or nodes), and E, a set of
edges; each edge has either one or two vertices associated with it, called its endpoints; an edge is
said to connect its endpoints.
• Remark 1: The set of vertices V of a graph G may be infinite. A graph with an infinite vertex set or an
infinite number of edges is called an infinite graph, and in comparison, a graph with a finite vertex set
and a finite edge set is called a finite graph. In this book, we will usually consider only finite graphs.
Remark 2: A graph in which each edge connects two different vertices and where no two edges
connect the same pair of vertices is called a simple graph.
Note that in a simple graph, each edge is associated with an unordered pair of vertices, and no other edge is associated
with this same edge. Consequently, when there is an edge of a simple graph associated with {u, v}, we can also say,
without possible confusion, that {u, v} is an edge of the graph.
Graphs and Graph Models
• Now suppose that a network is made up of data centers and communication links between
computers. We can represent the location of each data center by a point and each communications
• In general, we visualize graphs by using points to represent vertices and line segments, possibly
curved, to represent edges, where the endpoints of a line segment representing an edge are the
• A computer network may contain multiple links between data centers, as shown in Figure
2. To model such networks, we need graphs that have more than one edge connecting
the same pair of vertices. Graphs that may have multiple edges connecting the same
vertices are called multigraphs.
• Sometimes a communications link connects a data center with itself, perhaps a feedback
loop for diagnostic purposes. Such a network is illustrated in Figure 3. To model this network,
we need to include edges that connect a vertex to itself. Such edges are called loops.
• Definition 2 A directed graph (or digraph) (V, E) consists of a nonempty set of vertices V and a
set of directed edges (or arcs) E.
Each directed edge is associated with an ordered pair of vertices. The directed edge
associated with the ordered pair (u, v) is said to start at u and end at v.
• When a directed graph has no loops and has no multiple directed edges, it is called a
simple directed graph.
• Directed graphs that may have multiple directed edges are called directed multigraphs.
• A graph with both directed and undirected edges is called a mixed graph.
Graphs and Graph Models- Graphs
10.1.1 Graph Models
• Social Networks: Graphs are extensively used to model social structures based on
different kinds of relationships between people or groups of people. These social
structures, and the graphs that represent them, are known as social networks.
Graph Models
• Example 1 What is the length of the shortest path between a and z in the weighted
• We now give the details of Dijkstra’s algorithm. It begins by labeling a with 0 and the other
vertices with ∞. We use the notation L0(a) = 0 and L0(v) = ∞ for these labels before any
iterations have taken place (the subscript 0 stands for the “0th” iteration).
• These labels are the lengths of shortest paths from a to the vertices, where the paths contain
only the vertex a. (Because no path from a to a vertex different from a exists, ∞ is the length of
the shortest path between a and this vertex.)
Dijkstra’s algorithm
• Dijkstra’s algorithm proceeds by forming a distinguished set of vertices. Let Sk denote this set
after k iterations of the labeling procedure. We begin with S0 = ∅. The set Sk is formed from
• Once u is added to Sk, we update the labels of all vertices not in Sk, so that Lk(v), the label of
the vertex v at the kth stage, is the length of the shortest path from a to v that contains
vertices only in Sk (that is, vertices that were already in the distinguished set together with
u).
• Note that the way we choose the vertex u to add to Sk at each step is an optimal choice at each step, making
this a greedy algorithm. (We will prove shortly that this greedy algorithm always produces an optimal
solution.)
Dijkstra’s algorithm
• Dijkstra’s algorithm proceeds by forming a distinguished set of vertices. Let Sk denote this set
after k iterations of the labeling procedure. We begin with S0 = ∅. The set Sk is formed from
• Once u is added to Sk, we update the labels of all vertices not in Sk, so that Lk(v), the label of
the vertex v at the kth stage, is the length of the shortest path from a to v that contains
vertices only in Sk (that is, vertices that were already in the distinguished set together with
u).
• Note that the way we choose the vertex u to add to Sk at each step is an optimal choice at each step, making
this a greedy algorithm. (We will prove shortly that this greedy algorithm always produces an optimal
solution.)
Dijkstra’s algorithm
• Let v be a vertex not in Sk. To update the label of v, note that Lk(v) is the length of the
shortest path from a to v containing only vertices in Sk. The updating can be carried out
efficiently when this observation is used: The shortest path from a to v containing only
elements of Sk is either the shortest path from a to v that contains only elements of Sk−1 (that
is, the distinguished vertices not including u), or it is the shortest path from a to u at the (k −
1)st stage with the edge {u, v} added. In other words,
L k (a, v) = min{L k − 1 (a, v), L k − 1 (a, u) + w(u, v)} ,
where w(u, v) is the length of the edge with u and v as endpoints. This procedure is iterated
by successively adding vertices to the distinguished set until z is added. When z is added to
the distinguished set, its label is the length of the shortest path from a to z.
Dijkstra’s algorithm
Dijkstra’s algorithm
• EXAMPLE 2 Use Dijkstra’s algorithm to find the length of the shortest path between
the vertices a and z in the weighted graph displayed in Figure 4(a).
Dijkstra’s algorithm
Solution: The steps used by Dijkstra’s
algorithm to find the shortest path
between a and z are shown in Figure 4.
• For instance, the planar representation of the graph shown in Figure 8 splits the plane into
six regions. These are labeled in the figure. Euler showed that all planar representations of a
graph split the plane into the same number of regions.
• He accomplished this by finding a relationship among the number of regions, the number of
vertices, and the number of edges of a planar graph.
10.7.2 Euler’s Formula
• EULER’S FORMULA Let G be a connected planar simple graph with e edges and v vertices.
Let r be the number of regions in a planar representation of G. Then r = e − v + 2.
• COROLLARY 2 If G is a connected planar simple graph, then G has a vertex of degree not
exceeding five.
Solution: The graph K5 has five vertices and 10 edges. However, the inequality e ≤ 3v − 6 is
not satisfied for this graph because e = 10 and 3v − 6 = 9. Therefore, K5 is not planar
10.8 Graph Coloring
• EXAMPLE What are the chromatic numbers of the graphs G shown in Figure 3?
• Solution: The chromatic number of G is at least three because the vertices a, b, and c must
be assigned different colors.
• To see if G can be colored with three colors, assign red to a, blue to b,
and green to c. Then, d can (and must) be colored red because it is
adjacent to b and c.
• Furthermore, e can (and must) be colored green because it is adjacent
only to vertices colored red and blue, and f can (and must) be colored
blue because it is adjacent only to vertices colored red and green.
• Finally, g can (and must) be colored red because it is adjacent only to
vertices colored blue and green. This produces a coloring of G using
exactly three colors.
Graph Coloring
• THEOREM 1 THE FOUR COLOR THEOREM The chromatic number of a planar graph is
no greater than four.
• EXAMPLE What are the chromatic numbers of the graphs H shown in Figure 4?
• Solution:
• The graph H is made up of graph G with an edge connecting a and g.
• Any attempt to color H using three colors must follow the same
reasoning as that used to color G, except at the last stage, when all
vertices other than g have been colored.
• Then, because g is adjacent (in H) to vertices colored red, blue, and
green, a fourth color, say brown, needs to be used.
• Hence, H has a chromatic number equal to 4. A coloring of H is
shown in Figure 4.
Graph Coloring
• EXAMPLE What is the chromatic number of the graph Cn, where n ≥ 3? (Recall