0% found this document useful (0 votes)
109 views17 pages

RH18T1B57 - Home Work 3 - CSE408 - Archana Sinha 10807781

The document discusses Prim's algorithm for finding the minimum spanning tree of a weighted graph. It explains that Prim's algorithm can be implemented using a min heap to efficiently retrieve the minimum weight edge at each step. The time complexity of Prim's algorithm using a min heap is O(E log V), where E is the number of edges and V is the number of vertices. Pseudocode is provided to demonstrate how to implement Prim's algorithm using a min heap to track the minimum weight edges connecting vertices in the growing minimum spanning tree.

Uploaded by

Archana Sinha
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views17 pages

RH18T1B57 - Home Work 3 - CSE408 - Archana Sinha 10807781

The document discusses Prim's algorithm for finding the minimum spanning tree of a weighted graph. It explains that Prim's algorithm can be implemented using a min heap to efficiently retrieve the minimum weight edge at each step. The time complexity of Prim's algorithm using a min heap is O(E log V), where E is the number of edges and V is the number of vertices. Pseudocode is provided to demonstrate how to implement Prim's algorithm using a min heap to track the minimum weight edges connecting vertices in the growing minimum spanning tree.

Uploaded by

Archana Sinha
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 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 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 VT 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
Add vertex v to VT;

add edge (v, w) to ET;

}until (VT == V);

Complexity of Prim’s Algorithm

Graph and Minimum edge weight data structure Time complexity (total)

adjacency matrix and array O(V2)

adjacency list and binary heap O((V + E) log(V)) = O(E log(V))

Adjacency list and Fibonacci heap O(E + V log(V))

Implementation of Prim’s Algorithm using MinHeap

• The implementation of Prim's algorithm uses an Entry class, which contains the following
three fields:

– know: a boolean variable indicating whether the weight of the edge from this
vertex 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, initially


unknown 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;
}

The idea is to use a table array to remember, for each vertex, the smallest edge
connecting T with that vertex. Each entry in table represents a vertex x. The object
stored at table entry index(x) will have the predecessor of x and the corresponding edge
weight.

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:


Example:-

B
2 0
3 0 5
1 2
2 C 6 E
A

9 6
D

A B C D E
0 1 2 3 4 f r o n t q u e u e r e a r
t a b l e

F F F F F 0
0 ∞ ∞ ∞ ∞ A
n u l ln u l ln u l ln u l ln u l l

A B C D E
0 1 2 3 4 f r o n t q u e u e r e a r
t a b l e

T F F F F 2 9 3 0
0 3 0 2 9 ∞ C D B
n u l l A A A n u l l
A B C D E
0 1 2 3 4 f r o n t q u e u e r e a r
t a b l e

T F T F F 5 6 6 9 3 0
0 5 2 6 6 B D E D B
n u l l C A C C

A B C D E
0 1 2 3 4 f r o n t q u e u e r e a r
t a b l e

T T T F F 6 6 9 3 0
0 5 2 6 6 D E D B
n u l l C A C C

A B C D E
0 1 2 3 4 f r o n t q u e u e r e a r
t a b l e

T T T T F 6 9 3 0
0 5 2 6 6 E D B
n u l l C A C C
A B C D E
0 1 2 3 4 f r o n t q u e u e r e a r
t a b l e

T T T T T
0 5 2 6 6
n u l l C A C C

The Final result

B
5
2 C 6 E
A

6
D
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 workers
assigned jobs with the minimum total cost.

Ans:- In assignment problem given n tasks to be completed individually


by n people, what is the minimum cost of assigning all n tasks to n
people, one task per person, where ci;j is the cost of assigning task j to
person 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 assigned
to each task; the second set of constraints ensures that each person is
assigned exactly one task. Notice that the upper bound constraints on
the xij are unnecessary.

greedy algorithm for solving job assignment problem----.

When choices are considered, the choice that looks best means have
minimum weight in the current problem is chosen, without considering
results from sub problems.

Steps in Design Greedy Algorithms


 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 optimal


choices is the greedy choice. Thus, it is always safe to
make the greedy choice.

• Show that all but one of the sub problems induced by


having 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.909
3 22 31 1.409
4 15 33 2.2

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


Using above formula we find the following solution:

Start i=0, w=0, v=0,


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

With item W/O 1


i=0, w=0, v=0,
i=1, w=2, v=11, v/w=1.909 UB=0+
v/w=1.909 UB=11+(40- (40-0)(1.909) =76.36
2)(1.909) =83.542 not considered.

With item W /O
2
i=2, w=2+11=13,
v=11+21=32, i=2, w=2, v=11,
v/w=1.409 UB=32+(40- v/w=1.409
13)(1.409) =70.043 UB=11+(40-2)(1.409)
=64.542 not
considered.
With item W/O

i=3, w=13+22=35, i=3, w=13, v=32,


v=31+32=63, v/w=2.2 v/w=2.2 UB=32+
UB.=63+(40-35)(2.2) (40-13)(2.2) =91.4
=74 not considered.

With item W/O


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

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 relative
frequencies 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


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
Final tree

We assign 0 for each left node and 1 for right node after that code for each
node:-
Node a b c d e f I
Code 011 011 01 11 11 0
s 0 1 0 0 1 0 10

5. Topological sorting on a directed acyclic graph may be carried out by removing


nodes with in-degree zero one by one. Design an algorithm for this and analyze its
complexity for both the adjacency matrix and adjacency list representations

Ans:-

• Topological sort is a method of arranging the vertices in a directed acyclic graph


(DAG), as a sequence, such that no vertex appear in the sequence before its
predecessor.

• The graph in (a) can be topologically sorted as in (b)


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 vertex
with 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;
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);

}
}

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 each
vertex exactly once.

B
A
Ans:-
F

D C

E
Ans: A

C F

D E E

E D F C

(back track)
F
D
f

(back track) A

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

You might also like