Graph Flashcards

(44 cards)

1
Q

Fenwick tree - usage, complexity (create, update, search)

A

Usage: fast prefix sum

Space complexity: O(n)
Time complexity: create->O(nlogn), update all elements->O(logn), search->O(logn)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Binary tree - traversal methods (three) and how

Time complexity?

A

1) pre order (visit - left - right) (usual one)
2) in order (L-V-R)
3) post order (L-R-V)

Time complexity: O(n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Binary tree - searching time (balanced/unbalanced)

A

Balance: O(logn)
Unbalanced: O(n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Binary tree - insertion time complexity (balanced/unbalanced)

A

Balance: O(logn)
Unbalanced: O(n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Topological sort - time complexity

A

Time: O(v+e)

Where v is the number of vertices and e is the number of edges

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Topological sort - how does it work?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Dijkstra’s algorithm - restriction

A

It can only work when there is no negative distance on edges.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Dijkstra’s algorithm - usage

A

Given a source node

Find all shortest path from that node to every other node

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Dijkstra’s algorithm - space and time complexity

A

Time: O(elogv)
Space: O(e+v)
Where e is the number of edges and v is the number of vertices

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

DFS (cycle detection (directed graph)) - time complexity

A

Time: O(e+v)

Where e is the number of edges and v is the number of vertices

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

DFS (cycle detection (directed graph)) - how it works

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Edges list (graph representation) - space complexity

A

Space: O(v+e)

Where v is the number of vertices and e is the number of edges

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Edges list (graph representation) - how does it work?

A

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);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Edges list (graph representation) - time complexity of finding all adjacent node of given node

A

Time: O(n)

Where n is the size of edges list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Edges list (graph representation) - what is it?

A

I can’t believe you forgot about this important thing!

Go Google for photo.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Adjacency graph (graph representation) - space complexity

A

Space: O(v^2)

Where v is the number of vertices in the graph

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Adjacency graph (graph representation) - time complexity on finding adjacent node and check two node connectivity

A

1) O(v)

2) O(1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Graph representation - ways(3)

A

1) adjacency list - good
2) adjacency matrix - ok but consume space
3) vertices list- ok but time consuming on searching

19
Q

Disjoint set (cycle detection (undirected graph)) - how it works?

A

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

20
Q

Tree - root/node/leaf node/non-leaf node/ancestor/descendant/sibling/degree

A

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

21
Q

AVL tree (self balancing binary tree) - rotations (4)

A
  • RR -> rotate left
  • LL -> rotate right
  • LR -> rotate left and then right
  • RL -> rotate right and then left
22
Q

Binary tree - full / proper / strict binary tree

A
  • each node has 0 or 2 children
23
Q

Binary tree - complete binary tree

A
  • all level of node are completely filled (except last level)
  • node from the last level are filled from left to right
24
Q

Binary tree - perfect binary tree

A
  • all leaf node are on the same level

- all internal node has two children

25
Binary tree - degenerated binary tree
- all node has only 1 child
26
AVL tree (self balancing binary tree) - priorities
- the balancing factor of each node must be {-1,0,1} | - balancing factor = height of left subtree - height of right subtree
27
Red black tree (self balancing binary tree) - priorities
- root node has to be black - there cannot be two consecutive red node - the number of black node of every path from the node to (the node after leaf node = null) should be the same
28
Red black tree (self balancing binary tree) - insertion
if (tree == empty) { create a root node with color black; } else { follow the instructions of binary insertion to create a node with red colour; if (the parent of the created node == black) { return; } else { if (the sibling node of the parent of the created node == red) { the sibling node of the parent of the created node = black; the parent of the created node = black; if (the parent is above two node != root node) { the parent is above two node = red; recheck(); } } else { rotation(); recolour(); } } }
29
Binary tree - deletion (with no child node / one child node / two children node)
No - just delete it One - replace the node with its child and delete the child node (recursively call until reach leaf node) Two - choose inorder predecessor / inorder successor and replace it Predecessor = the largest node from the left tree Successor = the smaller node from the right tree
30
Spanning tree - what is spanning tree
- connected tree - no cycle - have same vertices as the original graph - have v-1 edges
31
Prim’s algorithm (find minimum spanning tree) - how it works
1) remove loop and parallel 2) choose a random node to start with 3) choose the minimum edges connected to visited node 4) choose (|v|-1) edges and done
32
Kruskals algorithm (find minimum spanning tree) - how it works
1) remove loop and parallel 2) create a list of edges with given weight in increasing order 3) choose edges and form a graph without any cycle 4) choose (|v|-1) edges
33
Connected component - how to find
The number of call (dfs / bfs) to the graph = ans
34
Bellman Ford algorithm (single source shortest path) - time complexity
Time: O(ev) | Where e is the number of edges and v is the number of vertices
35
Bellman Ford algorithm (single source shortest path) - drawback
It cannot work on negative weighted cycle
36
Bellman Ford algorithm (single source shortest path) - how it works
1) list all the edges 2) relax all the edges for (|v|-1) times Relax: d[u] = min(d[u], d[v]+c(u, v))
37
Floyd Warshell algorithm (all pair shortest path) - time and space complexity
Time - O(v^3) Space - O(v^3) Where v is the number of vertices
38
Floyd Warshell algorithm (all pair shortest path) - formula
Dk[i, j] = min(D(k-1)[i, k]+D(k-1)[k, j], D(k-1)[i, j])
39
Segment tree - usage
Perform range quiet and range update in O(logn) time
40
Segment tree - how to implement
1) create an array with 2n size 2) all leaf node are the original data 3) internal node will be the sum(or can be other with different situation) of theirs children
41
Segment tree - update time (point/range)
O(logn)
42
Last propagation (segment tree) - how
1) keep a tree with same size of the segment tree with 0 2) every time when the update range is not the leaf node - lazy tree will keep an pending update of the children 3) when every time reaches the node, the same node in lazy tree need to be checked 4) if 0 = no update, else update it with the (number x range) and pass the update to its children
43
Strongly connected component - ?
Only exists in directed graph | When two arbitrary node is selected, they will have a path
44
Tree - properties
Having only one connected component | Edges +1 = vertices