INF234 - Algorithms

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

The algorithm of Kruskal for constructing a Minimum Spanning Tree works on graphs with negative edge weights

True. Kruskal's works with any edge weights!

BFS

breadth first search. algorithm to search all neighbors of a node, then traverse the next level. uses a QUEUE

Master Theorem Case 2

c == log(b)a => O(n^d * logn)

natural numbers: positive integers

In an undirected unweighted graph G, a shortest path between two given vertices s and t can be found using Breadth-First Search

True.

Adding one to every edge weight of a graph G increases the cost of a minimum weight spanning tree of G by |V(G)| − 1

True. An MST has V-1 edges.

"for all" symbol

For a directed graph G, whether G is acyclic can be decided using Depth-First Search.

True

.

.

Topological ordering

A linear ordering of vertices such that for every directed edge u-v, vertex u comes before v in the ordering. Graph must be a DAG to have a topological ordering and vice versa. Modified approach using DFS for ordering

codeword

A binary code encodes each character as a binary string. AKA a codeword. Eg. C == 10101 for a particular Huffman tree. Looking to minimize the length

Certifier

A certifier algorithm is a program that quickly checks if a proposed solution to a problem is correct, with the help of a short additional piece of information called a certificate. The certifier doesn't find the solution itself but verifies its correctness using the provided certificate.

Bipartite graphs

A graph where the vertices can be divided into two sets such that all edges connect a vertex in one set to a vertex in the other set. There are no edges between vertices within the sets. A bipartite graph cannot have an odd cycle. To test Bipartiteness, use BFS and alternate "colors" for each layer of neighbors. Finding a neighbor with the current color of the "cycle" means the graph is not bipartite.

Bipartite matching (maximum)

A matching in a Bipartite Graph is a set of the edges chosen in such a way that no two edges share an endpoint. A maximum matching is a matching of maximum size (maximum number of edges). In a maximum matching, if any edge is added to it, it is no longer a matching. There can be more than one maximum matchings for a given Bipartite Graph. MBP can be solved by converting it into a flow network, and using Ford Fulkerson to find the maximum flow.

simple path

A path that does not repeat vertices is called a simple path

When is a problem in NP?

A problem is nondeterministic polynomial (NP) when there exists a nondeterministic Turing Machine that can solve the problem in polynomial time. It must also be verifiable in polynomial time.

When is a problem in P?

A problem is polynomial (P) when it can be solved in polynomial time. For instance, a problem for which there exists an algorithm running in O(n^x) and the input is n.

How do we show that a problem is NP-complete?

A problem x is NP-Complete if: - x is in NP - for every problem in NP is reducible to X in polynomial time You could also reduce an existing NP-Complete problem Y to the problem X, showing that X is at least as hard as Y

Strongly connected components

A strongly connected component is a component of only a directed graph that has a path from every vertex to every other vertex in that component. Brute force between all pairs or use Tarjan's Algorithm

Vertex Cover

A vertex cover in a graph is a set of vertices S such that every edge of the graph has at least one endpoint in S. NP-Complete Brute Force: runtime - O(n+m) for every edge u-v in G: if u not in S and v not in S return False return True Minimum VC Algo: O(2^n * (n+m)) for all subsets S in V(G): best = n if VC(G,S) best = min(best, S) return Best

Edmonds-Karp Algorithm

An implementation of Ford-Fulkerson Algorithm that uses BFS to ensure the algorithm terminates even if loops exist. It takes longer though, running at O(|V|*|E|^2) time and using O(|E|+|V|) space.

divide & conquer

Breaks a problem into smaller subproblems recursively and solves them independently, combining the solution - Mergesort is this

Master Theorem Case 3

C < log(b)a => O(n^(log(b)a))

Master Theorem Case 1

C > log(b)a => O(n^c)

Find a minimum s-t-Cut in a directed graph

Can use either Ford-Fulkerson: O(E+maxflow) or Edmonds-Karp: O(VE^2)

independent set ON TREES

Colour nodes of the tree by 2 colours (e.g. using BFS). Return true if and only if the more frequent colour is assigned to at least k nodes. O(n).

If graph G has edges of negative weights but no negative cycles, then Dijkstra's algorithm will work correctly on G.

Dijkstra's fails on negative weights. Since it is greedy, it can't reconsider a node even if it later finds a better minimum path. You must use the DP Bellman-Ford Algorithm.

If f(n) = n^5 then f(n) is O(n^6)

True - upper bound is anything above

In an undirected unweighted graph G, a longest path between two given vertices s and t can be found using Depth-First Search

False

The recurrence T(n) = 4T(n − 2), with T(0) = T(1) = 1, solves to T(n) = O(n^4).

False, if you keep substituting in the recursive calls: T(0) = T(1) = 1 T(2) = 4(T(2-2)) T(n) = 4(4T(n-4)) T(n) = 4(4(4T(n-6))) ... This reduces to 4^(n/2) * T(0) which is O(2^n)

The existence of a problem in NP that admits a polynomial-time algorithm implies that there exists a polynomial-time algorithm for 3-SAT.

False. 3-SAT is known NP Complete. 2SAT is in NP w/ a polynomial time algorithm, but this isn't necessarily true for 3SAT.

In an undirected weighted graph G, the shortest path between two vertices lies in some minimum weight spanning tree of G.

False. An MST doesn't necessarily give the shortest path. That's why we have Dijkstra's.

In the Huffman encoding of the string "YOHOHO" the letters O and H receive equally long codewords.

False. Build a Huffman tree to see

If e is an edge of maximum weight in a cycle, then e is not contained in any minimum weight spanning tree of G

False. Consider a small MST where multiple edges have the same max weight

The size of the maximum flow from vertex s to vertex t in a network is equal to the size of the maximum cut between s and t

False. It is the size of the minimum cut.

Graph G is represented by adjacency list. It takes time O(1) to check whether two vertices of G are adjacent.

False. Must iterate through the list. min(dag(u), dag(v)) or worstcase O(n)

Bipartite graph G = (V1, V2, E) has a perfect matching if and only if |V1| = |V2|

False. This is necessary, but not sufficient when alone. Consider if there are no edges.

Suppose we run a DFS on a directed graph G, with root r, and see that it yields a forward edge. Then, in every DFS of G starting from r, there must be at least one forward edge.

False. This is not necessarily the case. There can be multiple DFS trees for one graph G.

Recurrence T(n) = 2 · T(n/2) + O(n) solves to T(n) = O(n)

False. This is the recurrence relation for Merge Sort which has a runtime of O(nlogn)

Recurrence T(n) = 8·T(n/2)+O(n) solves to T(n) = O(n log n).

False. This resolves to Case 3 of the Master Theorem, so T(n) = O(n^3)

Interval Partitioning

Given N events with their starting and ending times, find a minimum number of resources to schedule all lectures so that no two occur at the same time using the same resource (classroom problem) This problem can be solved optimally with a greedy strategy of scheduling requests based on earliest start time i.e., from the set of remaining requests

Interval Scheduling

Given N events with their starting and ending times, find a schedule that includes as many events as possible. Algorithm: always select the next possible event that ends as early as possible

Knapsack problem

Given N items where each item has some weight and profit associated with it and also given a bagterm-55 with capacity W. The task is to put the items into the bag such that the sum of profits associated with them is the maximum possible. Dynamic Programming Problem. If we get a subproblem the first time, we can solve this problem by creating a 2-D array that can store a particular state (n, w). Now if we come across the same state (n, w) again instead of calculating it in exponential complexity we can directly return its result stored in the table in constant time Runtime O(N *W)

Dijkstra's algorithm

Given a non-negative weighted graph and a source vertex in the graph, find the shortest paths from the source to all vertices in the given graph. Works with both directed and undirected graphs. 1. Mark the ending vertex with a distance of zero. Designate this vertex as current. 2. Find all vertices leading to the current vertex. Calculate their distances to the end. Since we already know the distance the current vertex is from the end, this will just require adding the most recent edge. Don't record this distance if it is longer than a previously recorded distance. 3. Mark the current vertex as visited. We will never look at this vertex again. 4. Mark the vertex with the smallest distance as current, and repeat from step 2.

Scheduling to minimize lateness

Given a set of n jobs all of which must be scheduled on a single resource such that all jobs arrive at time s and each job has a deadline di and a length ti, minimize the maximum lateness of the resulting schedule This problem can be solved optimally with a simple greedy strategy of scheduling jobs based on earliest deadline

Subset Sum

Given a set of non-negative integers and a value sum, the task is to check if there is a subset of the given set whose sum is equal to the given sum. Solution - DP/Memoization

Set Cover

Given a universe U of n elements, a collection of subsets of U say S = {S1, S2...,Sm} where every subset Si has an associated cost. Find a minimum cost subcollection of S that covers all elements of U. NP-Complete

3-SAT

In the 3-SAT problem, each clause has at most literals. This problem is NP-Complete. In the 2-SAT problem, every clause has two literals and is solvable in a polynomial-time problem.

vertex cover ≤p set cover proof

In the transformation from Vertex Cover to Set Cover, a polynomial-time construction is performed by associating elements of the graph with subsets of the ground set. The goal is to prove that a solution to the original Vertex Cover instance corresponds to a solution in the Set Cover instance and vice versa. If there exists a Vertex Cover of size at most j in graph G, the constructed set of nodes in Set Cover forms a collection of subsets covering the ground set. Conversely, a Set Cover of size k in the constructed instance corresponds to a set of vertices in G, containing at most j nodes, such that each edge in the ground set is covered by at least one set associated with its endpoints. http://www.cs.cornell.edu/courses/cs482/2007su/NPComplete.pdf

NP-Hard

Intuitively these are the problems that are even HARDER than the NP-complete problems. Note that NP-hard problems do not have to be in NP (they do not have to be decision problems). TL;DR the hardest problems that are NOT decision problems (NP-complete are the hardest decision problems)

front edge

It is an edge (u, v) such that v is a descendant but not part of the DFS tree.

back edge

It is an edge (u, v) such that v is the ancestor of node u but is not part of the DFS tree.

cross edge

It is an edge that connects two nodes such that they do not have any ancestor and a descendant relationship between them

Edit distance (Levenshtein)

Levenshtein distance is a measure of the similarity between two strings, which takes into account the number of insertion, deletion and substitution operations needed to transform one string into the other. The iterative technique with a full matrix uses a 2D matrix to hold the intermediate results of the Levenshtein distance calculation. It begins with empty strings and iteratively fills the matrix row by row. It computes the minimum cost of insertions, deletions, and replacements based on the characters of both strings.

The problem of deciding whether a given bipartite graph has a matching of size at least k is in NP.

Technically yes. This is a known P problem, and P is a subset of NP.

The "element of" symbol. Means that an element is a member of a set.

SAT

The Boolean Satisfiability Problem, first problem shown to be NP-Complete. The problem of determining if a Boolean formula is satisfiable or unsatisfiable. Satisfiable : If the Boolean variables can be assigned values such that the formula turns out to be TRUE, then we say that the formula is satisfiable. Unsatisfiable : If it is not possible to assign such values, then we say that the formula is unsatisfiable.

The intersection symbol. Represents the intersection of two sets, which is the set containing common elements between both sets.

The subset symbol. Means that the left set has few or all elements equal to the other set

Hall's theorem

There is a matching of size A if and only if every set S ⊆ A of vertices is connected to at least |S| vertices in B

vertex cover ON TREES

This problem is NP-Complete for graphs, but polynomial for trees 1) Root is part of vertex cover: In this case root covers all children edges. We recursively calculate size of vertex covers for left and right subtrees and add 1 to the result (for root) 2) Root is not part of vertex cover: In this case, both children of root must be included in vertex cover to cover all root to children edges. We recursively calculate size of vertex covers of all grandchildren and number of children to the result (for two children of root). Use DP memorization for recursion

If a DFS on a directed graph G produces exactly one back edge, then it is always possible to remove an edge e from G such that the obtained graph G − e is a DAG

True. Removing the back edge gets rid of the cycle which results in a Directed Acyclical Graph.

Return Shortest Paths or a Negative Cycle in Graph

Use Bellman-Ford which can return shortest paths from a source or detect a negative cycle

Is P in NP?

Yes. P is a subset of NP

T(n) = aT(n/b) + f(n)

a = # of subproblems, n/b = size of subproblem, f(n) = number of steps needed to both divide the original problem instance and combine the a solutions into a final solution

Dynamic Programming

a computer programming technique where an algorithmic problem is first broken down into overlapping sub-problems, the results are saved (memoized) so a sub problem is only solved once. - an optimization over recursion - eg. fibonacci

Hamiltonian (Rudrata) cycle

a cycle that visits each vertex exactly once

Huffman Code

a data compression algorithm where variable-length codes are assigned to characters. Lengths of the codes are based on the frequencies of the characters. a Huffman Tree is built from the bottom up. Leaf nodes represent a unique character. The "bottom" layer of the leaves will be characters with the least frequencies, increasing as the tree moves up. For a node, left and right children are 0 and 1. Internal node is the sum of the two children frequencies Runtime O(nlogn) where n is the # of unique characters.

adjacency list

a data structure used to represent a graph where each node in the graph stores a list of its neighboring vertices

flow network

a directed graph where each edge has a capacity and each edge receives a flow. The amount of flow on an edge cannot exceed the capacity

prim's algorithm

a greedy algorithm for finding a MST. The idea is to maintain two sets of vertices. The first set contains the vertices already included in the MST, and the other set contains the vertices not yet included. At every step, it considers all the edges that connect the two sets and picks the minimum weight edge from these edges. After picking the edge, it moves the other endpoint of the edge to the set containing MST.

Minimum Cut

a min set of edges (weight) such that after removing these edges, there is no s-t path

cut

a partition of the vertices of a graph into two disjoint subsets

pairwise nonadjacent

a set of vertices or of edges is pairwise nonadjacent, AKA independent, if no two of its elements are adjacent

Independent Set

a set of vertices such that no two vertices in S are adjacent to each other. It consists of non- adjacent vertices. Finding I.S. is NP-Complete

augmenting path

a simple path from the source to the sink in the corresponding residual network . Intuitively, an augmenting path tells us how we can change the flow on certain edges in G so that we increase the overall flow from the source to the sink.

Bellman-Ford Algorithm

a single source shortest path algorithm that determines the shortest path between a given source vertex and every other vertex in a graph. This algorithm can be used on both weighted and unweighted graphs. Can handle negative weights unlike Dijkstra, but only on directed graphs. Works by iterating through the graph V-1 times finding the shortest paths. Then, another iteration of: for any edge(u, v, weight), if dist[u] + weight < dist[v]. Basically if the nodes continue to decrease

Clique

a subset of vertices of an undirected graph such that every two distinct vertices in the clique are adjacent

ford-fulkerson algorithm

an algorithm that tackles the max-flow min-cut problem 1. Start with initial flow as 0. 2. While there exists an augmenting path from the source to the sink: Find an augmenting path using any path-finding algorithm, such as breadth-first search or depth-first search. Determine the amount of flow that can be sent along the augmenting path, which is the minimum residual capacity along the edges of the path.Increase the flow along the augmenting path by the determined amount. 3. Return the maximum flow. run time depends on implementation and how edges are found

greedy algorithms

an approach for solving a problem by selecting the best option available at the moment, without worrying about the future result

DFS

depth first search. algorithm to search a tree down one path as far as possible, then comes back up to go down another path. uses a STACK

empty set symbol

Kruskal's clustering algorithm

finds a minimum spanning tree/forest for a weighted, undirected graph. 1. Sort all the edges in non-decreasing order of their weight. 2. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If the cycle is not formed, include this edge. Else, discard it. 3. Repeat step 2 until there are (V-1) edges in the spanning tree.

Counting Inversions

indicates - how far (or close) the array is from being sorted Naive: two nested for loops comparing adjacent elements Better: using mergesort

residual graph

indicates how much more flow is allowed in each edge in the network graph. residual capacity of a directed edge is the capacity minus the flow

Weighted Interval Scheduling

interval scheduling but now weighted with values of importance. Find the maximum profit subset of jobs such that no two jobs in the subset overlap. Dynamic Programming Problem.

When is an algorithm O(2^n) time?

iterating over all possible subsets of a set. ie. Knapsack problem. Or recursive problems that solve a problem of size N by recursively solving two smaller problems of size N-1

∈/

the "not element of" symbol. means that there is no set membership.

|·|

the cardinality symbol. Represents the number of elements in a set.

Finding the Closest Pair of Points

the problem is to find out the closest pair of points in the array. The Brute force solution is O(n^2), compute the distance between each pair and return the smallest. We can calculate the smallest distance in O(nLogn) time using Divide and Conquer strategy. Split the points with line L so that half the points are on each side. Recursively find the pair of points closest in each half.

\

the relative complement symbol. represents objects that belong to A and not to B.

cut-set

the set of edges that have one endpoint in each subset of the partition

the strict subset symbol. means the left subset has fewer elements than the other set.

the union symbol. represents the set of all unique elements from both sets.


Set pelajaran terkait

Public Finance - Government Revenue and Taxation

View Set

Word Basics 4 - Headers / Footers and Page-Numbers

View Set

Network Security Basics/Computer Security Chapter 4

View Set

ESL Supplemental - Theories (B.F. Skinner)

View Set

PREP U-Intracranial regulation and palliation

View Set