You are on page 1of 5

Algorithm

a process or set of rules to be followed in calculations or other problem-solving operations,


especially by a computer.

An algorithm is a procedure used for solving a problem or performing a computation.

 Brute Force Algorithm: it goes through all possible choices until a solution is found.
 Sorting Algorithm: to arrange elements of an array/list in a specific order
 Searching Algorithm: to retrieve information stored within particular data structure, or calculated in
the search space of a problem domain
Search space of an algorithm
A search space is the set or domain through which an algorithm searches.

Searching space of an algorithm Search space represents a set of possible solutions, which a system may
have.

Propositional logic
Proposition: Simplest statements also called atomic formula

Propositional logic (PL) is the simplest form of logic where all the statements are made by
propositions. A proposition is a declarative statement which is either true or false. It is a
technique of knowledge representation in logical and mathematical form.

AND = true if all variables are true

OR = true if any variable is true

Time complexity, what is it and how to find it.


number of steps or arithmetic operations to complete its task

calculated or measured by counting the number of key operations

such as comparisons in sorting algorithm.

Space Factor − The space is calculated or measured by counting the maximum memory space
required by the algorithm.

Asymptomatic notation /Techniques to show complexity


Asymptotic Notation is used to describe the running time of an algorithm - how much time an
algorithm takes with a given input, n. There are three different notations:
Big O, works for upper bound worst case / order at most

Big Omega (Ω), works for lower bound best case / order at least

Big Theta (Θ), works for average bound average case / order exactly

Difference b/w DfS n BFS

BFS stands for Breadth First DFS stands for Depth First Search.
Search.

BFS uses Queue DS DFS uses stack DS

BFS works om the concept of FIFO . DFS works on the concept of LIFO

BFS is a traversal technique in DFS is also a traversal technique in


which all the nodes of the same which traversal is started from the
level are explored first, and then root node and explore the nodes as
we move to the next level. far as possible until we reach the
node that has no unvisited adjacent
nodes.

BFS does not use the backtracking DFS uses backtracking to traverse all
concept. the unvisited nodes.

BFS is slower than DFS. DFS is faster than BFS.

It is not suitable for the decision It is suitable for the decision tree.
tree because it requires exploring Based on the decision, it explores all
all the neighboring nodes first. the paths. When the goal is found, it
stops its traversal.

It is not memory efficient as it It is memory efficient as it requires


requires more memory than DFS. less memory than BFS.
Shortest path algorithm
finds the shortest path between a given node (which is called the "source node") and all other
nodes in a graph. This algorithm uses the weights of the edges to find the path that minimizes
the total distance (weight) between the source node and all other nodes.

Shortest Path Algorithm


 Dijkstra's algorithm solves the single-source shortest path problem with non-negative edge
weight.
 Bellman–Ford algorithm solves the single-source problem if edge weights may be negative.
Tree kia hay?
Tree Set of nodes and edges, has root node, always directed

Graph kia hay?


Set of vertices and edges, don’t have root node, directed or undirected

Kia tree ko graph ma convert kr skty hain?


Yes, we can by fully traversing the tree in any manner

Dynamic programming
Dynamic Programming is mainly an optimization over plain recursion. Wherever we see a recursive
solution that has repeated calls for same inputs, we can optimize it using Dynamic Programming. The
idea is to simply store the results of subproblems, so that we do not have to re-compute them when
needed later.

Mathematical Induction
It is a mathematical technique which is used to prove a statement, a formula or a theorem is true for
every natural number.

• Prove for Base value

• Suppose for k

• Prove for k+1

Knapsack Problem
Items are given with weights

Value of bag/sack is given

We have to fit the most valuable items according to the value of sack

0/1 knapsack = either an item completely goes in bag or not

Fractional knapsack = can pick part

Stack
Stack is a linear data structure that follows a particular order in which the operations are performed.
The order is LIFO (Last in First Out).

Can we convert graph into tree


Yes, we can by using BFS

Yes graphs can be converted into trees by using minimum spanning tree approach i.e. prim’s algorithm
and kruskal’s algorithm

prim’s algorithm is a greedy algorithm that finds a minimum spanning tree for a weighted
undirected graph.

Kruskal's algorithm is a minimum spanning tree algorithm that takes a graph as input and finds
the subset of the edges of that graph.

Compression technique example


Huffman coding is a lossless data compression algorithm.

Undirected graph
Undirected graphs have edges that do not have a direction. The edges indicate a two way relationship,
in that each edge can be traversed in both directions.

Directed graph
Directed graph have edges with direction which indicate one-way relationship, in that each edge can be
traverse in either one direction.

Compression technique
Huffman codes are a widely used and very effective technique for compressing data – Savings of 20% to
90% are typical, depending on the characteristics of the data being compressed.

• Huffman‟s algorithm uses table of frequencies of occurrence of characters to build up an optimal way
of representing each character as a binary string.

• Objective in Huffman coding is to develop a code that represents given text as compactly as possible

Difference of brute force and greedy approach


The brute force approach is a guaranteed way to find the correct solution by listing all the possible
candidate solutions for the problem. It is a generic method and not limited to any specific domain of
problems.
A greedy algorithm is an approach for solving a problem by selecting the best option available at the
moment. It doesn't worry whether the current best result will bring the overall optimal result. Greedy
approach never reverses the earlier decision even if the choice is wrong. It works in a top-down
approach. This algorithm may not produce the best result for all the problems. It's because it always
goes for the local best choice to produce the global best result.

Can we convert an undirected graph into directed graph


Every undirected graph can be easily converted to an equivalent directed graph by replacing every
undirected edge with two directed edges in opposite directions.

Name any algo for shortest path


Dijkstra's Algorithm finds the shortest path between a given node (which is called the "source node")
and all other nodes in a graph.

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.

A binary search tree (BST) adds these two characteristics:


 Each node has a maximum of up to two children.
 For each node, the values of its left descendent nodes are less than that of the current
node, which in turn is less than the right descendent nodes (if any).

dynamic programming

compression technique

Asympotatic notation

You might also like