More graph Algos Flashcards
Forest
is an undirected, disconnected, acyclic graph. In other words, a disjoint collection of trees
Kruskal’s algo for a connected (+ edge weighted + undirected) graph does what?
Finds a minimum spanning tree
Is kruskals greedy or Non greedy
Greedy
What is a spanning tree?
(Pertains to undirected graphs). a subgraph that is a tree which includes all of the vertices of The graph
Prim’s algo benefits vs other MST algos
for graphs that are sufficiently dense, Prim’s algorithm can be made to run in linear time, meeting or improving the time bounds for other algorithms.
List the three basic greedy algorithms to find an MST
Prim’s (Jarnik’s), Kruskal’s, Borůvka’s (Sollin’s) algorithms
Kruskals
create a forest F (a set of trees), where each vertex in the graph is a separate tree
create a set S containing all the edges in the graph
while S is nonempty and F is not yet spanning
remove an edge with minimum weight from S
if the removed edge connects two different trees then add it to the forest F, combining two trees into a single tree
At the termination of the algorithm, the forest forms a minimum spanning forest of the graph. If the graph is connected, the forest has a single component and forms a minimum spanning tree.
Topological sort definition
a linear ordering of a graph’s vertices such that for every directed edge uv from vertex u to vertex v, u comes before v in the ordering.
Canonical use case of topological sort
in scheduling a sequence of jobs or tasks based on their dependencies. The jobs are represented by vertices, and there is an edge from x to y if job x must be completed before job y can be started (for example, when washing clothes, the washing machine must finish before we put the clothes in the dryer)
List names of Topological sort implementations - Kahn, Tarjan’s DFS solution, PRAM with adjacency matrix squaring, parallelized Khan with a distributed memory machine
Kahn, Tarjan (DFS), adjacency matrix squaring on a PRAM, parallelized Kahn on a distributed memory machine
High level how to quickly compute shortest path thru weighted directed a cyclic graph
Use topological sort
Low level implementation of finding shortest paths on directed acyclic weighted graph
…
When are topological ordering a impossible
When there are cycles
DAG
Directed acyclic graph
Known Time complexity of finding a topological sorting of a DAG
O(n)
FAS stands for
Feedback Arc Set
Feedback Arc set is aka
Feedback Edge Set
Feedback arc set
set of edges which, when removed from the graph, leaves an acyclic graph. Put another way, it is a set containing at least one edge of every cycle in the graph.
What algorithm do you use to find shortest path from a source to all vertices?
Djikstra’s
Algo name for shortest path from every vertex to every other vertex
Floyd Marshall
Memory complexity of sum of length of all adjacency lists for directed graph vs undirected graph
|E| vs 2|E|
For both directed and undirected graphs the memory complexity of the adjacency list representation is
O(V + E)
disadvantage of adjacency-list representation for edges
there is no quicker way to determine if a given edge (u, v) is present in the graph than to search for v in the adjacency list Adj[u]. which worst case could be O(V) if u is connected to all vertices
List and compare two alternatives to adjacency lists to represent edges
1) have Adj[u] be a hash table containing the vertices v for which (u, v) ∈ E. Expected time is O(1). Worst case time is O(V) (if you get a lot of hash collisions (but is that really even likely???)). 2) have Adj[u] be an array that you sort and then during look-up you perform binary search for an expected and worst case time of O(lg V)`
Djikstra binary heap implementation time complexity
Theta((|E|+|V|)log|V|) time complexity,
Djikstras does what?
Finds the shortest path to every node in the (weighted-edge) graph from a single source
Bellman Ford does what?
Similar to Dijkstra’s algorithm, the Bellman-Ford algorithm 1)works to find the shortest path between a given node and all other nodes in the graph. Though it is 2)slower than the former, Bellman-Ford makes up for its a disadvantage with its versatility. Unlike Dijkstra’s algorithm, Bellman-Ford is capable of 3) handling graphs in which some of the edge weights are negative.
4) It’s important to note that if there is a negative cycle – in which the edges sum to a negative value – in the graph, then there is no shortest or cheapest path. Meaning the algorithm is prevented from being able to find the correct route since it terminates on a negative cycle. 5)Bellman-Ford is able to detect negative cycles and report on their existence.
Floyd Marshal does what?
The Floyd-Warshall stands out in that unlike djikstra and bellman Ford it is not a single-source algorithm. Meaning, it calculates the shortest distance between every pair of nodes in the graph, rather than only calculating from a single node
What is Floyd marshal very useful for?
when it comes to generating routes for multi-stop trips as it calculates the shortest path between all the relevant nodes. For this reason, many route planning software’ will utilize this algorithm as it will provide you with the most optimized route from any given location. Therefore, no matter where you currently are, Floyd-Warshall will determine the fastest way to get to any other node on the graph.
Johnson’s algorithm works best when?
With sparse graphs
Can Floyd warshal detect the presence of a negative weight cycle ?
Yes