# advanced algorithmics (6eap) - kursused - algorithmics (6eap) ... , list, stack, queue, deque,...

Post on 13-Mar-2018

219 views

Category:

## Documents

Embed Size (px)

TRANSCRIPT

Linearstructures,sorting,searching,etc

JaakVilo2017Fall

1JaakVilo

• Big-Ohnotationclasses

Class Informal Intuition Analogy

f(n) (g(n)) fisdominatedbyg Strictlybelow

• Conclusions

Algorithmcomplexitydealswiththebehaviorinthelong-term worstcase -- typical averagecase -- quitehard bestcase -- bogus,cheating

Inpractice,long-termsometimesnotnecessary E.g.forsorting20elements,youdontneedfancyalgorithms

• Linear,sequential,ordered,list

Memory, disk, tape etc is an ordered sequentially addressed media.

• Physicalorderedlist~array

Files(character/bytelist/linesintextfile,)

Disk Diskfragmentation

• Lineardatastructures:Arrays Array Bidirectionalmap Bitarray Bitfield Bitboard Bitmap Circularbuffer Controltable Image Dynamicarray Gapbuffer

Hashedarraytree Heightmap Lookuptable Matrix Parallelarray Sortedarray Sparsearray Sparsematrix Iliffevector Variable-lengtharray

• Lineardatastructures:Lists

Differencelist

• Lists:Array

3 6 7 5 2

0 1 size MAX_SIZE-1

L = int[MAX_SIZE]

L[2]=7

• Lists:Array

3 6 7 5 2

0 1 size MAX_SIZE-1

L = int[MAX_SIZE]

L[2]=7 L[size++] = new

3 6 7 5 2

1 2 size MAX_SIZE

L[3]=7 L[++size] = new

• Multiplelists,2-D-arrays,etc

• 2Darray

& A[i,j] = A + i*(nr_el_in_row*el_size) + j*el_size

• LinearLists

Operationswhichonemaywanttoperformonalinearlistofn elementsinclude:

insert anewelementbeforeorafterthekthelement

delete thekthelementofthelist

Reference:Knuth,TheArtofComptuerProgramming,Vol1,FundamentalAlgorithms,3rd Ed.,p.238-9

Examples vector,list,stack,queue,deque,priorityqueue,table(map),

associativearray,set,graph,digraph

ofthedatatypewithoutchangingitsuse Thedatatypeisthusdescribedbyasetofprocedures Theseoperationsaretheonlythingthatauserofthe

abstractioncanassume

• Primitive&compositetypesPrimitivetypes Boolean (forboolean values

True/False) Char (forcharactervalues) int (forintegralorfixed-precision

values) Float (forstoringrealnumber

values) Double (alargersizeoftypefloat) String (forstringofchars) Enumerated type

Compositetypes Array Record (alsocalledtupleorstruct)

Union Taggedunion(alsocalledavariant,variantrecord,discriminatedunion,ordisjointunion)

Plainolddatastructure

ContainerListSetMultisetMapMultimap

StackGraphQueuePriority queueDouble-ended queueDouble-ended priority queue

• Abstractdatatypes:

Dictionary (key,value) Stack(LIFO) Queue(FIFO) Queue(double-ended) Priorityqueue(fetchhighest-priorityobject) ...

• Dictionary

Containerofkey-element(k,e)pairs Requiredoperations:

insert(k,e), remove(k), find(k), isEmpty()

Mayalsosupport(whenanorderisprovided): closestKeyBefore(k), closestElemAfter(k)

Note:Noduplicatekeys

Array Sequence/list

Ordered Array Sequence(SkipLists) BinarySearchTree(BST) AVLtrees,red-blacktrees (2;4)Trees B-Trees

Valued HashTables ExtendibleHashing

• Lineardatastructures

Arrays Array Bidirectional

map Bitarray Bitfield Bitboard Bitmap Circularbuffer Controltable Image Dynamicarray Gapbuffer

Hashedarraytree

Heightmap Lookuptable Matrix Parallelarray Sortedarray Sparsearray Sparsematrix Iliffe vector Variable-length

array

• TreesBinarytrees AAtree AVLtree Binarysearch

tree Binarytree Cartesiantree Pagoda Randomized

binarysearchtree

Red-blacktree Rope Scapegoattree Self-balancing

binarysearchtree

tree Toptree

Treap Weight-balanced

tree

B-trees B-tree B+tree B*-tree Bsharptree Dancingtree 2-3tree 2-3-4tree Queap Fusiontree Bx-tree

Heaps Heap Binaryheap Binomialheap Fibonacciheap AF-heap 2-3heap

Softheap Pairingheap Leftistheap Treap Beap Skewheap Ternaryheap D-ary heap Tries Trie Radixtree Suffixtree Suffixarray Compressed

suffixarray FM-index Generalised

suffixtree B-trie Judyarray X-fasttrie Y-fasttrie

Ctrie

Multiway trees Ternarysearch

tree Andortree (a,b)-tree Link/cuttree SPQR-tree Spaghettistack Disjoint-setdata

structure Fusiontree Enfilade Exponentialtree Fenwicktree VanEmde Boas

tree

Space-partitioningtrees Segmenttree

Intervaltree Rangetree Bin Kd-tree Implicitkd-tree Min/maxkd-tree Adaptivek-dtree Kdb tree Quadtree Octree Linearoctree Z-order UB-tree R-tree R+tree R*tree HilbertR-tree X-tree Metrictree Covertree M-tree VP-tree

BK-tree Bounding

intervalhierarchy

BSPtree Rapidly-exploring

randomtree

Application-specifictrees Syntaxtree Abstractsyntax

tree Parsetree Decisiontree Alternating

decisiontree Minimax tree Expectiminimax

tree Fingertree

• Hashes,Graphs,Other Hashes Bloomfilter Distributedhashtable Hasharraymapped

trie Hashlist Hashtable Hashtree Hashtrie Koorde Prefixhashtree

stack Scenegraph Binarydecision

diagram Zerosuppressed

decisiondiagram And-invertergraph Directedgraph Directedacyclicgraph

Propositionaldirectedacyclicgraph

Multigraph Hypergraph

Other Lightmap Wingededge Quad-edge Routingtable Symboltable

• Lists:Array

3 6 7 5 2

0 1 size MAX_SIZE-1

3 6 7 8 2

0 1 size

5 2 Insert 8 after L[2]

3 6 7 8 2

0 1 size

5 2 Delete last

• Lists:Array

3 6 7 8 2

0 1 size

5 2 Insert 8 after L[2]

3 6 7 8 2

0 1 size

5 2 Delete last

Access i O(1) Insert to end O(1) Delete from end O(1) Insert O(n) Delete O(n) Search O(n)

• LinearLists

Otheroperationsonalinearlistmayinclude: determinethenumberofelements searchthelist sortalist combinetwoormorelinearlists splitalinearlistintotwoormorelists makeacopyofalist

• Stack

O(1)inallreasonablecasesJ

LIFO LastIn,FirstOut

size

size

• Operations

Arrayindexedfrom0 ton 1:

1 undertheassumptionwehaveapointertothekthnode,O(n) otherwise

k = 1 1 < k < n k = naccess/change the kth element O(1) O(1) O(1)

insert before or after the kth element O(n) O(n) O(1)delete the kth element O(n) O(n) O(1)

k = 1 1 < k < n k = naccess/change the kth element O(1) O(n) O(1)

insert before or after the kth element O(1) O(n) O(1)1 O(n) O(1)delete the kth element O(1) O(n) O(n)

• ImprovingRun-TimeEfficiency

Improvementsatoperationsrequiringaccesstothepreviousnode

Increasesmemoryrequirements...

• ImprovingEfficiency

1 undertheassumptionwehaveapointertothekthnode,O(n) otherwise

k = 1 1 < k < n k = naccess/change the kth element O(1) O(n) O(1)

insert before or after the kth element O(1) O(1)1 O(1)delete the kth element O(1) O(1)1 O(1)

k = 1 1 < k < n k = naccess/change the kth element O(1) O(n) O(1)

insert before or after the kth element O(1) O(n) O(1)1 O(n) O(1)delete the kth element O(1) O(n) O(n)

• Arrayindexedfrom0 ton 1:

k = 1 1 < k < n k = naccess/change the kth element O(1) O(1) O(1)insert before or after the kth element O(n) O(n) O(1)

delete the kth element O(n) O(n) O(1)

k = 1 1 < k < n k = naccess/change the kth element O(1) O(n) O(1)insert before or after the kth element O(1) O(n) O(1)1 O(n) O(1)delete the kth element O(1) O(n) O(n)

k = 1 1 < k < n k = naccess/change the kth element O(1) O(n) O(1)

insert before or after the kth element O(1) O(1)1 O(1)delete the kth element O(1) O(1)1 O(1)