Top Banner
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

RH18T1B57_Home Work 3_CSE408_archana Sinha 10807781

Apr 08, 2018

Download

Documents

Archana Sinha
Welcome message from author
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
Page 1: RH18T1B57_Home Work 3_CSE408_archana Sinha 10807781

8/6/2019 RH18T1B57_Home Work 3_CSE408_archana Sinha 10807781

http://slidepdf.com/reader/full/rh18t1b57home-work-3cse408archana-sinha-10807781 1/17

LOVELY PROFESSIONAL UNIVERSITY

HOME WORK: #3

School: LIE-LEET Department: CSE/IT

Name of the faculty member: Usha Mittal Course 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 thetime 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 = (V T, E T) that is a subgraph of G

Initialize: V T = {x}; // where x is an arbitrary vertex from V

Initialize: E T = ∅ ;

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

Page 2: RH18T1B57_Home Work 3_CSE408_archana Sinha 10807781

8/6/2019 RH18T1B57_Home Work 3_CSE408_archana Sinha 10807781

http://slidepdf.com/reader/full/rh18t1b57home-work-3cse408archana-sinha-10807781 2/17

Add vertex v to V T;

add edge (v, w) to E T;

}until (VT == V);

Complexity of Prim’s Algorithm

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;

Page 3: RH18T1B57_Home Work 3_CSE408_archana Sinha 10807781

8/6/2019 RH18T1B57_Home Work 3_CSE408_archana Sinha 10807781

http://slidepdf.com/reader/full/rh18t1b57home-work-3cse408archana-sinha-10807781 3/17

}

}

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:

Page 4: RH18T1B57_Home Work 3_CSE408_archana Sinha 10807781

8/6/2019 RH18T1B57_Home Work 3_CSE408_archana Sinha 10807781

http://slidepdf.com/reader/full/rh18t1b57home-work-3cse408archana-sinha-10807781 4/17

Example:-

A

D

E

B

C

3 0 52 0

69

21 2

6

A B C D E0 1 2 3 4

F0

n u l l

F∞

n u l l

F∞

n u l l

F∞

n u l l

F∞

n u l l

t a b l eq u e u ef r o n t r e a

0A

A B C D E0 1 2 3 4

T0

n u l l

F3 0A

F2A

F9A

F∞

n u l l

t a b l eq u e u ef r o n t r e a

2C

9D

3 0B

Page 5: RH18T1B57_Home Work 3_CSE408_archana Sinha 10807781

8/6/2019 RH18T1B57_Home Work 3_CSE408_archana Sinha 10807781

http://slidepdf.com/reader/full/rh18t1b57home-work-3cse408archana-sinha-10807781 5/17

A B C D E0 1 2 3 4

T0

n u l l

F5C

T2A

F6C

F6C

t a b l eq u e u ef r o n t r e a

6D

5B

6E

3 0B

9D

A B C D E0 1 2 3 4

T0

n u l l

T5C

T2A

F6C

F6C

t a b l eq u e u ef r o n t r e a

6E

6D

3 0B

9D

A B C D E0 1 2 3 4

T0

n u l l

T5C

T2A

T6C

F6C

t a b l eq u e u ef r o n t r e a

9D

6

E3 0

B

Page 6: RH18T1B57_Home Work 3_CSE408_archana Sinha 10807781

8/6/2019 RH18T1B57_Home Work 3_CSE408_archana Sinha 10807781

http://slidepdf.com/reader/full/rh18t1b57home-work-3cse408archana-sinha-10807781 6/17

The Final result

A B C D E0 1 2 3 4

T0

n u l l

T5C

T2A

T6C

T6C

t a b l e

q u e u ef r o n t r e a

A

D

E

B

C

5

6

2 6

Page 7: RH18T1B57_Home Work 3_CSE408_archana Sinha 10807781

8/6/2019 RH18T1B57_Home Work 3_CSE408_archana Sinha 10807781

http://slidepdf.com/reader/full/rh18t1b57home-work-3cse408archana-sinha-10807781 7/17

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

Page 8: RH18T1B57_Home Work 3_CSE408_archana Sinha 10807781

8/6/2019 RH18T1B57_Home Work 3_CSE408_archana Sinha 10807781

http://slidepdf.com/reader/full/rh18t1b57home-work-3cse408archana-sinha-10807781 8/17

Determine first the minimum value including all job persons.

After tha determine the optimal substructure of the problem.

Create a recursive solution.

• Prove that at any stage of the recursion, one of the optimalchoices is the greedy choice. Thus, it is always safe tomake the greedy choice.

• Show that all but one of the sub problems induced byhaving made the greedy choice are empty.

Generate a recursive algorithm that implements the greedy strategy.

If you want you can Convert the recursive algorithm to an iterative

algorithm. Because of reducing algorithm complexity.

3. Solve the following 0/1 knapsack problem using branch and bound

P=(11,21,31,33), W=(2,11,22,15) ,C=40, n=4

Ans:- First we create table with above given data. That is drawn below------

Table

Item No. Weight(w) Value(v) Value/Weight

1 2 11 5.5

2 11 21 1.9093 22 31 1.409

4 15 33 2.2

formula for upper bound : v+(W-w)(v i+1 /w i+1)

Page 9: RH18T1B57_Home Work 3_CSE408_archana Sinha 10807781

8/6/2019 RH18T1B57_Home Work 3_CSE408_archana Sinha 10807781

http://slidepdf.com/reader/full/rh18t1b57home-work-3cse408archana-sinha-10807781 9/17

Using above formula we find the following solution:

With item W/O 1

With item W /O

Start i=0, w=0, v=0,v/w=5.5 UB =0+(40-0)(5.5) =220

i=1, w=2, v=11,v/w=1.909 UB=11+(40-2)(1.909) =83.542

i=2, w=2, v=11,v/w=1.409UB=11+(40-2)(1.409)=64.542 notconsidered.

i=2, w=2+11=13,v=11+21=32,v/w=1.409 UB=32+(40-13)(1.409) =70.043

i=0, w=0, v=0,v/w=1.909 UB=0+(40-0)(1.909) =76.36

not considered.

Page 10: RH18T1B57_Home Work 3_CSE408_archana Sinha 10807781

8/6/2019 RH18T1B57_Home Work 3_CSE408_archana Sinha 10807781

http://slidepdf.com/reader/full/rh18t1b57home-work-3cse408archana-sinha-10807781 10/17

So the final solution is:-65

Part-B

4. Design Huffman codes for the following symbols: a,b,c,d,e,f and I having relativefrequencies 2,4,6,8,10,12,16 respectively

Ans:-

Huffman Algorithm:-

Huffman(c)

N=c

Q=c

For(i=1 to n-1)

Allocate a new node z

With item W/O

With item W/O

i=4, w=13, v=32,v/w=0 UB.=32+(40-13)(0) =32not considered.

i=4,w=13+15=28,v=32+33=65,v/w=0 UB=65+(40-28)(0) =65

i=3, w=13, v=32,v/w=2.2 UB=32+

(40-13)(2.2) =91.4

i=3, w=13+22=35,v=31+32=63, v/w=2.2

UB.=63+(40-35)(2.2)=74 not considered.

Page 11: RH18T1B57_Home Work 3_CSE408_archana Sinha 10807781

8/6/2019 RH18T1B57_Home Work 3_CSE408_archana Sinha 10807781

http://slidepdf.com/reader/full/rh18t1b57home-work-3cse408archana-sinha-10807781 11/17

z.left=x=extract min(q)

z.right=y=extract min(q)

z.freq=x.freq+y.freq

insert(q,z)

return extract min(q)

Table for given nodes and their frequencies is given below:

Node a b c d e f I

Frequency 2 4 6 8 10 12 16

Page 12: RH18T1B57_Home Work 3_CSE408_archana Sinha 10807781

8/6/2019 RH18T1B57_Home Work 3_CSE408_archana Sinha 10807781

http://slidepdf.com/reader/full/rh18t1b57home-work-3cse408archana-sinha-10807781 12/17

Final tree

We assign 0 for each left node and 1 for right node after that code for eachnode:-

Page 13: RH18T1B57_Home Work 3_CSE408_archana Sinha 10807781

8/6/2019 RH18T1B57_Home Work 3_CSE408_archana Sinha 10807781

http://slidepdf.com/reader/full/rh18t1b57home-work-3cse408archana-sinha-10807781 13/17

Page 14: RH18T1B57_Home Work 3_CSE408_archana Sinha 10807781

8/6/2019 RH18T1B57_Home Work 3_CSE408_archana Sinha 10807781

http://slidepdf.com/reader/full/rh18t1b57home-work-3cse408archana-sinha-10807781 14/17

Topological Sort Algorithm

• One way to find a topological sort is to consider in-degrees of the vertices.

• The first vertex must have in-degree zero -- every DAG must have at least one vertexwith in-degree zero.

• The Topological sort algorithm is:

Steps for toplogoical sorting :-

• First of all initialize a sorted list that is empty, and set a counter to 0

• Start Computing the indegrees of all nodes

• Take a queue and Store all nodes with indegree 0 in a queue

• Repeat steps while the queue is not empty

o Take a node U and put it in the sorted list. Increment the counter.

o For all edges (U,V) decrement the indegree of V, and put V in the

queue if the updated indegree is 0.• Check for cycle exists or not.

int topologicalOrderTraversal( ){

int numVisitedVertices = 0;

while(there are more vertices to be visited){

if(there is no vertex with in-degree 0)

break;

else{

select a vertex v that has in-degree 0;

visit v;

Page 15: RH18T1B57_Home Work 3_CSE408_archana Sinha 10807781

8/6/2019 RH18T1B57_Home Work 3_CSE408_archana Sinha 10807781

http://slidepdf.com/reader/full/rh18t1b57home-work-3cse408archana-sinha-10807781 15/17

numVisitedVertices++;

delete v and all its emanating edges;

}

}

return numVisitedVertices;

}

Implementation of Topological Sort

BinaryHeap queue = new BinaryHeap(numberOfVertices);

p = getVertices();

while(p.hasNext()){

Vertex v = (Vertex)p.next();

if(inDegree[getIndex(v)] == 0)

queue.enqueue(v);

}

while(!queue.isEmpty() && !visitor.isDone()){

Vertex v = (Vertex)queue.dequeueMin();

visitor.visit(v);

numVerticesVisited++;

p = v.getSuccessors();

while (p.hasNext()){

Vertex to = (Vertex) p.next();

if(--inDegree[getIndex(to)] == 0)

queue.enqueue(to);

}

Page 16: RH18T1B57_Home Work 3_CSE408_archana Sinha 10807781

8/6/2019 RH18T1B57_Home Work 3_CSE408_archana Sinha 10807781

http://slidepdf.com/reader/full/rh18t1b57home-work-3cse408archana-sinha-10807781 16/17

}

return numVerticesVisited;

}

ANALYSIS after implementation:

When using Matrix representation: O (|V|2)

When using Adjacency lists: O (|E| + |V|)

6. Find a Hamiltonian circuit in the following graph using backtracking.

A Hamiltonian path (or traceable path) is a path in an undirected graph which visits eachvertex exactly once.

Ans:-

A

E

D

B

F

C

Page 17: RH18T1B57_Home Work 3_CSE408_archana Sinha 10807781

8/6/2019 RH18T1B57_Home Work 3_CSE408_archana Sinha 10807781

http://slidepdf.com/reader/full/rh18t1b57home-work-3cse408archana-sinha-10807781 17/17

Ans:

(back track)

(back track)

Root node

Backtracking concepts come when we don’t find any solution in that particular path.

So the final Hamiltonian circuit is A B F E C D A

B

C

D

E

Ff

E

D F

F

E

C

D

A

A