Algorithms Final Exam

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/18

flashcard set

Earn XP

Description and Tags

COP 4531 - Algorithm Design & Analysis

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

19 Terms

1
New cards

Greedy Algorithm: Coin Changing

There are scenarios in which it does not produce an optimal result!

Algorithm:

  1. Sort the array of coins in decreasing order.

  2. Initialize ans vector as empty.

  3. Find the largest denomination that is smaller than remaining amount and while it is smaller than the remaining amount:

    • Add found denomination to ans. Subtract value of found denomination from amount.

  4. If amount becomes 0, then print ans.

Time Complexity: O(n)

<p>There are scenarios in which it does not produce an optimal result!<br></p><p><strong>Algorithm:</strong></p><ol><li><p>Sort the array of coins in decreasing order.</p></li><li><p>Initialize ans vector as empty.</p></li><li><p>Find the <strong>largest denomination </strong>that is <strong>smaller </strong>than remaining amount and while it is smaller than the remaining amount:</p><ul><li><p>Add found denomination to ans. Subtract value of found denomination from amount.</p></li></ul></li><li><p>If amount becomes 0, then print ans.</p><p></p></li></ol><p><strong>Time Complexity:</strong> O(n)</p>
2
New cards

Greedy Algorithm: Interval Scheduling

Goal: Find maximum subset of mutually compatible jobs.

We want as many jobs as possible → earliest finish time!

Algorithm:

  1. Sort the intervals by their end times in ascending order.

  2. Initialize an empty schedule.

  3. Iterate through the sorted intervals:

    • Add the current interval to the schedule if it does not overlap with the last selected interval.

  4. Return the selected intervals as the result.

Time Complexity: O(nlogn)

<p>Goal: Find maximum subset of mutually compatible jobs.</p><p>We want as many jobs as possible → <strong>earliest finish time</strong>!</p><p></p><p><strong>Algorithm:</strong></p><ol><li><p>Sort the intervals by their end times in ascending order.</p></li><li><p>Initialize an empty schedule.</p></li><li><p>Iterate through the sorted intervals:</p><ul><li><p>Add the current interval to the schedule if it does not overlap with the last selected interval.</p></li></ul></li><li><p>Return the selected intervals as the result.</p><p></p></li></ol><p><strong>Time Complexity:</strong> O(nlogn)</p>
3
New cards

Greedy Algorithm: 01 Knapsack

Goal: Get as valuable as a load as possible without exceeding weight limit.

Taking the densest items DOES NOT WORK!!! → It does not consider the possibility of excluding certain items to achieve a better overall solution. → USE DP INSTEAD

Algorithm:

  1. Calculate density (value/weight) per item.

  2. Sort items in descending order by their density.

  3. Start with empty knapsack.

  4. Traverse the sorted items and add each item to the knapsack if it fits within the remaining capacity.

  5. If an item’s weight exceeds the capacity, skip it.

Time Complexity: O(nlogn)

4
New cards

Greedy Algorithm: Fractional Knapsack

Goal: Get as valuable as a load as possible without exceeding weight limit.

Algorithm:

  1. Calculate density (value/weight) per item.

  2. Sort items in descending order by their density.

  3. Start with empty knapsack.

  4. Iterate through the sorted items:

    • If the entire item can fit into the knapsack without exceeding capacity, take it entirely.

    • Else, take the fraction of the item that fits into the remaining capacity of the knapsack.

  5. Stop when the knapsack is full.

Time Complexity: O(nlogn)

5
New cards

Greedy Algorithm: Huffman Encoding

Given - a:12, b:7 z:2, x:5, s:9, m:10, i:18

Ans - a: 00, b: 011, z: 0100, x: 0101, s: 110, m:111, i:10

**All Huffman encoding trees are full binary trees!!!

Algorithm:

  1. Build a min heap (priority queue) where each element is a node representing a character and its frequency.

    • Nodes are ordered by smallest frequency at the top.

  2. While there is more than one node in the heap:

    • Extract two minimum frequency nodes from min heap.

    • Create a new internal node with a frequency equal to the sum of the two nodes frequencies.

    • Add the new node back into the heap.

  3. Repeat step 2 until the heap contains only one node. The remaining node is the root node.

  4. Traverse the Huffman tree from the root to each leaf node, assigning binary codes based on the path: left branch = 0, right branch = 1

Time Complexity: O(nlogn)

<p>Given - a:12, b:7 z:2, x:5, s:9, m:10, i:18</p><p>Ans - a: 00, b: 011, z: 0100, x: 0101, s: 110, m:111, i:10</p><p>**All Huffman encoding trees are <strong>full binary trees</strong>!!!</p><p></p><p><strong>Algorithm:</strong></p><ol><li><p>Build a min heap (priority queue) where each element is a node representing a character and its frequency.</p><ul><li><p>Nodes are ordered by smallest frequency at the top.</p></li></ul></li><li><p>While there is more than one node in the heap:</p><ul><li><p>Extract two minimum frequency nodes from min heap.</p></li><li><p>Create a new internal node with a frequency equal to the sum of the two nodes frequencies.</p></li><li><p>Add the new node back into the heap.</p></li></ul></li><li><p>Repeat step 2 until the heap contains only one node. The remaining node is the root node.</p></li><li><p>Traverse the Huffman tree from the root to each leaf node, assigning binary codes based on the path: left branch = 0, right branch = 1</p></li></ol><p></p><p><strong>Time Complexity: </strong>O(nlogn)</p>
6
New cards

Greedy Algorithm: Vertex Coloring

Goal: A coloring of the vertices using as few colors as possible.

Algorithm:

  1. Start with an empty coloring.

  2. Color the first vertex with the first color.

  3. For each of the remaining V-1 vertices:

    • Check the colors of all its adjacent vertices (neighbors).

    • Assign the lowest numbered color that is not already used by any of its neighbors.

    • If all previously used colors appear on vertices adjacent to v, assign a new color to it.

  4. Continue until all vertices are colored.

Time Complexity: O(V2 + E) → worst case

<p>Goal: A coloring of the vertices using as few colors as possible.</p><p></p><p><strong>Algorithm:</strong></p><ol><li><p>Start with an empty coloring.</p></li><li><p>Color the first vertex with the first color.</p></li><li><p>For each of the remaining V-1 vertices:</p><ul><li><p>Check the colors of all its adjacent vertices (neighbors).</p></li><li><p>Assign the lowest numbered color that is not already used by any of its neighbors.</p></li><li><p> If all previously used colors appear on vertices adjacent to v, assign a new color to it.</p></li></ul></li><li><p>Continue until all vertices are colored.</p></li></ol><p></p><p><strong>Time Complexity:</strong> O(V<sup>2</sup> + E) → worst case</p>
7
New cards

Dynamic Programming: Fibonacci Numbers

Instead of using recursion to get O(2n) time, we can use the two following methods.

Memoization → O(n):

fib(n){
	if (n in memo):
		return memo[n]
	if (n<= 2):
		f = 1
	else:
		f = fib(n-1)+fib(n-2)
		memo[n] = f
		return f
}

Bottom-Up → O(n):

fib = []
fib(n){
	for (k = 1; k<=n; k++):
		if (k<= 2):
			f = 1
		else:
			f = fib[k-1]+fib[k-2]
		fib[k] = f
		return fib[n]
}

Recurrence Formula: T(n) = T(n-1) + T(n-2)

8
New cards

Dynamic Programming: All Pairs Shortest Path

Ak[i,j] = Cost of the shortest path from i to j using k intermediate vertices (1, 2, 3, ...k)

Recurrence Formula: Ak[i,j] = min{ Ak-1[i,j], Ak-1[i,k] + Ak-1[k,j] }

for (k = 1; k <= n; k++)
{
	for(i = 1; i<=n; i++)
	{
		for(j = 1; j<=n; j++)
		{
			A[i,j] = min(A[i,j] , A[i,k] + A[1,k])
		}
	}
}

Time Complexity: O(n3)

<p><strong> </strong>A<sup>k</sup>[i,j] = <span>Cost of the shortest path from i to j using k intermediate vertices (1, 2, 3, ...k)</span></p><p><strong>Recurrence Formula: </strong>A<sup>k</sup>[i,j] = min{ A<sup>k-1</sup>[i,j], A<sup>k-1</sup>[i,k] + A<sup>k-1</sup>[k,j] }</p><pre><code class="language-plaintext">for (k = 1; k &lt;= n; k++)
{
	for(i = 1; i&lt;=n; i++)
	{
		for(j = 1; j&lt;=n; j++)
		{
			A[i,j] = min(A[i,j] , A[i,k] + A[1,k])
		}
	}
}</code></pre><p><strong>Time Complexity: </strong>O(n<sup>3</sup>)</p>
9
New cards

Dynamic Programming: 01 Knapsack

Recurrence Formula: T[i][j] = max{ T[i-1][j], T[i-1][j-wi]+vi }

Time Complexity: O(nw) where n is the number of items and w is the capacity.

<p><strong>Recurrence Formula: </strong>T[i][j] = max{ T[i-1][j], T[i-1][j-w<sub>i</sub>]+v<sub>i </sub>}</p><p><strong>Time Complexity:</strong> O(nw) where n is the number of items and w is the capacity.</p>
10
New cards

Dynamic Programming: Longest Common Subsequence

Recurrence Formula:

  • LCS[i][j] = LCS[i-1][j-1] + 1 if X[i] = Y[j]

  • LCS[i][j] = max{ LCS[i][j-1], LCS[i-1][j] } if X[i} Y[j]

Time Complexity: O(mn) where m and n are lengths of strings s1 and s2.

<p><strong>Recurrence Formula:</strong></p><ul><li><p>LCS[i][j] = LCS[i-1][j-1] + 1 if X[i] = Y[j]</p></li><li><p>LCS[i][j] = max{ LCS[i][j-1], LCS[i-1][j] } if X[i} <span style="font-size: medium">≠ </span>Y[j]</p></li></ul><p><strong>Time Complexity: </strong>O(mn) where m and n are lengths of strings s1 and s2.</p>
11
New cards

Dynamic Programming: Traveling Salesman

Input: list of cities and the distances between each pair of cities

Output: the shortest possible route that visits each city exactly once and returns to the origin city

Recurrence Formula: C(i, S) = min k ϵ S { wik + C(k, S-{k}) } where S is set of vertices

Time Complexity: O(2n*n2)

<p><strong>Input: </strong>list of cities and the distances between each pair of cities</p><p><strong>Output: </strong>the shortest possible route that visits each city exactly once and returns to the origin city</p><p><strong>Recurrence Formula:</strong> C(i, S) = min k ϵ S { w<sub>ik</sub> + C(k, S-{k}) } where S is set of vertices</p><p><strong>Time Complexity:</strong> O(2<sup>n</sup>*n<sup>2</sup>)</p>
12
New cards

Backtracking: N Queen

Time Complexity: O(N!)

<p><strong>Time Complexity: </strong>O(N!)</p>
13
New cards

Backtracking: Graph Coloring

Time Complexity: O(mV)

<p><strong>Time Complexity: </strong>O(m<sup>V</sup>)</p>
14
New cards

Backtracking: Sum of Subsets

Given m = 30 so if the current weight is not equal to 30, kill the node.

Time Complexity: O(2n)

<p>Given m = 30 so if the current weight is not equal to 30, kill the node.</p><p><strong>Time Complexity: </strong>O(2<sup>n</sup>)</p>
15
New cards

Branch and Bound: 01 Knapsack

Time Complexity: O(2n)

<p><strong>Time Complexity: </strong>O(2<sup>n</sup>)</p>
16
New cards

Class P

  • Set of deterministic algorithm which take polynomial time

    • EX: Binary Search, Huffman Coding, Insertion Sort, Minimum Spanning Tree and etc.

  • Do problems such as traveling salesperson and 0/1 Knapsack (no polynomial-time algorithm has been found), etc., belong to P? → No one knows

  • To know a decision problem is not in P, it must be proven it is not possible to develop a polynomial-time algorithm to solve it

<ul><li><p>Set of <strong>deterministic </strong>algorithm which take polynomial time</p><ul><li><p>EX: Binary Search, Huffman Coding, Insertion Sort, Minimum Spanning Tree and etc.</p></li></ul></li><li><p>Do problems such as traveling salesperson and 0/1 Knapsack (no polynomial-time algorithm has been found), etc., belong to P? → No one knows</p></li><li><p>To know a decision problem is not in P, it must be proven it is not possible to develop a polynomial-time algorithm to solve it</p></li></ul><p></p>
17
New cards

Class NP

  • The set of decision problems solvable in nondeterministic polynomial time. The output of these problems is a YES or NO answer.

  • Nondeterministic → a solution can be guessed out of polynomially many options in O(1) time. If any guess is a YES instance, then the nondeterministic algorithm will make that guess.

  • Set of non-deterministic algorithm which take polynomial time

    • There exists polynomial time verification algorithm for them

    • EX: 0/1 Knapsack problem, Hamiltonian path problem, Travelling salesman problem and etc

<ul><li><p><span>The set of decision problems solvable in <strong>nondeterministic </strong>polynomial time. The output of these problems is a YES or NO answer.</span></p></li><li><p><span><strong>Nondeterministic →</strong> a solution can be guessed out of polynomially many options in O(1) time. If any guess is a YES instance, then the nondeterministic algorithm will make that guess.</span></p></li><li><p><span>Set of non-deterministic algorithm which take polynomial time</span></p><ul><li><p><span>There exists polynomial time verification algorithm for them</span></p></li><li><p><span>EX: 0/1 Knapsack problem, Hamiltonian path problem, Travelling salesman problem and etc</span></p></li></ul></li></ul><p></p>
18
New cards

NP-Complete

  • A subset of NP (nondeterministic polynomial time) problems.

  • If you can find a polynomial-time algorithm to solve any NP-complete problem, you can also solve all problems in NP in polynomial time. In other words, NP-complete problems are the "hardest" problems in NP in terms of their potential to be solved efficiently.

  • They are both in NP and have the property that any problem in NP can be reduced to them in polynomial time.

  • Ex: Determining whether a graph has a Hamiltonian cycle, Determining whether a Boolean formula is satisfiable(SAT problem)

<ul><li><p>A subset of NP (nondeterministic polynomial time) problems. </p></li><li><p>If you can find a polynomial-time algorithm to solve any NP-complete problem, you can also solve all problems in NP in polynomial time. In other words, NP-complete problems are the "hardest" problems in NP in terms of their potential to be solved efficiently. </p></li><li><p>They are <strong>both in NP</strong> and have the property that any problem in NP can be reduced to them in polynomial time.</p></li><li><p>Ex: Determining whether a graph has a Hamiltonian cycle, Determining whether a Boolean formula is satisfiable(SAT problem)</p></li></ul><p></p>
19
New cards

NP-Hard

  • NP-hard problems are a broader category of problems that may or may not be in NP.

  • They are at least as hard as the hardest problems in NP.

  • Unlike NP-complete problems, NP-hard problems do not necessarily belong to NP. It's possible that an NP-hard problem is not in NP, which means that there may not be an efficient algorithm to verify a proposed solution in polynomial time.

  • However, if you can solve an NP-complete problem in polynomial time, you can use that solution to solve an NP-hard problem in polynomial time as well.

  • A problem is NP-hard if all problems in NP are polynomial time reducible to it. (EX: Traveling Salesman)

<ul><li><p>NP-hard problems are a broader category of problems that may or may not be in NP.</p></li><li><p>They are at least as hard as the hardest problems in NP.</p></li><li><p>Unlike NP-complete problems, NP-hard problems <strong>do not necessarily belong to NP</strong>. It's possible that an NP-hard problem is not in NP, which means that there may not be an efficient algorithm to verify a proposed solution in polynomial time.</p></li><li><p>However, if you can solve an NP-complete problem in polynomial time, you can use that solution to solve an NP-hard problem in polynomial time as well.</p></li><li><p>A problem is NP-hard if all problems in NP are <strong>polynomial time reducible </strong>to it. (EX: Traveling Salesman)</p></li></ul><p></p>