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

Download Advanced Algorithmics (6EAP) - Kursused -   Algorithmics (6EAP) ... , list, stack, queue, deque, priority queue, table ... • Plain old data structure. Abstract Data Types (ADT)

Post on 13-Mar-2018

219 views

Category:

Documents

6 download

Embed Size (px)

TRANSCRIPT

  • AdvancedAlgorithmics(6EAP)MTAT.03.238

    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

    Memory/address/ Garbagecollection

    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

    Doublylinkedlist Arraylist Linkedlist Self-organizinglist Skiplist Unrolledlinkedlist VList

    Xorlinkedlist Zipper Doublyconnectededgelist

    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:

    gainaccess tothekthelementofthelisttoexamine and/orchange thecontents

    insert anewelementbeforeorafterthekthelement

    delete thekthelementofthelist

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

  • AbstractDataType(ADT) High-leveldefinitionofdatatypes

    AnADTspecifies Acollection ofdata Asetofoperations onthedataorsubsetsofthedata

    ADTdoesnotspecifyhowtheoperationsshouldbeimplemented

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

    associativearray,set,graph,digraph

  • ADT Adatatype isasetofvaluesandanassociatedsetof

    operations Adatatypeisabstract iffitiscompletelydescribedbyitsset

    ofoperationsregradlessofitsimplementation Thismeansthatitispossibletochangetheimplementation

    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

  • AbstractDataTypes(ADT) SomecommonADTs,whichhaveprovedusefulinagreatvarietyofapplications,are

    EachoftheseADTsmaybedefinedinmanywaysandvariants,notnecessarilyequivalent.

    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

  • Somedatastructuresfor DictionaryADT Unordered

    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

    Lists Doublylinkedlist Linkedlist Self-organizinglist Skiplist Unrolledlinkedlist VList Xor linkedlist Zipper Doublyconnectededgelist

  • TreesBinarytrees AAtree AVLtree Binarysearch

    tree Binarytree Cartesiantree Pagoda Randomized

    binarysearchtree

    Red-blacktree Rope Scapegoattree Self-balancing

    binarysearchtree

    Splaytree T-tree Tangotree Threadedbinary

    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

    Graphs Adjacencylist Adjacencymatrix Graph-structured

    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

    *array (memory address)sizeMAX_SIZE

  • 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

    push(x) -- addtoend(addtotop) pop() -- fetchfromend(top)

    O(1)inallreasonablecasesJ

    LIFO LastIn,FirstOut

  • Linkedlistshead tail

    head tail

    Singly linked

    Doubly linked

  • Linkedlists:addhead tail

    head tail

    size

  • Linkedlists:delete(+garbagecollection?)

    head tail

    head tail

    size

  • Operations

    Arrayindexedfrom0 ton 1:

    Singly-linkedlistwithheadandtailpointers

    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

    Wecanimprovetherun-timeefficiencyofalinkedlistbyusingadoubly-linkedlist:

    Singly-linkedlist:

    Doubly-linkedlist:

    Improvementsatoperationsrequiringaccesstothepreviousnode

    Increasesmemoryrequirements...

  • ImprovingEfficiency

    Singly-linkedlist:

    Doubly-linkedlist:

    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:

    Singly-linkedlistwithheadandtailpointers

    Doublylinkedlist

    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)

  • Introductiontolinkedlists Considerthefollowingstructdefinition

    struct node {string word;int num;node *next; //pointer for the next node

    };node *p = new node;

    ? ?

    num word next

    p ?

Recommended

View more >