forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Prims_Algorithm.cs
77 lines (70 loc) · 2.3 KB
/
Prims_Algorithm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System;
namespace Declaring_Method
{
public class Prim_Algorithm
{
public static void Main(string[] args)
{
int a = 0, b = 0, u = 0, v = 0, n, i, j, edge_no = 1;
int min, mincost = 0;
int[] visited = new int[100];
int[, ] cost = new int[10, 10];
Console.WriteLine("Enter the number of Nodes");
n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the adjency matrix");
//Intilize all visited elements to 0
for (i = 0; i < 100; i++)
{
visited[i] = 0;
}
//Taking Matrix Input from the User
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
cost[i, j] = Convert.ToInt32(Console.ReadLine());
//Initialize Cost Matrix to Infintiy if user input is 0
if (cost[i, j] == 0)
cost[i, j] = 999;
}
}
visited[1] = 1;
Console.WriteLine(" ");
while (edge_no < n)
{
for (i = 1, min = 999; i <= n; i++)
for (j = 1; j <= n; j++)
if (cost[i, j] < min)
if (visited[i] != 0)
{
min = cost[i, j];
//Storing Position
a = u = i;
b = v = j;
}
// Print the Edge Number with its Cost
if (visited[u] == 0 || visited[v] == 0)
{
Console.WriteLine("Edge {0} : ({1} {2}) cost:{3} ", edge_no++, a, b, min);
mincost += min; // Calculating minimum Cost
visited[b] = 1;
}
cost[a, b] = cost[b, a] = 999;
}
Console.WriteLine("Minimum Cost : {0}", mincost);
}
}
}
/*Enter the number of Nodes
5
Enter the adjency matrix
0 2 0 6 0
2 0 3 8 5
0 3 0 0 7
6 8 0 0 9
0 5 7 9 0
Edge 1 : (1 2) cost:2
Edge 2 : (2 3) cost:3
Edge 3 : (2 5) cost:5
Edge 4 : (1 4) cost:6
Minimum Cost : 16 */