You are on page 1of 29

Algorithms

(of, by, and for)


Dummies
Akshay Padmanabha
Raja Selvakumar
MIT ESP- Spark 2015

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

Graph Search - BFS


BFS Breadth First Search

No
Pruning

Expands all surrounding nodes concurrently to find desired path

B
F
*Paths are entered in reverse alphabetic order (i.e.
A, AG,AE,AD)

Path from A -> C:


->A
->AG, AE, AD
->AG, AEB, ADB
->AEBF, AEBD,
ADBF, ADBE
->AEBFC

Graph Search - DFS


DFS Depth First Search

No
Pruning

Expands first node entered until path terminates

A
D

Path from A -> C:


G
->A
->AG,was
AE, AD
Which method
->AG, AEB
C
better (in->AEBF,
this AEBD
->AEBFC
example)?
F

*Paths are entered in reverse alphabetic order (i.e.


A, AG,AE,AD)

Graph Search - BFS


BFS Breadth First Search

With
No
Pruning

Expands all surrounding nodes concurrently to find desired path

B
F
*Paths are entered in reverse alphabetic order (i.e.
A, AG,AE,AD)

Find all nodes:


->A
->AG, AE, AD
->AG, AEB, ADB
->AEBF, AEBD,
AEBD
ADBF,
ADBE
->AEBFC
CYC
->AEBFC,
->AEBFC AEBDA,
LE
ADBFC, ADBEA
->AEBFC, AEDBAG,

Graph Search - DFS

With
No
Pruning

DFS Depth First Search

Expands first node entered until path terminates

B
F
*Paths are entered in reverse alphabetic order (i.e.
A, AG,AE,AD)

Find all nodes:


->A
->AG, AE, AD
->AG, AEBAD
->AEBF, AEBD
B
->AEBFC
AEBD
->AEBFC
A
->AEBDAG,
AEBDAE

Graph Search Discussion Question


Consider that Akshay has returned from his travels abroad to
Kendall Square. He wants to return back to main campus but as
he is tired from his journey, he wants to take the shortest path
back. Propose a path for Akshay to take using BFS and DFS.
Assume pruning is in place
Consider that paths are inserted in numerical order

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

Graph Search- Summary


BFS Breadth First Search
Prefer breadth over depth, expand all attached nodes
concurrently
Useful for finding nodes small distances away

DFS Depth First Search


Prefer depth over breadth, expand first node all the way
until breaks
Useful for finding nodes further away

Pruning
Prevents cycles in graph- no revisiting of nodes!

Topics in Algorithms
Graph
Searc
h
BFS

Data
Structur
es
DFS

BS
T

Hash
Tables

What is a binary tree?


BST stands for a Binary Search Tree
What is a binary tree?
A tree where each node has at most 2 children:
What is a tree?
A graph with no cycles!

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

Cool Things about BSTs Finding an


Element
If there are n elements in a balanced BST, at most how
many elements do we need to visit to find if the element is
in the BST?
log2 n!! (we have to travel the height of the tree worst-case)

Cool Things about BSTs Sorting a


List
How do we get a sorted list
from a BST?
In-order traversal!
Why does this work?
It works because the left child is
always less than the parent
node, and the right child is
always greater!
Thus, BSTs can be used for
sorting!

What is a Hash Table?


Say we have a (key,
value) pair:
For example, (John, 1),
(Lisa, 7), etc.

How do we put these


keys into a structure so
that we can retrieve
these keys easily?
Use a hash table!

What is a Hash Function?


A hash table uses a list
of buckets
These buckets is
simply an array of
objects
A hash function takes a
key and puts it into a
slot in the array

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.

You might also like