-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrim's Algorithm.txt
112 lines (95 loc) · 3.32 KB
/
Prim's Algorithm.txt
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
Code : Prim's Algorithm
Send Feedback
Given an undirected, connected and weighted graph G(V, E) with V number of vertices (which are numbered from 0 to V-1) and E number of edges.
Find and print the Minimum Spanning Tree (MST) using Prim's algorithm.
For printing MST follow the steps -
1. In one line, print an edge which is part of MST in the format -
v1 v2 w
where, v1 and v2 are the vertices of the edge which is included in MST and whose weight is w. And v1 <= v2 i.e. print the smaller vertex first while printing an edge.
2. Print V-1 edges in above format in different lines.
Note: Order of different edges doesn't matter.
Input Format:
Line 1: Two Integers V and E (separated by space)
Next E lines: Three integers ei, ej and wi, denoting that there exists an edge between vertex ei and vertex ej with weight wi (separated by space)
Output Format:
Print the MST, as described in the task.
Constraints :
2 <= V, E <= 10^5
1 <= Wi <= 10^5
Time Limit: 1 sec
Sample Input 1 :
4 4
0 1 3
0 3 5
1 2 1
2 3 8
Input Graph
Sample Output 1 :
0 1 3
1 2 1
0 3 5
//////////////////////============================>>>>>>>>>>>>>>>>>>>>>>>>>>
import java.util.Scanner;
public class Solution {
private static void prims(int[][] adjacencyMatrix) {
int v = adjacencyMatrix.length;
boolean visited[] = new boolean[v];
int weight[] = new int[v];
int parent[] = new int[v];
weight[0] = 0; //initialise weight for 1st as 0
parent[0] = -1; //initialise parebt of first as -1
for(int i = 1; i < v; i++){
weight[i] = Integer.MAX_VALUE; //initialize weights of all vertices as Infinity
}
//here we loop vtimes from 0 to v-1
for(int i = 0; i < v; i++){
// Pick vertex with min weight
int minVertex = findMinVertex(weight, visited);
visited[minVertex] = true;
// Explore its unvisited neighbors
for(int j = 0; j < v; j++){
if(adjacencyMatrix[minVertex][j] != 0 && !visited[j]){
if(adjacencyMatrix[minVertex][j] < weight[j]){
//if weight is less, uodate the weight, and also update the parent
weight[j] = adjacencyMatrix[minVertex][j];
parent[j] = minVertex;
}
}
}
}
// Print edges of MST
for(int i = 1; i < v; i++){
//print the nodes in increasing order lowest higest weight
if(parent[i] < i){
System.out.println(parent[i] + " "+ i +" "+ weight[i]);
}else{
System.out.println(i + " "+ parent[i] +" "+ weight[i]);
}
}
}
//A program to find minimum vertex from the unvisited with minimum weights
private static int findMinVertex(int[] weight, boolean visited[]) {
int minVertex = -1;
for(int i = 0; i < weight.length; i++){
if(!visited[i] && (minVertex == -1 || weight[i] < weight[minVertex])){
minVertex = i;
}
}
return minVertex;
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int v = s.nextInt(); //v is the number of vertices
int e = s.nextInt(); //e is the number of edges
int adjacencyMatrix[][] = new int[v][v];
//loop through the numver of edges to get input
for(int i = 0; i < e; i++){
int v1 = s.nextInt();
int v2 = s.nextInt();
int weight = s.nextInt();
adjacencyMatrix[v1][v2] = weight;
adjacencyMatrix[v2][v1] = weight;
}
prims(adjacencyMatrix);
}
}