Discrete Maths
Discrete Maths
Internal verification:
Contents
1. Set theory and functions....................................................................................................................3
I. Set theory.......................................................................................................................................3
1.Definition of sets, basic concepts...............................................................................................3
2. Operations in sets.......................................................................................................................3
3. The force of gathering................................................................................................................4
4. Theorems of sets........................................................................................................................5
5. Definition of multiset, characteristics, notation, and cardinality of multiset.............................5
II. Function theory.............................................................................................................................6
1. function definition......................................................................................................................6
2. Definition of single light, double light, full light.......................................................................6
3. Inverse function.........................................................................................................................6
2. Graph theory......................................................................................................................................7
II. Graph theory.....................................................................................................................................7
1. Graph definition.............................................................................................................................7
2. Types of graphs, how to classify and represent graphs on computers..........................................7
3. Define binary tree characteristics..................................................................................................7
4. Defines the spanning tree of the graph..........................................................................................8
5. Euler line, Hamilton cycle, 2 isomorphic graphs..........................................................................9
5.1. Euler's 1-line...........................................................................................................................9
5.2. Hamiltonian cycle.................................................................................................................10
5.3. Two isomorphic graphs........................................................................................................10
6. Algorithms: find Dijkstra's shortest path, find Prim's smallest spanning tree.............................11
6.1. Dijkstra's algorithm...............................................................................................................11
6.2. Prim's smallest frame tree.....................................................................................................12
III. Examine set theory and functions applicable to software engineering.........................................12
Part 1................................................................................................................................................12
Part 2:.............................................................................................................................................14
Part 3...............................................................................................................................................15
Part 4:.............................................................................................................................................17
Part 5:.............................................................................................................................................17
Part 6 :............................................................................................................................................18
IV. Reference......................................................................................................................................20
1. Set theory and functions.
I. Set theory
1.Definition of sets, basic concepts
- A set is a collection of objects, called elements, in which the order is not important and an object
cannot appear twice in the same set
- Example 1.1
Explicit definitions of sets, that is, where each element is listed, are:
A = {a, b, c}
B = {3, 4, 6, 7, 8, 9}
C = {Linda, Raka, Sue, Joe, Nigel, Mary}
a ∈ A means ‘a is an element of A’ or ‘a belongs to A’; therefore in the above examples:
3∈B
Linda ∈ C
- A set is a collection of objects, called elements, in which the order is not important and an object
cannot appear twice in the same set
- The universal set is the set of all objects we are interested in and will depend on the problem under
consideration. It is represented by E.
The empty set (or null set) is the set with no elements. It is represented by ∅ or { }.
- Sets can be represented diagrammatically – generally as circular shapes. The universal set is
represented as a rectangle. These are called Venn diagrams
- Sets can be represented diagrammatically – generally as circular shapes. The universal set is
represented as a rectangle. These are called Venn diagrams
2. Operations in sets
- Intersection:
Symbol: A ∩ B.
Meaning: The set of elements that simultaneously belong to both A and B.
For example: If A = {1, 2, 3} and B = {2, 3, 4}, then A ∩ B = {2, 3}.
- Union:
Symbol: A ∪ B.
Meaning: The set of all elements belonging to A or B or both.
For example: If A = {1, 2, 3} and B = {3, 4, 5}, then A ∪ B = {1, 2, 3, 4, 5}.
- Subtraction (Difference):
Symbol: A - B or A \ B.
Meaning: The set of elements that belong to A but do not belong to B.
For example: If A = {1, 2, 3} and B = {2, 3, 4}, then A - B = {1}.
- Complement:
Symbol: A̅ or complement(A).
Meaning: The set of elements in the sample space that do not belong to A.
For example: If U is the sample space and A = {1, 2, 3}, then A̅ is the set of elements that do not
belong to {1, 2, 3}.
3. The force of gathering
The cardinality of a set is a concept in mathematics expressed by the number of elements in that set.
This is also known as "cardinality" (usually denoted |A|) and is often used to measure the size or
number of elements of a set.
For example, if A is a set of natural numbers from 1 to 5, then the cardinality of A (denoted |A|) will
be 5, because there are 5 elements in that set.
For example:
The set A = {1, 2, 3} has cardinality |A| = 3.
The set B = {a, b, c, d} has cardinality |B| = 4.
The empty set ∅ has cardinality |∅| = 0.
4. Theorems of sets.
5. Definition of multiset, characteristics, notation, and cardinality of multiset.
- Multiset is a concept in mathematics, especially in set theory, which is similar to a regular set but
can contain more than one copy of each element. This means that an element can appear multiple
times in a multi-set, as opposed to a regular set where each element appears only once.
- Characteristics of multiple sets include:
+ Duplicate elements: Elements in a multi-set can appear multiple times.
+ Order is not important: The order of elements is not important in the multiset.
+ This means that {1, 2, 3} and {3, 2, 1} are considered the same multiset.
- Multi-set notation may vary depending on context and programming language. Some common
ways include using curly braces {} or merge marks (), and using dashes or colons to indicate
repeated elements. For example:
+ Curly braces: {1, 2, 2, 3, 3, 3}
+ Merge sign: (1, 2, 2, 3, 3, 3)
+ Symbols with dashes: {a, b, c, c, c} or {a, b, c3}
+ Symbol with dot: {a, b, c...}
- The power of multiple sets lies in modeling real-life situations where the multiple occurrence of
particular elements is important. Multiple sets are used in many fields such as statistics, information
technology, and consensus theory. In programming, languages like Python provide data structures
like Counter in the collections module to represent multiple collections.
II. Function theory
1. function definition
- A function is a rule or law that determines the relationship between elements of two different sets
such that each element of the first set is uniquely mapped to an element of the second set. two.
Simply put, a function is a rule that assigns each of its (independent) input values to a corresponding
(dependent) output value.
2. Definition of single light, double light, full light.
- One-to-one: A mapping f:A→B is said to be one-to-one if each element in set A is mapped to a
single element in set B. In other words, there are no two different elements in A which is mapped to
the same element in B.
- Bijective: A mapping f:A→B is called bijective if each element in set B has at least one element in
A mapped to it. In other words, there is no element in B that there is not at least one element in A
mapped to.
- Omnijective: A mapping f : A→B is called holographic if it is both unijective and bijective. In
other words, each element in A is mapped uniquely, and each element in B has at least one element
in A mapped to.
3. Inverse function.
An inverse function is a concept in mathematics related to the mapping between two sets. If a
function f : A→B is an injective, i.e. no two different elements in A are mapped to the same element
in B, then it can have an inverse function. island.
The inverse function of f, denoted f−1, is a function g : B→A such that if f(a) = b then (b) = a. In
other words, the inverse function works the opposite of the original function: if you apply the
function f to a value a and then apply the inverse function f−1 to that result, you will rotate return to
the original value a.
The condition for a function to have an inverse is that it must be a unijective and bijective mapping.
This ensures uniqueness and ensures every value in B is mapped to a unique value in A.
2. Graph theory
- How it works:
+ Create a set of vertices with known shortest paths and a set of vertices that have not yet been
considered.
+ Assign the distance value from the starting vertex to itself as 0, the remaining value is infinity.
+ Select the vertex with the shortest distance from the unconsidered set and mark it as considered.
+ Update the distance from the current vertex to the remaining adjacent vertices through the
considered vertex, if the new distance is shorter.
+ Repeat the process until all vertices have been considered.
- Dijkstra's algorithm:
+ Step 1: L(a) = 0, S = Ø, vV, v a: L(v) =
+ Step 2: If z S then finish.
+ Step 3: Choose v S so that L(v) is smallest.
Insert v into S.
+ Step 4: For each vertex x adjacent to v and x S, set: L(x) = min{L(x), L(v) + c(v,x)}
Go back to step 2.
6.2. Prim's smallest frame tree.
- Definition: The smallest spanning tree in a connected, weighted graph is a spanning tree with the
smallest total weight on its edges.
- Algorithm to find the smallest spanning tree:
¤ Prim (Robert Prim - 1957)
¤ Kruskal (Joseph Kruskal – 1965)
- How it works:
+ Starting from any vertex, choose the edge with the smallest weight connecting that vertex with
another vertex.
+ Select the edge with the smallest weight connecting a selected vertex with a vertex that is not yet
in the tree.
+ Repeat the above step until all vertices belong to the tree.
- Prim algorithm:
- Graph G = (V, E) is connected, has n vertices.
+ Step 1: Choose any edge with the smallest weight, place it in the spanning tree.
+ Step 2: In turn, add to the tree the edges with the smallest weight that belong to a vertex of the tree
and do not create a cycle in the tree.
+ Step 3: The algorithm stops when (n - 1) edges are merged into the tree.
- Kruskal algorithm:
- The graph G = (V, E) is connected, has n vertices.
+ Step 1: Choose any edge with the smallest weight, place it in the spanning tree.
+ Step 2: In turn, join the edges with the smallest weight into the tree without creating a cycle in the
tree.
+ Step 3: The algorithm stops when (n 1) edges are merged into the tree.
1. Let A and B be two non-empty finite sets. If cardinality of the sets A, B, and A ∩ B are 86, 43
and 11 respectively, find the cardinality of the set A ∪ B.
- Set |A| is the number of elements in the set A, |B| is the number of elements in set B, and |A ∩ B| is
the number of common elements of A and B. Use the formula for the total number of elements of
the union of two sets:
|A ∪ B∣=∣A∣ + ∣B∣ − ∣ A ∩ B ∣
Instead, we have:
∣A ∪ B∣= 86 + 43 – 11 = 118
3. Sets and multisets are widely used in software engineering. They can be used to represent
collections of items where the order does not matter. A set does not allow duplicate
elements, making it useful for tasks such as removing duplicates from a list or checking
membership. A multiset, on the other hand, allows duplicate elements, which can be useful
for tasks such as counting the frequency of elements.
Here is a simple Python example using sets to remove duplicates from a list:
def remove_duplicates(input_list):
return list(set(input_list))
print(remove_duplicates([1, 2, 2, 3, 4, 4, 5, 6, 6, 7]))
And here is a Python example using multisets (in Python, they are called Counter from the
collections module) to count the frequency of elements in a list:
def count_elements(input_list):
return Counter(input_list)
Part 3
1. Determine whether the following functions are invertible or not. If it is invertible, then find the
rule of the inverse f-1 (x)
a) For the function f: R -> R+ where f(x) = x2
the function f(x) = x2 is not injective, because there are 2 real numbers that are not images of any
value of x, i.e. no value of x so that x2 = -1
Therefore, f(x) is not bijective => the function f(x) is not invertible
b) For the function f: [0, ] → [-2,2]
f(x) = 2cosx
x = 2cosy
x
cosy =
2
x
=> y = arcos( )
2
x
=> f-1(x)= arcos( )
2
5
2. Function f(x) = (x - 32) converts Fahrenheit temperatures into Celsius. What is the
9
function for opposite conversion?
It can be found by solving the lavation for x:
9
f-1(x) = x + 32
5
Part 5:
Binary trees are tree data structures in which each node has at most two children, referred to as the
left child and the right child. Binary trees have various applications, advantages, and disadvantages
depending on the type and structure of the tree. Here are two examples of binary trees and their
properties:
Binary search tree: A binary search tree is a binary tree in which the value of each node is
greater than or equal to the value of its left child and less than or equal to the value of its
right child. Binary search trees are useful for storing and retrieving data efficiently, as they
allow binary search operations on the tree. The average time complexity of searching,
inserting, and deleting in a binary search tree is O(log n), where n is the number of nodes in
the tree. However, the worst-case time complexity can be O(n) if the tree is skewed or
unbalanced. To avoid this, binary search trees can be balanced using various techniques,
such as rotations, red-black trees, or AVL trees
Huffman tree: A Huffman tree is a binary tree that is used for data compression. It is
constructed by assigning frequencies to each symbol in the data and then creating a tree that
minimizes the total weighted path length of the symbols. The symbols are stored in the
leaves of the tree, and each symbol is encoded by the sequence of left and right branches
along the path from the root to the leaf. The Huffman tree ensures that the most frequent
symbols have the shortest codes and the least frequent symbols have the longest codes, thus
reducing the average code length and the size of the compressed data. The time complexity
of building a Huffman tree is O(n log n)
Part 6 :
1. State the Dijkstra's algorithm for a directed weighted graph with all non-negative edge weights
Dijkstra’s algorithm for a directed weighted graph with all non-negative edge weights is a
greedy algorithm that finds the shortest path from a given source vertex to all other vertices
in the graph.
The algorithm maintains a set of visited vertices and a set of unvisited vertices. It starts at the
source vertex and assigns it a distance of zero. It then selects the unvisited vertex with the
smallest distance from the source and marks it as visited. It then updates the distances of its
adjacent vertices by adding the weight of the edge to the current distance of the vertex. If the
new distance is smaller than the previous distance, it replaces it. This process repeats until all
vertices are visited or the destination vertex is reached.
The algorithm can be implemented using a priority queue or a heap to store the unvisited
vertices and their distances. This allows the algorithm to select the vertex with the smallest
distance in logarithmic time. The algorithm can also return the shortest path by storing the
predecessor of each vertex and tracing back from the destination to the source.
2. Find the shortest path spanning tree for the weighted directed graph with vertices A, B, C, D, E,
F, G given using Dijkstra's algorithm
Weight Edge
1 D -> G
2 A -> B
2 C -> E
2 B -> D
2 G -> E
3 B -> C
3 D -> E
3 E -> G
3 G -> F
4 A -> C
4 C -> B
6 E -> F
L = 2 + 2 + 2 + 2 + 1 + 3 = 12
IV. Reference
Session 1-Set theory
Session 4_Graph theory.