0% found this document useful (0 votes)
13 views2 pages

Greedy and Dynamic Programming Algorithms

The document outlines various algorithms along with their types and time complexities, including greedy algorithms like Job Sequencing, Huffman Encoding, and Dijkstra's Algorithm, each with O(n log n) or O(E log V) complexities. It also discusses NP-Hard and dynamic programming approaches for the Travelling Salesman Problem, highlighting the brute force method's O(n!) complexity and the more efficient DP method's O(n^2 * 2^n) complexity. Additionally, it describes the general dynamic programming strategy for solving overlapping subproblems.

Uploaded by

maryamzahra.3366
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)
13 views2 pages

Greedy and Dynamic Programming Algorithms

The document outlines various algorithms along with their types and time complexities, including greedy algorithms like Job Sequencing, Huffman Encoding, and Dijkstra's Algorithm, each with O(n log n) or O(E log V) complexities. It also discusses NP-Hard and dynamic programming approaches for the Travelling Salesman Problem, highlighting the brute force method's O(n!) complexity and the more efficient DP method's O(n^2 * 2^n) complexity. Additionally, it describes the general dynamic programming strategy for solving overlapping subproblems.

Uploaded by

maryamzahra.3366
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

Algorithms and Their Time Complexities

Job Sequencing with Deadlines

Type: Greedy Algorithm

Time Complexity: O(n log n)

Description: Sort jobs by profit, then schedule each job to the latest available slot before its deadline.

Huffman Encoding

Type: Greedy Algorithm

Time Complexity: O(n log n)

Description: Build a min-heap from character frequencies, merge the two smallest until one tree remains.

Optimal Merge Pattern

Type: Greedy Algorithm

Time Complexity: O(n log n)

Description: Merge the two smallest files repeatedly using a min-heap to minimize total merge cost.

Prim's Algorithm

Type: Greedy Algorithm

Time Complexity: O(E log V) with Min-Heap

Description: Start from any node, add the smallest edge connecting to the MST, repeat until all nodes

included.

Kruskal's Algorithm

Type: Greedy Algorithm

Time Complexity: O(E log E)

Description: Sort all edges, add the smallest edge that does not form a cycle using Union-Find (Disjoint Set).

Dijkstra's Algorithm

Type: Greedy Algorithm

Time Complexity: O((V + E) log V) with Min-Heap

Description: Find the shortest path from a source node to all other nodes using a priority queue.

Travelling Salesman Problem (Brute Force)


Type: NP-Hard

Time Complexity: O(n!)

Description: Try all permutations of cities to find the shortest tour. Accurate but very slow.

Travelling Salesman Problem (DP - Held-Karp)

Type: Dynamic Programming

Time Complexity: O(n^2 * 2^n)

Description: Use bit masking and memoization to store visited states and paths. Much faster than brute force.

Dynamic Programming - General

Type: DP Strategy

Time Complexity: Varies

Description: Break problems into overlapping subproblems, solve each once, and reuse results using a table

or memo.

Common questions

Powered by AI

Kruskal's Algorithm has a time complexity of O(E log E) while Prim's Algorithm, when implemented with a Min-Heap, has a time complexity of O(E log V). Kruskal's complexity is primarily due to the requirement of sorting all edges, which takes O(E log E), and performing union-find operations, which take nearly constant time per operation due to path compression. Prim's complexity arises from selecting the smallest edge in each iteration, which is efficiently managed with a Min-Heap. V, the number of vertices, influences Prim’s complexity heavily due to the repeated edge selections using the Min-Heap structure .

Dijkstra’s Algorithm is considered a greedy approach because it incrementally constructs the shortest path tree by continuously choosing the shortest reachable vertex not yet processed, exemplifying the principle of making locally optimal choices at each step. The use of a Min-Heap is crucial in ensuring that the next vertex chosen is always the one with the minimal computed distance from the source. This approach guarantees that once a vertex's shortest path has been added to the tree, there is no other cheaper path to reach that vertex, achieving optimal results based on the greedy strategy .

The brute force solution to the Travelling Salesman Problem (TSP) has a complexity of O(n!), as it involves evaluating all possible permutations of city arrangements to identify the shortest route, which is computationally infeasible for larger n due to factorial growth. In contrast, the dynamic programming Held-Karp method utilizes bit masking and memoization to systematically explore subsets of cities and store results for future reference, effectively reducing the complexity to O(n^2 * 2^n). This design significantly reduces the problem's state space by building optimal solutions through previously computed states, illustrating the power of dynamic programming in handling NP-Hard problems more efficiently .

In the Optimal Merge Pattern algorithm, a min-heap is essential for efficiently selecting and merging the two smallest files at each step. The use of a min-heap allows the algorithm to repeatedly extract the pair of minimum size quickly, which minimizes the total merge cost by always combining the smallest elements first. This approach is guided by the greedy principle of making local optimal choices to achieve a globally optimal solution, leading to a time complexity of O(n log n) due to heap operations for extraction and insertion .

The job sequencing with deadlines problem exemplifies a greedy algorithm by selecting jobs based on maximum profit and scheduling each to the latest available time slot before its deadline. This greedy choice ensures that all potential profits are maximized while respecting the scheduling constraints of deadlines. The algorithm, with a time complexity of O(n log n), aims to maximize the total profit by leveraging sorted job priorities and the flexibility to schedule efficiently within available time frames .

Prim’s Algorithm and Kruskal’s Algorithm differ primarily in their execution: Prim’s constructs the spanning tree by expanding from a starting node and adding the smallest edge that connects the tree to new vertices. It is particularly efficient for dense graphs when implemented with a Min-Heap, having a time complexity of O(E log V). Kruskal’s, however, builds the tree by considering all edges in increasing order of weight and selecting the smallest that doesn’t form a cycle, which makes it well-suited for sparse graphs and gives it a time complexity of O(E log E). The disjoint set data structure used in Kruskal’s is key for detecting cycles, contrasting the direct edge exploration in Prim’s .

The Travelling Salesman Problem (TSP) is classified as NP-Hard due to its requirement of checking all possible solutions to find the shortest tour, which results in exponential time complexity characteristic of NP-Hard problems. The brute force approach attempts every permutation of cities and has a complexity of O(n!), which is computationally infeasible for large values of n. In contrast, the Held-Karp dynamic programming approach uses bit masking and memoization to store visited states, leading to a complexity of O(n^2 * 2^n); this is still exponential but considerably faster than brute force by leveraging overlapping subproblems and efficient state management .

The Travelling Salesman Problem is classified as NP-Hard because there is no known polynomial-time solution that can solve all instances of the problem efficiently. Brute force strategies attempt all possible routes, providing exact answers but with a complexity of O(n!), they are practically implausible for large datasets. Conversely, the dynamic programming approach, such as the Held-Karp algorithm, reduces the complexity to O(n^2 * 2^n) by using memoization and overlapping subproblem solutions, making it more feasible for moderate-sized instances. Although still exponential, this enables tackling larger inputs more practically compared to brute force, highlighting significant advancement in solving NP-Hard problems through algorithmic innovation .

Huffman Encoding employs a greedy algorithm by iteratively merging the two least frequent nodes until a single tree is formed. This is done using a min-heap to efficiently select the nodes with minimum frequency at each step. The greedy choice of combining the smallest trees first minimizes the weighted path length of characters, thus achieving optimal compression. This method is effective for data compression as it assigns shorter codes to more frequent characters, reducing the overall data size significantly while maintaining lossless compression .

Dynamic programming's strategy of breaking problems into overlapping subproblems provides significant advantages by allowing each subproblem to be solved once and storing its result for future reference. This eliminates redundant calculations, vastly improving efficiency over methods like brute force that solve the same subproblem multiple times. By using a table or memoization to store results, dynamic programming can transform exponential time complexities into polynomial time for many problems, leveraging reusability of the computed solutions .

You might also like