Exam2 Algo-Combined Flashcards
RSA/Graph Theory
Undirected Graph
Depth First Search
Purpose, Input, Output, Runtime
Purpose: Perform a depth first search on an undirected graph G.
Input: Undirected Graph G = (V, E)
Output: 4 arrays: prev, pre, post each of length n
- Prev[z] The parent index of vertex Z in the DFS visition
- pre[z] the pre number of vertex z in the DFS visitation
- post[z] the post number of the vertex z in the DFS visitation
- (Optional) ccnum[z] the connected component number, if stored after SCC algorithm run on graph.
Runtime: O(n + m)
AKA:O(|V| + (|E|)
Directed Graph
Depth First Search
Input, Output, Runtime
Purpose: Perform a depth first search on a directed graph G
Input: Directed Graph G=(V, E)
Output: 3 arrays, prev, pre, post, each of length n
- prev[z]: The parent index of vertex Z in the DFS visitation
- pre[z]: the pre order number of vertex Z
- post[z]: the post order number of vertex Z.
Runtime: O(n + m)
AKA: O(|V| + |E|)
Topographial Sort Directed
Purpose, input, output, runtime.
Purpose: Sort the verticies of an input graph in topological order. (Following left to right on resulting list means all preconditions will be done in order)
Input: directed acyclic graph G = (V, E)
Output: array topo length n
- topo[i]: The vertex number of the i’th vertex in topological order from left to right (in descending post order)
Runtime: O(n + m) no need to do sort after DFS, we know post order numbers and their ranges.
AKA: O(|V| + |E|)
Sink: Node with smallest post order
Source: Node with highest post order.
Find Directed Strongly Connected Components
Purpose, input, output, runtime
Purpose: Find the strongly conencted components in a directed graph G.
Input: Directed Graph G=(V,E)
Output: Array ccnum of length n, array topo length t, where t is the number of SCCs, a directed acyclic metagraph Ga = Va, Ea
- ccnum[z]: The connected component numer of vertex z for z in V.
- topo[i] the SCC number for the ith SCC in topological order from left to right.
- Ga = the SCC acyclic metagraph.
Runtime: O(n + m)
AKA: O(|V| + |E|)
BFS_Explore
Purpose, Input, Output, Runtime, Note
Purpose: Explore a graph G (directed or undirected) from a given vertex s using the breath-frist strategy.
Input: a graph G = (V, E) (directed or undirected) and a “source” vertex s in V
Output: an array dist such that dist[u] = distantce (in terms of #edges crossed from vertex s to vertex u
Runtime: O(n + m). AKA: O(|V| + |E|)
note: dist[u] = +inf if the vertex u is not reachable from s, and BFS doesn’t account for edge weights, it can only find the shortest path from s to u in terms of edge count.
dfs_explore
Purpose, input, output, runtime
Purpose: Explore a graph G (directed or undirected) from a given vertex v
Input: G=(V, E), v in Vertexes
Output: (For reachable nodes, -1 for the rest)
- prev[z]
- pre[z]
- post[z]
Runtime: O(m) AKA O(E), only iterating over edges.
Reverse Directed Graph
(Considered housekeeping not need to be black boxed)
Purpose, Input, output, runtime
Purpose: Reverse a given directed graph G
input: A driected Graph G=(V,E)
output: A reversed graph Gr = (Vr, Er)
runtime: O(n + m) AKA O(|V| + |E|)
KruskalMST
Purpose, Input, Output, Runtime
+Note
Purpose: Finds the minimum spanning tree over an input undirected connected graph. (Sorts edges and selects edges not traversing graph)
Input: an undirected graph G = (V,E) and a weight function w such that w(u,v) = weight of edge.
Output: a set of edges X, which is the minimum spannign tree of G.
Runtime: O(m log n) AKA: O(E log (v))
Note:If the graph is not connected, we get a minimum spanning forest.
Prim MST
Purpose, input, output, runtime, note
Purpose: Finds the minimum spanning tree G over an input undirected connected graph.
Input: An undirected graph G=(V,E) and a weight function w such that w(u,v) = weight of edge.
Ouptut: an array prev where prev[n] is the parent vertex number of vertex v. prev[root] = null by def.
runtime: O(m log(n)) AKA: O(E log (V))
Note: PRIM expects a connected graph. This is similar to dikjstra’s only instead of using the weight of the full path in the priority tree, it uses the weight of the solitary edge to build up on the frontier.
Dijkstra’s Single Source Shortest Path
Purpose, input, output, runtime, note
Purpose: Finds the shortest path to all nodes from a source s in a weighted graph G (directed or undirected) with non-negative weights.
Input: A graph G=(V,E) directed or undirected, a source vertex and edge weights.
Output: dist[v] shortest distance from s to v, and prev[v] = prev of v in the shortest path from s to v.
Runtime: O((n + m) log(n))
Note: if the graph is connected, M dominates n+m and our time is O(mlog(n))
Directed Acyclic Graph Single Shorted Path
Purpose, Input, output, runtime, note
Purpose: Find the shortest path and its length from the given source vertex s to all vertices v in V, in a directed acyclic weighted graph G=(V,E)
“Same as depth first search, topological sorting”
Input: a DAG G=(V,E), a source vertex s in V, and weights
Output: dist[v] the shortest distance from s to v and prev[v] = the prev of v in the shortest path from s to v.
Runtime: O(n + m)
Note: If it is not possible to reach a vertex u from s, then dist[u] = inf, and negative edges are allowed in this algorithm.
BellmanFord Single Source Shortest Path
Purpose, Input, Output, Runtime, Note
Purpose: Find the shortest path to all nodes from a source s in a weight graph. With postiive or negative weights. (CAPABLE OF FINDING SHORTEST PATHS WITH NEGATIVE WEIGHTS)
Input: A graph G=(V,E), a source vertex s in v, and edge weights
Output: Dist[v] shortest distance from s to v, prev[v] previous vertex in shortest path from s to v.
Runtime: O(nm)
Note: Normally runs in V-1 cycles, however runnin gone more cycle can help detect negative weighted cycles. Also only cycles reachable from s can be detected.
FloydWarshall All Point Shortest Path
Purpose, input, output, runtime, note
Purpose: Finds the shortest path for all pairs, with positive or negative edge weights.
Input: A graph G(V,E) directed or undirected and edge weights.
Output: dist[s,t]: the shortest distance from s to t. (dist is an nxn array
Runtime O(n3)
Can detect any negative cycles by checking distance from node to itself, if it is less than zero, there is a negative weight cycle.
Ford-Fulkerson (max flow/min cut)
Purpose, Input, output, runtime
Purpose: Finds the maximum st-flow in a flow network
Input: Flow Network G=(V,E) with capacities and source sink s & t
Output: Flow fe for e in Edges
Runtime: O(mC) where C is the size of the maximium flow.
Edmonds-Karp (max flow/min-cut)
purpose, input, output, runtime, note
Purpose: Finds the maximium st-flow in a flow network (a directed graph G=(V,E) with capacities
Input: Flow network G=(V,E) with capacties, a source, and a sink
Ouput: Flow fe for edges
Runtime: O(m2n)
Note: Same as F-F, but uses a BFS to find augmenting paths to give a runtime without the C.
What is a Valid Flow?
What is a Maximum Flow?
A Valid flow in a flow network is a flow in g that satisfies all capacity constraints for the edges. (E.g. fe <= C(E)
A Maximum flow is a flow of maximum value.
Let G = (V,E) be a flow network with source s, sink t and integer capacities. Suppose that we are given a maximum flow in G.
Suppose that the capacity of a single edge (u, v) ∈ E is increased by 1. Give an O(V + E) time algorithm to update the maximum flow.
Compose the residual graph on the original flow. Add a positive 1 capacity on the edge that has been increased. Using BFS, search for an augmenting path; if the path exists, we can update the flow, otherwise, the flow is unchanged. We only need to do this once, as the augmenting path, if it exists, increases the flow by 1, which is the maximum increase possible.
Let G = (V,E) be a flow network with source s, sink t and integer capacities. Suppose that we are given a maximum flow in G.
Suppose that the capacity of a single edge (u, v) ∈ E is decreased by 1. Give an O(V + E) time algorithm to update the maximum flow.
Again, compose the residual graph on the original flow. If the decreased edge was not at capacity (that is, it still has positive residual capacity), then we can decrease the edge capacity by one without affecting the maximum flow. If not, then we add one to the negative capacity on the edge, and look for an augmenting path in reverse (going from t to s instead of from s to t) which includes the decreased edge.
Suppose someone presents you with a solution to a max-flow problem on some network. Give a linear time algorithm to determine whether the solution does indeed give a maximum flow.
First, verify that the solution is a valid flow by comparing the flow on each edge to the capacity of each edge, for cost O(|E|). If the solution is a valid flow, then compose the residual graph (O(|E|)) and look for an augmenting path, which using BFS is O(|V | + |E|). The existence of an augmenting path would mean the solution is not a maximum flow.
Using Fermats Little Theorem how can we avoid testing every number 1 to P
for a single number
Prob that flt returns yes when N is prime is 1
Prob that flt returns yes when N is not prime is <= 1/2
So depending on the amount of certainty we pick k different a values, which gives us
1/2k as the probability flt returns yes when it is not prime.
What theorem would you use to solve:
wx - yx divisible by N
if
- N can be factored into two primes
- GCD(w,N) = 1 and GCD(y,N) = 1
Euler’s Theorem:
If N is the product of 2 prime numbers and GCD(a,n) = 1
Then A(p-1)(q-1) === 1 mod N
E.g. Is 41536 - 94824 divisible by 35
35 is product of 5 & 7 (two primes)
gcd(4,35) = 1
gcd(9,35) = 1
(p-1)(q-1) = (5-1) * (7-1) = 24
Z24 = 1 mod 35
1536/24 = 64 so 4(24*64) equals 1 mod 35
4824/24 = 201 so 9(24)(201) equals 1 mod 35
1 mod 35 = 1, 1 mod 35 = 1
1 -1 = 0, there is no left over so yes it is divisible!!!
What is the substitution rule for x and y
in modulo arithmetic in terms of x’ and y’
if
x = x’ (mod n) and y = y’ (mod n)
then
x + y === x’ + y’ (mod n)
and
xy === x’y’ (mod n)
10 * 15 (mod 4) =>
(10 (mod 4) * 15) (mod 4) -or-
(10 * 15 (mod 4)) (mod 4)-or-
(10 (mod 4) * 15 (mod4)) (mod 4)
Can you prove that if a mod b has an inverse then b mod a must also have an inverse?
Yes, a (mod b) only has an inverse if gcd (a,b) = 1
so the formula b (mod a) only has an inverse if gcd(b,a) = 1
gcd(a,b) and gcd(b,a) are the same formula.
Euler’s Theorem
For N = pq where p & q are prime,
For any Z where gcd(Z, N) = 1
Then Z(p-1)(q-1) is equivliant to 1 mod N
Multiply Algorithm
Time O(n2)
Multiply(x,y):
if y == 0: return 0
z = multiply(x, floor(y/2))
if y is even:
return 2z
else
return x + 2z
How can we use Fermats Little Theorem to prove a number is prime.
Fermat’s little theorem says:
If p is prime, then for every 1 <=a
a(p-1) === 1 (mod p)
So if we have a number P that we want to test for primality, then calculate
a(p-1) for every value a between 1 and p.
If they all equal 1 mod p, then it is prime.
(Note: There are some charmichael numbers like 561 which pass the test, but aren’t prime.
Euclid’s Rule
if x & y are positive integers x >= y,
then gcd(x,y) = gcd(x mod y, y)
How are keys created for RSA
- Pick two large prime numbers p and q
- calculate n = pq. (n is the modulus for pub and priv)
- Calculate totient of n .. (p-1)(q-1)
- Choose e (the public key exponent) so that the gcd(e, totient(n)) = 1. (can be small like 3, 5, 35)
- Calculate d to be the inverse of e mod totient(n)
- Use Extended-Euclid (e,n)
- The number that returns as the multiple (x) for e is the inverse.
- if it’s negative you need to mod it. (Basically add N to it until it is positive.)
- This number alone not (x * e) but the x is the inverse x Mod N.
Public Key = (n, e)
Private key = (d)
Note if you get a decryption key of 1, then your selection of e was not gcd 1 with totn.
ModExp Algorithm
Time O(n3)
ModExp(x, y, N):
if y = 0: return 1
Z = modexp(x, floor(y/2), N)
if y is even:
return z2 mod N
else
return x * z2 mod N
What is Euclid’s Algorithm for GCD
Time O(n3)
Euclid(a,b):
// a and b with a >= b >= 0
if b = 0: return a
return Euclid(b, a mod b)
b, a mod b =>
if x & y are positive
x >= y
then
gcd(x,y) is equal to
gcd(y, x mod y)
p is prime, q is prime:
N = pq
What is totient(N)
(p-1)(q-1)