×
Nothing in the world is more common than unsuccessful people with talent.
--Your friends at LectureNotes

# Note for Data Structure and Algorithms - DSA By chaitanya varma

• Data Structure and Algorithms - DSA
• Note
• Vellore institute of Technology University - VIT
• Computer Science Engineering
• 14 Topics
• 26397 Views
• Uploaded 1 year ago
Touch here to read
Page-2

#### Note for Data Structure and Algorithms - DSA By chaitanya varma

Topic: / 121

0 User(s)

#### Text from page-2

Spanning Tree Definition: Spanning tree is a connected acyclic sub-graph (tree) of the given graph (G) that includes all of G’s vertices Example: Consider the following graph 1 b a 2 5 c d 3 The spanning trees for the above graph are as follows: 1 1 b b a a 2 5 d 5 3 c d c Weight (T2) = 8 Weight (T1) = 9 1 a b 2 d c 3 Weight (T3) = 6 Minimum Spanning Tree (MST) Definition: MST of a weighted, connected graph G is defined as: A spanning tree of G with minimum total weight. Example: Consider the example of spanning tree: For the given graph there are three possible spanning trees. Among them the spanning tree with the minimum weight 6 is the MST for the given graph Question: Why can’t we use BRUTE FORCE method in constructing MST ? Answer: If we use Brute force method• Exhaustive search approach has to be applied. • Two serious obstacles faced: 1. The number of spanning trees grows exponentially with graph size. 2. Generating all spanning trees for the given graph is not easy. 2

#### Text from page-3

MST Applications: • • • • • • Network design. Telephone, electrical, hydraulic, TV cable, computer, road Approximation algorithms for NP-hard problems. Traveling salesperson problem, Steiner tree Cluster analysis. Reducing data storage in sequencing amino acids in a protein Learning salient features for real-time face verification Auto config protocol for Ethernet bridging to avoid cycles in a network, etc Prim’s Algorithm -to find minimum spanning tree Some useful definitions: Fringe edge: An edge which has one vertex is in partially constructed tree Ti and the other is not. • Unseen edge: An edge with both vertices not in Ti • Algorithm: ALGORITHM Prim (G) //Prim’s algorithm for constructing a MST //Input: A weighted connected graph G = { V, E } //Output: ET the set of edges composing a MST of G // the set of tree vertices can be initialized with any vertex VT  { v0} ET  Ø for i  1 to |V| - 1 do Find a minimum-weight edge e* = (v*, u*) among all the edges (v, u) such that v is in VT and u is in V - VT VT  VT U { u*} ET  ET U { e*} return ET 3

#### Text from page-4

The method: STEP 1: Start with a tree, T0, consisting of one vertex STEP 2: “Grow” tree one vertex/edge at a time • Construct a series of expanding sub-trees T1, T2, … Tn-1. • At each stage construct Ti + 1 from Ti by adding the minimum weight edge connecting a vertex in tree (Ti) to one vertex not yet in tree, choose from “fringe” edges (this is the “greedy” step!) Algorithm stops when all vertices are included Example: Apply Prim’s algorithm for the following graph to find MST. 1 b 3 c 4 5 a 4 6 5 f d 2 8 6 e Solution: Tree vertices Remaining vertices a ( -, - ) b(a,3) c(-,∞) d(-,∞) e(a,6) f(a,5) b ( a, 3 ) c(b,1) d(-,∞) e(a,6) f(b,4) Graph b 3 a 1 3 b c a 1 c ( b, 1 ) d(c,6) e(a,6) f(b,4) b c 3 4 a f 4

#### Text from page-5

1 b c 3 f ( b, 4) d(f,5) e(f,2) 4 a f 2 e 1 b 3 e ( f, 2) d(f,5) a c 4 5 f d 2 e d( f, 5) Algorithm stops since all vertices are included. The weight of the minimum spanning tree is 15 Efficiency: Efficiency of Prim’s algorithm is based on data structure used to store priority queue. • Unordered array: Efficiency: Θ(n2) • Binary heap: Efficiency: Θ(m log n) • Min-heap: For graph with n nodes and m edges: Efficiency: (n + m) log n Conclusion: • • Prim’s algorithm is a “vertex based algorithm” Prim’s algorithm “Needs priority queue for locating the nearest vertex.” The choice of priority queue matters in Prim implementation. o Array - optimal for dense graphs o Binary heap - better for sparse graphs o Fibonacci heap - best in theory, but not in practice 5