LOVELY PROFESSIONAL UNIVERSITY HOME WORK: #3 School: LIE-LEET Department: CSE/IT Name of the facu lty mem ber: Usha Mitt al Cou rse No: CSE408 Course Title: ALGORITHM ANALYSIS AND DESIGN Section: H18T1 Registration number-10807781 Roll number:-RH18T1B57 Max. Marks:7 DOA: 24/03/2011 DOS: 0 1/04/2011 Part-A 1. Show how Prim’s algorithm can be implemented using heap. What would be the time complexity of the algorithm. Ans:-prim’s algorithm continuously increases the size of a tree starting with a single vertex until it spans all the vertices of G. it is used to find minimum spanning tree from a given weighted graph. ALgo:- Consider a weighted, connected, undirected graph G=(V, E) and a free tree T = (VT, ET) that is a subgraph of G Initialize: VT = {x}; // where x is an arbitrary vertex from V Initialize: ET = ∅; repeat{ Choose an edge (v,w) with minimal weight such that v is in V T and w is not (if there are multiple edges with the same weight, choose arbitrarily but consistently) ; // add an minimal weight edge that will not form a cycle
17
Embed
RH18T1B57_Home Work 3_CSE408_archana Sinha 10807781
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
8/6/2019 RH18T1B57_Home Work 3_CSE408_archana Sinha 10807781
Time complexity (total) Graph and Minimum edge weight data structure
O(V 2)adjacency matrix and array
O((V + E) log(V)) = O(E log(V))adjacency list and binary heap
O(E + V log(V))Adjacency list and Fibonacci heap
Implementation of Prim’s Algorithm using MinHeap
• The implementation of Prim's algorithm uses an Entry class, which contains the followingthree fields:
– know: a boolean variable indicating whether the weight of the edge from thisvertex to an active vertex v1 is known, initially false for all vertices.
– weight : the minimum known weight from this vertex to an active vertex v1,initially infinity for all vertices except that of the starting vertex which is 0.
– predecessor : the predecessor of an active vertex v1 in the MCST, initiallyunknown for all vertices
public class Algorithms{
static final class Entry{
boolean known;
int weight;
Vertex predecessor;
Entry(){
known = false;
weight = Integer.MAX_VALUE;
predecessor = null;
8/6/2019 RH18T1B57_Home Work 3_CSE408_archana Sinha 10807781
The idea is to use a table array to remember, for each vertex, the smallest edgeconnecting T with that vertex. Each entry in table represents a vertex x. The objectstored at table entry index(x) will have the predecessor of x and the corresponding edgeweight.
make a table of values (known, weight(edge), predecessor) initially (FALSE, ∞ ,null)for each vertex except the start vertex table entry is set to (FALSE, 0, null) ;
MinHeap = ∅ ;
MinHeap.enqueue( Association (0, startVertex));
// let tree T be empty;
while (MinHeap is not empty) { // T has fewer than n vertices
dequeue an Association (weight(e), v) from the MinHeap;
Update table entry for v to (TRUE, weight(e), v); // add v and e to T
for( each emanating edge f = (v, u) of v){
if( table[u].known == FALSE ) // u is not already in T
if (weight(f) < table[u].weight) { // find the current min edge weight
table[u].weight = weight(f);
table[u].predecessor = v;
enqueue(Association(weight(f), u);
}
}
}
}
Prim’s algorithm can be implemented as shown below:
8/6/2019 RH18T1B57_Home Work 3_CSE408_archana Sinha 10807781
2. We have assign n jobs to n workers, one job per worker. There is a definite cost of assigning workers I to job j , 1<=I, j<=n, Design a greedy algorithm so that workersassigned jobs with the minimum total cost.
Ans: - In assignment problem given n tasks to be completed individuallyby n people, what is the minimum cost of assigning all n tasks to npeople, one task per person, where ci;j is the cost of assigning task j toperson i? Let the decision variables be de¯ned as:
xi;j = ½
1 if person i takes on task j 0 otherwise
The first set of constraints ensures that exactly one person is assignedto each task; the second set of constraints ensures that each person isassigned exactly one task. Notice that the upper bound constraints onthe xij are unnecessary.
greedy algorithm for solving job assignment problem----.
When choices are considered, the choice that looks best means haveminimum weight in the current problem is chosen, without consideringresults from sub problems.
Steps in Design Greedy Algorithms
8/6/2019 RH18T1B57_Home Work 3_CSE408_archana Sinha 10807781