# spanning trees

Post on 21-Feb-2017

25 views

Embed Size (px)

TRANSCRIPT

Spanning Trees

*

GRAPHSGraphs are one of the most Interesting data structures in computer science.Graphs and trees are somewhat similar by their structure and in fact tree is derived from the graph data structure.Commonly used graph traversal algorithms are:

*

BFSDFSIn this we visit the nodes level by level, so it will start with level 0, which is the root node then next, then the last level.In this we visit the root node first then its children until it reaches the end node.Queue is used to implement BFS.E.G.

Its BFS will be A,B,C,D,E,F.Stack is used to implement DFS.E.G.

Its DFS will be A,B,E,F,C,D

*Spanning treesSuppose you have a connected undirected graphConnected: every node is reachable from every other node.Undirected: edges do not have an associated direction...thus in the Mathematical field of graph theory, a spanning tree of the graph is a connected sub-graph in which there are no cycles.A Spanning tree for a graph has no cycles but still connects to every house.If G is a connected graph with n vertices and m edges, spanning tree of G must have n-1 edges, and no. of edges deleted from G to get a spanning tree must be m-(n-1)=m-n+1.A Graph may have many spanning trees; for instance the complete graph on four vertices.

*

*Finding a spanning treeTo find a spanning tree of a graph,

Pick an initial node and call it part of the spanning treedo a search from the initial node:each time you find a node that is not in the spanning tree, add to the spanning tree both the new node and the edge you followed to get to it.

*

*Minimizing costsSuppose you want to supply a set of houses (say, in a new subdivision) with:electric powerwatersewage linestelephone linesTo keep costs down, you could connect these houses with a spanning tree (of, for example, power lines)However, the houses are not all equal distances apartTo reduce costs even further, you could connect the houses with a minimum-cost spanning tree

*

*Minimum-cost spanning treesSuppose you have a connected undirected graph with a weight (or cost) associated with each edgeThe cost or weight of a spanning tree would be the sum of the costs of its edges.A minimum-cost spanning tree is a spanning tree of a connected undirected graph that has the lowest cost.If there are n vertices in the graph , then each spanning tree has n vertices & n-1 edges.

*

*Finding minimum spanning treesThere are two basic algorithms for finding minimum-cost spanning trees, and both are greedy algorithms i.e., these always processes the edge with the least weight or cost.

Kruskals algorithm: Start with no nodes or edges in the spanning tree, and repeatedly add the cheapest edge that does not create a cycleHere, we consider the spanning tree to consist of edges only

Prims algorithm: Start with any one node in the spanning tree, and repeatedly add the cheapest edge, and the node it leads to, for which the node is not already in the spanning tree.Here, we consider the spanning tree to consist of both nodes and edges

*

*Kruskals algorithmInput :A Connected weighted graph GOutput :A minimal spanning tree T.T = empty spanning tree;E = set of edges;N = number of nodes in graph;while T has fewer than N - 1 edges {remove an edge (v, w) of lowest cost from Eif adding (v, w) to T would create a cyclethen discard (v, w)else add (v, w) to T}Finding an edge of lowest cost can be done just by sorting the edges

*

E.G of Krusals AlgorithmFind a minimum spanning tree of the below connected undirected graph by Krusals algorithm.List the edges in non-decreasing order

shown below:

Therefore the minimum spanning tree that will be produced will be:*

*Prims algorithmInput :A connected weighted Graph G. Output :A minimum spanning tree T. T = a spanning tree containing a single node s; E = set of edges adjacent to s; while T does not contain all the nodes {remove an edge (v, w) of lowest cost from Eif w is already in T then discard edge (v, w)else {add edge (v, w) and node w to Tadd to E the edges adjacent to w}}

*

E.G OF PRIMS ALGORITHMFind a minimum spanning tree by using prims algorithm of the below given graph:

*

*

DIFFERENCEPRIMS ALGORITHMIn this, at every step edges of minimum weight that are incident to a vertex already in the tree and not forming a cycle is choosen .

KRUSALS ALGOTITHMIn this, at every step edges of minimum weight that are not necessarily incident to a vertex already in the tree and not forming a cycle is choosen .

*

*The End

*

*

*

*

*

*

*

*

*

*