Graph Flashcards
(44 cards)
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)
Graph representation - ways(3)
1) adjacency list - good
2) adjacency matrix - ok but consume space
3) vertices list- ok but time consuming on searching
Disjoint set (cycle detection (undirected graph)) - how it works?
1) create a set for every element
2) start union two set by edges
3) if their representative is not the same, combine them
Else, that edge make a cycle on the graph
Tree - root/node/leaf node/non-leaf node/ancestor/descendant/sibling/degree
Root - top of the tree/has no parent
Node - all other node except root
Leaf node - they don’t have child node
Non-leaf node - they have child node
Ancestor - all node from root to the given node (exclude given node)
Descendant - all node from given node to leaf node (exclude given node)
Sibling - two node having the same parent is called sibling
Degree - number of leaf node to the given node
AVL tree (self balancing binary tree) - rotations (4)
- RR -> rotate left
- LL -> rotate right
- LR -> rotate left and then right
- RL -> rotate right and then left
Binary tree - full / proper / strict binary tree
- each node has 0 or 2 children
Binary tree - complete binary tree
- all level of node are completely filled (except last level)
- node from the last level are filled from left to right
Binary tree - perfect binary tree
- all leaf node are on the same level
- all internal node has two children