1 / 12

Algorithm simulation activity

Algorithm simulation activity. Dijkstra’s alg. SingleSourceShortestPath ( directed graph G, vertex v) Distance is the best-known distance from v to each vertex. Set Distance=  for each vertex, except Distance(v)=0 Repeat until all vertices done

toya
Download Presentation

Algorithm simulation activity

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. Algorithm simulation activity

  2. Dijkstra’s alg SingleSourceShortestPath (directed graph G,vertex v) Distance is the best-known distance from v to each vertex. Set Distance= for each vertex, except Distance(v)=0 Repeat until all vertices done Take the vertex with the shortest distance (first will be v) Mark that it’s done See if there are any shorter routes to vertices that have edges from v. If so, update them with the shorter route

  3. Dijkstra’s alg - code SingleSourceShortestPath (directed graph G,vertex v) set of nodes U foreachvertex v in G Distance(v) =  insert(U,v) endfor Distance(S) = 0 { Distance could be implemented using a heap } repeat |G| times v = vertex with smallest Distance(v) delete(U,v) foreachneighbor w of v do if member(U,w) then Distance(w)=min(Distance(w), Distance(v)+Cost(v,w) endif endfor endrepeat

  4. Shortest paths from Homer, NY

  5. Dijkstra activity • Simulate Dijkstra’s Single Source Shortest Paths algorithm to find the shortest paths from Homer, NY (using the graph of the highways around Homer in New York). • As you run the algorithm, write on the graph to make pointers along the shortest paths. • You can record current distances, and “mark” (circle) nodes using the supplied table. • Optionally use a Heap to find the least Distance(v)

  6. Kruskall’s Minimum Spanning Tree alg MinSpanTree (undirected graph G) Put all edges in G in a priority queue Remove all edges from G, and think of the nodes as a bunch of unconnected components While there is more than 1 component do Get the least-cost edge <u,v> from the priority queue If nodes u and v are in different components Merge them into the same one Add edge <u,v> into the edges of the min spanning tree Note we have 1 less component Endif Endwhile

  7. Kruskall’s alg - code MinSpanTree (undirected graph G) E=edges in G V=vertices in G set of unconnected component C C=V { each vertex is its own component } int numC=|V| { # of unnconnected components } priority queue EQ insert all edges in E into EQ while numC > 1 do <u,v>=EQ.DeleteMin U=Find(u) V=Find(v) if U  V then Union(U,V) <u,v> is an edge in the min span tree numC = numC - 1 endif endwhile

  8. Source: infoplease.com, Encyclopaedia Brittanica, National Geodetic Survey Air distance between cities in Statute miles

  9. Nodes of Minimum Spanning Tree Use boxes to write Up-Tree parent

  10. Kruskall activity • Simulate Kruskall’s algorithm using Up-Trees to find the minimum spanning tree for the cities given.

  11. (reverse) Topological Sort alg TopSort (DAG G) foreachvertex v in G Mark v TopSort2 (G,v) endfor TopSort2 (DAG G,vertex v) foreachedge <v,w> in G TopSort (G,w) endfor print v

  12. Top sort Activity • Run TopSort on this graph (or as much of it as you feel is necessary) • Try to pick random nodes to visit first (to see why the alg works)

More Related