You are on page 1of 28

CIS 2203 – APPLIED

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.

Different notations for representing Sets:


 List the elements between braces:
S = {a, b, c, d}={b, c, a, d, d}
(Note: listing an object more than once does not change the set. Ordering means
nothing.)
 Specification by predicates: S= {x| P(x)}, S contains all the elements from U which make the
predicate P true.
 Brace notation with ellipses: S = { . . . , -3, -2, -1},
The set of all negative integers.

Common Universal Sets


 R = reals
 N = natural numbers = {0,1, 2, 3, . . . }, the counting numbers
 Z = all integers = {. . , -3, -2, -1, 0, 1, 2, 3, 4, . .}
 Z+ is the set of positive integers

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
AB = {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: AB
 (note: Here, ““ has nothing to do with if… then)

 If f:AB, we say that A is the domain of f and B is the codomain of f.


 If f(a) = b, we say that b is the image of a and a is the pre-image of b.
 The range of f:AB is the set of all images of elements of A.
 We say that f:AB maps A to B.

 Let us take a look at the function f:PC with


P = {Linda, Max, Kathy, Peter}
C = {Boston, New York, Hong Kong, Moscow}
f(Linda) = Moscow
f(Max) = Boston
f(Kathy) = Hong Kong
f(Peter) = New York
Here, the range of f is C.
 Let us re-specify f as follows:
f(Linda) = Moscow
f(Max) = Boston
f(Kathy) = Hong Kong
f(Peter) = Boston
Is f still a function? yes
What is its range? {Moscow, Boston, Hong Kong}
 Other ways to represent f:

Injection, Surjection and Bijection


 A function f:AB is said to be one-to-one (or injective), if and only if
 x, yA (f(x) = f(y)  x = y)
 f is one-to-one if and only if it does not map two distinct elements of A onto the same element
of B.

Properties of function
 A function f:AB is called onto, or surjective, if and only if for
every element bB there is an element aA with f(a) = b.
 In other words, f is onto if and only if its range is its entire
codomain.
 A function f: AB 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:AB.
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 fg, 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

Sequences and Summations


 A sequence is a function from a subset of the natural numbers (usually of the form {0, 1, 2, . . . }
to a set S.
 Note: the sets {0, 1, 2, 3, . . . , k} and {1, 2, 3, 4, . . . , k} are called initial segments of N.
 Notation: if f is a function from {0, 1, 2, . . .} to S we usually denote f(i) by a i and we write

 where k is the upper limit (usually ).

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:

 A geometric progression is a sequence of the form


a, ar, ar2, ar3, ar4, . . . .
 We can prove that

(you can figure out what it is if r = 1).


 You should also be able to determine the sum
 if the index starts at k vs. 0
 if the index ends at something other than n
(e.g., n-1, n+1, etc.).

CLO1 – Logical Structures for basic


mathematical reasoning
Logic
 Is the study of the principles and methods that distinguish between a valid and an invalid
argument.

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

Logic and Bit Operations


 Computer bit operations correspond to the logical connectives. By replacing
true by a one and false by a zero in the truth tables for the operators ∧, ∨,
and ⊕, the tables shown for the corresponding bit operations are obtained.
 We will also use the notation OR, AND, and XOR for the operators ∨,∧, and
⊕, as is done in various programming languages.

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

 Most statements are neither tautologies nor contradictions.


 The negation of a tautology is a contradiction and vice versa.
 In common usage we sometimes say that two statement are contradictory. By this we mean
that their conjunction is a contradiction: they cannot both be true.

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

Linear Search Algorithm


 linear search, which simply checks the items in sequence until the desired item is found
 The simplest solution to the sequence search problem is the sequential or linear search
algorithm.
 This technique iterates over the sequence, one item at a time, until the specific item is found or
all items have been examined.
 In Python, a target item can be found in a sequence using the in operator:

1. Get the search element from the user.


2. Get the list from the user.
3. Compare the search element with the first element in the list.
4. If both are matched, then return the element index and stop searching.
5. If both are not matched continue to next element in the list.
6. Repeat steps 4 and 5 until search element is compared with last element in the list.
7. If last element in the list also doesn't match, then return -1.
 The complexity of the linear (sequential) search is

Binary Search Algorithm


 To reduce the time complexity, we can use the binary search instead of the linear search.
 It is possible to take greater advantage of the ordered list if we are clever with our comparisons.
 In the sequential search, when we compare against the first item, there are at most n−1 more
items to look through if the first item is not what we are looking for.
 Instead of searching the list in sequence, a binary search will start by examining the middle
item. If that item is the one we are searching for, we are done. If it is not the correct item, we
can use the ordered nature of the list to eliminate half of the remaining items.
1. Get the list from the user.
2. Get the search element from the user.
3. Calculate the middle index
4. Compare the search element with the element in the middle index.
5. If both are matched, then return the element index and stop searching.
6. If both are not matched.
1. If the target value is greater than the number in the middle index, then pick sub
list elements to the right of the middle index, and start with Step 3
2. If the target value is less than the number in the middle index, then pick the sub
list elements to the left of the middle index, and start with Step 3.
7. Repeat steps 3, 4 and 5 until search element is compared with last element in the list.
8. If last element in the list also doesn't match, then return -1.
 Time Complexity of Binary Search O(log n)

CLO2 – Sort Algorithms


Sorting
 Given a list of values and a function that compares two values, order the values in the list from
smallest to largest.
 The values might be integers, or strings or even other kinds of objects. We will examine two
algorithms:
 Selection sort, which relies on repeatedly selecting the next smallest item
 Insertion sort, which relies on removing entries one at a time and then insert each of them into a
sorted part (initially empty)
 Bubble sort, which relies on comparing each item in the list with the item next to it, and
swapping them if required. In other words, the largest element has bubbled to the top of the
array.

Selection Sort Algorithms


 The list is divided into two parts, the sorted part at the left end and the unsorted part at the
right end. Initially, the sorted part is empty and the unsorted part is the entire list.
 The smallest element is selected from the unsorted array and swapped with the leftmost
element, and that element becomes a part of the sorted array. This process continues moving
unsorted array boundary by one element to the right.
 This algorithm is not suitable for large data sets as its average and worst case complexities are of
Ο(n2), where n is the number of items.
1. Get a list to sort
2. Iterate elements of the list starting from index x
3. Set min to the element in the index x
4. Iterate unsorted list starting from x
5. Compare each element of unsorted list with the min value
6. Maintain the min value in the unsorted list
7. Maintain the min value location L
8. Swap min found in location L with element in index x
9. Repeat until list is sorted

Insertion Sort Algorithms


 A sub-list is maintained which is always sorted. For example, the lower part of an array is
maintained to be sorted.
 An element which is to be inserted in this sorted sub-list, has to find its appropriate place and
then it has to be inserted there. Hence the name, insertion sort.
 The array is searched sequentially and unsorted items are moved and inserted into the sorted
sub-list (in the same array). This algorithm is not suitable for large data sets as its average and
worst case complexity are of Ο(n2), where n is the number of items.
1. Get a list
2. Pick the first element in the unsorted list.
3. If the list has one element, terminate.
4. Pick next element
5. Compare with all elements in the sorted sub-list
6. Shift all the elements in the sorted sub-list that is
greater than the value to be sorted
7. Insert the value
8. Repeat until list is sorted

Bubble Sort Algorithm


 Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent
elements if they are in wrong order.
 This sorting algorithm is comparison-based algorithm in which each pair of adjacent elements is
compared and the elements are swapped if they are not in order.
 This algorithm is not suitable for large data sets as its average and worst case complexity are of
Ο(n2) where n is the number of items.
 Bubble sort, referred to as sinking sort, is a simple sorting algorithm that repeatedly steps
through the list to be sorted, compares each pair of adjacent items and swaps them if they are
in the wrong order.
 The pass through the list is repeated until no swaps are needed, which indicates that the list is
sorted. The algorithm, which is a comparison sort, is named for the way smaller elements
"bubble" to the top of the list.
 Bubble sort is too slow comparing to insertion sort.
1. Get a list L of n elements with values or records L0,
L1, …, Ln-1.
2. Compare first two elements L0, L1 in the list.
3. if L1 < L0, swap those elements and continue with next 2
elements.
4. Repeat the same step until whole the list is sorted, so no
more swaps are possible.
5. Return the final sorted list.

CLO3 – Algorithm Analysis


Comparing algorithms with complexity
 The asymptotic behavior of a function f(n) refers to the growth of f(n) as n gets large- i.e. it
tends to infinity!
 So, we compare two algorithms using the asymptotic notation as the problem (input) size tends
to infinity!
 A good rule of thumb is: the slower the asymptotic growth rate, the better the algorithm (this
has exceptions)

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.

Asymptotic notation : big O


 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).
 Big-O notation (O)
Let f and g be functions from the set of integers or the set of real numbers. We say
that f (x) is O(g(x))
if there are constants C and k such that |f (x)| ≤ C|g(x)|
 Whenever x > k. [This is read as “f (x) is big-oh of g(x).”]
 The vertical bars || mean absolute value
o This is because we are only interested in positive values
 Note for algorithm analysis:
o x will always be a positive integer
o f(x) and g(x) will always be positive
 We see from our previous examples that the fastest
growing function may not always have the greater value
of y, specially for some small values of x
 We will see later in algorithm analysis that we are
interested whether one function produces values of y greater than another function, even if it
does so after few small values of x
Explain the definition:
Going back to our functions f(x)= X2+4 and g(x)= X2+X from previous slide
Let C = 1 and k = 4, then:
|(f(x)| ≤ C| g(x)|, x > k  |f(x)| ≤ |g(x)|, for x > 4
So we can say that f(x) is Big-O of g(x)
Growth of functions
 What we can say is:
o g(x) > f(x) for all values of x > 4 OR
o f(x) < g(x) for all values of x > 4
 We write this mathematically as:
o f(x) ≤ g(x) for x > 4

 What if we change g(x) to g(x)=x2


 For C = 1 and k >=1, we can no longer say |f(x)| ≤ |g(x)|

Asymptotic notation: Big-Omega Ω


 Big-Omega notation (Ω)
Let f and g be functions from the set of integers or the set of real numbers. We say that
f (x) is Ω(g(x)) if there are positive constants C and k such
that |f (x)| ≥ C|g(x)| whenever x > k.

[This is read as “f (x) is big-Omega of g(x).”]

 Notice that the difference between Big-O and Big-Omega is that


o in Big-O: |f (x)| ≤ C|g(x)| and
o in Big-Omega : |f (x)| ≥ C|g(x)| .
let’s re-visit our f(x) and g(x) functions:
f(x)= x2 + 4 and g(x) = x2
This time let C=1 and k >=1
|f(x)| ≥ C|g(x)|  |f(x)| ≥ |g(x)|, for k >=1
This means f(x) is Big-Omega of g(x) or Ω(g(x))

Asymptotic notation: Big-Theta (ϴ)


 Big-Theta notation (ϴ)
Let f and g be functions from the set of integers or the set of real numbers.
We say that f(x) is ϴ(g(x)) if f (x) is O(g(x)) and f (x) is Ω(g(x)).
 In the example of f(x) and g(x) we have been using
f(x) =x2 + 4 and g(x)=x2
 We have seen that f(x) is O(g(x)) and Ω(g(x)) therefore it is ϴ(g(x))
 Notice that values for C were different:
o For f(x) to be O(g(x)), C = 2 (ref. 14)
o For f(x) to be Ω(g(x)), C = 1 (ref. 14)
 Since C can be different for Big-O and Big-Omega we can say
for f(x) to be Big-Theta of g(x)
C1|g(x)| ≤ |f(x)| ≤ C2|g(x)|
 Continuing with our example we see:
g(x)| ≤ |f(x)| ≤ 2|g(x)|
 Plotting all three functions, f(x) =x2 + 4, g(x)=x2 and g(x)=2x2 on
the same graph we can see how f(x) lies in between functions
x2 and 2x2
For more examples visit the excel sheets

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

Counting operations of an Algorithm

 Assignment: eg. Kount = 5;


 Comparison: eg. If kount = 10 , If kount < 29
If kount > 10 , If kount<= 10
If kount>= 10
Operations within a loop
 NOTE: Work done by the computer such as calculation or logical operation should be considered
as operation

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)

Simple, Multigraphs, Pseudo graph, Regular graph


 A Simple graph is where each edge connects two different vertices and no two edges connect
the same pair of vertices.
 A simple directed graph has no loops and no multiple edges

 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 nn
 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 nm
 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

 diagnostic self-links at data centers: we use pseudograph

 network with multiple one-way links: we use a directed


(multi) graph

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.

 Edge coloring is the assigning a color to each edge so that no two


adjacent edges share the same color.

CLO 4 – Tree Structure


Introduction to trees
 A tree is a finite set of one or more nodes
such that:
 There is a specially designated node called the root.
 The remaining nodes are partitioned into n>=0
disjoint sets T1, ..., Tn, where each of these sets is a
tree.
 We call T1, ..., Tn the subtrees of the root.
 A tree is a connected undirected graph with
 No simple circuits
 No multiple edges
 No loops
 An undirected graph is a tree if and only if there is a unique simple path between any two of its
vertices.

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.

m-ary tree /binary tree


 A rooted tree is called an m-ary tree if every internal vertex has no more than m children.
 A tree is called a full m-ary tree if every internal vertex has exactly m children.
 An m-ary tree with m  2 is called a binary tree.

Ordered Rooted Tree


 An ordered rooted tree is one where the children of each internal vertex are ordered.
 In an ordered binary tree, if an internal vertex has two children, then they are called left child
and right child.
 The subtree rooted at the left child of a vertex is called the left subtree and subtree rooted at
the right child of a vertex is called the right subtree.

Height and depth of a tree


 The height of the tree is the length of the longest path
between any node and the root
 The depth (level) of a node is the length of the path to the
root
 The height of the tree is 3
 The depth or level of nude n5 is 2. The depth of the root
node is always 0
 The level of a vertex v in a rooted tree is the length of the
unique path from the root to this vertex.
 What is the level of the root? 0
 The height of a rooted tree is the maximum of the levels of the vertices.

Balanced Trees
 A tree with n vertices has n1 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

Minimum Spanning Tree


 A Minimum Spanning Tree (MST) is a subgraph of an undirected graph such
that the subgraph spans (includes) all nodes, is connected, is acyclic, and
has minimum total edge weight.

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.

You might also like