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

Detailed_Algorithms_Examples

The document discusses various algorithms and techniques in dynamic programming, including Matrix Chain Multiplication, Floyd-Warshall for shortest paths, and Optimal Binary Search Trees. It also covers graph traversal methods like DFS and BFS, as well as backtracking and branch and bound strategies, exemplified by the N-Queens and Knapsack problems. Additionally, it touches on sorting lower bounds and NP-Complete theory, providing definitions and examples for clarity.

Uploaded by

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

Detailed_Algorithms_Examples

The document discusses various algorithms and techniques in dynamic programming, including Matrix Chain Multiplication, Floyd-Warshall for shortest paths, and Optimal Binary Search Trees. It also covers graph traversal methods like DFS and BFS, as well as backtracking and branch and bound strategies, exemplified by the N-Queens and Knapsack problems. Additionally, it touches on sorting lower bounds and NP-Complete theory, providing definitions and examples for clarity.

Uploaded by

Ankita Pimpalkar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Detailed Algorithms and Examples

Dynamic Programming: Method and Matrix Chain Multiplication

Dynamic Programming (DP) is a technique to solve problems by breaking them into smaller

overlapping subproblems.

Example: Matrix Chain Multiplication

Problem: Multiply matrices A1(10x20), A2(20x30), and A3(30x40) with minimum scalar

multiplications.

Steps:

1. Define dp[i][j] as the minimum cost to multiply matrices from i to j.

2. Use the recurrence relation:

dp[i][j] = min(dp[i][k] + dp[k+1][j] + p[i-1]*p[k]*p[j]) for all k from i to j-1.

Result: Minimum scalar multiplications = 18000.

Dynamic Programming: Shortest Paths

Floyd-Warshall Algorithm computes the shortest paths between all pairs of vertices in a weighted

graph.

Example:

Graph: A -> B (3), A -> C (1), B -> C (7), B -> D (5), C -> D (2).

Steps:

1. Start with a distance matrix initialized with edge weights.

2. Update distances using the recurrence relation:


Detailed Algorithms and Examples

d[i][j] = min(d[i][j], d[i][k] + d[k][j]) for every intermediate vertex k.

Time Complexity: O(V^3).

Result: Shortest paths between all pairs are computed.

Optimal Binary Search Trees

Optimal Binary Search Trees (BSTs) minimize the cost of searching keys given their frequencies.

Example:

Keys: {10, 20, 30}, Frequencies: {3, 2, 4}.

Steps:

1. Define cost[i][j] as the minimum cost of a BST containing keys i to j.

2. Use the recurrence:

cost[i][j] = min(cost[i][r-1] + cost[r+1][j] + sum(freq[i:j+1])) for all r in [i, j].

Result: Optimal BST cost = 25.

Graph Traversal Techniques: DFS and BFS

Depth-First Search (DFS):

- Recursively explores each vertex as deeply as possible.

- Applications: Detecting cycles, finding connected components, topological sorting.

Breadth-First Search (BFS):


Detailed Algorithms and Examples

- Explores all neighbors of a vertex before moving deeper.

- Applications: Shortest path in unweighted graphs, testing bipartiteness.

Example:

Graph: A -> B, A -> C, B -> D.

DFS Order: A -> B -> D -> C.

BFS Order: A -> B -> C -> D.

Backtracking

Backtracking is used to find solutions by trying all possibilities and discarding invalid solutions.

Example: N-Queens Problem

Place 4 queens on a 4x4 chessboard so no two queens threaten each other.

Steps:

1. Place a queen in each column recursively.

2. Backtrack if a placement leads to a conflict.

Result: Solutions include [1, 3, 0, 2] and [2, 0, 3, 1].

Branch and Bound

Branch and Bound optimizes by exploring solution space using bounds to prune suboptimal

solutions.
Detailed Algorithms and Examples

Example: Knapsack Problem

Items: {(value: 40, weight: 2), (value: 100, weight: 5), (value: 50, weight: 3)}, Capacity: 5.

Steps:

1. Use upper and lower bounds to prioritize promising branches.

2. Use a priority queue to explore feasible subsets.

Result: Maximum value = 90.

Lower-Bound Theory

Sorting Lower Bound:

- Comparison-based sorting has a lower bound of O(n log n).

- Derivation: Sorting modeled as a decision tree where each leaf represents a permutation.

Result: No comparison-based algorithm can sort faster than O(n log n).

NP-Complete Theory

Definitions:

- P: Problems solvable in polynomial time.

- NP: Problems verifiable in polynomial time.

- NP-Complete: Problems in NP that are as hard as any other NP problem.

Example Reduction:

SAT -> 3-SAT:


Detailed Algorithms and Examples

Convert a Boolean formula into an equivalent formula in Conjunctive Normal Form (CNF) with three

literals per clause.

You might also like