0% found this document useful (0 votes)
15 views

App. Algorithms Summary

Uploaded by

ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

App. Algorithms Summary

Uploaded by

ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

whyalgorithmsare important

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

Divide andconquer Algorithm



1 Dividedividethe problemintosub problems using recrusion
2
conquer recrusively solvesmallersub problems it subproblem issmall enoughsolve it directly
3
combine combine solutions ofthesunproblemtogetsolution oforiginal problem

Divideand conquer vs Dynamic approach



Divideand conquer doesn't storeresultofeach subproblem

Dynamic approach storestheresultofe achsubproblemtheresultcanbeusedmultipletimesinthefuture

Divide and conquer


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

castinfirstoutopposite to a queue LI ito

Queue

firstperson inthelineisthefirste nteringthebus

firstinfirstout Fito

Priority Queue

eachelement isassociatedwith a priority andisservedaccordingly

elementswiththesamepriorityareservedaccordingtotheiro rder inthequeue

highestvalue highestpriority

firstinfirstoutr ule

valuesareremovedbased onpriority

priority queueapplications

Dijkstra's algorithm

loadbalancing and interrupt handling in Os

datacompression in muttmancode

heap Data structure

complete
binary tree satisfying the neapproperty

completebinary tree

every level exceptpossiblethelast isfined

annodesareasfarleftaspossible

childnodesarealwayslarger

Heapity

process of
creating a neapdatastructure froma binarytree

Double ended Queue

aeque

type ofqueue withinsertion andremoval of elementsfrom frontorrear

does not followthe firstin firstoutrule

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

gotonodejustbeforerequiredpositionofnode setnextpointers tothenextbutoneelement



changnextpointerstoinclude newnode between


Hash Table

data structure thatrepresents data inthe form of value pairs
key

each to a data in a hashtable
key index is mapped value

Direct address table



used whenenoughmemory space isgiven

the
keysaregivenby smallandunique integers

eachslotofthedirect addresstable containsapointer tothe corresponding data

if there isnoelementfora certain itisleftas nun
key

Limitations of Directaddresstable

valuesofthe shouldbesmall
key

notsmall reservestoomuchspace

sizelimitof
arraymaybe a restriction

Hash table

keys produce anewindexthatmapstotherequiredelementinasnings

Let himbe a nash functionand r a


key nine is calculatedanditsused asanindexfortheelement

Limitations of hashtable

sameindexcanbe produced nashfunctionformultiple


by regs collision

uniquekeys cannotbeproducedasthesizeoftheuniverse u islargerthanthepossiblesizeofthekeys m

goodhashfunctioncanreduce thenumberofcollisions

PROS ofhashtable over directa ccesst able

reduces rangeoftheindex

hence size ofarray

no memory wasted providing a


by biggera rray

Collision resolution
bychaining

sameindexformultipleelementselementsarestored inthesameindex a linkedlist


by

multipleelements containsa pointertothehead ofthelistelements

no elements nun

functions
Hash

goodhashfunctionhas

consistentnashfunctionmapssameinputeach t imetosameindex

does notgenerateregsthat aretoolargewhileonlyfewelementsneedtobestored

generatea nashvalues should be distributed overrange to minimizecollisions

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

selects smallestelementf roman unsorted listandplaces element at beginning ofthelist

Insertionsort

similartosortingcards acardgame
in

assumefirstc ardissorted

selectnextc ard it greaterplaceitontheright itnotthenleft

anunsortedcardsare considered and tothe p lace


put right

merge sort

divideandconqueralgorithm

problemis dividedinto sub
problems

whenthesolution ineachsubproblemisreadytheresultsfromthesubproblems are combined

Quicksort

basedondivideandconquerapproach splitting
arraysintosub a rrays

sorting iscanea recrusively tosorttheelements

conquer leftand rightsubpartsarerecrusively partitionedagain

Countingsort

works
onlyif natural numbers havetobesorted

numbers shouldbeclosetogether withoutextremeoutliers

sortelements counting number ofoccurrences


by the


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

binary searchtrees forquick checkitelement ispresentin a set



neap isusedforneapsort

moaitiea tree usedin routersto storeinformation


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

anneat elements mustleantowards left

final neat element mightnothave a rightsibling doesnothavea fullbinarytree

degenerate tree pathologicalt rees c hildeitherleftorright at eachnode


single

skewed a ndrightsubtree foreachnode is either oor a


binarytreedifference inheightbetweentheleft

Binarytree representation


Binarytreeevery parenthaseithertwo ornochildren
Full


Examples

Balanced Binarytree

heightof theleftandrightsubtree of nodediffer notmore than a


any by

conditions

difference between lefta ndright subtree for


anynode isnotmorethanone

left subtree is balanced

right subtree is balanced

Binary searchtree

quicklyanons to maintain asortedlistofnumbers

usedto search forthepresenceof a number in orogen time

properties to separate fromregulart ree

annodesofleft subtreearelessthan theroot node

annodes of right subtree aremore thantherootnode

bothsubtrees ofeachnode arealso binarytrees



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

eachnodehasnoto nlyone butmoreregs


key

is saidto be oforder m

Properties

aforeachnodethekeysa re storedinincreasingorder

aineachnodethereis a Boolean name it noae isaneatn ode


3it m is orderoft hetreeeach internalnodecontainsatmost m n keysand a pointertoeacheniaimaxmchildren


aeachinternalnonelexeptroot has emasto mchild



leavesnavethesamedepth
s.am

aroothasatleast achildrenwithminimumof akey


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

a puto nevertexontopofa stack


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

atakethefronti temofthequeueandaddit tothevisitedlist


3createlistofthatvertex'sadjacent nodes

addtheones which aren't inthevisite list totheback ofthequeue
a

akeeprepeating steps zand3 untilthequeue is


empty


complexity

oev eswherevisthenumberofnodesandeisthenumber ofedges

Application

buildindex searchindex
by

ons

pathtinaing algorithm

Spanning tree

connected graph

always apathfromonevertextoanother

A B

D c

spanning tree

undirected connectedgraphwhichincludes an vertices ofthe graphwithminimum possible number ofedges

1 2
3

Minimum spanning tree

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

thatis thetaskoftheunion findalgorithm

preforms two operations

Finddeterminewhichsubset a particularelementisin

determining it twoelements areinthesame sunset

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

3take minimumandadd it tothetree


arepeatsteps 2and3untilminimum spanningt reeis reached

Dijkstra's algorithm

finds shortestpathbetweentwovertices ofa graph i atool


s ay

ideaisthat sun
any path B to c is theshortestpath

similarto primes but withupdatingtheshortestdistances

starts with overestimating thedistance ofeachvertexandcontinuously upgradingtheshortestd istanceforeachsub


p ath

greedyalgorithm

Bellman Ford's algorithm

finds shortestpath between twovertices in a weightedgraph

negativeweights

mightbeuseful for cashflow calculations

cancreate negativeweight cycles

similar to Dijkstra's algorithm butalsoworks for edgeswith negative weights

uniformed search vs informed search


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

steps ofthe geneticalgorithm

a
createinitalpopulationof random chromosomes

2measurefitness ofeach chromosomeitanyexceedsthersnoiareturnthechromosomelogarithmends


3select chromosomes toreproduceinignerf itness nigher probability


acrossoverofsome selected chromosomes


s
mutation of some chromosomesnewpopulation replacestheprevious populationgeneration

itmaximumnumberof generations reachedendalgorithmotherwise repeatf romstep2

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

zipping inuttman coding



techniquetocompressdata

usedin zipping programs

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

You might also like