Graph Flashcards
Fenwick tree - usage, complexity (create, update, search)
Usage: fast prefix sum
Space complexity: O(n)
Time complexity: create->O(nlogn), update all elements->O(logn), search->O(logn)
Binary tree - traversal methods (three) and how
Time complexity?
1) pre order (visit - left - right) (usual one)
2) in order (L-V-R)
3) post order (L-R-V)
Time complexity: O(n)
Binary tree - searching time (balanced/unbalanced)
Balance: O(logn)
Unbalanced: O(n)
Binary tree - insertion time complexity (balanced/unbalanced)
Balance: O(logn)
Unbalanced: O(n)
Topological sort - time complexity
Time: O(v+e)
Where v is the number of vertices and e is the number of edges
Topological sort - how does it work?
Prepare: set, stack
1) choose a random node
2) Mark the node as visited and put it into the set
3) explore its children and continue with step 2
4) if the node has no children, put it into the stack and go back
5) the stack order from top to the bottom is the final state of topological sort
Dijkstra’s algorithm - restriction
It can only work when there is no negative distance on edges.
Dijkstra’s algorithm - usage
Given a source node
Find all shortest path from that node to every other node
Dijkstra’s algorithm - space and time complexity
Time: O(elogv)
Space: O(e+v)
Where e is the number of edges and v is the number of vertices
DFS (cycle detection (directed graph)) - time complexity
Time: O(e+v)
Where e is the number of edges and v is the number of vertices
DFS (cycle detection (directed graph)) - how it works
Prepare: white set(not visit), grey set(visiting), black set(visited)
1) randomly pick a node from white set
2) do dfs while putting passed node into great set
3) if dfs find a node who is going to visit and also in the grey set, it is found
4) if not put the node in the black set so we are not going to visit again
Edges list (graph representation) - space complexity
Space: O(v+e)
Where v is the number of vertices and e is the number of edges
Edges list (graph representation) - how does it work?
1) create a class with starting and ending vertices and optionally adding value
2) store in a list..
struct edge { Int start; Int end; Int value; };
Vector list(n);
Edges list (graph representation) - time complexity of finding all adjacent node of given node
Time: O(n)
Where n is the size of edges list
Edges list (graph representation) - what is it?
I can’t believe you forgot about this important thing!
Go Google for photo.
Adjacency graph (graph representation) - space complexity
Space: O(v^2)
Where v is the number of vertices in the graph
Adjacency graph (graph representation) - time complexity on finding adjacent node and check two node connectivity
1) O(v)
2) O(1)