Analysis And Design Of Algorithms

Course Code: 24PCA401

By: Dr. Praveen N

Nitte Institute of Professional Education(NIPE)

NITTE (Deemed to be University), Mangaluru

Note: Please use your Nitte email ID to access the Google Drive files.

Sl NoTopicLink
1Introduction
2Fundamentals of Problem Solving
3Algorithm Efficiency
4Recursive Algorithms
5Brute Force & Search
6Brute Force Approaches (Contd..)
7Transform and Conquer
8Decrease and Conquer
9Space and Time Trade-Offs
10Dynamic Programming
11Greedy Technique
12Limitations of Algorithmic Power

Lab Programs & Performance Analysis

Floyd's Algorithm

def floyd_warshall(graph):
    v = len(graph)
    dist = list(map(lambda i: list(map(lambda j: j, i)), graph))
    for k in range(v):
        for i in range(v):
            for j in range(v):
                dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
    return dist

Complexity: $O(V^3)$

Download Code

0/1 Knapsack (DP)

Solve the 0/1 Knapsack problem using Dynamic Programming.

Complexity: $O(N \times W)$

Download Code

Greedy Knapsack

Discrete & Continuous Knapsack using Greedy Approximation.

Complexity: $O(N \log N)$

Download Code

Quick Sort

Sorting with Quick Sort, including empirical time analysis for $n > 5000$.

Complexity: $O(N \log N)$ average

Quick Sort Analysis Download Code

Merge Sort

Sorting with Merge Sort, including empirical time analysis for $n > 5000$.

Complexity: $O(N \log N)$

Merge Sort Analysis Download Code

N Queen's Problem

def solve_n_queens(n):
    # Solves N-Queens using Backtracking
    board = [['.'] * n for _ in range(n)]
    # ... (Backtracking logic)

Complexity: $O(N!)$

Download Code

Selection Sort

def selection_sort(arr):
    n = len(arr)
    for i in range(n - 1):
        min_index = i
        for j in range(i + 1, n):
            if arr[j] < arr[min_index]:
                min_index = j
        arr[i], arr[min_index] = arr[min_index], arr[i]

Complexity: $O(n^2)$

Selection Sort Analysis Download Code

Bubble Sort

def bubble_sort(arr):
    n = len(arr)
    for i in range(n - 1):
        for j in range(0, n - i - 1):
            if arr[j] > arr[j + 1]:
                arr[j], arr[j + 1] = arr[j + 1], arr[j]

Complexity: $O(n^2)$

Bubble Sort Analysis Download Code

Insertion Sort

def insertion_sort(arr):
    n=len(arr)
    for i in range(1,n):
        j=i-1
        v=l[i]
        while j>=0 and arr[j]>v:
            arr[j+1]=l[j]
            j=j-1
        arr[j+1]=v

Complexity: $O(n^2)$

Bubble Sort Analysis Download Code

Topological Sort

Using source removal (indegree) method.

Download Script

Dijkstra's Algorithm

Shortest path algorithm for weighted graphs.

Download Script

Prim's & Kruskal's

Minimum Spanning Tree implementations.

Prim's Kruskal's

Newton's Square Root

guess = n / 2
while(guess != (guess + n / guess) / 2):
    guess = (guess + n / guess) / 2
Download Code

Heap Operations

Implementation of heap data structure and operations.

Download Code

Subset Sum

Find subsets that add up to a given sum.

Download Code