270 likes | 384 Vues
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
E N D
Algorithms (andDatastructures) Lecture 3 MAS 714 part 2 Hartmut Klauck
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
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
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]]++
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
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
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
Radix Sort • Rewritekeys x in theformati=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
Radix Sort • Correctness: • Letx,ybetwonumbers in thesequence. • Letxidenotethemostsignificantposition on whichtheydiffer • Thenstep i putsx,y in theright order, andlaterstepsneverchangethat order (due tothestabilityofcountingsort)
Further topicsaboutsorting • Time versus space • Sorting on parallel machines • Sorting on word RAMs, fasterthan n log n • Deterministicsorting in O(n log n)
Graph Algorithms • Manybeautifulproblemsandalgorithms • Nice settingtostudyalgorithm design techniques
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
Graph problems • Therearemanyinterestingalgorithmicproblemsforgraphs • Example: Find a smallestsubsetofedges such thateveryvertexis in oneoftheedges • Example: Whatisthelargestsizeof a setofverticeswherenotwoverticesareconnectedby an edge
Graph representations • Therearetwomajorwaystorepresentgraphs: • Adjacencymatrix • Adjacency List • (Incidencelist)
Adjacencymatrix • The adjacencymatrixof a graph G=(V,E) has n rowsandcolumnslabeledwithvertices • A[i,j]=1 iff (i,j)2 E • Works forbothundirectedanddirectedgraphs • undirectedgraphsmayuseonlytheuppertriangle
Adjacencymatrix • Advantages: • easy accesstoedges • can do linear algebra on thematrix • Disadvantage: • not a compactrepresentationofsparsegraphs • sparsemeansm=o(n2) [oreven m=O(n)]
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
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
Linked Lists/Adjacency List • Exampleof a linkedlist • Adjacencylist
Weighted Graphs • Graphs oftencomewithweights • Weights on vertices • Weights on edges • Example: Directed Graph withweightededges • Representas a matrixofweights • Either 0 or1marksabsenceof an edge
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
Traversing Graphs • Example: Findingtheexitof a maze
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)
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
The Datastructure • Wedistinguishbytherulethatdeterminesthenextactivevertex • Alternative 1: queue • FIFO (first in first out) • Alternative 2: stack • LIFO (last in first out)
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.
Traversing Graphs • Withbothmethodseventually all reachableverticesarevisited • Different applications: • BFS canbeusedto find shortedpaths in unweightedgraphs • DFS canbeusedtotopologicallysort a directedacyclicgraph