Algorithm simulation activity

1 / 12

# Algorithm simulation activity - PowerPoint PPT Presentation

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

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'Algorithm simulation activity' - toya

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### 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

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

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

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)
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

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

Nodes of Minimum Spanning Tree

Use boxes to write Up-Tree parent

Kruskall activity
• Simulate Kruskall’s algorithm using Up-Trees to find the minimum spanning tree for the cities given.
(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

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)