You are on page 1of 14

UNIT 1: INTRODUCTION

What is algorithm?

An algorithm is a set of well-defined instructions in sequence to solve a problem.


Qualities of a good algorithm
1. Input and output should be defined precisely.
2. Each step in the algorithm should be clear and unambiguous.
3. Algorithms should be most effective among many different ways to solve a problem.
4. An algorithm shouldn't include computer code. Instead, the algorithm should be written in such a way that it
can be used in different programming languages.

Notion of algorithm:

An algorithm is finite set of instructions that is followed, accomplishes a particular task. In addition, all algorithms
must satisfy the following criteria:

1. Input. Zero or more quantities are externally supplied.

2. Output. At least one quantity is produced.

3. Definiteness. Each instruction is clear and produced.

4. Finiteness. If we trace out the instruction of an algorithm, then for all cases, the algorithm terminates after a
finite number of steps.

5. Effectiveness. Every instruction must be very basic so that it can be carried out, in principal, by a person using
only pencil and paper. It is not enough that each operation be definite as in criterion 3; it also must be feasible.

~1~
Fundamentals of Algorithmic problem solving:

• Understanding the problem

• Ascertain the capabilities of the computational device

• Exact /approximate soln.

• Decide on the appropriate data structure

• Algorithm design techniques

• Methods of specifying an algorithm

• Proving an algorithms correctness

• Analysing an algorithm

Understanding the problem: The problem given should be understood completely. Check if it is similar to some
standard problems & if a Known algorithm exists. otherwise a new algorithm has to be devised. Creating an
algorithm is an art which may never be fully automated. An important step in the design is to specify an in- stance
of the problem.

Read the problem carefully and ask questions for clarifying doubts.

Ascertain the capabilities of the computational device: Once a problem is understood we need to Know the
capabilities of the computing device this can be done by Knowing the type of the architecture,speed & memory
availability.

Exact /approximate solution.: Once algorithm is devised, it is necessary to show that it computes answer for all
the possible legal inputs. The solution is stated in two forms ,Exact solution or approximate solution .examples
of problems where an exact solution cannot be obtained are i)Finding a square root of number. ii)Solutions of
non-linear equations.

Decide on the appropriate data structure: some algorithms do not demand any data structures but some
algorithms demand data structures like array, stacks, queues, list etc…

Algorithm design techniques:. Dynamic programming is one of the algorithm technique. Some of the techniques
are divide and conquer, decrease and conquer, brute force, greedy technique.

Methods of specifying an algorithm: There are mainly two options for specifying an algorithm: use of natural
language or pseudocode & Flowcharts.

A Pseudo code is a mixture of natural language & programming language like constructs. A flowchart is a method
of expressing an algorithm by a collection of connected geometric shapes.

Proving an algorithms correctness: Once algorithm is devised, it is necessary to show that it computes answer
for all the possible legal inputs. checking answers for all inputs. A common method for this is mathematical
induction.

~2~
Analyzing algorithms: we consider factors:
i) Time: it refers to amount of time taken by an algorithm to run (slow/fast).
ii) Space: it refers to space taken by an algorithm.
iii) Simple: generating simple instructions which is easy to understand.
iv) General: we should always write general instructions.
v) Range of inputs: your algorithms should handle any range of inputs.

Implementation: use any programming languages like C, C++, PYTHON, JAVA to implement your algorithms.

Important Problem Types


✓ Sorting.
✓ Searching.
✓ String processing (e.g. string matching)
✓ Graph problems (e.g. graph coloring problem)
✓ Combinatorial problems (e.g. maximizes a cost)
✓ Geometric problems (e.g. convex hull problem)
✓ Numerical problems (e.g. solving equations ).

1.Sorting: the process of rearranging the given items so that they are in ascending/descending order. It is necessary
to arrange the numbers, strings, record of students/employees. Examples: bubble sort, quick sort, merge sort.

~3~
2.Searching: the process of finding a particular item in a large amount of data. If it is sorted , easy to search.
Examples : linear search, binary search.
3. String processing: A string is a sequence of characters from an alphabet. String processing deals with
manipulation of characters/strings like string matching, string searching, replacing a string, deleting a string etc.

4.Graph problems: a graph can be thought of as a collection of points called vertices, some of which are
connected by line segments called edges. It is represented using arrays and linked list. Basic graph algorithms
include graph-traversal algorithm and topological sorting, traveling salesman problem (TSP).

5.Combinatorial Problems: this problem involves permutation and combination, subsets. These are most difficult
in computing both theortically and practically. If the size of the problem increases combinatorial objects grows
in an imaginary magnitude. the traveling salesman problem and the graph-coloring problem are examples
of combinatorial problems.

6.Geometric Problems: geometric algorithms deal with geometric objects such as points, lines, and poly-gons.
These are used in computer graphics. examples:
The closest-pair problem : given n points in the plane, find the closest pair among them. The convex-hull
problem asks to find the smallest convex polygon that would include all the points of a given set.
7. Numerical problems: it involves mathematical manipulation such as solving a equations, differential equations,
integrations etc.
Examples: newton Raphson methods, gauss elimination methods.

Fundamental data structure:


Data structure means the way the data are structured/organized in the memory. It has 2 types:
1. Primitive data structure: these are manipulated directly by machine instructions. Examples: int, char, double,
float.

2. Non primitive data structure: these can’t be manipulated directly by machine instructions.
Examples: graphs, trees, linked list, files etc.
Non primitive data structure are further divided into 2 types:
Linear ds: the data are stored in a sequential manner. In linear data structure, single level is involved. Examples:
array, stack, queue, linked list.
Non linear ds: the data are storedin an non linear way(not sequentially). In a non-linear data structure, single
level is not involved. Examples: trees, graphs, files.

~4~
ARRAY: (linear data structure)
1. An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of
the same type together.
2. Each element can be uniquely identified by their index in the array.
arr[0],arr[1],arr[2]….arr[n]. 0,1,2….n are index .

3. An element is accessed by indexing the array name. This is done by placing the index of the element within
square brackets after the name of the array. For example –
int i = arr[2];

STACK:( linear data structure)

1. Stack is a linear data structure which follows a particular order in which the operations are performed. The
order may be LIFO(Last In First Out).
2. Operation on stack are : Inserting: PUSH , Deleting : POP, overflow, underflow.
3. Stack is an ordered list of similar data type.
4. Stack is a LIFO(Last in First out) structure.
5. push() function is used to insert new elements into the Stack and pop() function is used to remove an element
from the stack. Both insertion and removal are allowed at only one end of Stack called Top.
6. Stack is said to be in Overflow state when it is completely full and is said to be in Underflow state if it is
completely empty.
7. Implementation:
There are two ways to implement a stack:
Using array
Using linked list
~5~
QUEUE:
1. A Queue is a linear structure which follows a particular order in which the operations are performed. The order
is First In First Out (FIFO).
2. A queue is open at both its ends. One end is always used to insert data (enqueue) called rear and the other is
used to remove data (dequeue) called front.
3. Queue in data structure is of the following types
Simple Queue, Circular Queue, Priority Queue, Dequeue (Double Ended Queue)
4. Operation on queue are : insert , delete, display

LINKED LIST:

A linked list is a linear data structure, in which one or nodes are connected. The elements in a linked list are linked
using pointers. It has 2 fields:
1.contains the data node.
2.contains the address of the next node.

~6~
1. Types of linkedlist: Singly Linked List, Doubly Linked List, circular linked list.
2. The operations we can perform on singly linked lists are insertion, deletion and traversal.
3. Singly linked lists contain nodes which have a data part as well as an address part i.e. next, which points to
the next node in the sequence of nodes.
4. In a doubly linked list, each node contains a data part and two addresses, one for the previous node and one
for the next node.
5. In circular linked list the last node of the list holds the address of the first node hence forming a circular
chain.

Graphs
A Graph is a non-linear data structure consisting of nodes and edges. The nodes are sometimes also referred to
as vertices and the edges are lines or arcs that connect any two nodes in the graph.

TYPES OF GRAPHS:

1. Undirected Graph:

In an undirected graph, nodes are connected by edges that are all bidirectional.

2.Directed Graph

In a directed graph, nodes are connected by directed edges – they only go in one direction.

~7~
The weight of an edge can represent:

▪ Cost or distance = the amount of effort needed to travel from one place to
another

▪ Capacity = the maximim amount of flow that can be transported from one
place to another

o Weighted graph = a graph whose edges have weights

Example:

WEIGHTED GRAPH IS REPRESENTED IN 2 WAYS:

1. Adjacent matrix

2. Adjacent list

~8~
Adjacency matrix representation
To store weighted graph using adjacency matrix form, we call the matrix as cost matrix. Here each cell at position
M[i, j] is holding the weight from edge i to j. If the edge is not present, then it will be infinity . For same node, it
will be 0.

0 ∞ 6 3 ∞

3 0 ∞ ∞ ∞

∞ ∞ 0 2 ∞

∞ 1 1 0 ∞

∞ 4 ∞ 2 0

Adjacency List representation


In the adjacency list, each element in the list will have two values. The first one is the destination node, and the
second one is the weight between these two nodes. The representation is like below.

~9~
TERMS USED IN GRAPH:

1. PATH: The path in a graph is an infinite sequence of edges which connects the vertices in a graph. A path
from vertex u to vertex v of a graph is a sequence of adjacent vertices that starts with u and ends with v.
Example: There exists a path from vertex 1 to 4. It is as follows: 1-2-3-4 .

2. CYCLE : It is a closed path. It is a path of positive length that starts and end at same vertex and does not traverse
the same edge more than one.In the below example, A –B-C-A is a cycle , A-B-C-D-A is a cycle

3. LENGTH : It is the total number of vertices in the vertex sequence defining the path minus 1 , which mean
number of edges in a graph. In the below example, A-B-C-F is a path of length 3 from A to F and A-B-C-F-E-
D is a path of length 5

~ 10 ~
FOREST AND SELF LOOP GRAPH:

➢ A forest is an acyclic graph, which is not connected .


➢ A loop is an edge which starts and ends on the same vertex as shown below

fig: forest graph

MULTIGRAPH:

➢ A multigraph is a graph with multiple occurrence at same edge between two vertices.
➢ It is a graph which is permitted to have multiple edges (also called parallel edges), that is, edges that
have the same end nodes. Thus two vertices may be connected by more than one edge.

~ 11 ~
COMPLETE GRAPH : is a graph in which each pair of vertices is connected by an edge

TREES : A tree is a nonlinear data structure. A tree can be empty with no nodes or a tree is
a structure consisting of one node called the root and zero or one or more subtrees.

• It shows parent-child relationship.

TERMS USED:
➢ Root: first node written at the top is root.
➢ Edge: In a tree data structure, the connecting link between any two nodes is called as edge.
➢ Parent: The node which has child / children.
➢ Child node: The node obtained from a parent node.
➢ Sibling : The nodes(TWO OR MORE) having same parent.
➢ Left and right node: the nodes that lies towards left subtree is left node and the nodes that lies towards right
subtree is right node.
➢ Degree : number of subtree of a node.
➢ Level : the distance of a node from the root .
➢ Height/depth : The height of a node is the number of edges from the node to the deepest leaf.
➢ Leaf node: a leaf is a node with no child.

~ 12 ~
ROOTED TREE AND ORDERED TREE:

1. A rooted tree is a tree in which one vertex is designated as a root and every edge is directed away from the root.
2. (TAKE EXAMPLE WHICH I SOLVED THROUGH ONLINE FOR ROOTED TREE.)
3. Ordered tree: it contain nodes which can be ordered according to particular criteria .
4. Example for ordered tree:
a. it is a binary tree, i.e. nodes have at most two children (conveniently called the left and right child).
b. The tree is ordered when at every node, when number assigned to each parent node is larger than all the
number in its left child tree and are smaller than number in its right subtree .

SETS AND DICTIONARY:

• A set stores distinct values of the same type in a collection with no defined ordering.
• For example: S={12,15,17,19}
• Operation on set: searching an item , union and intersection of two sets.
• A dictionary stores associations between keys of the same type and values of the same type in a collection
with no defined ordering.
• Each value is associated with a unique key, which acts as an identifier for that value within the dictionary.
• Operation on dictionary: adding an new item, searching an item, deleting an item.
• Difference b/w Sets and dictionary:
• Sets Are unordered collection
• Set can not contain duplicates
• Sets can store only Hashable types or custom types that conforms to Hashable protocol
• Set stores elements of the same type.
• Dictionary is an unordered collection.
• Dictionaries can not contain duplicates.
• Dictionaries store associations between keys of the same type with values of the same type.
• Keys act as identifier and you access a value in dictionary by using its identifier unlike arrays where you can
access an object using its index.

~ 13 ~
EUCLID S ALGORITHM:

STEP 1: IF N=0 RETURN THE VALUE OF M AS THE ANSWER , OTHERWISE STEP2

STEP 2: DIVIDE M/N AND ASSIGN THE VALUE OF THE REMAINDER TO R.

STEP 3: ASSIGN THE VALUE OF N TO M AND THE VALUE OF R TO N , THEN REPEAT /GO
TO STEP 1.

ALGORITHM EUCLIDS (M,N)

//INPUT: 2 NON-NEGATIVE NUMBERS, NOT ZERO INTEGERS

//OUTPUT: GCD OF 2 NUMBERS IE M AND N

WHILE N!=0

R=M%N

M=N

N=R

RETURN M

~ 14 ~

You might also like