0% found this document useful (0 votes)
7 views

Practical Theory

Theory for DAA practical exam

Uploaded by

PARTH VYAVHARE
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Practical Theory

Theory for DAA practical exam

Uploaded by

PARTH VYAVHARE
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Practical Theory

Finished?

Selection Sort
Repeatedly selects smallest element from the unsorted part of the array
and swaps it with the first element of the unsorted array

Is time complexity O(N^2)

Does not preserve the relative order of the elements(Unstable sort)

Does not work on large data set

Quick Sort
Based on Divide And Conquer strategy

Picks element as pivot and partitions the given array by placing pivot in
correct position in sorted array which puts smaller elements to left side and
larger elements to right side

Partitions are done recursively on both side of pivot after it has been placed
in correct position in sorted array

Time complexity

Best and Average case N log N

Worst case N^2

Unstable sort

Heap Sort
Comparison based sorting algorithm based on Binary Heap

We find smallest element by help of Mean Heap

Min Heap - Binary tree where parent element must be smaller than all
child nodes

Practical Theory 1
We place the smallest element at beginning by removing it from root of the
min heap

We then replace root with last node of heap and heapify root of heap

Time complexity of Heap Sort N log N

Unstable Sort

Merge Sort
Based on Divide And Conquer strategy

Recursively divides input array into smaller sub arrays and sorting those
arrays by merging them back together

Divide - Divide list or array recursively into two halves until it can no
more be divided

Conquer - Each subarray is sorted individually using merge sort


algorithm

Merge - Sorted arrays are merged back together to form a sorted array

Time complexity for all cases is N log N

Stable sort

Insertion Sort
Works by iteratively inserting each element of an unsorted list into correct
position is sorted portion of the array

Stable sort - preserves the relative position of elements in the array

Time Complexity (N^2)

Topological Sort
For DAG

linear ordering of vertices such that for every directed edge u-v , vertex u
comes before v in ordering

Steps

Practical Theory 2
Create Graph with n nodes and m directed edges

Initialize visited stack to n size

For each unvisited node

Call DFS function

In DFS function mark node as visited and recursively call DFS for all
unvisited neighbors of the vertex

Once all neighbors are visited push the vertex in the stack

Once all vertex are visited pop all elements from the stack and append it
to o/p list which is array

Time complexity O(V+E)

Strassen’s Matrix Multiplication


For a 2x2 matrix

Regular Matrix Multiplication requires 8 multiplication and 4 additions

For Strassen’s it requires 7 multiplication and 18 addition

Time Complexity

Regular is N^3

Strassen’s is N^2

Prim’s for MST


Greedy Method

Algorithm starts with single node

Steps

Time Complexity is O(V^2)

Knapsack(Greedy)
For given N items with in form of (profit, weight) putting these items into
capacity W to get maximum profit

Practical Theory 3
ratio of p/w for each item is found out and the item are sorted on basis of
the ratio and can be added as a whole element or a fraction of it

Time Complexity O(N log N)

Knapsack(Dynamic Programming)
Time Complexity (N*W)

LCS
O(m*n)

Dijkstra’s for Single Source Shortest Path


Graph should not contain negative edges

Steps

Set of Visited and unvisited nodes are maintained

Starts at source vertex and iteratively selects unvisited vertex with


smallest tentative distance

It then visits neighbor and updates if tentative distance is shorter

Process continues till destination vertex is reached

Dynamic Programming

Travelling Salesman Problem (Nearest


Neighbor Method)
Time complexity N^2 log to the base 2 N

Optimal Binary Search Tree


Binary Search tree that minimizes the expected search cost(the number of
comparisons required to search for a given key)

Practical Theory 4
Construction Time Complexity N^2

To find key Time Complexity log N

Sum of Subsets(Backtracking)
Time complexity 2^N

BFS
Involves visiting of all connect nodes of a graph level by level

It visits all vertices at current level before moving to the next depth level

BFS uses queue data structure

Time Complexity O(V)

DFS
Involves visiting node as far as possible along each branch before
backtracking

Uses Stack data structure

Time Complexity is O(V+E)

Matrix Chain Multiplication


Given dimension of sequence of matrices in a array , task is to find most
efficient way to multiply theses matrices such that total number of
multiplication remains minimum

N Queens (by backtracking)


Problem is to place N queens in N x N board such that no queen attacks
each other

Solving in backtracking

Queen is placed in a column and then another queen is placed in next


column

Practical Theory 5
If the queen does not attack , we make it part of solution

if it clashes then we return false i.e. it is not part of solution set and
backtrack

Time complexity N!

Hamiltonian Cycle
It is cycle in Graph which visits every vertex ones and returns to starting
vertex

Practical Theory 6

You might also like