ecology labfaculty.cse.tamu.edu/.../spanningtreesandprims.docx · web viewspanning trees connected,...
TRANSCRIPT
Prims and Spanning TreesSpanning Trees
connected, undirected graph.o consists of vertices and edges
sub graph of T of an undirected graph G = (V,E) T is a spanning tree of G if it is a tree and contains every vertex of G everything is doesn’t need to be directly connected to everything.
Spanning Tree Examples
no cycles!!!
1
What is a Minimum Spanning Tree (MST) an undirected connected weighted graph is a spanning tree of minimum
weight o among all the possible Spanning treeso all vertices V have at least one weighted edge E.o typically has less edges than a normal spanning tree
The summation of the all the edges is the weight of the whole graph.
MSTs
2
Solving for a MST find the shortest path from every node to every node completed on UNDIRECTED graphs tree since no cycles T = (V, F) of G
o F is a different SET of edges but will be a set of Eo F edges will be |V-1| since it will need a single edge to connect to
vertices, and not use all given, just enough to make a tree If G is not connected, then T really won’t be a tree, but a forest
How T and G relateG T of G T of G (Cleaned up)
This is the MST of G
3
Prim’s Algorithm Visually could start from anywhere pick lowest cost edge available
o repeat over and over with new options each time eventually, find the lowest cost to connect all vertices
Prim’s Algorithm VisuallyOriginal (starting at a)
Showing Step by Step
What’s next?
4
PsuedoCode and details for Prim’s Algorithm algorithm completes this task by starting the tree at any vertex and then by adding the “lightest” edge each iteration until all the vertices have been added
greedy Algorithm used to minimize edges to reduce cost Input: A non-empty connected weighted graph with vertices V and edges E
(the weights can be negative). Initialize: Vnew = {x}, where x is an arbitrary node (starting point) from V, Enew
= {} Repeat until Vnew = V:
o Choose an edge {u, v} with minimal weight such that u is in Vnew and v is not (if there are multiple edges with the same weight, any of them may be picked)
o Add v to Vnew, and {u, v} to Enew
Output: Vnew and Enew describe a minimal spanning tree
5
Prim’s Step-by-Step (Scan, Update, Sort) notice a few other data structures are used to house all of the info start with a current node
o “Known” is set to Trueo scan connected edges for costso update any costs if smaller than what is already recorded
real difference is o builds from the start vertex to shortest edge and spreads to the next
connect shortest edge, etc…o at the end, the PATH is just that, to make the MST
the complete example
Prim’s 1st Example(starting at 1)
6
1 has completed its’ scan
Also notice 1’s “known” has been set to true
7
Picked 2 since lowest edge cost (5) and not known
8
Scanning 2’s edges with an update
(Same graph as above)
will now become
9
2 is done all scan and updates
Who would we pick next for our current vertex??When all said and done (notice paths)
10
Try you own. Start at 2, but could be anywhere!! Answerb:
11
Prim’s answers no matter where you start, you SHOULD(?) get the same answer
Prim solutions starting at different Vertices – Ex 1
Tree
Starting at 2 Start at 4
Notice that the overall MST is exactly the same as the previous.
12
Prim solutions starting at different Vertices – Ex 2Tree
Start at 5 Start at 4
13
Prim’s Complexity Analysis Supposed to take a spanning tree and make it a minimum spanning tree V - # of vertices E - # of edges worst case runtime: O(|V|2) or will see sometimes O(|N|2)
o depends on the data structure used
Prim’s Complexity OptionsMinimum Edge Weight Data Structure Time Complexity When to use
Adjacency Matrix, Search (no heaps) O(V*V) Dense Graphs
Binary Heap and Adjacency List O(E*log(V)) Sparse Graphs
Fibonacci Heap and Adjacency List O(E + V*log(V)) eh
14
Famous Traveling Salesman Application a salesman is traveling around Germany and wants to find the shortest path he
has to take to visit Hamburg, Hannover, Berlin, Mainz and Munchen using Prim’s algorithm, the shortest path would be
o Munchen -> Hannover -> Berlin -> Hamburg - > Mainz But, if he were starting in Hannover he would go
o Hannover -> Munchen -> Hannover-> Berlin -> Hamburg - > Mainz
or a little more locally
Other Applications of this Prim’s MST15
in general, any situation where you have multiple destinations, that don’t require WHEN you visit them, and you can travel both TO and from our destinations, or nodes.
Exampleso Notice how each of these examples have the edges (roads/wiring/piping)
flowing in BOTH DIRECTIONSo Edison Power using this to make sure cables reach every distribution and
power center while also using the least amount of cable. Each edge, or transmission line, is given a distance which
represents the weight
o MAZE generation can use this in creating false paths, where each path is randomly given a weight, and each minimum weight path is given a dead end, and the algorithm continues on the largest weight path
16
o Saving resources in laying road. Highways need to reach major destinations such as towns, cities, and major business parks.
All major cities in the US are connected via highway, the cities being nodes and roads being the edges.
o Piping layout, similar to what Comcast has to deal with their underground fiber cables.
Water distributing and draining facilities are the nodes, and water moving BACK AND FORTH in parallel piping.
17
Answers
https://www.youtube.com/watch?v=uBrMUCQ7cfI
Sourceshttps://www.youtube.com/watch?v=YyLaRffCdk4http://en.wikipedia.org/wiki/Prim%27s_algorithmhttp://www.cse.ust.hk/~dekai/271/notes/L07/L07.pdfhttp://www.cs.rit.edu/~lr/courses/alg/student/1/minspan.pdfhttp://en.wikipedia.org/wiki/Minimum_spanning_treejsinger (2013). SeqAn Tutorials - Graphs. [ONLINE] Available at: http://trac.seqan.de/wiki/Tutorial/Graphs. [Last Accessed 09 December 13].http://www.intechopen.com/download/get/type/pdfs/id/29863http://www.astrolog.org/labyrnth/algrithm.htmhttp://www.cs.usfca.edu/~galles/visualization/Prim.html(Prim image)http://homepages.ius.edu/rwisman/C455/html/notes/Chapter23/Ch23-7.png
18