0% found this document useful (0 votes)
18 views5 pages

Dsa 05

The document discusses advanced algorithms including Dynamic Programming, Greedy Algorithms, Backtracking, and Divide and Conquer, highlighting their characteristics and providing examples. Each technique is explained with its respective time and space complexities, showcasing their applications in solving complex computational problems. Mastery of these algorithms is essential for competitive programming and practical problem-solving.

Uploaded by

22bph016
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views5 pages

Dsa 05

The document discusses advanced algorithms including Dynamic Programming, Greedy Algorithms, Backtracking, and Divide and Conquer, highlighting their characteristics and providing examples. Each technique is explained with its respective time and space complexities, showcasing their applications in solving complex computational problems. Mastery of these algorithms is essential for competitive programming and practical problem-solving.

Uploaded by

22bph016
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Algorithms Part 2 - Advanced Algorithms

Introduction to Advanced Algorithms


Advanced algorithms help solve complex computational problems efficiently. The most common
advanced techniques include Dynamic Programming, Greedy Algorithms, Backtracking, and
Divide and Conquer.

Dynamic Programming (DP)


Dynamic Programming solves problems by breaking them down into overlapping subproblems,
storing solutions to avoid recalculating.

Characteristics:

●​ Optimal substructure​

●​ Overlapping subproblems​

Example: Fibonacci Sequence

def fibonacci(n):

fib = [0, 1]

for i in range(2, n+1):

fib.append(fib[i-1] + fib[i-2])

return fib[n]

●​ Time Complexity: O(n)​

●​ Space Complexity: O(n)​

Example: 0/1 Knapsack Problem


def knapsack(weights, values, W):

n = len(weights)

dp = [[0 for _ in range(W+1)] for _ in range(n+1)]

for i in range(1, n+1):

for w in range(1, W+1):

if weights[i-1] <= w:

dp[i][w] = max(values[i-1] + dp[i-1][w-weights[i-1]], dp[i-1][w])

else:

dp[i][w] = dp[i-1][w]

return dp[n][W]

Greedy Algorithms
Greedy algorithms build a solution step by step by choosing the locally optimal choice.

Example: Activity Selection Problem

def activity_selection(activities):

activities.sort(key=lambda x: x[1])

selected = [activities[0]]

for i in range(1, len(activities)):

if activities[i][0] >= selected[-1][1]:

selected.append(activities[i])

return selected

●​ Time Complexity: O(n log n)​


Example: Huffman Coding

●​ Efficient lossless compression algorithm.​

●​ Uses a priority queue to build an optimal binary tree.​

Backtracking
Backtracking involves exploring all possible solutions and retreating when a solution fails.

Example: N-Queens Problem

def solve_n_queens(n):

board = [-1] * n

def is_safe(row, col):

for i in range(row):

if board[i] == col or abs(board[i] - col) == row - i:

return False

return True

def place_queen(row):

if row == n:

return [board[:]]

solutions = []

for col in range(n):

if is_safe(row, col):

board[row] = col

solutions += place_queen(row + 1)

return solutions
return place_queen(0)

Divide and Conquer


Divide the problem into subproblems, solve them recursively, and combine results.

Example: Merge Sort (Divide and Conquer)

def merge_sort(arr):

if len(arr) > 1:

mid = len(arr) // 2

L = arr[:mid]

R = arr[mid:]

merge_sort(L)

merge_sort(R)

i=j=k=0

while i < len(L) and j < len(R):

if L[i] < R[j]:

arr[k] = L[i]

i += 1

else:

arr[k] = R[j]

j += 1

k += 1

●​ Time Complexity: O(n log n)​


Comparison of Techniques

Technique Use Case Complexity Spac


e

Dynamic Programming Optimization problems Varies Varies

Greedy Immediate decision-making O(n log n) O(1)

Backtracking Constraint satisfaction Varies O(n)

Divide and Conquer Recursive problem solving O(n log n) O(n)

Conclusion
Advanced algorithms offer efficient solutions for complex problems. Mastering these techniques
is crucial for competitive programming and real-world problem-solving.

You might also like