Exam2-Algorithms Flashcards
Depth First Search
- Input
- Output
- Arrays available to access
- Graph types applicable
- Runtime
- Input
- G=(V,E)
- Output
- topological sort on a DAG
- Arrays available
- prev, visited
- pre, post on DIRECTED graphs
- ccnum on UNDIRECTED GRAPHS
- Graph types
- Unweighted
- Undirected and Directed
- Runtime
- O(n+m)
Explore
- Input
- Output
- Arrays available to access
- Graph types applicable
- Runtime
- Input
- G=(V,E)
- Start vertex v in V
- Output
- visited[], which is set to True for all vertices reachable from v
- Arrays available
- ccnum[]
- visited[]
- Graph types
- Unweighted
- Undirected and Directed
- Runtime
- O(n+m)
Breadth First Search
- Input
- Output
- Arrays available to access
- Graph types applicable
- Runtime
- Input
- G=(V,E)
- Start vertex v in V
- Output
- dist[]
- For all vertices u reachable from v, dist[u] is the shortest path distance from v to u. If no such path, infinity.
- dist[]
- Arrays available
- prev[]
- This provides the immediate parent vertex of a vertex. If there is no immediate parent, the array has Null/0.
- prev[]
- Graph types
- Unweighted
- Undirected and Directed
- Runtime
- O(n+m)
Dijkstra’s
- Input
- Output
- Arrays available to access
- Graph types applicable
- Runtime
- Input
- G=(V,E)
- Start vertex v in V
- Output
- dist[]
- For all vertices u reachable from v, dist[u] is the shortest path distance from v to u. If no such path, infinity.
- dist[]
- Arrays available
- prev[]
- This provides the immediate parent vertex of a vertex. If there is no immediate parent, the array has Null/0.
- prev[]
- Graph types
- Weighted
- Undirected and Directed
- No negative weights
- Runtime
- O((n+m)log n)
- O((m log n) if the graph is strongly connected
Bellman-Ford
- Input
- Output
- Arrays available to access
- Graph types applicable
- Runtime
Bellman-Ford is used to derive the shortest path from s to t using only a single vertex s.
- Input
- G=(V,E)
- Start vertex s
- Output
- Shortest path from s to all other vertices
- Arrays available
- Detect negative weight cycles
- Graph types
- Weighted
- Undirected and Directed
- Can negative weights
- Runtime
- O(nm)
Floyd-Warshall
- Input
- Output
- Arrays available to access
- Graph types applicable
- Runtime
FW is primarily used to find the shortest path from ALL nodes to all other nodes where negative weights are allowed.
- Input
- G=(V,E)
- Output
- Shortest path from all vertices to all other vertices
- Arrays available
- We can detect negative weight cycles by checking the diagonals (T[n, i, i])
- Graph types
- Weighted
- Undirected and Directed
- Can negative weights
- Runtime
- O(n^3)
Strongly-Connected Components
- Input
- Output
- Arrays available to access
- Graph types applicable
- Runtime
- Input
- G=(V,E)
- Output
- meta-graph (DAG) that contains the connected components, represented as an adjacency list
- Topological sorting of the meta-graph
- With a source SCC first and a sink SCC last
- Arrays available
- adjacency list of meta graph in topological order
- ccnum
- Graph types
- Directed
- Runtime
- O(n+m)
Kruskal’s
- Input
- Output
- Graph types applicable
- Runtime
Kruskal’s algorithm is used to find a minimum spanning tree of a connected, undirected graph. The strategy here is all edge weights are sorted, and the algorithm chooses the lowest edge weight to process.
- Input
- G(V,E) - connected, undirected
- Output
- MST
- Graph types applicable
- connected, undirected, weighted
- Runtime
- O(m log n)
Prim’s
- Input
- Output
- Graph types applicable
- Runtime
Prim’s algorithm is used to find a minimum spanning tree of a connected, undirected graph. The strategy here is a start vertex is chosen, and then Dijkstra’s is used to choose the lowest weight, unvisited neighbors for exploration.
- Input
- G(V,E) - connected, undirected
- Output
- MST
- Graph types applicable
- connected, undirected, weighted
- Runtime
- O(m log n)
Ford Fulkerson
- Input
- Output
- Arrays available
- Graph types applicable
- Runtime
A greedy algorithm to find max flow on networks. The algorithm continually sends flow along paths from the source (starting node) to the sink (end node), provided there is available capacity on all edges involved. This flow continues until no further augmenting paths with available capacity are detected.
- Input
- G = (V, E)
- Flow capacity c
- Source node s
- Sink node t
- Output:
- Max flow
- Arrays available
- Residual network G_r
- Max flow of G
- Graph types applicable
- Directed, weighted
- Runtime
- O(C * m)
- C is the maximum flow in the network
- m is the number of edges
Edmonds Karp
- Input
- Output
- Arrays available
- Graph types applicable
- Runtime
The Edmonds-Karp algorithm is utilized to determine the maximum flow in a network. This is analogous to the Ford-Fulkerson method but with one distinct difference: the order of search for finding an augmenting path must involve the shortest path with available capacity (BFS for G where all edge weights equal 1).
- Input
- G = (V, E)
- Flow capacity c
- Source node s
- Sink node t
- Output:
- Max flow
- Arrays available
- Residual network G_r
- Max flow of G
- Graph types applicable
- Directed, weighted
- Runtime
- O(nm^2)
RSA Procedure
- Choose primes p and q. Let N = pq
- Find e where gcd(e, (p-1)(q-1)) = 1
- (p-1)(q-1) is used because of Fermat’s Little Theorem z^((p-1)(q-1)) === 1 mod N
- e is part of the public key used to encrypt
- Let d === e^-1 mod (p-1)(q-1)
- Use Extended Euclid to find d
- This is the private key used to decrypt
- Publish public key (N,e)
- Encrypt message m: y === m^e mod N
- Decrypt y: m = y^e mod N
If graph G has more than |V| − 1 edges, and there is a unique heaviest edge, then this edge cannot be part of a minimum spanning tree.
False. Consider a graph with two connected components and the heaviest edge is the only one connecting them.
If G has a cycle with a unique heaviest edge e, then e cannot be part of any MST.
True. Consider the order in which edges would be processed by Kruskal’s.
Let e be any edge of minimum weight in G. Then e must be part of some MST.
True. e will belong to some MST if running Kruskal’s.