CSC 349 Algorithms & Definitions

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Prove that in an undirected graph, P u∈V that sum(degree(V)) = 2|E|

(=>) Suppose that V1 is the vertices of even degree and V2 be the vertices of odd degree. Since V1 has even degrees for each vertex, the sum of them must be even. For V2, there must be an even number of vertices with odd degress for their sum to be even, thus = 2E (<=) Suppose there are an even amount of edges in a graph. Then the sum of the degrees of vertices must also be even.

Activity Selection

--ActivitySelection(s1, f1,s2, f2, . . .sn, fn)-- Input: A set of n activities each with a start time si and finish time fi . Output: S, a maximal solution set of non-conflicting activities. 1: Let S = ∅ 2: Let list A be the list of activities sorted in increasing order according to their finish time. 3: while A 6= ∅. do 4: Find the earliest finish time for an activity in A, activity x. 5: Add x to S. 6: Delete all activities, y such that sy < fx from A. return S.

Design an analyze an algorithm to determine whether an undirected graph is bipartite or not

--BipartiteDivide-- Input: A graph G = (V,E) Output: Whether or not the graph is bipartite 1: for all v in V do: discovered(v) = false 2: for all v in V do: 3: if discovered(v) = false: 4: bipartite = explore(G, v) 5: if bipartite = false: return bipartite 6: return True --explore-- Input: A graph G and a vertex v. Output: Whether or not the graph is bipartite 1: discovered(v) = true 2: color(v) 3: for all neighbors of v, u do: 4:if discovered(u) = false: return explore(G, u) 5: else if discovered(u) = true and color(v) == color(u): return False 6: return True --color-- colors graph either red or blue, flips after each use

Design and analyze a divide and conquer algorithm to find the closest pair of points in the Euclidean plane

--ClosestPoint-- Input: A set A of n points in the plane {a1, a2, ... , an} Output: The two closest points in the set 1: if n <= 1: return point 2: else: 3: left_points = ClosestPoint(left side of A) 4: right_points = ClosestPoint(right side of A) 5: mid_points = {closest(left side, floor(n/2)), closest(right side, floor(n/2))} 6: if distance(left_points) < distance(right_points) and distance(left_points) < distance(mid_points): return left_points 7: else if distance(right_points) < distance(left_points) and distance(right_points) < distance(mid_points): return right_points 8: else: return mid_points --closest-- Finds the closest point of a set of points by the distance to a set midline in O(n). --distance-- Returns the euclidian distance of two points, none if there is just one point

Coin Row Dynamic Program

--Coin Row-- Input: A list of n coins, c1, c2, ..., cn whose values are some positive integers (not necessarily distinct), v1, v2, v3, ... vn Goal: Find a subset of coins with maximum value subject to the constraint that no two consecutive coins in the input list can be selected Precise Definition: Let CR[i] be the value of the maximum value subset of coins (with above constraint) drawing from the first i coins Base Cases: CR[0] = 0 CR[1] = v1 Solution: CR[n] Formula: CR[i] = max{CR[i-1], vi + CR[i - 2]} O(n) since table size is 1xn and it takes constant time to fill each cell

Design and analyze an algorithm which takes as an input an undirected graph G and an edge e = (u, v) and determines whether G has a cycle containing e.

--DFS-- Input: A graph G = (V, E) and an edge e = (u, v) Output: Whether or not a cycle exists for that edge 1: for all t in V do: 2:discovered(t) = false 3: if discovered(v) = false then: return explore(G, v, u) --explore-- Input: A graph G and vertices v and u Output: Whether there exists a cycle or not 1: discovered(v) = true. 2: for all neighbors of v, w do: 3: if discovered(w) = false then: 4: return explore(G, w) 5: if discovered(w) = true: 6: for all neighbors of w, z do: 7: if z = u or z = w: return True 8: return False

Independent Set (Trees)

--IndependentSet-- Input: A tree T = (V, E) Goal: Find a largest independent set (an independent set with the most vertices) Precise Definition: Let IST[u] be the size of the largest independent set for the subtree rooted at u Base Cases: IST[leaves] = 1 Solution: IST[root] Formula: IST[u] = max { 1 + sum( IST[w]) where w a grandchild of u, sum(IST[w]) where w a child of u} O(n^2)

Design and analyze a divide and conquer algorithm for an index search whether there exists an index i such that ai = i in a sorted list

--IndexSearch-- Input: A sorted list of distinct integers A {a1, a2, a3, ..., an}, and integers min and max. (min = 0 and max = n - 1) Output: A truth value depending on whether there exists an index i such that ai = i 1: i = floor((max + min) / 2) 2: if i = max and i = min and i != ai: return False 3: else: 4: if i = ai: return True 5: else if i < ai: return IndexSearch(A, min, i - 1) 6: else: return IndexSearch(A, i + 1, max) Lemma: If there is no such i then IndexSearch returns false. Proof by contradiction: Suppose there is no such i and that IndexSearch returns something other than -1. Only way to return something that is not -1 is if there exists such an i. Thus, our contradiction. Lemma: If there exists such an i and max - min = n, then IndexSearch returns true. Proof by induction: Base Case: Suppose that there exists such an i and max - min = 0. Then max = min = i = floor((max + min) / 2). Line 4 of IndexSearch returns True Inductive Hypothesis: If there exists an i and max - min <= k, then IndexSearch returns True Inductive Step: Suppose that there exists an i and max - min = k + 1 1. i < A[midpoint] 2. i > A[midpoint] 3. i = A[midpoint] Case 1: There exists such an i in A(min, midpoint - 1) since the list is sorted and (midpoint - 1) - min <= k, so the inductive hypothesis is met and Line 5 of IndexSearch returns True Case 2: Symmetrical to Case 1 Case 3: On Line 6, IndexSearch returns True if i = A[midpoint]

Design and analyze a divide and conquer algorithm that counts the number of occurrences of x in a sorted list

--IntegerCount-- Input: An integer x, and a sorted list of integers A {a1, a2, ... an} Output: The number of occurrences of x in the list 1: midpoint = floor((max + min) / 2) 2: if x = A[min] and x = A[max]: return (max - min) + 1 3: else: 4: if x < A[midpoint]: return IntegerCount(A[min, midpoint - 1]) 4: else if x > A[midpoint]: return IntegerCount(A[midpoint + 1, max]) 5: else if x = midpoint and x != midpoint - 1: return IntegerCount(A[midpoint, max] 6: else: return IntegerCount(A[min, midpoint])

Longest Increasing Subsequence Dynamic Program

--LIS-- Input: A sequence of numbers a1, a2, a3, ... an Goal: Find an increasing subsequence of greatest length Precise Definition: Let LIS[i] be the length of the longest increasing subsequence of the sequence a1, a2, ... ai, including ai Base Cases: LIS[1] = 1 Solution: The maximum value in the table Formula: LIS[i] = max{LIS[j]} + 1 where aj < ai if no such aj exists LIS[i] = 1 O(n^2)

Design and analyze a divide and conquer algorithm to find the largest sum of a contiguous sub list of numbers

--LargestSum-- Input: A list A of unsorted numbers with n elements {a1, a2, ... an} Output: A maximum sum of a sublist 1: if n <= 1: A[0] 2: else return max(LargestSum(A[0, ... , floor(n/2)]), LargestSum(A[floor(n/2), ..., n - 1]), MaxSum(A)) --max-- Returns the max value of the integers provided --sum-- Returns the sum of a list of integers --MaxSum-- Input: A list of n integers A Output: A maximum sum 1: left = sum(A[0, ... , floor(n/2)]) 2: right = sum(A[floor(n/2), ... n - 1]) 3: return left + right

Levenshtein Distance Dynamic Program

--LevenshteinDistance-- Precise definition: Let L[i, j] be the Levenshtein distance between a[1, 2, ... , i] and b [1, 2, ..., j] Base Cases: L[i,0] = i L[0, j] = j Solution: L[|a|, |b|] Formula: L[i,j] = min {L[i, j-1] + 1, L[i-1, j] + 1, L[i-1, j-1] + Xi,j Xi,j = 0 if a[i] = b[j] and Xi,j = 1 otherwise. O(|a||b|)

Design a divide and conquer algorithm to determine if there is a majority element, and if so, to find that element in an unsorted list. O(nlogn)

--MajoritySearch-- Input: A list of n elements A {a1, a2, ... , an} Output: A majority element if one exists 1: if n <= 1: return A[0] 2: else: 3: left_maj = MajoritySearch(A[0, ... , floor(n/2)]) 4: right_maj = MajoritySearch(A[floor(n/2) + 1, ... , n - 1]) 5: if left_maj = right_maj: return left_maj 6: else if left_maj = none: 7: count right_maj in A[0, ... , floor(n/2)]) return right_maj if count > half the elements 8: else if right_maj = none: 9: count left_maj in A[floor(n/2) + 1, ..., n-1]) return left_maj """"" 10: else: count left_maj in A count right_maj in A 11: if count_left > count_right: return left_maj 12: else if count_left == count_right: return none 12: else: return right_maj

Design and analyze a divide and conquer algorithm that finds the heavy marble out of 3^n marbles using the balancing scale at most n times

--MarbleSearch-- Input: A set of marbles M that weigh identically except for one heavier marble and a balancing scale function BalancingScale() that compares weights Output: The heavy marble 1. if set = one marble: return marble 2: Let the set of marbles be divided equally into 3 subsets named first, second and third. 3: if BalancingScale(first) > BalancingScale(second) and BalancingScale(first) > BalancingScale(third): MarbleSearch(first) 4: else if BalancingScale(second) > BalancingScale(first) and BalancingScale(second) > BalancingScale(third): MarbleSearch(second) 5: else: MarbleSearch(third)

Design a divide and conquer algorithm that finds the optimal (buy, sell) pair where buy <= sell pair that would maximize our profit in a list of n numbers representing stock prices on a single day

--MaximizeProfit-- Input: A list of n numbers representing stock prices on a single day A {a1, a2, ... an}, Output: A (buy, sell) pair that maximizes profit made that day 1: if n <= 1: return A[0] 2: else: 3: left_profit = MaximizeProfit(A[0, ... , floor(n/2)]) 4: right_profit = MaximizeProfit(A[floor(n/2) + 1, ..., n-1]) 5: mid_profit = {min(A[0, ... , floor(n/2)]), max(A[floor(n/2) + 1, ..., n-1])} 6: if profit(left_profit) > profit(right_profit) and if profit(left_profit) > profit(mid_profit): return left_profit 7: else if profit(right_profit) > profit(left_profit) and profit(right_profit) > profit(mid_profit): return right_profit 8: else: return mid_profit --max-- Finds the max of a list of n numbers in O(n) time --min-- Finds the min of a list of n numbers in O(n) time --profit-- Returns the profit of the buy - sell pair, none if there is only one value

Design and analyze an algorithm to find the maximum spanning tree, that is, a spanning tree with largest total weight.

--MaximumTree-- Input: A weighted, connected, undirected graph G = (V, E) Output: A minimum weight spanning tree T of G 1: Let T be a graph on the same vertices as G but with no edges 2: Let A be a list containing E sorted in decreasing order according to weights 3: for each edge, e, in A do 4: if adding e to T doesn't create a cycle: 5: Add e to T

Design and analyze an algorithm that computes the minimum number of quarters necessary to complete the curriculum

--MinimumCurriculum-- Input: A directed graph, G = (V, E), and a vertex A. Output: The minimum number of quarters to complete the curriculum 1: T = result of BFS on (G, A) 2: return dist(|V|) + 1 --BFS(G, A)-- Input: A graph G = (V, E) and a vertex A. Output: For all vertices, X, dist(X) is set to be the distance from A to X 1: for all X in V do: 2: dist(X) = infinity 3: dist(A) = 0 4: Q = [A] (a queue containing A) 5: while Q is not empty do: 6: X = dequeue(q) 7: for all outgoing edges (X, Y) do 8: if dist(Y) = infinity then 9: enqueue(Q, Y) 10: dist(Y) = dist(X) + 1

Describe how to determine if a back edge in an undirected graph creates an odd length cycle using only pre and post order numbers for each vertex

--OddCycle-- Input: A graph, G, with vertices V and edges E, and a backedge b Output: Whether or not the backedge creates an odd length cycle 1: T = result of DFS(g) 2: v1, v2 are the vertices associated with b, where pre[v2] > pre[v1] 3: if (pre[v2] - pre[v1] + 1) % 2 = 0 then: return True else: return False

Design and analyze a divide and conquer algorithm to remove all duplicates from the list in time O(nlogn)

--RemoveDuplicates-- Input: A list of n elements A {a1, a2, ... , an} Output: A sorted list in increasing order without duplicates 1: if n <= 1: return A 2: else: return Merge(RemoveDuplicates(A[0, ... floor(n/2)], RD(A[floor(n/2 + 1, ..., n)])) --Merge-- Input: Two lists x and y of k and l elements, respectively Output: A list that contains the elements of both lists with no duplicate elements 1: if x = none return y 2: if y = none return x 3: if x[0] = y[0]: return Merge(x[1, ... , k - 1], y[1, ... , l - 1]) 4: else if x[0] < y [0]: return x[0] * Merge(x[1, ... , k - 1], y[0, ..., l - 1]) 5: else: return y[0] * Merge(x[0, ... , k - 1], y[1, ..., l - 1])

Design and analyze a divide and conquer algorithm for computing skylines

--Skyline-- Input: A set of n buildings B, where a building Bi is a triplet with two x coordinates and a height (Li, Hi, Ri) Output: A skyline of a set of n buildings, which is a list of x coordinates and the heights connecting them in order from left to right 1: if n <= 1: return B[0] 2: else: Merge(Skyline(B[0, ..., floor(n/2)]), Skyline(B[floor(n/2), ..., n - 1])) --Merge-- Input: Two skylines Output: Merged Skyline 1: if A = none: return B 2: if B = none: return A 3: current_h = 0 4: skyline = {} 5: for next x in coord, in order: 6: if height(x) > current_h or height(x) < current_h: 7: skyline.add(x, height(x)) 8: else: 9: skyline.add(x, current_h) return skyline

Vertex Cover (Tree) Dynamic Program

--Vertex Cover-- Input: A tree T = (V, E) Goal: Find a vertex cover of minimum size (subproblems are subtrees) Precise definition: Let VC[v, 0] be the size of the smallest vertex cover for the subtree rooted at v, not including v Let VC[v, 1] be the size of the smallest vertex cover for the subtree rooted at v, including v Base Cases: VC[leaves, 1] = 1 VC[leaves, 0] = 0 Solution: min{VC[root, 0], VC[root, 1]} Formula: VC[u, 0] = sum(VC[w, 1]) where w a child of u VC[u, 1] = 1 + sum(min{VC[w,0], VC[w, 1]} where w a child of u) O(n^2)

Rank the following running times in order from fastest to slowest: 17n, n^3, 2^n, n!, 36, log b (n), 48 n log b (n), 367 n^2 log b (n), n^n

36, log b (n), 17n, 48 n log b (n), 367 n^2 log b (n), n^3, 2^n, n!, n^n

Give an example of a strongly connected directed graph G = (V, E) such that, for every v in V, removing v from G leaves a directed graph that is not strongly connected

A directed graph with three nodes: A->B B->C C->A if C is removed then: A->B which is not strongly connected but still a directed graph.

Big O Notation

An analysis / upper bound on the running time of an algorithm. slowest to fastest: O(1) O(log n) O(n^c) (where 0<c<1) O(n) O (n * log n) O(n^2) O(n^c) (where c > 1) O(c ^ n) O(n!) O(n^n)

Breadth First Search

BFS(G, A) Input: An undirected graph, G = (V, E), and a vertex, A. Output: For all vertices, X, dist(X) is set to be the distance from A to X. 1: for all X ∈ V do 2: dist(X) = ∞ 3: dist(A) = 0 4: Q = [A] (a queue containing A) 5: while Q is not empty do 6: X = dequeue(Q) 7: for all edges (X, Y ) ∈ E do 8: if dist(Y ) = ∞ then 9: enqueue(Q, Y ) 10: dist(Y ) = dist(X) + 1

Binary Search Divide and Conquer

BinarySearch(A, x, min, max) Input: An array of n sorted numbers A, number x, integers min, and max. Output: A position of x in A if x ∈ A, or −1 if x ∈/ A. (Initially min = 0 and max = n − 1) 1: if max < min then return −1 2: else 3: if x < A[b(max + min)/2c] then return BinarySearch(A, x, min,b(max + min)/2c − 1) 4: else if x > A[b(max + min)/2c] then return BinarySearch(A, x ,b(max + min)/2c + 1, max) 5: else return b(max + min)/2c

What is Coin Row as a decision problem?

Coin Row: Input: A list of n coins c1, c2, ..., cn, whose values are some positive integers v1, v2, ... , vn and a threshold g. Output: A subset of coins subject to the constraint and with a value g

In any connected undirected graph G=(V,E) there is a vertex v ∈ V whose removal leaves G connected

Consider a graph G=(V,E) and a Depth-first-search tree T constructed from the graph using DFS. Given any connected graph, generation of T is linear time O(V+E) and there exist one unique tree T. Denote the leaves of the tree T by L(T). If we choose any vertex v ∈ L(T), removal of that vertex and the edges associated with it will keep T connected (by the definition of tree). Thus Tˆ = T - {v} is connected which implies that the graph Gˆ=(V-{v},E- {(i, j) : i = v ∨ j = v}) is connected since Tˆ and Gˆ are isomorphic.

Design a dynamic programming algorithm to find the weight of a set of non-conflicting activities with maximum weight

Definition: Let AS[i] be the value of the maximum weight subset of activities that are non-conflicting, drawing from the first i activities including i Base Cases: AS[0] = 0 AS[1] = w1 Solution: The maximum value in the table Formula: AS[i] = max{AS[j]} + wi where fj <= si

Give a dynamic programming algorithm to determine if there's some subset of a set of n positive integers that adds up to some positive integer k

Definition: Let ATK[k, i] be the maximum sum value of some subset of a1, a2, ... , ai that is within capacity k Base Cases: ATK[k, 0] = 0 ATK[0, i] = 0 Solution: ATK[K, i] Formula: ATK[k, i] = max { ATK[k, i-1], ATK[k-aj, i-1] } k >= aj

Give a dynamic programming algorithm for an unlimited supply of coins of denominations to make change for v. Also try for using each denomination at most once

Definition: Let CM[v, i] = 1 if it is possible to make change for v using the first i denominations. CM[v, i] = 0 if it is not possible Base Cases: CM[0, i] = 1 CM[v, 0] = 0 Solution: CM[V, n] Formula: CM[v, i] = max{CM[v-xi, i], CM[v, i-1]} At most once: CM[v, i] = max{CM[v-xi, i-1], CM[v, i-1]} and CM[0, 0] = 1

Design a dynamic programming algorithm for the maximum sum of a contiguous sub sequence

Definition: Let CS[i] be the value of the maximum sum of a consecutive subsequence a1, a2, ... , ai, including ai. Base Cases: CS[0] = 0 CS[1] = a1 Solution: The maximum value in the table Formula: CS[i] = max{CS[i-1] + ai, ai}

Design a dynamic programming algorithm that determines the best return on the X x Y piece of cloth, that is, a strategy for cutting the cloth so that the products made from the resulting pieces give the maximum sum of selling prices.

Definition: Let C[i, j] be the maximum sum of selling prices on the cloth sized i x j from a list of n products Base Cases: C[0, j] = 0 for all j <= y C[i, 0] = 0 for all i <= x Solution: C[X, Y] Formula: C[i, j] = max{max{C[k, j] + C[i-k, j] } where 0<=k<=i, max {C[i, p] + C[i, j-k]} where 0<=p<=j, yield (i, j) } *yield returns the max selling price of cloths sized (ixj) or 0 if none exist)

Give a dynamic programming algorithm to compute the maximum expected total profit subject to the given constraints for Firestone

Definition: Let FS[i] be the value of the maximum expected total profit subject pi, to the given constraints, of the increasing subseries a1, a2, ... ai, including ai Base Cases: FS[0] = 0 FS[1] = P1 Solution: The maximum value in the table Formula: FS[i] = max { FS[j] + pi } where mj - mi >= k and j < i

Give a dynamic programming algorithm to find the length of the longest common substring

Definition: Let LCS[i, j] be the length of the longest common substring from substrings x1, x2, ... xi and y1, y2, ... , yj, including xi and yj. Base Cases: LCS[i, 0] = 0 LCS[0, j] = 0 Solution: The maximum value in the table Formula: LCS[i, j] = LCS[i-1, j-1] + 1, if a[i] != b[j] LCS[i,j] = 0

Give a dynamic programming algorithm that takes as input two string x[1 ... n] and y[1 ... m] and a scoring matrix $ and returns the highest scoring alignment

Definition: Let SA[a, b] be the highest scoring alignments of the two substrings length a and b based on the scoring matrix $ Base Cases: SA[a, 0] = 0 SA[0, b] = 0 Solution: SA[n, m] Formula: SA[a, b] = max { SA[a-1, b-1] + $(a, b), SA[a, b-1] + $(a, b), SA[a-1, b] + $(a, b) }

Give a dynamic programming algorithm to determine the optimal sequence of hotels at which to stop

Definition: Let SDP[j] be the minimum total penalty of the subsequence of hotels at mile posts a1 < a2 < ... < aj, including aj Base Cases: SDP[0] = 0 SDP[1] = (300 - a1) ^2 Solution: SDP[j] Formula: SDP[j] = min {SDP[j] + (300 - (aj - ak)) ^ 2 } where k < j

Prove or counterexample: If G is a graph on more than |V | − 1 edges, and there is a unique heaviest edge, then this edge cannot be a part of any minimum spanning tree.

False. Since this is not a complete graph, it is possible that this unique heaviest edge is the only edge across a cut of G. The following is a counterexample: A->B = 4 B->C = 4 A->C = 3

True or false? If a directed graph G is cyclic but can be made acyclic by removing one edge, then a depth-first search in G will encounter exactly one back edge

False. You can have multiple back edges, yet it can be possible to remove one edge that destroys all cycles. For example, in graph G = (V, E) = ({a, b, c}, {(a, b),(b, c),(b, a),(c, a)}), there are two cycles ([a, b, a] and [a, b, c, a]) and a DFS from a in G returns two back edges ((b, a) and (c, a)), but a single removal of edge (a, b) can disrupt both cycles, making the resulting graph acyclic.

Let G = (V, E) be an undirected graph. Prove that if all edge weights are distinct, then it has a unique minimum spanning tree

If T1 and T2 are distinct minimum spanning trees, then consider the edge of minimum weight among all the edges that are contained in exactly one of T1 or T2. Without loss of generality, this edge appears only in T1, and we can call it e1. Then T2∪{e1} must contain a cycle, and one of the edges of this cycle, call it e2, is not in T1. Since e2 is a edge different from e1 and is contained in exactly one of T1 or T2, it must be that w(e1)<w(e2). Note that T=T2∪{e1}∖{e2} is a spanning tree. The total weight of T is smaller than the total weight of T2, but this is a contradiction, since we have supposed that T2 is a minimum spanning tree.

Does Dijkstra's algorithm work with negative weight edges? Why or why not?

If all weights are non-negative, adding an edge can never make a path shorter, so no it does not work.

What is Independent Set as a decision problem?

Independent Set: Input: A graph G and a threshold g Output: A set of g independent vertices G

A 2-Approximation Algorithm for Vertex Cover

Input: A graph G. Output: A vertex cover that is within 2 of the optimal vertex cover. 1: Find a maximal matching of G, M ⊆ E 2: Return S, all endpoints of edges in M. We have no way of finding the optimal vertex cover in polynomial time. We can easily find another structure, a maximal matching with two key Its size gives us a lower bound on the optimal vertex cover. It can be used to build a vertex cover, whose size can be related to that of the optimal cover OPT <= S <= 2OPT

Huffman

Input: A set of symbols, S, and their frequencies, f . Output: A prefix-free binary code with minimum expected codeword length. 1: Arrange the symbols in increasing order according to their frequency. 2: Construct the Huffman tree. (?) 3: Label all left edges 0 and all right edges 1. 4: The code word for letter i is the labels for all edges on the path from the root to the vertex representing i.

Kruskal Greedy Algorithm

Input: A weighted, connected, undirected graph, G = (V, E). Output: A minimum weight spanning tree, T, of G. 1: Let T be a graph on the same vertices as G but with no edges 2: Let A be a list containing E sorted in increasing order according to weights 3: for each edge, e, in A do: 4: if adding e to T doesn't create a cycle then 5: Add e to T Kruskal's Algorithm Correctness In Step 3 of Kruskal each edge, e, is considered. There are two cases: 1 Adding e to T creates a cycle. Then e is the maximum weight edge in that cycle. The cycle property states that e will not be in any MST. 2 Adding e = (u, v) does not create a cycle. Let S be the set of vertices reachable from u. e is the minimum weight edge in the cut created by S and V \ S. The cut property states that e is in the MST.

Knapsack w and w/o repetition

Knapsack Input: said above, items, weights, values, and a threshold W Goal: Find a subset with possible repetitions SCN, of the items such that the weight is less than the threshold and vi is maximized W/O REPETITION Precise definition: Let K[w] be the maximum value achievable with a knapsack of capacity w. Base Case: K[0] = 0 Solution: K[W] Formula: K[w] = max{K[w - wi] + vi} where w >= wi W REPETITION Precise definition: Let K[w, i] be the maximum value achievable with a knapsack of capacity w drawing from the first i items. Base Cases: K[w, 0] = 0 K[0, i] = 0 Solution: K[W, n] Formula: K[w, i] = max{K[w-wi, i -1] + vi, K[w, i-1]} where w >= wi

What is Knapsack as a decision problem?

Knapsack: Input: A set, N, of n items each with weight w1, w2, ... , wn and a value v1, v2, v3, ... , vn and a threshold k. Output: A subset, S C N, of distinct items such that sum(wi) <= W where i in S and sum(Vi) = k

Design and analyze an algorithm for finding an odd length cycle in a strongly connected directed graph

Let G be strongly connected. Run BFS (!) from an arbitrary vertex s. BFS creates a leveled tree where level of a vertex v is it's directed distance from s. If while running BFS you have never seen an edge (u,v) that goes between levels of the same parity then G is bipartite. The parity of the level of u defines the partition that u belongs to. Note that since every vertex is reachable from s, BFS will explore all the edges, so all of the edges would be going between the two blocks of the partition. Otherwise, you find an edge (u,v) that goes between levels of the same parity. Suppose level of u is even and level of v is even. Then run DFS (or BFS) from v to get a path to s. If this path is of even length then you get an odd directed cycle s⇝u→v⇝s. If this path is of odd length then you get an odd directed cycle s⇝v⇝s. Analogous thing holds when level of u is odd and level of v is odd. Since this algorithm simply does 2 runs of BFS/DFS, it runs in linear time.

What is Longest Increasing Subsequence as a decision problem?

Longest Increasing Subsequence: Input: A sequence of numbers a1, a2, a3, ... , an and a threshold g Output: An increasing subsequence of length g.

MergeSort Divide and Conquer

MergeSort(A[0, . . . , n − 1]) Input: A list of unsorted numbers A[0, . . . , n − 1] Output: The same list, sorted in increasing order 1: if n ≤ 1 then return A 2: else return Merge(MergeSort(A[0, . . . , bn/2c]),MergeSort(A[bn/2c+ 1, . . . , n − 1])) Merge(x[0, . . . , k − 1],y[0, . . . , ` − 1]) Input: Two sorted lists, x[0, . . . , k − 1] and y[0, . . . , ` − 1] Output: One sorted list that contains all elements of both lists. 1: if x = ∅ then return y 2: if y = ∅ then return x 3: if x[0] ≤ y[0] then return x[0]◦Merge(x[1, . . . , k − 1], y[0, . . . , ` − 1]) 4: else return y[0]◦Merge(x[0, . . . , k − 1], y[1, . . . , ` − 1])

T(n) = 3T(n/3) + O(1)

O(n) 1 + 3 + 9 + ... + n/3 + n n/n + n/(n/3) + n/(n/9) + ... + n/3 + n n * (1/n + 1/(n/3) + ... + 1/3 + 1) <= O(3 * n) = O (n)

T(n) = T(n/2) + O(n)

O(n) n + n/2 + n/4 + ... +n/n n * (1 + 1/2 + 1/4 + ... + 1/n) <= O(2n) = O(n)

T(n) = T(n/3) + O(n)

O(n) n + n/3 + n/6 + ... + 1 n + n/3 + n/6 + ... + n/n n * (1 + 1/3 + 1/6 + ... + 1/n) <= O(3 * n) = O(n)

T(n) = T(n/2) + O(n^2)

O(n^2) n^2 + (n/2)^2 + (n/4)^2 + ... + (n^2 / n ^ 2) n^2 * ( 1 + 1/4 + 1/16 + ... + 1/n^2) <= O(4/3 * n^2) = O(n^2)

If X and Y are n x n matrices and X = [ A B, C D] Y = [ E F, G H] Prove the product property that XY can be expressed as

Subdivide X into a (1, 1) - (n/2, n/2) elements in the top right, b (1, n/2 + 1) -(n/2, n) elements in the top left, c (n/2 + 1, 1) - (n, n/2) elements in the bottom left, and d (n/2 + 1, n/2 + 1) -(n, n) in the bottom right, and do the same for Y with e, f, g, and h respectively. Then combine the two using the dot product and show each subdivision = its product (i.e. top left = AE + BG but with elements)

Theorem (Cycle Property) For any cycle, C, if e is the (strictly) maximum weight edge in C, then e does not belong to any MST

Suppose that e is the (strictly) maximum weight edge in some cycle, C.Further suppose, for a contradiction, that e is an edge of MST, T. Removing e from T disconnects T. Let S be one component of disconnected T. Notice that some other edge, f , in cycle C has exactly one endpoint in S. Thus, T 0 = T ∪ f \ e is a spanning tree. And since w(f ) < w(e): w(T 0 ) < w(T). A contradiction to T being an MST.

Theorem (Cut Property) For any cut, C, in a graph, if e is the (strictly) minimum weight edge of C, then this edge belongs to all MSTs of the graph.

Suppose that e is the (strictly) minimum weight edge for some cut, C. Further suppose, for a contradiction, that e is not an edge of MST, T. Adding e to T creates a cycle in T. Notice that some other edge, f , in the cycle is also an edge of the cut, C. Thus, T 0 = T ∪ e \ f is a spanning tree. And since w(e) < w(f ): w(T 0 ) < w(T). A contradiction to T being an MST

Is it always possible to make an undirected graph with two connected components connected by adding a single edge? Does the same hold true for directed graphs and strongly connected components?

Suppose there are two connected components of an undirected graph. Then there exists a path to every other vertex from any vertex in each separate component. Now suppose there's a vertex v in component one,a vertex u in component two, and an edge that can connect v and u. If we add the edge between v and u, there is now a path to every vertex from every other vertex for the combined vertices of the connected components, making the whole graph connected. For directed graphs, the same does not apply as if there is a vertex that is connected that only has incoming edges, it will not create a path to the other edges, whether or not the component is strongly connected or not.

There are three containers: a 10 liter container, a 7 liter container, and a 4 liter container. Is there a sequence of pourings that leaves exactly 2 liters in the 4 liter container? Model this as a graph problem. What algorithm can be used to solve this problem?

The graph is a list of states (undirected) such that G = {(0, 7, 4), (4,7,0), (7, 0, 4)} where each edge is determined by whether or not the state can be changed to the other. The algorithm that can be used to solve this problem would be BFS, as the problem that needs to be solved is the farthest distance state from the current state.

Suppose you are given a source word and a target word. Your goal is to transform the source word to the target word in as few valid steps as possible. (cat -> hut). Model this problem as a graph. What algorithm can be used to solve this problem?

The graph would be a directed graph where each edge represents a valid step in changing one character of the word. DFS would be perfect for finding this, as it would create a tree and the shortest branch would be the transformed word

True or False? If DFS search produces exactly one back edge, then it is possible to choose an edge e

True. Removing the back edge will result in a graph with no back edges, and thus a graph with no cycles (as every graph with at least one cycle has at least one back edge). Notice that a graph can have two cycles but a single back edge, thus removing some edge that disrupts that cycle is insufficient, you have to remove specifically the back edge. For example, in graph G = (V, E) = ({a, b, c}, {(a, b),(b, c),(a, c),(c, a)}), there are two cycles [a, b, c, a] and [a, c, a], but only one back edge (c, a). Removing edge (b, c) disrupts one of the cycles that gave rise to the back edge ([a, b, c, a]), but another cycle remains, [a, c, a].

Find the minimum spanning tree for the following graph

Use Kruskal's algorithm, order the edges, and add an edge as long as it does not introduce a cycle into the tree. If the edges are distinct then there is one unique minimum spanning tree

What is Vertex Cover as a decision problem?

Vertex Cover: Input: A graph G, and a threshold g Output: A vertex cover on G w/ g vertices

Is it possible to reconstruct a tree through pre and post orderings?

Yes

Prove: If e is any edge of strictly minimum weight in G, then e must be a part of some minimum spanning tree

n: Proceed by contradiction. Let T be some minimum spanning tree of graph G not containing edge e = (u; v). Since T is a spanning tree, there is a path from v to u. For any edge (x; y) on the path from v to u, take the edge (x; y) out of the tree and add the edge (u; v). Since the weight of (u; v) is less than or equal to the weight of all edges including the edge (x; y), the new tree T 0 has weight less than or equal to T. So, T 0 is a minimum spanning tree which is a contradiction

Prove that KruskalReverseDelete is correct

no thank you! im going to pass out if you keep talking ;P


Kaugnay na mga set ng pag-aaral

Psychology Developmental Ch. 13 & 14

View Set

Chpt. 7 Nutrients Involved in Fluid and Electrolyte Balance

View Set

The Child with a Gastrointestinal Alteration

View Set

Chapter 33 Coronary Artery Disease and Acute Coronary Syndrome - Complex Fall 2021

View Set

Pharmacology - Gastrointestinal Medications

View Set

Naturfag - Bigbang og Kapittel 6

View Set

Chapter 18: The French Revolution

View Set

Music Appreciation - Midterm Exam (Quiz 4+)

View Set

GSCM 410 Final Study Guide (CH#1-3)

View Set

Dave Ramsey Personal Finance Chapter 4: Debt

View Set