0% found this document useful (0 votes)
3 views

Daa Unit 4 Prims

The document discusses the greedy technique in algorithms, emphasizing that choices must be feasible, locally optimal, and irrevocable. It specifically details Prim's algorithm for finding a minimum spanning tree in a weighted connected graph, explaining how it expands subtrees by attaching the nearest vertex not in the tree. The efficiency of Prim's algorithm varies based on the data structures used, with running times of O(|V|^2) for a weight matrix and O(|E| log |V|) for adjacency lists with a min-heap.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Daa Unit 4 Prims

The document discusses the greedy technique in algorithms, emphasizing that choices must be feasible, locally optimal, and irrevocable. It specifically details Prim's algorithm for finding a minimum spanning tree in a weighted connected graph, explaining how it expands subtrees by attaching the nearest vertex not in the tree. The efficiency of Prim's algorithm varies based on the data structures used, with running times of O(|V|^2) for a weight matrix and O(|E| log |V|) for adjacency lists with a min-heap.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Unit 4_Part2

GREEDY TECHNIQUE
On each step—and this is the central point of this
technique—the choice made must be:

feasible, i.e., it has to satisfy the problem’s constraints

locally optimal, i.e., it has to be the best local choice


among all feasible choices available on that step

irrevocable, i.e., once made, it cannot be changed on


subsequent steps of the algorithm
As a rule, greedy algorithms are both intuitively appealing and simple.

Given an optimization problem, it is usually easy to figure out how to proceed in a


greedy manner, possibly after considering a few small instances of the problem.

The second way to prove optimality of a greedy algorithm is to show that on each step
it does at least as well as any other algorithm could in advancing toward the
problem’s goal.

The third way is simply to show that the final result obtained by a greedy algorithm is
optimal based on the algorithm’s output rather than the way it operates.
9.1 Prim’s Algorithm

DEFINITION A spanning tree of an undirected connected


graph is its connected acyclic subgraph (i.e., a tree) that
contains all the vertices of the graph.

If such a graph has weights assigned to its edges, a


minimum spanning tree is its spanning tree of the
smallest weight, where the weight of a tree is defined as
the sum of the weights on all its edges.

The minimum spanning tree problem is the problem of


finding a minimum spanning tree for a given weighted
connected graph.
Prim’s algorithm constructs a minimum spanning tree through a sequence
of expanding subtrees.

The initial subtree in such a sequence consists of a single vertex


selected arbitrarily from the set V of the graph’s vertices.

On each iteration, the algorithm expands the current tree in the greedy
manner by simply attaching to it the nearest vertex not in that tree.

By the nearest vertex, we mean a vertex not in the tree connected to a


vertex in the tree by an edge of the smallest weight.

Ties can be broken arbitrarily.


EXAMPLE
 The nature of Prim’s algorithm makes it necessary to provide each
vertex not in the current tree with the information about the shortest
edge connecting the vertex to a tree vertex.

 We can provide such information by attaching two labels to a vertex:


the name of the nearest tree vertex and the length (the weight) of
the corresponding edge.

 Vertices that are not adjacent to any of the tree vertices can be given
the ∞ label indicating their “infinite” distance to the tree vertices and
a null label for the name of the nearest tree vertex.

 Alternatively, we can split the vertices that are not in the tree into
two sets, the “fringe” and the “unseen.”
 With such labels, finding the next vertex to be added to the current
tree T = *VT , ET + becomes a simple task of finding a vertex with the
smallest distance label in the set V − VT .

 Ties can be broken arbitrarily.

 After we have identified a vertex u∗ to be added to the tree, we need


to perform two operations:

 Move u∗ from the set V − VT to the set of tree vertices VT .

 For each remaining vertex u in V − VT that is connected to u∗ by a


shorter edge than the u’s current distance label, update its labels by
u∗ and the weight of the edge between u∗ and u, respectively.
 How efficient is Prim’s algorithm?
 The answer depends on the data structures chosen for the graph itself
and for the priority queue of the set V − VT whose vertex priorities
are the distances to the nearest tree vertices.
 (You may want to take another look at the example in Figure 9.3 to
see that the set V − VT indeed operates as a priority queue.)
 In particular, if a graph is represented by its weight matrix and the
priority queue is implemented as an unordered array, the algorithm’s
running time will be in (|V |2).
 Indeed, on each of the |V | − 1 iterations, the array implementing the
priority queue is traversed to find and delete the minimum and then
to update, if necessary, the priorities of the remaining vertices.
 If a graph is represented by its adjacency lists and the priority queue
is implemented as a min-heap, the running time of the algorithm is in
O(|E| log |V |).

 This is because the algorithm performs |V | − 1 deletions of the


smallest element and makes |E| verifications and, possibly, changes
of an element’s priority in a min-heap of size not exceeding |V |.

 Each of these operations, as noted earlier, is a O(log |V |) operation.


 Hence, the running time of this implementation of Prim’s algorithm is
in
(|V | − 1 + |E|)O(log |V |) = O(|E| log |V |) because, in a
connected graph, |V | − 1 ≤ |E|.

You might also like