5 Algorithms Graph
5 Algorithms Graph
5 Algorithms Graph
What is Graph
- A tree which at least one cycle
V(G) = {1,2,3,4}
E(G) = {(1,2),(1,3),(3,3,),(3,4),(4,1)}
Graph Representation
- Adjacency Matrix representation
- Adjacency List
- Adjacency Multilist representation
Graph Representation
Graph Representation
- Adjacency Matrix representation
Graph Representation
- Adjacency List
Question ?
What graph topics should I study in order to be
adequately prepared for a Google Software Engineer
interview?
Depth-first Search, Breadth-first Search, and Dijkstra's algorithm are obviously required.
Would it be worthwhile to also study algorithms for minimum spanning trees, maximum network flows, bipartite matching, coloring
algorithms, topological sorting, etc...? How good should I be at implementing these algorithms from scratch?
Disclaimer
The previous suggestion is the view of the specific
person.
Use your own judgement on what to study.
You are responsible for your career.
If extra knowledge does not confuse you, it does not
harm.
Explanation
visit(root);
Do processing needed
root.visited = true;
Processing is done
if (n.visited == false) {
Example
BFS Explanation
When we pick a node we insert all the
children at the rear end of the queue.
Activity
What will be the
DFS order ?
What will be the
BFS order ?
Operations
What are the possible operations we can do on the states
of the water jug ?
Operations
What are the possible operations we can do on the states
of the water jug ?
When we pour from one jug to another
Either we fill the other jug to its capacity
Or we empty to first jug of whatever it had
Problem 1
1. Implement a function to check if a binary tree is
balanced. For the purposes of this question, a balanced
tree is defined to be a tree such that the heights of the
two subtrees of any node never differ by more than one.
Hint to Problem 1
During DFS on each node get the height and also check if
each sub left tree and right tree is balanced. If any sub tree
is not return -1 to indicate that a subtree is not balanced.
Problem 2
On a chessboard, a knight is placed at a random position
and a rook is placed at another random position. We need
to determine whether the knight can move over rook and
how many minimum steps will it take to reach there.
Hint to Problem 2
Use BFS
Use int[8][8] to mark the fact that the position is visited
Problem 3
3. Design an algorithm and write code to find the first common ancestor of two
nodes in a binary tree. Avoid storing additional nodes in a data structure.
Hint to Problem 3
A node is the LCA if
its left child reports it as /ancestor of one node
its right child reports it as /ancestor of other node
Or
If it itself is one node
the left or right child contains other node
Do a post order DFS and let every node determine whether it is a LCA node or
not. Pass to the parent as /ancestor information for both first node and second
node.
Problem 4
4. You are given a binary tree in which each node contains a value. Design an
algorithm to print all paths which sum to a given value. The path does not need
to start or end at the root or a leaf.
Hint to Problem 4
Do DFS and we pass the function the full path from root to
node & ask, "Does this node complete a path with the
sum?
Problem 5
5. Given a binary tree, design an algorithm which creates a linked list of all the
nodes at each depth (e.g., if you have a tree with depth D, you'll have D linked
lists).
Hints to Problem 5
Use a linked list of linked lists to store the result
You can actually use either DFS or BFS for this purpose