Fundamentals of Computing Algorithms

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 60

ADONG OLIVER SALUME

FUNDAMENTALS OF 20/U/2472/GIT
BAGAMBE AMOS
COMPUTING ALGORITHMS. 20/U/2473/GIT
OKUMU OSCAR
20/U/0822/GCS/PS
ALGORITHM.
It’s a process or set of rules to be followed in calculations or other problem solving
operations especially by a computer.
Examples.
Daily morning routine.
Recipe.
Driving directions.
TYPES OF PROGRAMMING
ALGORITHMS
•Sort algorithms
•Search algorithms
•Hashing algorithms
•Brute Force algorithms
•Recursive algorithms
•Backtracking algorithms
•Divide and conquer algorithms
•Dynamic programming algorithms
•Randomized algorithms
BRUTE FORCE
This essentially attempts all the chances until an acceptable result is found.
This is the most fundamental and least complex type of algorithm.
Such an algorithm can be:
Optimizing: Find the best solution. This may require finding all solutions,
or if a value for the best solution is known, it may stop when any best
solution is found
Example: Finding the best path for a travelling salesman
Satisficing: Stop as soon as a solution is found that is good enough
Example: Finding a travelling salesman path that is within 10% of optimal
Example
If there is a lock of 4-digit PIN. The digits to be chosen from 0-9 then the brute force
will be trying all possible combinations one by one like 0001, 0002, 0003, 0004, and
so on until we get the right PIN. In the worst case, it will take 10,000 tries to find the
right combination.
DIVIDE AND CONQUER
ALGORITHMS
A divide and conquer algorithm consists of two parts:
Divide the problem into smaller sub problems of the same type, and solve these sub
problems recursively
Combine the solutions to the sub problems into a solution to the original problem
Traditionally, an algorithm is only called divide and conquer if it contains two or
more recursive calls
DYNAMIC PROGRAMMING
ALGORITHMS

A dynamic programming algorithm remembers past results and uses them


to find new results
Dynamic programming is generally used for optimization problems
 Multiple solutions exist, need to find the “best” one
 Requires “optimal substructure” and “overlapping sub problems”
 Optimal substructure: Optimal solution contains optimal solutions to sub
problems
 Overlapping sub problems: Solutions to sub problems can be stored and
reused.
This differs from Divide and Conquer, where sub problems generally need not
overlap
RECURSIVE ALGORITHMS
An algorithm is called recursive if it solves a problem by reducing it to an
instance of the same problem with a smaller input.
Example;
To count the number of elements in a list:
 If the list is empty, return zero; otherwise,
 Step past the first element, and count the remaining elements in the list
 Add one to the result
To test if a value occurs in a list:
 If the list is empty, return false; otherwise,
 If the first thing in the list is the given value, return true; otherwise
 Step past the first element, and test whether the value occurs in the
remainder of the list
BACKTRACKING
ALGORITHMS
Backtracking algorithms are based on a depth-first recursive search
A backtracking algorithm:
 Tests to see if a solution has been found, and if so, returns it; otherwise
 For each choice that can be made at this point,
 Make that choice
 Recur
 If the recursion returns a solution, return it
 If no choices remain, return failure
In other words, a backtracking algorithm solves a sub problem, and if it fails
to solve the problem, it undoes the last step and starts again to find the
solution to the problem.
GREEDY ALGORITHMS
An optimization problem is one in which you want to find, not just a
solution, but the best solution
A “greedy algorithm” sometimes works well for optimization problems
A greedy algorithm works in phases: At each phase:
 You take the best you can get right now, without regard for future
consequences
 You hope that by choosing a local optimum at each step, you will end up at
a global optimum
EXAMPLE
Suppose you want to count out a certain amount of money, using the fewest
possible notes and coins.
A greedy algorithm would do this would be:
At each step, take the largest possible bill or coin that does not overshoot
 Example: To make Ugx. 1.3255m , you can choose:
 20 Ugx.50000 notes to make Ugx.1m
 15 Ugx.20000 notes, to make Ugx. 1.3m
 20 Ugx.10000 notes, to make Ugx. 1.32m
 a Ugx.5000 note, to make Ugx.1.325m
 a Ugx.500 coin, to make Ugx.1.3255m
RANDOMIZED ALGORITHMS
A randomized algorithm is an algorithm that employs a degree of randomness
as part of its logic or procedure.
It uses a random number at least once during the computation to make a
decision.

 Example: In Quicksort, using a random number to choose a pivot

 Example: Trying to factor a large prime by choosing random numbers as


possible divisors
SEARCHING ALGORITHMS
These are algorithms designed to retrieve an element from any data structure
where its used.
Based on the type of search operation, these algorithms are generally
classified into two categories:
Sequential Search: In this, the list or array is traversed sequentially and
every element is checked. For example: Linear Search

Interval Search: These algorithms are specifically designed for searching in


sorted data-structures. These type of searching algorithms are much more
efficient than Linear Search as they repeatedly target the center of the search
structure and divide the search space in half. For Example: Binary Search.
LINEAR SEARCH
Linear search is a very simple search algorithm. In this type of search, a
sequential search is made over all items one by one. Every item is checked
and if a match is found then that particular item is returned, otherwise the
search continues till the end of the data collection.
ALGORITHM AND
PSEUDOCODE
Linear Search ( Array A, Value x)

Step 1: Set i to 1
Step 2: if i > n then go to step 7
Step 3: if A[i] = x then go to step 6
Step 4: Set i to i + 1
Step 5: Go to Step 2
Step 6: Print Element x Found at index i and go to step 8
Step 7: Print element not found
Step 8: Exit

procedure linear search (list,


value)

for each item in the list


if match item == value
return the item's location
end if
end for

end procedure
BINARYSEARCH
Binary search is a fast search algorithm with run-time complexity of Ο(log n).
This search algorithm works on the principle of divide and conquer. For this
algorithm to work properly, the data collection should be in the sorted form.

Binary search looks for a particular item by comparing the middle most item
of the collection. If a match occurs, then the index of item is returned. If the
middle item is greater than the item, then the item is searched in the sub-array
to the left of the middle item. Otherwise, the item is searched for in the sub-
array to the right of the middle item. This process continues on the sub-array
as well until the size of the subarray reduces to zero.
HOW BINARY SEARCH
WORKS?
For a binary search to work, it is mandatory for the target array to be sorted. We
shall learn the process of binary search with a pictorial example. The following is
our sorted array and let us assume that we need to search the location of value 31
using binary search.

First, we shall determine half of the array by using this formula −

Mid = low + (high-low) / 2

Here it is, 0 + (9 - 0 ) / 2 = 4 (integer value of 4.5). So, 4 is the mid of the array.
Now we compare the value stored at location 4, with the value being searched, i.e. 31. We find that the value at location 4 is 27, which is
not a match. As the value is greater than 27 and we have a sorted array, so we also know that the target value must be in the upper portion
of the array.

We change our low to mid + 1 and find the new mid value again.
Low = mid +1

Mid = low + (high - low)/2

Our new mid is 7 now. We compare the value stored at location 7 with our target value 31.

The value stored at location 7 is not a match, rather it is more than what we are looking for. So, the value must be in the lower part from
this location.

Hence, we calculate the mid again. This time it is 5.


SORTING ALGORITHMS
Sorting Algorithms are methods of reorganizing a large number of items into some specific order
such as highest to lowest, or vice-versa, or even in some alphabetical order.
These algorithms take an input list, processes it (i.e., performs some operations on it) and produce
the sorted list.
For example, consider an array A = {A1, A2, A3, A4, ?? An }, the array is called to be in ascending
order if element of A are arranged like A1 > A2 > A3 > A4 > A5 > ? > An 
The importance of sorting lies in the fact that data searching can be optimized to a very high level, if
data is stored in a sorted manner. Sorting is also used to represent data in more readable formats.
Following are some of the examples of sorting in real-life scenarios −
Telephone Directory − The telephone directory stores the telephone numbers of people sorted by
their names, so that the names can be searched easily.
Dictionary − The dictionary stores words in an alphabetical order so that searching of any word
becomes easy.
TYPES OF SORTING
ALGORITHMS
•Bubble sort
•Insertion
•Selection
•Quick
•Merge
•Heap
BUBBLE SORT
Bubble sort is a simple sorting algorithm. This sorting algorithm is a 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.
Example:
First Pass:
( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1.
( 1 5 4 2 8 ) –>  ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) –>  ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap
them.
Second Pass:
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 )
( 1 4 2 5 8 ) –> ( 1 2 4 5 8 ), Swap since 4 > 2
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –>  ( 1 2 4 5 8 )
Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs
one whole pass without any swap to know it is sorted.
Third Pass:
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
INSERTION SORT
This is an in-place comparison-based sorting algorithm. Here, 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.
EXAMPLE
We take an unsorted array for our example.

Insertion sort compares the first two elements.

It finds that both 14 and 33 are already in ascending order. For now, 14 is in sorted sub-list.

Insertion sort moves ahead and compares 33 with 27.

And finds that 33 is not in the correct position.

It swaps 33 with 27. It also checks with all the elements of sorted sub-list. Here we see that
the sorted sub-list has only one element 14, and 27 is greater than 14. Hence, the sorted sub-
list remains sorted after swapping.
By now we have 14 and 27 in the sorted sub-list. Next, it
compares 33 with 10.
These values are not in a sorted order.

So we swap them.
However, swapping makes 27 and 10 unsorted.
Hence, we swap them too.

Again we find 14 and 10 in an unsorted order.

We swap them again. By the end of third iteration, we


have a sorted sub-list of 4 items.
SELECTION SORT
The selection sort algorithm sorts an array by repeatedly finding the minimum
element (considering ascending order) from unsorted part and putting it at the
beginning. The algorithm maintains two subarrays in a given array.
1) The subarray which is already sorted.
2) Remaining subarray which is unsorted.
In every iteration of selection sort, the minimum element (considering ascending
order) from the unsorted subarray is picked and moved to the sorted subarray.
Following example explains the above steps:
Arr[] = 64 25 12 22 11
// Find the minimum element in arr [0…….4] and place it at the beginning
11 25 12 22 64
// Find the minimum element in arr[1….4]
//and place it at beginning of arr[1…4]
11 12 25 66

//Find the minimum element in arr[1……4]


//and place it at the beginning of arr[2…4]
1112 22 25 64

//Find the minimum element in arr[3…4]


//and place it the begging of arr[3….4]
11 12 22 25 64
MERGE SORT
Merge sort is a sorting technique based on divide and conquer technique. With worst-
case time complexity being Ο(n log n), it is one of the most respected algorithms.
Merge sort first divides the array into equal halves and then combines them in a
sorted manner.
We know that merge sort first divides the whole array iteratively into equal halves unless
the atomic values are achieved. We see here that an array of 8 items is divided into two
arrays of size 4.
This does not change the sequence of appearance of items in the original.
Now we divide these two arrays into halves.

We further divide these arrays and we achieve atomic value which can no
more be divided.

Now, we combine them in exactly the same manner as they were broken down. Please note the color codes given to these lists.
We first compare the element for each list and then combine them into another list in a sorted manner. We see that 14 and 33 are in sorted positions.
We compare 27 and 10 and in the target list of 2 values we put 10 first, followed by 27. We change the order of 19 and 35 whereas 42 and 44 are
placed sequentially.

In the next iteration of the combining phase, we compare lists of two data values, and
merge them into a list of found data values placing all in a sorted order.

After the final merging, the list should look like this −
QUICK SORT
Like Merge Sort, Quicksort is a Divide and Conquer algorithm. It picks an element
as pivot and partitions the given array around the picked pivot. There are many
different versions of quicksort that pick pivot in different ways. 
It’s a highly efficient sorting algorithm and is based on partitioning of array of data
into smaller arrays. A large array is partitioned into two arrays one of which holds
values smaller than the specified value, say pivot, based on which the partition is
made and another array holds values greater than the pivot value.
Quicksort partitions an array and then calls itself recursively twice to sort the two
resulting subarrays. This algorithm is quite efficient for large-sized data sets as its
average and worst-case complexity are O(n2), respectively.
HEAPSORT
Heap sort is a comparison based sorting technique based on Binary Heap data
structure. It is similar to selection sort where we first find the minimum
element and place the minimum element at the beginning. We repeat the same
process for the remaining elements.
HASH TABLES
Hashing
Hashing is a technique or process of mapping keys, values into the hash table by using a hash
function. It is done for faster access to elements. The efficiency of mapping depends on the
efficiency of the hash function used.
It’s a technique to convert a range of key values into a range of indexes of an array.
Hash Table
It’s also called a hash map. It’s a data structure which stores data in an associative manner. In a
hash table, data is stored in an array format, where each data value has its own unique index value.
Access of data becomes very fast if we know the index of the desired data.
Thus, it becomes a data structure in which insertion and search operations are very fast
irrespective of the size of the data. Hash Table uses an array as a storage medium and uses hash
technique to generate an index where an element is to be inserted or is to be located from.
HASH FUNCTION
A hash function is any function that can be used to map data of arbitrary size to
fixed-size values. The values returned by a hash function are called hash values, hash
codes, digests, or simply hashes. The values are usually used to index a fixed-size
table.
Eg.
h(k) = k % m
Where k = key
m = mode
HASH TABLE OPERATIONS
HASH COLLISION
Hash collision or clash is when two pieces of data in a hash table share the same hash
value. The hash value in this case is derived from a hash function which takes a data
input and returns a fixed length of bits
COLLISION AVOIDANCE
STRATEGIES
Closed addressing/ open hashing
Chaining

Open addressing/ closed hashing


Linear probing
Quadratic probing
Double hashing
OPEN HASHING/ CLOSED
ADDRESSING
Open  hashing is a collision avoidance method which uses array of linked list to
resolve the collision. It is also known as the chaining method (each linked list is
considered as a chain).
Chaining.
In the chaining approach, the hash table is an array of linked lists i.e., each index has
its own linked list.
All key-value pairs mapping to the same index will be stored in the linked list of that
index.
OPEN ADDRESSING/ CLOSED
HASHING
Open addressing, or closed hashing, is a method of collision resolution in hash tables. With
this method a hash collision is resolved by probing, or searching through alternative
locations in the array (the probe sequence) until either the target record is found, or an
unused array slot is found, which indicates that there is no such key in the table.

Linear probing
Linear probing is one of the collision resolution technique of closed hashing in data
structure.
In linear probing technique we will not use any extra space for preventing collision, if a
collision happens at a cell we will store the collided key in the next available free space of
hash table.
While using the method of linear probing you have to use the modified hash function instead
of just the hash function for the insertion of keys into hash table.
QUADRATIC PROBING
Quadratic probing is also a collision resolution technique which comes under the
closed hashing in data structure.
In this technique also you will be utilizing the space inside the hash table to avoid
collisions between keys.

Function; U + i^2 where U= key


i = index
DOUBLE HASHING
The double hashing technique uses one hash value as an index into the table and then
repeatedly steps forward an interval until the desired value is located, an empty
location is reached, or the entire table has been searched; but this interval is set by a
second, independent hash function.
Unlike linear probing and quadratic probing, the interval depends on the data, so that
values mapping to the same location have different bucket sequences; this minimizes
repeated collisions and the effects of clustering.
BINARY SEARCH TREES
A Binary Search Tree (BST) is a tree in which all the nodes follow the below-
mentioned properties −The value of the key of the left sub-tree is less than the value
of its parent (root) node's key.
The value of the key of the right sub-tree is greater than or equal to the value of its
parent (root) node's key.
Thus, BST divides all its sub-trees into two segments; the left sub-tree and the right
sub-tree and can be defined as −

left_subtree (keys) < node (key) ≤ right_subtree (keys)


REPRESENTATION
BST is a collection of nodes arranged in a way where they maintain BST properties. Each node has
a key and an associated value. While searching, the desired key is compared to the keys in BST and
if found, the associated value is retrieved.
Following is a pictorial representation of BST −

We observe that the root node key (27) has all less-valued keys on the left sub-tree and the higher
valued keys on the right sub-tree.
BASIC OPERATIONS

Following are the basic operations of a tree −


•Search − Searches an element in a tree.
•Insert − Inserts an element in a tree.
•Pre-order Traversal − Traverses a tree in a pre-order manner.
•In-order Traversal − Traverses a tree in an in-order manner.
•Post-order Traversal − Traverses a tree in a post-order manner.
FULL BINARY TREE

If each node of binary tree has either two children or no child at all, is said to be a Full
Binary Tree.
Full binary tree is also called as Strictly Binary Tree.

Every node in the tree has either 0 or 2 children.


Full binary tree is used to represent mathematical expressions.
COMPLETE BINARY TREE
If all levels of tree are completely filled except the last level and the last level has all keys as left as possible,
is said to be a Complete Binary Tree.
Complete binary tree is also called as Perfect Binary Tree.

In a complete binary tree, every internal node has exactly two children and all leaf nodes are at same level.
For example, at Level 2, there must be 22 = 4 nodes and at Level 3 there must be 23 = 8 nodes.
DEGENERATE BINARY SEARCH
TREE
Degenerate Binary Tree is a Binary Tree where every parent node has only one child
node.
REPRESENTATION OF
GRAPHS
A graph is a finite set of nodes with edges between nodes.
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
DIRECTED VS. UNDIRECTED
GRAPHS
When the edges in a graph have no direction, the graph is called undirected.
DIRECTED
When the edges in a graph have a direction, the graph is called directed (or digraph).

Warning: if the graph is


directed, the order of the
vertices in each edge is
important !!
GRAPH TERMINOLOGY
•Adjacent nodes: two nodes are adjacent if they are connected by an edge

5 is adjacent to 7
7 is adjacent from 5

•Path: a sequence of vertices that connect two nodes in a graph


•Complete graph: a graph in which every vertex is directly connected to every other
vertex
TERMINOLOGIES CONT…
•Weighted graphs are graphs with edges assigned with values.
A weighted graph is therefore a special type of labeled graph in which the labels are
numbers (which are usually taken to be positive).
TYPES OF GRAPH
REPRESENTATIONS
Adjacency Matrix: 
Adjacency Matrix is a 2D array of size V x V where V is the number of vertices in a
graph. Let the 2D array be adj[][], a slot adj[i][j] = 1 indicates that there is an edge
from vertex i to vertex j.
Adjacency matrix for undirected graph is always symmetric. Adjacency Matrix is also
used to represent weighted graphs. If adj[i][j] = w, then there is an edge from vertex i
to vertex j with weight w. 
The adjacency matrix for the above example graph is: 
Advantages of adjacency matrix
From the adjacency matrix, to determine the connection of vertices is easy
easy to add or remove an edge
easy to check if an edge existing
Disadvantages of adjacency matrix:

The larger the graph (more nodes), the more memory is needed.
For a sparse graph, space will be wasted for not storing many edges.
If static array is used for the matrix, adding or deleting nodes may not be
easy
ADJACENCY LIST
An array of lists is used. The size of the array is equal to the number of vertices. Let
the array be an array[]. An entry array[i] represents the list of vertices adjacent to
the ith vertex.
This representation can also be used to represent a weighted graph. The weights of
edges can be represented as lists of pairs. Following is the adjacency list
representation of the above graph. 
Advantages of adjacency list:
 Easy to find successors of a node
 Easy to find all neighboring nodes
 Space efficient as it only stores connected nodes

Disadvantage of adjacency list:


 It is time consuming to check if an edge is part of a graph

 
DEPTH-FIRST VS BREADTH-
FIRST SEARCH TRAVERSALS.
The Depth–first search (DFS) algorithm starts at the root of the tree (or some
arbitrary node for a graph) and explored as far as possible along each branch
before backtracking.

The Breadth–first search (BFS) algorithm also starts at the root of the tree (or some
arbitrary node of a graph), but unlike DFS, it explores the neighbor nodes first, before
moving to the next-level neighbors. In other words, BFS explores vertices in the
order of their distance from the source vertex, where distance is the minimum length
of a path from the source vertex to the node.
REPRESENTATION
REVISION QUESTIONS
Briefly explain the different computing algorithms in data structures.
What are the sorting algorithms and briefly explain how any 3 of them works.
Define the following terms as used in data structures
Harsh table
Hashing
Keys
Harsh value
Explain any 4 applications of harsh tables
Briefly, describe the limitations of harsh tables
Explain the following harsh functions
Mid-square method
The folding method
Search function
REFERENCES
https://
www.tutorialspoint.com/data_structures_algorithms/data_structures_basics.htm
Wikipedia
https://www.studytonight.com/data-structures/bubble-sort
Heap sort; https://www.youtube.com/watch?v=MtQL_ll5KhQ
Quicksort; https://www.youtube.com/watch?v=Hoixgm4-P4M
Revision papers; https://tutorview.net/index.php?/
home/pdf- revision/157518190325A_S/DATA-STRUCTURES-AND-ALGORITH
MS-past-papers/home/pdf-revision/157518190325A_S/DATA-STRUCTURES-AN
D-ALGORITHMS-past-papers
QUESTIONS?

You might also like