Docsity

PRIM'S ALGORITHM ASSIGNMENT, Assignments of Computer Science

Partial preview of the text

Download PRIM'S ALGORITHM ASSIGNMENT and more Assignments Computer Science in PDF only on Docsity!

IMPLEMENT PRIM’S

ALGORITHM

Submitted By:

Shikhar Gupta IIT

Prakash Toppo IIT

Nikil Chouriya IIT

● In this paper we have implement

prim’s algorithm using adjacency

matrix.

● Prim’s Algorithm is an approach to

determine minimum cost spanning

tree..

ABSTRACT

● We start with single edge of graph and we

add edges to it and finally we get

minimum cost tree.

● In this case, as well, we have n-1 edges

when number of nodes in graph are n. We

again and again add edges to tree and

tree is extended to create spanning tree.

● While in case of Kruskal’s algorithm there

may be more than one tree, which is

finally connected through edge to create

spanning tree.

This algorithm creates spanning tree with

minimum weight from a given weighted

graph.

Step 1: Begin

Step 2: Create edge list of given graph with

their weights.

Step 3: Draw all nodes to create skeleton for

spanning tree.

Step 4: Select an edge with lowest weight And

add it to skeleton and delete Edge from edge

list.

ALGORITHM

#include<stdio.h> #include<stdlib.h> int a,b,u,v,n,i,j,ne=1; int visited[10]={0},min,mincost=0; int cost[10][10]; void main() { printf("\nEnter the number of nodes:"); scanf("%d",&n); printf("\nEnter the adjacency matrix:\n"); for(i=1;i<=n;i++) for(j=1;j<=n;j++) { PROGRAM:

scanf("%d",&cost[i][j]);

if(cost[i][j]==0)

cost[i][j]=999;

visited[1]=1;

printf("\n");

while(ne < n)

for(i=1,min=999;i<=n;i++)

for(j=1;j<=n;j++)

if(cost[i][j]< min)

if(visited[i]!=0)

In prim's algorithm using an adjacency matrix representation time requires is O(n^2) ● However, this running time can be greatly improved further by using binary heaps to implement finding minimum weight edges in the algorithm's inner loop. ● If the input graph is represented using adjacency list, then the time complexity of Prim’s algorithm can be reduced to O(E log V) with the help of binary heap. ● Worst case time complexity: Θ(E log V) using priority queues. ● Average case time complexity: Θ(E log V) using priority queues. ● Best case time complexity: Θ(E log V) using priority queues. ● Space complexity: Θ(E + V) TIME AND SPACE COMPLEXITY:

● Using above adjacency matrix

representation we have implemented

prim’s algorithm

● And also discuss the time and space

complexity of prims algorithm

CONCLUSION :