Professional Documents
Culture Documents
Introduction of ourselves
Akshay
Padmanabha
Course VI, VIII,
Class of 2017
Raja Selvakumar
Course X, Class of
2017
What is an algorithm?
Finite and precise sequence of instructions
for solving a problem
Minimizing costs of operation: time,
resources, energy
Examples: tying a shoe, determining
maximum in list of numbers, getting
directions to classes for SPLASH
Topics in Algorithms
Graph
Searc
h
BFS
Data
Structur
es
DFS
BS
T
Hash
Tables
Graph Search
List of all adjacent nodes adjacency matrix
B
F
A -> E,D,G
D -> B,A
B -> D,E,F
E -> A,B
F -> B,C
C -> F
G -> A
No
Pruning
B
F
*Paths are entered in reverse alphabetic order (i.e.
A, AG,AE,AD)
No
Pruning
A
D
With
No
Pruning
B
F
*Paths are entered in reverse alphabetic order (i.e.
A, AG,AE,AD)
With
No
Pruning
B
F
*Paths are entered in reverse alphabetic order (i.e.
A, AG,AE,AD)
1
1 5
6
2
1
2
1
7
1
0
1
1 4
3 1
1
9
8
2
2
6
2
1
2
5
2
2
6
5
2
3
Pruning
Prevents cycles in graph- no revisiting of nodes!
Topics in Algorithms
Graph
Searc
h
BFS
Data
Structur
es
DFS
BS
T
Hash
Tables
So what is a BST?
A Binary Search Tree (BST) is an ordered binary tree
What does this mean?
The value at a node is larger than its left child and
smaller than its right child
Inserting an Element
How do we insert an element into a BST?
If the element is greater than the node, we move to the
right child
Otherwise, we move to the left child
9
2
15
Balanced BST
What is a balanced BST?
If there are n elements in the BST, the height of a
balanced BST is at most log2 n
Simpler terms: most nodes have 2 children
1
2
4
8
=
For15
16:
log2
16 =
4
(new
heigh
Small Example
Suppose we have an empty array of size 5: [ - , - , - , - , - ]
Say the hash function we are using is put the object at the
location specified by the value of the object (looping if
necessary)
For example, adding 2: [ - , 2 , - , - , - ]
Adding 9: [ - , - , - , 9 , - ]
Take this empty array and this hash function and use this as
a hash table to put the following values: 5, 7, 14, 1
[1,7,-,9,5]
Retrieving Values
Lets take the final array from before: [ 1 , 7 , - , 9 , 5 ]
How do we check to see if a value is in the array?
Let us see if 7 is in the array.
First, we take 7 and apply our hash function to it. This lets us
know that 7 should be in slot 2. We then check slot 2 in the
array and we see that in fact 7 IS in the array! This only took
looking at one slot in the array.
Let us now try finding 2 in the array. We apply our hash
function and find that we need to look at slot 2. Since we do
not see a 2 in the slot, we know that 2 is NOT in the array.
Again, this only took looking at one slot in the array.
Collisions
Lets take the final array from before: [ 1 , 7 , - , 9 , 5 ]
What if we try to put 2?
This spot is taken! This is called a collision
What do we do?
We can put the value in the next available open slot (this is
called open addressing) :
[1,7,2,9,5]
However, looking for an element can take up to n look-ups
in the array (where there are n elements in the array),
which is very bad
Chaining
What are other ways we can handle collisions?
We can use chaining!
Let us use the same array as before ([ 1 , 7 , - , 9 , 5 ])
and try to add 2 again.
This time, we keep track of all the elements hashed into
the slot:
[ 1 , [7, 2] , - , 9 , 5 ]
Now, the chance that we have to use more than one
lookup is smaller than before, as long as we are using
good hash functions!
Deleting Elements
How do we delete elements?
Let us use the same array in the chaining example: [ 1 , [7,
2] , - , 9 , 5 ]
How do we delete 9? First we must see if 9 is in the array
(using the hashing function) and then if it exists, we replace
it with a -:
[ 1 , [7, 2] , - , - , 5 ]
However, if we try to delete 7, we should actually delete it,
rather than replacing it with a -: [ 1 , 2 , - , 9 , 5 ]
We see that this works better with chaining rather than open
addressing: we need a flag for when an element is deleted!
[ 1 , 7 , 2 , 9 , 5 ] becomes [ 1 , F , 2 , 9 , 5 ]
Interview Question 1
You are given two arrays. Find all the elements that are
in both arrays.
We put all the elements of the first array in a hash table.
Then, we see if any of the elements in the second array
are in the hash table.
We see that this algorithm is really fast because it goes
through each element only once.
Interview Question 2
You are given a large graph with a starting node. How
would you find all paths from the starting node to every
node 2 edges away from the starting node?
We use BFS from the starting node and stop it when we
are two edges away from the starting node.
We see that this algorithm is really fast (unlike BFS)
because it does not travel down the whole depth of the
graph.
Interview Question 3
Find the number of times every element appears in an
array.
We create a hash table. If the element is not in the hash
table, we add it to the hash table with value 1. If the
element is in the hash table, we increment the value for
that key by 1. Finally, we go through each key in the hash
table and return the key-value pair, which represents the
number of times each element appears in the array.
We see that this algorithm is really fast because goes
through each element in the array only once.