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

DAA MINI PROJECT

Uploaded by

gp6408
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)
40 views

DAA MINI PROJECT

Uploaded by

gp6408
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/ 11

PRIMS ALGORITHM

A MINI PROJECT REPORT

18CSC204J - DESIGN AND ANALYSIS OF


ALGORITHMS

Submitted by

ANDALAM SAI VIGNESH


[RA2111003011353]
G YADU PRAVEER
[RA2111003011374]

Under the guidance of

Dr. A. Anbarasi
Assistant Professor, Department of Computer Science and Engineering

in partial fulfillment for the award of the degree of

BACHELOR OF TECHNOLOGY
in

COMPUTER SCIENCE & ENGINEERING


of

FACULTY OF ENGINEERING AND TECHNOLOGY


TABLE OF CONTENTS

ABSTRACT

TABLE OF CONTENTS

1 PROBLEM STATEMENT

2 DESIGN APPROACH

3 PRIMS ALGORITHM

4 PSEUDOCODE

5 CODE

6 OUTPUT

7 TIME COMPLEXITY

8 IMPLEMENTATION AND ANALYSIS

9 CONCLUSION
ABSTRACT:
Prim's algorithm is a greedy algorithm used for finding the minimum
spanning tree (MST) of a weighted undirected graph. The algorithm
starts with a single vertex and incrementally adds vertices to the tree,
always choosing the edge with the minimum weight that connects the
tree to a vertex not yet in the tree. The process continues until all vertices
are included in the MST. Prim's algorithm is efficient and has a time
complexity of O(E log V), where E is the number of edges and V is the
number of vertices in the graph. It has applications in network design,
circuit layout, and transportation planning.

PROBLEM STATEMENT:
You are given a map of a city with N intersections and M roads
connecting them. Each road has a certain length and a certain traffic
congestion level. You want to find the best route from your home to your
office that minimizes the total travel time.
you can use Prim’s algorithm directly on the original graph, by starting
from your home and adding the edge with the smallest weight that
connects to an unvisited vertex, until you reach your office. This will also
give you the shortest path from your home to your office, but it may not
be part of the minimum spanning tree of the graph.

About:
A Minimum Spanning Tree (MST) is a subset of the edges of a
connected, edge-weighted undirected graph that connects all the vertices
together, without any cycles and with the minimum possible total edge
weight. That is, it is a spanning tree whose sum of edge weights is as
small as possible.

DESIGN APPROACHE:
• Prim’s Algorithm

PRIMS ALGORITHM:
Prim's algorithm is another popular greedy algorithm used to find the
minimum spanning tree of a connected, weighted, undirected graph. It
works as follows:
Choose an arbitrary vertex to start with and add it to the minimum
spanning tree.
Find the edge with the smallest weight that connects the minimum
spanning tree to a vertex not yet in the minimum spanning tree.

PSEUDOCODE:
Procedure prims
G-input graph
U-random vertex
Vvertices in graph G
begin

T= 0;
U = \{1\} while (U = V)
let (u, v) be the least cost edge such that u in U and v in V - U ;
T=T cup \{(u, v)\}
U=U cup \{v\} end
procedure

CODE:
#include<stdio.h> main()

{
int size,i,j,mini,minj,k=1,mincost=0,total=0,p,q;
printf("\nEnter the number of elements in graph:");
scanf("%d",&size); char name[size][10]; int
graph[size][size],visited[size];

//input zone; printf("\nEnter the elements?\n");


for(i=0;i<size;i++) scanf("%s",&name[i]);

printf("\nEnter the graph?\n"); for(i=0;i<size;i++)


{
for(j=0;j<size;j++)
{
scanf("%d",&graph[i][j]);
if(graph[i][j]==0) graph[i][j]=999;

} visited[i]=0;

}
visited[0]=1; //Processing
unit; while(k<size)

{
mini=0; minj=0;
for(i=0;i<size;i
++)

{
for(j=0;j<size;j++)
{
if(graph[i][j]<graph[mini][minj] && visited[i]!=0)
{
mini=i; minj=j;
mincost=graph[i][j];

}
}
}
if(visited[mini]==0 || visited[minj]==0)
{
printf("\n%d-edge (%s,%s)
=%d\n",k++,name[mini],name[minj],mincost)
; total+=mincost; visited[minj]=1;

}
graph[mini][minj]=999; graph[minj][mini]=999;
}
printf("\nTotal minimum cost=%d.\n",total);
}
OUTPUT:
Enter the number of elements in graph:6 Enter
the elements?

012345
Enter the graph:
044000
402000
420342
003030
004303
002030

1-edge (0,1) =4
2-edge (1,2) =2
3-edge (2,5) =2
4-edge (2,3) =3
5-edge (3,4) =3
Total minimum cost=14.

TIME COMPLEXITY:
In summary, Prims algorithm requires- •
A worst-case time complexity of O(V^2).
• An average-case time complexity of
O(E + (V)log V).
• A best-case time complexity of O (V).
• A space complexity of O(E+V).
IMPLEMENTATION OF PRIMS ALGORITHM
ANALYSIS:
The advantage of Prim’s algorithm is its complexity .Therefore, Prim’s
algorithm is helpful when dealing with dense graphs that have lots of
edges.

However, Prim’s algorithm doesn’t allow us much control over the chosen
edges when multiple edges with the same weight occur. The reason is
that only the edges discovered so far are stored inside the queue.
CONCLUSION:

Prims algorithm is more efficient and more easy to find shortest path from
your home to your office

You might also like