Lecture 6
Lecture 6
Lecture 6
قومية كلية
ل معتمدة من الهيئة ال عتماد
ضمان جودة التعليم واال
• Proof by induction:
• Base case: For n = 1, 1 = 1(1+1)/2, which is true.
• Inductive step: Assume the statement is true for n = k: 1 + 2 + 3 + ... + k =
k(k+1)/2. Now, we need to prove it's true for n = k+1:
1 + 2 + 3 + ... + k + (k+1) = k(k+1)/2 + (k+1)
Simplifying the right side: (k+1)(k+2)/2
• Therefore, the statement is true for n = k+1.
Proof by induction (Cont.)
• Example: Use mathematical induction to show that
1 + 2 + 22 +· · ·+2n = 2n+1 − 1 for all nonnegative integers n.
• Proof by induction: Solution: Let P(n) be the proposition that 1 + 2 + 22 +· · ·+2n = 2n+1 − 1 for the integer n.
• BASIS STEP: P(0) is true because 20 = 1 = 21 − 1.
• INDUCTIVE STEP: We assume that P(k) is true for an arbitrary nonnegative integer k. That is, we assume that
1 + 2 + 22 +· · ·+2k = 2k+1 − 1.
• We must show that when we assume that P(k) is true, then P(k + 1) is also true. That is, we must show that
1 + 2 + 22 +· · ·+2k + 2k+1 = 2(k+1)+1 − 1 = 2k+2 − 1
assuming the inductive hypothesis P(k). Under the assumption of P(k), we see that
1 + 2 + 22 +· · ·+2k + 2k+1 = (1 + 2 + 22 +· · ·+2k) + 2k+1
= (2k+1 − 1) + 2k+1 using IH
= 2 · 2k+1 − 1
= 2k+2 − 1.
• We have completed the inductive step.
Strong induction
• Strong induction is a mathematical proof technique that extends
the principle of mathematical induction. Strong induction allows
you to assume that the statement holds for all values up to a
particular value.
• We can prove the following formula for the nth Fibonacci number
using strong induction:
F(n) = [(1 + √5)/2]^n - [(1 - √5)/2]^n / √5
Strong induction (Cont.)
• Base Cases:
• Inductive Conclusion: We need to prove that the formula holds for k+1. Using the recursive definition of Fibonacci numbers:
• Therefore, by strong induction, the formula holds for all non-negative integers n.
Recursive Definitions
• Recursively Defined Functions
• BASIS STEP: Specify the value of the function at zero.
• RECURSIVE STEP: Give a rule for finding its value at an integer from its values at smaller integers.
• We illustrate the workings of the function with a trace when the input is a = 5, b = 8.
• gcd(5, 8) = gcd(8 mod 5, 5) = gcd(3, 5).
• gcd(3, 5) = gcd(5 mod 3, 3) = gcd(2, 3)
• cd(2, 3) = gcd(3 mod 2, 2) = gcd(1, 2)
• gcd(1, 2) = gcd(2 mod 1, 1) = gcd(0, 1).
• Finally, to find gcd(0, 1) it uses the first step with a = 0 to find that gcd(0, 1) = 1.
• Consequently, the function finds that gcd(5, 8) = 1.
Recursive Algorithms (Cont.)
• Example: Give a recursive algorithm for Fibonacci Numbers
• Call Graphs: Graphs can be used to model telephone calls made in a network.
• Each telephone number is represented by a vertex and each telephone call is represented by a directed edge.
• The edge representing a call starts at the telephone number from which the call was made and ends at the telephone
number to which the call was made.
• We need directed edges because the direction in which the call is made matters.
• We need multiple directed edges because we want to represent each call made from a particular telephone number to a
second number.
Graph Models (Cont.)
• The Web Graph: The World Wide Web can be modelled as a directed graph where each Web page is
represented by a vertex and where an edge starts at the Web page a and ends at the Web page b if there is a
link on a pointing to b. Because new Web pages are created and others removed somewhere on the Web
almost every second, the Web graph changes on an almost continual basis.
• Citation Graphs: Graphs can be used to represent citations in different types of documents. In such graphs,
each document is represented by a vertex, and there is an edge from one document to a second document if
the first document cites the second in its citation list.
• Road Networks: Graphs can be used to model road networks. In such models, vertices represent intersections
and edges represent roads.
• ….etc.
Graph Terminology
• Two vertices u and v in an undirected graph G are called adjacent (or neighbors) in G if u and v are endpoints
of an edge e of G.
• Such an edge e is called incident with the vertices u and v and e is said to connect u and v.
• The set of all neighbours of a vertex v of G = (V ,E), denoted by N(v), is called the neighborhood of v. If A is a
subset of V , we denote by N(A) the set of all vertices in G that are adjacent to at least one vertex in A. So, N(A)
= Uv∈A N(v).
• The degree of a vertex in an undirected graph is the number of edges incident with it, except that a loop at a
vertex contributes twice to the degree of that vertex. The degree of the vertex v is denoted by deg(v).
Graph Terminology (Cont.)
• EXAMPLE:
• deg(a) = 2, deg(b) = deg(c) = deg(f ) = 4, deg(d ) = 1, deg(e) = 3, and deg(g) = 0.
• The neighborhoods of these vertices are N(a) = {b, f }, N(b) = {a, c, e, f }, N(c) =
{b, d, e, f }, N(d) = {c}, N(e) = {b, c, f }, N(f ) = {a, b, c, e}, and N(g) = ∅.
Graph Terminology (Cont.)
• EXAMPLE
• deg(a) = 4, deg(b) = deg(e) = 6, deg(c) = 1, and deg(d ) = 5.
• The neighborhoods of these vertices are N(a) = {b, d, e}, N(b) = {a, b, c, d, e}, N(c) = {b}, N(d) = {a, b, e},
and N(e) = {a, b, d}.
Graph Terminology (Cont.)
THE HANDSHAKING THEOREM Let G = (V ,E) be an undirected graph with m edges. Then
2𝑚 = 𝑑ⅇ𝑔 𝑣
𝑣𝜖𝑉
(Note that this applies even if multiple edges and loops are present.)
• Example: How many edges are there in a graph with 10 vertices each of degree six?
• Solution: Because the sum of the degrees of the vertices is 6 ・ 10 = 60, it follows that 2m = 60 where m
is the number of edges. Therefore, m = 30.
Graph Terminology (Cont.)
• When (u, v) is an edge of the graph G with directed edges, the vertex u is called the initial vertex of (u, v), and v
is called the terminal or end vertex of (u, v). The initial vertex and terminal vertex of a loop are the same.
• In a graph with directed edges the in-degree of a vertex v, denoted by deg− (v), is the number of edges with v as
their terminal vertex. The out-degree of v, denoted by deg+ (v), is the number of edges with v as their initial
vertex. (Note that a loop at a vertex contributes 1 to both the in-degree and the out-degree of this vertex.)
• EXAMPLE: Find the in-degree and out-degree of each vertex in the graph G with directed edges.
• Solution: The in-degrees in G are deg−(a) = 2, deg−(b) = 2, deg−(c) = 3, deg−(d) = 2, deg−(e) = 3, and
deg−(f ) = 0.
• The out-degrees are deg+ (a) = 4, deg+(b) = 1, deg+(c) = 2, deg+(d) = 2, deg+(e) = 3, and deg+(f ) = 0.
Graph Terminology (Cont.)
• THEOREM: Let G = (V ,E) be a graph with directed edges. Then
deg − 𝑣 = deg + 𝑣 = 𝐸
𝑣∈V 𝑣∈V
Some Special Simple Graphs
• Complete Graphs: A complete graph on n vertices, denoted by Kn, is a simple graph that contains exactly
one edge between each pair of distinct vertices.
Some Special Simple Graphs (Cont.)
• Cycles: A cycle Cn, n ≥ 3, consists of n vertices v1, v2, . . . , vn and edges {v1, v2}, {v2, v3}, . . . , {vn−1, vn},
and {vn, v1}.
Some Special Simple Graphs (Cont.)
• Wheels: We obtain a wheel Wn when we add an additional vertex to a cycle Cn, for n ≥ 3, and connect this
new vertex to each of the n vertices in Cn, by new edges.
Some Special Simple Graphs (Cont.)
• n-Cubes An n-dimensional hypercube, or n-cube, denoted by Qn, is a graph that has vertices representing
the 2n bit strings of length n. Two vertices are adjacent if and only if the bit strings that they represent differ in
exactly one bit position.
Some Special Simple Graphs (Cont.)
• A simple graph G is called bipartite if its vertex set V can be partitioned into two disjoint sets V1 and V2 such
that every edge in the graph connects a vertex in V1 and a vertex in V2 (so that no edge in G connects either
two vertices in V1 or two vertices in V2). When this condition holds, we call the pair (V1, V2) a bipartition of
the vertex set V of G.
• For example, consider the graph representing marriages between men and women in a village, where each
person is represented by a vertex and a marriage is represented by an edge. In this graph, each edge connects a
vertex in the subset of vertices representing males and a vertex in the subset of vertices representing females.
• The following figure show a bipartite graph, because its vertex set can be partitioned into the two sets V1 =
{v1, v3, v5} and V2 = {v2, v4, v6}, and every edge of C6 connects a vertex in V1 and a vertex in V2.
Some Special Simple Graphs (Cont.)
• THEOREM: A simple graph is bipartite if and only if it is possible to assign one of two different colors to
each vertex of the graph so that no two adjacent vertices are assigned the same color.
• Example: Use the Theorem to determine whether the following graph is bipartite.
• Solution: Yes
• Example: Use the Theorem to determine whether the following graph is bipartite.
• Solution: No
Representing Graphs
• Adjacency lists, which specify the vertices that are adjacent to each vertex of the graph.