Stop thinking about WHAT WILL HAPPEN and start thinking about WHAT YOU CAN DO.
--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
  • 747 Offline Downloads
  • Uploaded 1 year ago
0 User(s)
Download PDFOrder Printed Copy

Share it with your friends

Leave your Comments

Text from page-1

GREEDY TECHNIQUE Definition: Greedy technique is a general algorithm design strategy, built on following elements: • configurations: different choices, values to find • objective function: some configurations to be either maximized or minimized The method: • • • • • • Applicable to optimization problems ONLY Constructs a solution through a sequence of steps Each step expands a partially constructed solution so far, until a complete solution to the problem is reached. On each step, the choice made must be Feasible: it has to satisfy the problem’s constraints Locally optimal: it has to be the best local choice among all feasible choices available on that step Irrevocable: Once made, it cannot be changed on subsequent steps of the algorithm NOTE: • Greedy method works best when applied to problems with the greedy-choice property • A globally-optimal solution can always be found by a series of local improvements from a starting configuration. Greedy method vs. Dynamic programming method: • • • LIKE dynamic programming, greedy method solves optimization problems. LIKE dynamic programming, greedy method problems exhibit optimal substructure UNLIKE dynamic programming, greedy method problems exhibit the greedy choice property -avoids back-tracing. Applications of the Greedy Strategy: • • Optimal solutions: Change making Minimum Spanning Tree (MST) Single-source shortest paths Huffman codes Approximations: Traveling Salesman Problem (TSP) Fractional Knapsack problem 1

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

Lecture Notes