1 / 19

Trees and Cycles.

Trees and Cycles. In this section we will focus on some properties of trees and in particular Prim's and Kruskal's algorithms for minimal spanning trees. Weighted graphs; Digraphs. A weighted graph is one in which a weight (such as cost, distance, margin, …) is associated with each edge.

gala
Télécharger la présentation

Trees and Cycles.

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. Trees and Cycles. In this section we will focus on some properties of trees and in particular Prim's and Kruskal's algorithms for minimal spanning trees.

  2. Weighted graphs; Digraphs • A weighted graph is one in which a weight (such as cost, distance, margin, …) is associated with each edge. • A digraph, or directed graph, is a graph whose adjacency matrix is not necessarily symmetric, i.e. eij  eji may be true. The edges have a directional component.

  3. The Travelling Salesman Problem • In an Eulerian path we cover all roads just once; in a Hamiltonian path we never return to a town once we’ve been there. A travelling salesman tries to visit all towns (returning home) but minimizing the cost of the trip. • Given a connected weighted graph, find a circuit of the graph that minimizes the total weight (cost) along the path. [Solution still not found.]

  4. Cycles in Graphs • A cycle is a path with at least one edge, no repeated edges and first and last vertices coinciding. Any vertex can be the beginning (and ending) vertex, but there are no other repeated vertices.

  5. Trees • A tree is a connected graph with NO cycles. • Because there is a path from any one vertex to any other, adding an edge between two vertices will produce a cycle. • Since there is only one path between any two vertices, removing an edge will result in a disconnected graph. • A tree within a graph thatconnects all vertices is aspanning tree for the graph.

  6. There are many options for spanning trees

  7. Total Number of Spanning Trees • A complete graph with n vertices hasn(n-2) spanning trees. • 53 is 125 86 is 262144 108 is 100 million 10098 is 10196Compare: there are 31.5576*106 seconds in a year. • A nanosecond is one billionth (10-9) of a second. (An electrical signal can travel about 30cm in a nanosecond.) There are • 31.5576*1015 nanoseconds in a year. • We are not going to be able to find all spanning trees for large graphs even on the fastest computers, at least not in our lifetimes. We have to get smart about trees.

  8. Theorem • A tree with n vertices has n -1 edges. • Proof by Induction resting on the fact that there is a vertex of degree 1.(proof of this by contradiction) • 1) a two vertex tree clearly has just one edge.2) If a k vertex tree has k -1 edges then a k +1 vertex tree, being the vertex of degree 1 together with a k vertex tree must have k edges.

  9. Theorem • A connected graph withn vertices and n -1 edges is a tree. • [Proof not given.] • There are graphs, which are not trees, that have n vertices and n - 1 edges, but they are not connected.

  10. Weighted graph problems • Suppose we have a weighted graph and wish to find a connected sub-graph containing all the vertices but fewer, if possible edges so that we achieve the minimal total weight.It must be a tree: aspanning tree.

  11. Prim's Algorithm • Start with any vertex. Pick the edge with the least weight and add the vertex at the other end of it to the sub-graph.Again pick the edge with least weight from amongst all the edges of the vertices in the sub-graph, and, provided it does not create a cycle, add it to the sub-graph.Repeat.

  12. Kruskal's Algorithm • Keep adding the lowest weighted edges provided they don't produce a circuit until you've covered all the vertices.

  13. What's so great about these "obvious" algorithms? • Anybody could have come up with the algorithm, but proving that the algorithm actually does give a minimal spanning tree, is the achievement. • [These proofs are not given in Grossman -- see reference texts.]

  14. Kruskal's vs Prim's • For graphs of n vertices and m edges, Kruskal's and Prim's algorithms have worst case orders of m log2m and n2 respectively. • If m = n = 1000 (as many vertices as edges) 10002 is 1 million; 1000 log2 1000 is about 9996 or ~10,000. • but if m= 2000 (edges) and n= 100 (vertices) 1002 = 10,000; • 2000 log2 2000 ~ 21,932

  15. Greedy Algorithms • A greedy algorithm is a search strategy that assumes that the optimal path will be a path that's optimal at any particular point. • Prim's algorithm is a greedy algorithm. • (Greedy algorithms don't always work.)

  16. Travelling (greedy) salesman • A greedy algorithm would seem to require the walk:HEACBADH with weight: 96 • But if we go on the walk:HEABCDH or HEBACDH(to avoid the cycle) we get a weight of 88, but if, for example, EB were 17 then the second of these is preferred.

  17. Exercise:Using both Prim’s and Kruskals’ algorithmsfind the minimal spanning tree

  18. Minimum weight is 37

  19. What’s the big idea? • A graph that is a tree (n vertices & n-1 edges) • Spanning trees for a graph. • Minimal spanning trees for weighted graphs (Kruskal’s algorithm and Prim’s greedy algorithm)

More Related