App. Algorithms Summary
App. Algorithms Summary
coding interview
solvesproblems efficiently
hashing cryptography
is analgorithm
what
set ofwelldefined instructions in afinite sequence solvea problem
to
Qualities
ofagoodalgorithm
input outputarewelld efined
eachstep isambiguousandclear
shouldbeeffective
structuresandalgorithms
Data
formatof storingandmanaging information
eachformat hasprosandcons
Guessing Game
oneperson thinksof a number between n
roo
Foreveryguessthepersontens low
too toonigncorrect
Linearsearch
startw ith a a s antheway to 99
up
itthenumberwas a itwould takeaaguesses
Binary search
startwith soand eliminatehaltthenumbers
nextguess isinthemiddle oftheremaining numbers and eliminating haltthenumbers
Big O
snowsthesteps ortimeincreasesw ithan increaseofthedata
itanalgorithmtakesthesamet ime itdataisdoublethealgorithmruns with ons
itthetimeneededdoubledthealgorithmrunswithlineartime
timewhentimeincrease logarithmically
log
considers aworst casemeasure
Recrusion
processtodefinetheproblem in asimplerversionofi tself
parts oftherecrusive algorithm
Basecase
worktowardsBasecase
recrusivecan
PROS CONS
complexitycanbereduced many problemscan'tbereduced to easiersubproblems
easilyusedformultiprocessing systems
maysolvesamesubproblem multipletimesas solutionisnotstored
subproblemscanbesolvedin different eras
Efficientuseofmemory
Stack v s Queue
Stack
similarto apileofplateskeptontop
ofeachother
pushputanewplateontop
pop removethetopplate
toremovethebottomplateanotherplates havetoberemovedfirst
Queue
firstinfirstout Fito
Priority Queue
highestvalue highestpriority
firstinfirstoutr ule
valuesareremovedbased onpriority
priority queueapplications
Dijkstra's algorithm
datacompression in muttmancode
complete
binary tree satisfying the neapproperty
completebinary tree
annodesareasfarleftaspossible
childnodesarealwayslarger
Heapity
process of
creating a neapdatastructure froma binarytree
aeque
Linked list
using
arraydata is stored in sequence
conwhenadatapointisinsertinthemiddleandata afterwards orbeforehastobesnit tea
con it space runsout hastobe completing copied toanotherlocationin the memory
a rray
solution tixsize expensive itnotneeded
array
groupoff riendssitting in acinema
itoneleaves
manyotherfriendshavetoshift onebyone
t heirs eat
itanadditionalf riendarrivestheirisnoavailable seat
anfriendshavetostand andlookforanewrowwithenoughtreeseats
up
seriesof connected nodes
ate achnodethedata isstoredwiththeaddress ofthenextn ode
accessfinale lementanelementshavetobelookedatfirst
list
Linked applications
Dynamic memoryallocation
implementedinstackandqueue
undofunctionalityofsoftware
mashtables
element
Aaa Delete element
tobeginning tobeginning
storedatainnewnode pointh eadtosecond node
changenextofnewnodetopointto head
changeheadtopointoncreatednode
toend to end
storedatainnewnode goto second lastelement
set next to nun
gotofinaln ode
changenexto fl ast anode
n odetorecentlycreate
tomiddle tomiddle
storedatainnewnode
go to elementbeforeelementtobedeleted
Hash Table
data structure thatrepresents data inthe form of value pairs
key
each to a data in a hashtable
key index is mapped value
Limitations of Directaddresstable
valuesofthe shouldbesmall
key
notsmall reservestoomuchspace
sizelimitof
arraymaybe a restriction
Hash table
Limitations of hashtable
goodhashfunctioncanreduce thenumberofcollisions
reduces rangeoftheindex
Collision resolution
bychaining
no elements nun
functions
Hash
goodhashfunctionhas
consistentnashfunctionmapssameinputeach t imetosameindex
Division method
it k is
keyand misthesizeofthehashtablecalculatedas hers a modm
example m no r na narkmemodno a
Multiplication method
n in floorim in a moan
example m 28 256 a
set ninescanseal not
openaddressing
collisionresolving technique
itthecurrentpositionisoccupiedthenextpossible isdetermined
indicationvariable isneeded it a positionistreeagain
openaddressingmethods
Linear probingcollision isresolved checkingthenextslot
by
itcollisionoccursatnineonthenninn ischecked
cluster of adjacentslotsistinea
whenadding anewelementtheentirec lustermustbetraversed
Quadraticprobingsimilar to linearp robing a notherslotischosen
spacingin betweenslots ischosen
bya function herit thin t ai t c illmodmforsomeconstants a c o
Doublehashing it collisionoccursanothernashfunctioncanbeused tocalculatethenexts lotherit minitin in modm
tableapplication
Hash
constantt imelookupandinsertionisrequired
cryptography
indexingdatais required
mobilephone boon for lookups
Sorting
overviewcomplexity
howquicklytheruntimegrows
relativetotheinput n
astheinputg etsarbitrarily large n es
Bubble sort
startfromfirsti ndexandcomparewiththesecondelement
bothelementsareswayed if firstelementisgreaterthansecondelement
repeatprocessuntilfinalelement
largestelement isnowattheend
Selection sort
Insertionsort
similartosortingcards acardgame
in
assumefirstc ardissorted
merge sort
divideandconqueralgorithm
problemis dividedinto sub
problems
whenthesolution ineachsubproblemisreadytheresultsfromthesubproblems are combined
Quicksort
basedondivideandconquerapproach splitting
arraysintosub a rrays
Countingsort
works
onlyif natural numbers havetobesorted
Datastructure
Tree
nonlinear hierarchicaldatastructure
consistson nodes connected edges
by
graphwithoutcircles
quickeraccesstothedata
linear datastructures
arrays
tinnedlists storedatainasequence
stack
queue
terminologies
nodeentitycontaininga orvalue andpointers to itschild nodes
key
finaln odesofeachpathareseatnodesexternaln odes
I don'tcontain apointer tochildnodes
nodewitha child internaln ode
edgelinkbetweentwonodes
roottopmostnodeof atree
heightofnodenumberofsagestothedeepestl eaf
depthnumberofcages fromtheroot tothenode
heightoftreedepthofthedeepestn ode
degreeofnode numberof branchesfromthatn ode
forest connection of disjointtrees
canbecreatedbycuttingtherooto f atree
2 3 2 3
a s a s
typesoftree
binary
binarysearch
a
avitree
B tree
applications
Tree
traversal
tree
specificn odehastobereached
providestheaccess totherequiera nodeinthetree
hierarchicaldatastructurecanbetraversed indifferentways
Typesoftraversal
in
o rdertraversal preorder traversal
visitannodesonleftsubtree visitrootnode visitan nodes in left sunrere
visitrootnode visitan nodes in left sunrere visitan nodes in rightsubtree
visita nnodes inrightsubtree m mmmm visitrootnode
Binary tree
each parentnodehas at most two children
full
binary tree everynodehaseither two or no children
perfectbinarytreeeveryinternalnode has exactly twochildnodes andall leat nodes are at samelevel
the
complete binarytreetailbinarytreewithconditions
everylevelmustbecom p rettytinea
Binarytree representation
Binarytreeevery parenthaseithertwo ornochildren
Full
Examples
Balanced Binarytree
conditions
Binary searchtree
Search operation
it a value isbelowtherootwecan thatthevalue isnot intheright subtree
say
search intheleftsubtree
insert operation
dependingonthevalue eitherleftor right subtreeuntilpoint
go right orleft subtree is nun
newnode istheninstalledthere
a
tree
B
is saidto be oforder m
Properties
aforeachnodethekeysa re storedinincreasingorder
Graphs
based ondatastructure
collection ofnodesthath avedataandareconnected to othern odes
collectionofvertices v
collection ofcages e represented as ordered pairs ofvertices in v
o 3
terminology
avertexadjacent toanothervertexitthereis a connectingcage
vertices z anas in exampleare not adjacent
sequence ofeages to from vertex a to vertex B iscanedapath o n n e and o z
go
inexampleabovearebothpathsfromvertex o to vertex 2
directedgraph edge in u doesnotnecessarilyimplythatthere alsoanedge ivus
is
eages are represented byarrows tosnowthedirection oftheedge
Strongly connected component
cluster in a directedgraph in whichthereis apathbetween anvertices
Graph operations
check itelement isincluded ingraph
graphtraversal
aaa vertices ana or edgestograph
tinapathfromonevertex to another
Graph representation
adjacency matrices
matrixof v xu vertices
eachrowandcolumn represent a vertex
it thevalueofelement ai n thereisanedge connecting vertex i withvertexj
PROS CONS
addingorremovingan edge orchecking it vertex memory spacefor
everypossible linkhasto be stored
i and are connected anedge is extremely
j by
fast constanttimes some operations lineages areexpensive
matrix calculation workfast
Adjacency list
graphs as linkedlists
index i ofthearray representsavertex
eachelement inits linnealist represents an adjacent vertex
Traversal
allvertices ofthegraph are visited
considerthe Depth firstsearch and breadthfirstsearch algorithm
ideaisthatthe Depthfirstsearch traverse through a subtree firstbeforetraversing throughothersubtrees
Breathfirstsearch traversethrough firstl evel ofanchildnodesbefore traversingthrough levelofgrandchild nodes
Depth firstsearch
recrusivealgorithm searching anverticesof agraph
markeachvertexasvisited ornotv isited
markavoidscycles
Procedure
ataketopvertexandaddit tothevisitedlist
3manelistofvertex'sadjacentnodes
vertices whichare notvisitedareaddedtothe ofthestack
top
a
repeatstep 2and3untilstack is empty
complexity
Depthsearch ocute
spacecomplexity Ow
Application
Finding path
Detectcyclesingraph
Breath Firstsearch
recursivealgorithm for searching anthevertices of agraph ortreedatastructure
standardimplementp utseachvertex ofthegraphintoone of two categories
visited
notvisited tomarkeachvertexasvisitedwhileavoidingcycles
graphmighthavedisco
procedure
a
startp uttinganyo neofthegraph'sverticesatthebackof aqueue
3createlistofthatvertex'sadjacent nodes
addtheones which aren't inthevisite list totheback ofthequeue
a
complexity
oev eswherevisthenumberofnodesandeisthenumber ofedges
Application
buildindex searchindex
by
ons
pathtinaing algorithm
Spanning tree
connected graph
always apathfromonevertextoanother
A B
D c
spanning tree
1 2
3
thesumofweightoftheedges is a smallaspossible
application
computer networkrouting
clusteranalysis
paths in
maps
Algorithm
Prim's algorithm
oielogv1
Kruskal'salgorithm
Oleloget
union Find
Kruskal'salgorithm
onlyworkswhen it canbe determined it a cycleisfound
Finddeterminewhichsubset a particularelementisin
unionso
in twosubsetsinto a singlesunset
union rankideais to always aitaten smallerdepthtreeundertheroot ofthedeepertree
by
rankis notalways equal toheight
path compresion idea istoflatten thetreewhen final iscaned
makethefoundrootasparentof x soitdoesnotn eed totraversean intermediate nodesagain
it x isrootof a subtreet henpathnorootsfromannodesunder x compresses
Kruskal's algorithm
findthe minimum spanningtree addingstep stepedgeswithlowweights
by by
Don'taddedge betweentwonodes whicharealready inthespanningtreecycles
Procedure
a
sortanedgesstartingfromedge withlowestweight
2
startwithedgewithlowestweight
3
Aaanewedgewithnextlowest weighttothespanningtreereject it cycleiscreated
a
repeat 3 untilan verticesare reached
Prim's Algorithm
Procedure
istartwithrandomvertex
findan treetonewvertices
eagesconnect
Dijkstra's algorithm
ideaisthat sun
any path B to c is theshortestpath
greedyalgorithm
negativeweights
DFS BFS
depth first search continuesonepathuntil theendisreached best first search
then continue atthelastforkintheroad willcontinue anpossiblepaths
PROS
attheend itwillfinda path at the end it willfindapath
itwillfindtheshortest distance
CONS CONS
Best First search a search
informed algorithm improved informed algorithm
idea is not totakerandomdirections considerstepsalreadytaken plustheminimumnumberofsteps totake
pointingtowards thegoal
manhattandistanceminimumnumberofstepstothegoal
PROS PROS
takes advantageofinformationwherethe is
goal
itwill visitas takesadvantage ofthestepsalready taken
not many nodesasBrsandDrs
CONS willfindshortestpath
m
Genetic algorithms
groupof individuals chromosomes
chromosomes consist ofgenes tosolvesomeproblems
fitnessmeasuresnowwe theproblemissolved
goes through generations
selectedchromosomesthat aremorefit
runsuntil a giventhersh oldforthefitnessfunctionis reached or a maxnumber of generations
chromosome
fitness
randomly selection of agene forfirstgens
crossover combination ofself
i a ndothers
mutationism anchangetoitself
a
createinitalpopulationof random chromosomes
topics ofalgorithms
the minimax algorithmworks backwards
player x triestomaximizetheoutcomewas
player o tries tominimizetheoutcome i n
iscaned recrusivelyuntilt herootnotes arereached
Knapsack problem
Solution
a
Listallp ossiblesubsetofstationsc anpossiblesunset as eachstationcanbe inoroutoftheset
pickthesmallestsetofstations thatcoveranstates
a
n z
3
A
complexity
time complexity for encoding is given
by oin logni
Steps
a
countoccurrence ofano
c urvingcharacter
aBuildpriority queue
3
Buildmuttmantree
acreate tableofcodesfromtree
write muttmantreeandcodeddatato tie
s