1 / 27

Algorithms ( and Datastructures )

Algorithms ( and Datastructures ). Lecture 3 MAS 714 part 2 Hartmut Klauck. A linear time sorting algorithm. Assume all A[i] are integers from 1 to m Sorting algorithm : for i=1 to m c[i]=0 for i=1 to n c[A[i]]++ for i=1 to m If c[i]> 0 output i, c[i] times

dasan
Download Presentation

Algorithms ( and Datastructures )

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Algorithms (andDatastructures) Lecture 3 MAS 714 part 2 Hartmut Klauck

  2. A linear time sortingalgorithm • Assume all A[i] areintegersfrom 1 to m • Sortingalgorithm: • for i=1 to m • c[i]=0 • for i=1 to n • c[A[i]]++ • for i=1 to m • If c[i]> 0 output i, c[i] times • Clearlythisalgoruns in time O(n+m), linear if m=O(n) • Itis not comparisonbased

  3. CountingSort • The abovealgorithmhasthedrawbackthatitsortsonly a listofnumbers (keys), withnootherdataattached • Toproperlysort (including additional data) weneedtocomputewhereitemswithkey K start in thesortedsequenceandmovethedatathere • Furthermorewewantthealgorithmtobestable • Stability: Items withthe same keyremain in the same order • Quicksortis not stable

  4. CountingSort • for i=1 to m: C[i]=0 //Initialize • for i=1 to n): C[A[i]]++ //Count elements • Pos[1]=0 //Array of positions • for i = 2 to m: //Compute positions Pos[i]=Pos[i-1]+C[i-1] • for i=1 to n: //Produce Output Output[Pos[A[i]] = A[i] Pos[A[i]]++

  5. CountingSort • The thirdloopcomputesthepositionpos(i), atwhichelementswithkey i start in thesortedarray • pos(1)=0 • pos(i)=pos(i-1)+c(i-1) • The fourthloopcopieselements A[i] intothearray Output, atthecorrectpositions • The algorithmisstable, becausewekeepelementswiththe same key in their original order

  6. Linear time sorting • Radix sortsorts n integer numbersofsizenk in time O(kn) • Thisis linear time for k=O(1) • I.e., wecansortpolynomialsizeintegers in linear time

  7. Radix Sort • Main Idea: • Represent n numbers in a number system with base n • Given that numbers are size nk the representation has at most k digits • Sort by digits from the least significant to the most significant • Use a stable sorting algorithm • For each step use Counting Sort

  8. Radix Sort • Rewritekeys x in theformati=0…kxini • x isthenrepresentedby (xk,..,x0) • Sortthesequencebydigit/position 0, i.e. sortthesequenceusingthex0digitsaskeys • Stablysort on position 1 • etc. for all positions k • Time is O(kn)=O(n) for k=O(1) • Note: not comparisonbased, onlyworksforsorting „small“ integer numbers

  9. Radix Sort • Correctness: • Letx,ybetwonumbers in thesequence. • Letxidenotethemostsignificantposition on whichtheydiffer • Thenstep i putsx,y in theright order, andlaterstepsneverchangethat order (due tothestabilityofcountingsort)

  10. Further topicsaboutsorting • Time versus space • Sorting on parallel machines • Sorting on word RAMs, fasterthan n log n • Deterministicsorting in O(n log n)

  11. Graph Algorithms • Manybeautifulproblemsandalgorithms • Nice settingtostudyalgorithm design techniques

  12. Graphs • A graph G=(V,E) consistsof a setofvertices V and a set E ofedges. EµV£V • usuallythereare n vertices • usuallythereare m edges • Graphs canbeundirected (i,j)2E ) (j,i)2Eordirected (no such condition) • Edgesofundirectedgraphsarepairsofverticeswithno order • Edges (i,i) arecalledselfloopsandareoftenexcluded

  13. Graph problems • Therearemanyinterestingalgorithmicproblemsforgraphs • Example: Find a smallestsubsetofedges such thateveryvertexis in oneoftheedges • Example: Whatisthelargestsizeof a setofverticeswherenotwoverticesareconnectedby an edge

  14. Graph representations • Therearetwomajorwaystorepresentgraphs: • Adjacencymatrix • Adjacency List • (Incidencelist)

  15. Adjacencymatrix • The adjacencymatrixof a graph G=(V,E) has n rowsandcolumnslabeledwithvertices • A[i,j]=1 iff (i,j)2 E • Works forbothundirectedanddirectedgraphs • undirectedgraphsmayuseonlytheuppertriangle

  16. Adjacencymatrix • Advantages: • easy accesstoedges • can do linear algebra on thematrix • Disadvantage: • not a compactrepresentationofsparsegraphs • sparsemeansm=o(n2) [oreven m=O(n)]

  17. Adjacency List • The adjacencylistof G=(V,E) is an arrayoflength n. Eachentry in thearrayis a listofedgesadjacentto v2V • Fordirectedgraphs a listofedgesstarting in v • Size oftherepresentationis O(n+m) entries, closeto optimal • Itisharderto find a specificedge • Standard representationforgraphs

  18. Linked Lists • The listofverticesadjacentto v has variable lengthfor different v • Use a linkedlist • Linkedlistsare a datastructuretorepresentsequences • A linkedlistconsistsofnodes • Eachnodeconsistsof a cellfordataand a pointer • Thereis a pointertothefirstelement • Last elementpointsto NIL • Itis easy toadd an elementinto a linkedlist, andtosequentiallyreadthelist • Advantage overarrays: lengthisarbitrary/canbechanged • Disadvantage: norandomaccess

  19. Linked Lists/Adjacency List • Exampleof a linkedlist • Adjacencylist

  20. Weighted Graphs • Graphs oftencomewithweights • Weights on vertices • Weights on edges • Example: Directed Graph withweightededges • Representas a matrixofweights • Either 0 or1marksabsenceof an edge

  21. Example Problem • Single Source Shorted Path (SSSP) • Give a directedgraph G withnonnegativeedgeweights, a vertex s • Inputs(V,E) and W: E R+ and s • Output: thelengthoftheshortestpaths in thegraphfrom s to all othervertices • Array of n distances • Explanation: A pathfrom s to v is a sequenceofedges (s,v1), (v1,v2)…(vt,v) • The lengthof a pathisthesumofedgeweights on thepath

  22. Traversing Graphs • Example: Findingtheexitof a maze

  23. Traversing Graphs • Wearegiven a graph G=(V,E) • Startingvertex s • The goalisto traverse thegraph, i.e., tovisiteachvertexat least once • Forexampleto find a markedvertex t ordecideif t isreachablefrom s • Twovariants: • Breadth First Search (BFS) • Depth First Search (DFS)

  24. Traversing Graphs • Common tobothprocedures: • Use a datastructurewiththefollowingoperations: • Insert a vertex • Remove a vertex • Maintain an activevertex (startwith s) • Maintain an arrayofverticesalreadyvisited • Then: • Insert all (unvisited) neighborsoftheactivevertex, markitasvisited • Remove a newvertex v andmakeitactive

  25. The Datastructure • Wedistinguishbytherulethatdeterminesthenextactivevertex • Alternative 1: queue • FIFO (first in first out) • Alternative 2: stack • LIFO (last in first out)

  26. Result • Alternative 1: FIFO • Breadth First Search • Neighborsof s will bevisitedbeforetheirneighbors etc. • Alternative 2: LIFO • Depth First Search • Insert neighbors, last neighborbecomesactive, theninserthisneighbors, last neighborbecomesactive etc.

  27. Traversing Graphs • Withbothmethodseventually all reachableverticesarevisited • Different applications: • BFS canbeusedto find shortedpaths in unweightedgraphs • DFS canbeusedtotopologicallysort a directedacyclicgraph

More Related