190 likes | 350 Views
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.
E N D
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. • 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.
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.]
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.
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.
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.
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.
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.
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.
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.
Kruskal's Algorithm • Keep adding the lowest weighted edges provided they don't produce a circuit until you've covered all the vertices.
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.]
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
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.)
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.
Exercise:Using both Prim’s and Kruskals’ algorithmsfind the minimal spanning tree
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)