Professional Documents
Culture Documents
DISCRETE MATHE
Final Exam Notes
Contents
CLO 1 - Basic Data Structures..................................................................................................................2
CLO1 – Logical Structures for basic mathematical reasoning..................................................................7
CLO2 – Algorithms.................................................................................................................................10
CLO2 – Searching Algorithms.................................................................................................................12
CLO2 – Sort Algorithms..........................................................................................................................13
CLO3 – Algorithm Analysis.....................................................................................................................15
CLO4 – Graphs.......................................................................................................................................18
CLO 4 – Tree Structure...........................................................................................................................24
CLO 1 - Basic Data Structures
Sets
A set is a collection or group of objects or elements or members. (Cantor 1895)
A set is said to contain its elements.
There must be an underlying universal set U, either specifically stated or understood.
Further Notations
x is a member of S or x is an element of S:
x S.
x is not an element of S:
x S.
Subsets
The set A is a subset of the set B, denoted
A B, iff
x [x A x B]
The void set, the null set, the empty set, denoted , is the set with no members.
T[.,he assertion x is always false.
Hence x [x x B]
is always true(vacuously). Therefore, is a subset of every set.
A set is always a subset of itself.
If A B but A B the we say A is a proper subset of B, denoted A B (in some texts).
The set of all subset of a set A, denoted P(A), is called the power set of A.
Example: If A = {a, b} then
P(A) = {, {a}, {b}, {a,b}}
The number of (distinct) elements in A, denoted |A|, is called the cardinality of A.
If the cardinality is a natural number (in N), then the set is called finite, else infinite.
Example: A = {a, b},
|{a, b}| = 2,
|P({a, b})| = 4.
A is finite and so is P(A).
Useful Fact: |A|=n implies |P(A)| = 2n
The Cartesian product of A with B, denoted
A x B, is the set of ordered pairs {(a, b) | a A b B}
Notation:
Example:
A = {a,b}, B = {1, 2, 3}
AxB = {(a, 1), (a, 2), (a, 3), (b, 1), (b, 2), (b, 3)}
Set Operations
Propositional calculus and set theory are both instances of an algebraic system called a Boolean
Algebra.
The operators in set theory are defined in terms of the corresponding operator in propositional
calculus
As always there must be a universe U. All sets are assumed to be subsets of U
Two sets A and B are equal, denoted A = B, iff
x [x A x B].
By a previous logical equivalence we have
A = B iff x [(x A x B) (x B x A)]
or
A = B iff A B and B A
Examples:
U = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
A= {1, 2, 3, 4, 5},
B = {4, 5, 6, 7, 8}. Then
AB = {1, 2, 3, 4, 5, 6, 7, 8}
A B = {4, 5}
A = {0, 6, 7, 8, 9, 10}
B = {0, 1, 2, 3, 9, 10}
A - B = {1, 2, 3}
B - A = {6, 7, 8}
Functions
A function f from a set A to a set B is an assignment of exactly one element of B to each element
of A.
We write f(a) = b
if b is the unique element of B assigned by the function f to the element a of A.
If f is a function from A to B, we write
f: AB
(note: Here, ““ has nothing to do with if… then)
Properties of function
A function f:AB is called onto, or surjective, if and only if for
every element bB there is an element aA with f(a) = b.
In other words, f is onto if and only if its range is its entire
codomain.
A function f: AB is a one-to-one correspondence, or a
bijection, if and only if it is both one-to-one and onto.
Obviously, if f is a bijection and A and B are finite sets, then |A| = |B|.
Examples:
In the following examples, we use the arrow representation to illustrate functions
f:AB.
In each example, the complete sets A and B are shown.
Composition
Let f: B C, g: A B. The composition of f with g, denoted fg, is the function from
A to C defined by
f g(x) = f(g(x))
Example. let f(x) = x2 and g(x) = x – 3 are functions
from R to R. Find the compositions fog and gof
Solution.
(fog)(x) = f(g(x)) = f(x – 3) = (x –3)2
(gof)(x) = g(f(x)) = g(x2) = x2 – 3
This shows that in general: fog gof
Summation Notation
Given a sequence we can add together a
subset of the sequence by using the summation and function notation
or more generally
Examples:
Simple statement
A statement is a declarative sentence that is either true (T) or false (F) but not both.
A statement is also referred to as a proposition
EXAMPLES
2+2 = 4.
It is Sunday today.
If a proposition is true, we say that it has a truth value of “True ”. If a proposition is false, its
truth value is “ false". The truth values “true” and “false” are, respectively, denoted by the
letters T and F.
Logical connectives
EXAMPLES:
“3 + 2 = 5” and “Dubai is a city in the UAE”
“The grass is green” or“ It is hot today”
“Discrete Mathematics is not difficult to me”
AND, OR, NOT are called LOGICAL CONNECTIVES.
Symbolic Representation
Statements are symbolically represented by letters such as p, q, r,..
EXAMPLES:
P = “Abu Dhabi is the capital of the UAE”.
q =“17 is divisible by 3”.
P ∧q =“Abu Dhabi is the capital of the UAE and 17 is divisible by 3”.
P ∨q=“Abu Dhabi is the capital of the UAE or 17 is divisible by 3”.
~P = “It is not the case that Abu Dhabi is the capital of the UAE” or simply “Abu Dhabi is
not the capital of the UAE”.
Truth table
Is a truth table specifies the truth value of a compound proposition for all possible truth values
of its constituent propositions.
Negation (~)
if p is a statement variable, then negation
of p,“not p”, is denoted as “~p” It has
opposite truth value from p i.e., if p is true, then ~ p is false; if
p is false, then ~ p is true.
Conjunction (˄)
If p and q are statements, then the conjunction of p and q is “p and q”, denoted as “p ˄ q”.
p ˄ q is true only when both p and q are true.
If either p or q is false, or both are false, then p ˄ q is false.
Disjunction (˅)
If p & q are statements, then the disjunction of p and
q is “p or q”, denoted as “p ˅ q”.
p ˅ q is true when at least one pf p or q is true.
p ˅ q is false only when both p and q are false
Exclusive OR
When OR is used in its exclusive sense, the statement “p or q” means “p or q but not both” or “p
or q” not “p and q” which translates into symbols as (p ˅ q) ˄ ˜ (p ˄ q).
It is abbreviated as p Ꚛ q or p XOR q.
Conditional Statements
If p and q are statements variables, the conditional of q by p is
“ if p the q” pf “p implies q” and is denoted p → q.
p → q is false when p is true and q is false ; otherwise it is
true.
The arrow is the conditioner operator.
The p → q ,the statement p is called hypothesis (or
antecedent) and q is called conclusion (or consequent ).
Biconditional
If p&q are statement variables ,the biconditional of p&q is “p if and only if q”.
it is denoted p ↔ q,”if and only if “is abbreviated as iff.
the double headed arrow is biconditional operator
p ↔ q is true only when p and q are both true or both false
p ↔ q is false when either p or q is false
Logical equivalency
if two logical expressions have the same logical value in the truth table, then we say that the
two logical expressions are legally equivalent. In the following example, ˜(˜p) is logically
equivalent p. So it is written as ˜(˜p) ≡ p
Tautology
A tautology is a statement from there that is always true regardless
of the truth values of the statement variables. A tautology is
represented by the symbol t.
Contradiction
A contradiction is a statement form that is always
false regardless of the truth values of the statement
variables. A contradiction is represented by the
symbol “c”.
So if we have to prove that a given statement form is
CONTRADICTION, we will make the truth table for
the statement form and if in the column of the given
statement form all the entries are F, then we say that
statement form is contradiction
Application of logic
NOT – gate
A NOT-gate (or inverter) is a circuit with one input and one output
signal. If the input signal is 1, the output signal is 0. Conversely, if
the input signal is 0, then the output signal is 1.
AND – gate
An AND-gate is a circuit with two input signals and one output
signal. If both input signals are 1, the output signal is 1.
Otherwise the output signal is 0. Symbolic representation &
Input/Output Table
OR – gate
An OR-gate is a circuit with two input signals and one output
signal. If both input signals are 0, then the output signal is 0.
Otherwise, the output signal is 1. Symbolic representation &
Input/Output Table
COMBINATIONAL CIRCUIT
A Combinational Circuit is a compound circuit consisting of the
basic logic gates such as NOT, AND, OR.
CLO2 – Algorithms
Algorithms
An Algorithm is a well-defined, step-by-step procedures that allows a computer to solve a
problem.
Algorithms are finite processes that if followed will solve the problem.
Although there are many ways that algorithms can be compared, we will focus on two factors
that are of primary importance to many data processing algorithms:
Time complexity: how the number of steps required depends on the size of the input
Space complexity: how the amount of extra memory or storage required depends on the size of
the input
An algorithm is a finite sequence of steps to solve a specific problem
Properties of an algorithm
1. Input: an algorithm has input values from a specified set
2. Output: from each set of input values an algorithm produces output values from a specified set.
The output values are the solution to the problem
3. Definiteness: the steps of an algorithm must be defined precisely
4. Correctness: an algorithm should produce the correct output values for each set of input values
5. Finiteness: an algorithm should produce the desired output after a finite (but perhaps large)
number of steps for input in the set
6. Effectiveness: it must be possible to perform each step of an algorithm exactly and in a finite
amount of time
7. Generality: the procedure should be applicable for all problems of the desired form not just for a
particular set of input values.
Criteria of an algorithm
1. An algorithm is an unambiguous description that makes clear what has to be implemented.
2. An algorithm expects a defined set of inputs. For example, it might require two numbers where
both numbers are greater than zero.
3. An algorithm produces a defined set of outputs. It might output the larger of the two numbers.
4. An algorithm is guaranteed to terminate and produce a result, always stopping after a finite
time. If an algorithm could potentially run forever, it wouldn’t be very useful because you might
never get an answer.
5. An algorithms should guaranteed to produce the correct result. It’s rarely useful if an algorithm
returns the largest number 99% of the time, but 1% of the time the algorithm fails and returns
the smallest number instead.
6. If an algorithm imposes a requirement on its inputs (called a precondition), that requirement
must be met. For example, a precondition might be that an algorithm will only accept positive
numbers as an input. If preconditions aren’t met, then the algorithm is allowed to fail by
producing the wrong answer or never terminating.
Algorithms VS Programming
What is Programming?
Implementation is the process of taking an algorithm and encoding it into a notation, a
programming language such as python, so that it can be executed by a computer. Although
many programming languages and many different types of
computers exist, the important first step is the need to have
the solution. Without an algorithm there can be no program.
CLO2 – Searching
Algorithms
Searching
Finding the position of a value within a list
Problem: “Locate an element x in a list of distinct elements
a1, a2, …, an, or determine that it is not in the list.”
We should provide as a solution to this search problem the location of the term in the list that
equals x.
A search typically answers either True or False as to whether the item is present.
On occasion it may be modified to return where the item is found.
In Python, there is a very easy way to ask whether an item is in a list of items.
We use the in operator.
>>> 15 in [3,5,2,4,1]
False
>>> 3 in [3,5,2,4,1]
True
Time Complexity
The complexity of an algorithm is a measure of the amount of time and/or space required by an
algorithm for an input of a given size (n).
The commonly used notation for calculating the running time complexity of the algorithm is as
follows:
1. Big O notation: Big O is used to measure the performance or complexity of an algorithm.
In more mathematical term, it is the upper bound of the growth rate of a function. if a
function f(x) grows no faster than a function g(x), then f is said to be a member of O(g).
2. Big Ω notation: The notation Ω(n) is the formal way to express the lower bound of an
algorithm's running time. It measures the best case time complexity or the best amount
of time an algorithm can possibly take to complete.
3. Big θ notation: The notation θ(n) is the formal way to express both the lower bound and
the upper bound of an algorithm's running time.
Performance of Algorithms
Performance of an algorithm depends on the type of problem and its size
Type of problem:
Counting number of registered students in a course vs. Sorting names of all registered students
in a course.
*Counting will be lot faster than sorting
Size of problem:
Sorting names of all registered students in one course vs. Sorting names of all students in the
college
*Sorting names of all students in the college will take longer
Primitive Operations
In some books, other operations are also used. However such operations here are not
considered
Such as :
Indexing an array A[5] not counted
Print(x) not counted
Function calling Max(lst) not counted
Print(Max(lst) ) not counted
However, the following are counted.
Assign of a list element Marks = A[5] counted
MaxValue = Max(lst) counted
Paying closer attention we see that there is a loop that runs the following two operations
repeatedly:
o if item > MaxVal // comparison
o MaxVal = lst[k] // assignment
It repeats these operations for each element in the array or for the size of the array
If the numbers arrays was:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
The above two operations would be repeated 10 times
So the total number of operations would be 3*10 + 3 =33
What if the arrays had 100 elements
o Total number of operations would be 3*100 + 3 = 303
CLO4 – Graphs
Graphs
A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes
to each other.
The set of edges describes relationships among the vertices
A graph G consists of a set V of vertices and a set E of edges that are ordered pairs of elements
in V.
G=(V,E)
o V(G): a finite, nonempty set of vertices
o E(G): a set of edges (pairs of vertices)
Example 1:This is a graph with four vertices and five edges
Graph Terminology
Two vertices u and v in an undirected graph G are called adjacent
(or neighbors) in G if {u, v} is an edge in G.
If e = {u, v}, the edge e is called incident with the vertices u and v.
The edge e is also said to connect u and v.
The vertices u and v are called endpoints of the edge {u, v}.
A vertex of degree 0 is called isolated, since it is not adjacent to any vertex.
A vertex of degree 1 is called pendant. It is adjacent to exactly one other vertex.
Directed vs. undirected graphs
Undirected graphs
When the edges in the graph have no direction, The graph is called
undirected. The maximum number of edges in a undirected graph without a
loop is n(n-1)/2 where n is the number of vertex .
Directed graphs
When the edges in a graph have a direction, the graph is called directed (or
digraph)
A multigraph may have multiple directed edges. When there are m directed edges from the
vertex u to the vertex v, we say that (u,v) is an edge of multiplicity m.
A pseudograph may include loops, as well as multiple edges connecting the same pair of
vertices. An edge that connects a vertex to itself is called a loop.
A regular graph is a graph where each vertex has the same number of
neighbors; i.e. every vertex has the same degree.
Complete and Bipartite graphs
Complete graphs
Let n be a positive integer. A complete graph on n vertices, denoted Kn, is a simple graph with n
vertices and exactly one edge connecting each pair of distinct vertices.
Complete Graphs on n Vertices: K 1, K2, K3, K4, K5.
Bipartite Graphs
A simple graph G is bipartite if V can be partitioned into two disjoint subsets V1 and V2 such that
every edge connects a vertex in V1 and a vertex in V2.
Connectivity of Graphs
An undirected graph is connected if there is a path from every node to every
other node
A directed graph is strongly connected if there is a path from every node to
every other node
A directed graph is weakly connected if there is a path between any two
vertices, ignoring direction
A complete graph has an edge between every pair of vertices
Degree of a graph
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.
In other words, you can determine the degree of a vertex in a displayed graph by counting the
lines that touch it.
The degree of the vertex v is denoted by deg(v).
Representing Graphs
The following ways can be used to represent a graph:
o Adjacency List
o Adjacency Matrix
o Incidence Matrix
Adjacency Lists
An adjacency list can be used to represent a graph with no multiple edges by specifying the
vertices that are adjacent to each vertex of the graph.
Adjacency matrix
Let G = (V, E) be a simple graph with |V| = n.
Suppose that the vertices of G are listed in arbitrary
order as v1, v2, …, vn.
The adjacency matrix A (or AG) of G, with respect to
this listing of the vertices, is the nn
zero-one matrix with 1 as its (i, j)th entry when v i and vj
are adjacent, and 0 otherwise.
In other words, for an adjacency matrix A = [a ij],
aij = 1 if {vi, vj} is an edge of G,
aij = 0 otherwise.
Incidence matrix
Let G = (V, E) be an undirected graph with |V| = n.
Suppose that the vertices and edges of G are listed in arbitrary order as v 1, v2, …, vn and e1, e2, …,
em, respectively.
The incidence matrix of G with respect to this listing of the vertices and edges is the nm
zero-one matrix with 1 as its (i, j)th entry when edge e j is incident with vi, and 0 otherwise.
In other words, for an incidence matrix M = [m ij],
mij = 1 if edge ej is incident with vi
mij = 0 otherwise.
Isomorphism of Graphs
The simple graphs G1 = (V1, E1) and G2 = (V2, E2) are isomorphic In other words, G1 and G2 are
isomorphic if their vertices can be ordered in such a way that the adjacency matrices M G1 and
MG2 are identical.
The properties that two isomorphic simple graphs must both have.
1.the same number of vertices.
2.the same number of edges.
3.the same degrees of vertices.
Paths
A path: is a sequence of edges that begins at a vertex of a graph and travels from vertex to
vertex along edges of the graph.
o A path is simple if it does not contain the same edge more than once.
o The path is a circuit if it begins and ends at the same vertex, that is, if u = v .
Applications:
o Determining whether a message can be sent between two computers.
o Efficiently planning routes for mail delivery.
A walk in a graph is an alternating sequence of adjacent vertices and edges.
A path is a walk that does not contain a repeated edge.
Example:
path: Points and edges in unrepeatable
walk: Allows point and duplicate path
path: u, v, y
walk: u, v, w, v, x, v, y
A closed walk is a walk that starts and ends at the same vertex.
A circuit is a closed walk that does not contain a repeated edge.
A simple circuit is a circuit which does not have a repeated vertex except for the first and last.
Shortest path
Shortest path: The shortest path length between two vertices i and j is the number of edges
comprising the shortest path (or a shortest path) between i and j.
Graphs that have a number assigned to each edge are called weighted graphs.
The length of a path in a weighted graph is the sum of the weights of the edges of this path.
Shortest path Problem: Determining the path of least sum of the weights between two vertices
in a weighted graph
We can assign weights to the edges of graphs, for example to represent
the distance between cities in a railway network:
Such weighted graphs can also be used to model computer networks with
response times or costs as weights.
Graph Models:
Computer Networks
In graph models, the vertices represent data centers and the edges represent communication
links. To model a computer network where we are only
concerned whether two data centers are connected by a
communications link, we use a simple graph. This is the
appropriate type of graph when we only care whether
two data centers are directly linked (and not how many
links there may be) and all communications links work in
both directions.
Network where we care about the number of links: we use
multigraph
Planar graphs
A graph is called planar if it can be drawn on a plane without edges crossing.
A plane graph is a drawing of planar graph in the plane.
Graph coloring
A coloring of a simple graph is the assignment of a color to each vertex of the graph so that no
two adjacent vertices are assigned the same color.
It is an assignment of colors to elements of a graph subject to certain constraints.
Graphs are used to find what is in conflict with what and colors are used to denote the state of a
vertex.
The coloring theory is the theory of partitioning the sets having internal unknown conflicts.
Applications of Graph Colorings
o Scheduling
o Frequency assignments
o Pattern matching
Vertex coloring is a way of coloring the vertices of a graph such that no two adjacent vertices
share the same color.
Tree Terminology
If v is a vertex of tree T other than the root, the parent of v is the
unique vertex u such that there is a directed edge from u to v.
When u is the parent of v, v is called the child of u.
If two vertices share the same parent, then they are called siblings.
The ancestors of a vertex other than the root are the vertices in the path
from the root to this vertex, excluding the vertex itself and including the
root.
The descendants of a vertex v are those vertices that have v as an ancestor.
A vertex with no children is called a leaf.
Vertices with children are called internal vertices.
Properties of tree
Rooted tree
A rooted tree is a tree in which one vertex has been designated the root. Specify a
vertex as root, then direct each edge away from the root. The resulting tree is
called a rooted tree.
Forest
A forest is an undirected graph, all of whose connected components are
trees; in other words, the graph consists of a disjoint union of trees.
Subtrees
If a is a vertex in a tree, the subtree with a as its root is:
The subgraph of the tree consisting of a and its
descendants, and all edges incident to these descendants.
Balanced Trees
A tree with n vertices has n1 edges.
An full m-ary tree with i internal vertices contains n = mi + 1 vertices.
A rooted m-ary tree of height h is called balanced if all leaves are at levels h or h–1.
Tree Traversal
Let T be an ordered rooted tree with root r.
Suppose T1, T2, …,Tn are the subtrees at r from left to right in T.
Inorder Traversal
Step 1: Visit T1 in inorder
Step 2: Visit r
Step 3: Visit T2 in inorder
Step n+1: Visit Tn in inorder
Kruskal’s Algorithm
Work with edges, rather than nodes. This is steps:
1. Sort all the edges in non-decreasing order of their weight.
2. Pick the smallest edge. Check if it forms a cycle with the
spanning tree formed so far. If cycle is not formed, include this
edge. Else, discard it.
3. Repeat step#2 until there are (V-1) edges in the spanning tree.