Algorithm Design & Data Structures
Algorithm Design & Data Structures
COM
Input: there are zero or more quantities which are externally supplied
Output: at least one quantity is produced
Definiteness: each instruction must be clear and unambiguous
Finiteness: if we trace out the instructions of an algorithm, then for all cases the algorithm
will terminate after a finite number of steps
Effectiveness: every instruction must be sufficiently basic that it can in principle be carried
out by a person using only pencil and paper.
gi
st
Worst case: It measures the behaviour of algorithm with respect to the worst possible case
of the input instance.
Average case: It measures the behaviour of algorithm when the input is randomly drawn
from a given distribution.
Best case: It measures the behaviour of algorithm with respect to the best possible case of
the input instance
an
.co
Performance Analysis: For an algorithm performance analysis can be divided into two major phases:
Asymptotic analysis: Evaluate the performance of an algorithm in terms of input size (dont measure
the actual running time).And calculate, how does the time (or space) taken by an algorithm increases
with the input size.
en
Asymptotic notations: The following 3 asymptotic notations are mostly used to represent time
complexity of algorithms:
Notation: For a given function g(n), we denote
((g(n)) = {f(n): there exist positive constants c1, c2 and n0 such that 0 <= c1*g(n) <= f(n) <= c2*g(n)
for all n >= n0}
Big O Notation: For a given function g(n), we denote O (g(n)) is following set of functions.
an
.co
O(g(n)) = { f(n): there exist positive constants c and n0 such that 0 <= f(n) <= cg(n) for all n >= n0}
en
gi
st
(g(n)) = {f(n): there exist positive constants c and n0 such that0 <= cg(n) <= f(n) for all n >= n0}
Substitution method
Iteration method
Mater method
Divide and conquer (DAC): This algorithm solves a problem using following three steps:
Following are some standard algorithms that are Divide and Conquer algorithms.
.co
Dynamic programming: This algorithm solves problem by dividing into sub problems. It is used when
sub problems are not independent whereas DAC solves a subproblem n times. This algorithm solves
a problem using following three steps:
an
gi
st
Greedy Algorithm: It solves the problems by making the choice that seems best at that particular
moment. A greedy algorithm works if a problem exhibits the following 2 properties:
en
1. Greedy choice property: A globally optimal solution can be arrived at by making a locally
optimal solution.
2. Optimal substructure: optimal solution contain optimal sub solution.
Name
Best
Average
Worst
Stable
Merge sort
Yes
Heap sort
No
Quick sort
Yes
.co
Insertion sort
No
an
Selection sort
Shell sort
No
best known is
gi
st
or
Bubbel sort
Yes
en
Elementary data structure: A data structure is a particular way of storing and organizing data in a
computer so that it can be used efficiently. Following are some data structure:
Array: It consists of collection of elements, each identified by at least one array index or key. For
example: Age 0, Age1 etc is index value and 30,32 etc are array elements.
Age 0
Age 1
Age 2
Age 3
Age 4
Age 5
Age 6
Age 7
Age 8
Age 9
30
32
54
32
26
29
23
43
34
Type of array:
One-dimensional arrays
Multidimensional arrays (2-D , 3-D etc)
Polish notation
Evaluation of postfix/prefix expression
Application of stack:
gi
st
an
.co
Queue: It is a data structure in which item are added (enqueue) at one end and deleted (dequeue) at
another end. It works on the principle of first in first out (FIFO).
Types of queue:
en
Deques: It stands for Double ended queue and elements can be added or removed form at
either end but not in the middle.
Priority queues: It is a queue, where additionally each element has a "priority" associated
with it. An element with high priority is served before an element with low priority. If two
elements have the same priority, they are served according to their order in the queue.
Circular queue: In circular queue the last node is connected back to the first node to make a
circle
Linked list: it is a data structure whose length can be increased or decreased at run time. For
example: 12 , 99 etc are data to be stored and along with data part address of the next element is
stored.
5
Tree: It is a data structure consisting of nodes organised as a hierarchy. Tress dnt has cycle.
gi
st
an
.co
Binary tree: A binary tree is one in which each node has at most two descendants - a node can have
just one but it can't have more than two.
Binary search tree: A tree is binary search tree if it hold following property:
The left subtree of a node contains only nodes with keys less than the node's key.
The right subtree of a node contains only nodes with keys greater than the node's key.
The left and right subtree each must also be a binary search tree.
There must be no duplicate nodes.
en
For example: Below is a binary search tree of size 9 and depth 3, with root 8 and leaves 1, 4, 7 and 1
B Tree: A m-way tree in which (m way means the tree has m children):
The root has at least one key
.co
not a B-tree
gi
st
an
B-tree of order 3
Graph Algorithms:
en
Simple cycle: It is a simple path but here first and last vertex is same.
Complete graph: graph in which all edges are present.
BFS (Breadth first search): BFS is a strategy for searching in a graph or tree. Search is performed by
two operations:
o
o
an
.co
DFS(Depth first search): DFS is an algorithm for traversing or searching tree or graph data structures.
One starts at the root (selecting some arbitrary node as the root in the case of a graph) and explores
as far as possible along each branch before backtracking. Depth First Traversal of the following graph
is 2, 0, 1, 3
en
gi
st
Topological Sort: Topological sorting for Directed Acyclic Graph (DAG) is a linear ordering of vertices
such that for every directed edge uv, vertex u comes before v in the ordering. Topological Sorting for
a graph is not possible if the graph is not a DAG. For example, a topological sorting of the following
graph is 5 4 2 3 1 0
8
Minimum spanning Tree (MST):
Given a connected and undirected graph, a spanning tree of that graph is a subgraph that is
a tree and connects all the vertices together.
A single graph can have many different spanning trees.
MST for a weighted, connected and undirected graph is a spanning tree with weight less
than or equal to the weight of every other spanning tree.
The weight of a spanning tree is the sum of weights given to each edge of the spanning tree
Prims algorithm: It is a greedy algorithm that finds a minimum spanning tree for a connected
weighted undirected graph. This means it finds a subset of the edges that forms a tree that includes
every vertex, where the total weight of all the edges in the tree is minimized.
.co
Kruskal's algorithm: It is a greedy algorithm in graph theory that finds a minimum spanning tree for
a connected weighted graph. This means it finds a subset of the edges that forms a tree that
includes every vertex, where the total weight of all the edges in the tree is minimized. If the graph is
not connected, then it finds a minimum spanning forest (a minimum spanning tree for each
connected component).
Single source shortest path: In graph theory, the shortest path problem is the problem of finding a
path between two vertices (or nodes) in a graph such that the sum of the weights of its constituent
edges is minimized.
BellmanFord algorithm:
It is an algorithm that computes shortest paths from a single source vertex to all of the other
vertices in a weighted digraph.
It is slower than Dijkstra's algorithm for the same problem, but more versatile, as it is
capable of handling graphs in which some of the edge weights are negative numbers.
gi
st
an
Dijkstra's algorithm: It is a graph search algorithm that solves the single-source shortest path
problem for a graph with non-negative edge path costs, producing a shortest path tree. This
algorithm is often used in routing and as a subroutine in other graph algorithms.
en
FloydWarshall algorithm: It is a graph analysis algorithm for finding shortest paths in a weighted
graph with positive or negative edge weights (but with no negative cycles, see below) and also for
finding transitive closure of a relation R. A single execution of the algorithm will find the lengths
(summed weights) of the shortest paths between all pairs of vertices, though it does not return
details of the paths themselves.
Articulation Points (or Cut Vertices) in a Graph: A vertex in an undirected connected graph is an
articulation point (or cut vertex) if removing it (and edges through it) disconnects the graph.
Following are some example graphs with articulation points encircled with red color.
gi
st
an
.co
Biconnected graph: An undirected graph is called Biconnected if there are two vertex-disjoint paths
between any two vertices. In a Biconnected Graph, there is a simple cycle through any two vertices.
By convention, two nodes connected by an edge form a biconnected graph, but this does not verify
the above properties. Following are some examples.
en
Bridges in a graph: An edge in an undirected connected graph is a bridge if removing it disconnects the
graph. Following are some example graphs with bridges highlighted with red color.
Eulerian path and circuit: Eulerian Path is a path in graph that visits every edge exactly once.
Eulerian Circuit is an Eulerian Path which starts and ends on the same vertex.
10
.co
Strongly Connected Components: A directed graph is strongly connected if there is a path between all
pairs of vertices. A strongly connected component (SCC) of a directed graph is a maximal strongly
connected subgraph. For example, there are 3 SCCs in the following graph.
en
gi
st
an
Graph Coloring: Graph coloring problem is to assign colors to certain elements of a graph subject to
certain constraints.
Vertex coloring: It is the most common graph coloring problem. The problem is, given m
colors, find a way of coloring the vertices of a graph such that no two adjacent vertices are
colored using same color.
The other graph coloring problems like Edge Coloring (No vertex is incident to two edges of
same color) and Face Coloring (Geographical Map Coloring) can be transformed into vertex
coloring.
Chromatic Number:
The smallest number of colors needed to color a graph G is called its chromatic number. For
example, the following can be colored minimum 3 colors.
11
NP-complete problems are the hardest problems in NP set. A decision problem L is NP-complete
if:
en
gi
st
an
.co
L is in NP (Any given solution for NP-complete problems can be verified quickly, but there is
no efficient known solution).
Every problem in NP is reducible to L in polynomial time (Reduction is defined below).
12