Skip to content
This repository has been archived by the owner on May 29, 2024. It is now read-only.

Bugfix prims java #1305

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
192 changes: 124 additions & 68 deletions algorithms/Java/graphs/Prims.java
Original file line number Diff line number Diff line change
@@ -1,81 +1,137 @@
/**
Approach-
It starts with an empty spanning tree. The idea is to maintain two sets of vertices.
The first set contains the vertices already included in the MST, the other set contains the vertices not yet included.
At every step, it considers all the edges that connect the two sets and picks the minimum weight edge from these edges.
After picking the edge, it moves the other endpoint of the edge to the set containing MST.
It starts by initializing three arrays, parent, key, and inMST.
The parent array is used to store the parent of each vertex in the MST.
The key array is used to store the minimum weight edge required to reach that vertex.
The inMST array is used to keep track of whether a vertex is in the MST or not.
A priority queue (pq) is used to keep track of vertices and their key values. The priority queue is ordered based on the key values of the vertices.
The algorithm starts with an arbitrary node (vertex 0 in this case) and adds it to the priority queue with key value 0.
The main loop continues until the pririty queue is empty, and in each iteration, the algo extracts the vertex with the minimum key value from the priority queue.
For each neighbor of the extracted vertex that is not yet in the MST and has lower key value, the algo updates its key value and parent in the parent array.
*/

/*
Time complexity -
Time Complexity: O(V^2)
If the input graph is represented using an adjacency list, then the time complexity of Prim’s algorithm can be reduced to O(E log V) with the help of a binary heap.
In this implementation, we are always considering the spanning tree to start from the root of the graph.
Time Complexity: O(E log V)
The main operations contributing to the time complexity are the priority queue operations inside the main loop
In the worst case, each edge is considered once, and the priority queue operations take O(log V) time, where V is the number of vertices

Auxiliary Space: O(V)
Auxiliary Space: O(V + V + V + V) = O(V)
*/

import java.util.Scanner;
public class Prims{
public static void main(String[] args) {
int w[][]=new int[10][10];
int min,mincost=0, u, v, flag=0;
int sol[]=new int[10];
System.out.println("Enter the number of vertices");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
System.out.println("Enter the weighted graph");
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
w[i][j]=sc.nextInt();
System.out.println("Enter the source vertex");
int s=sc.nextInt();
for(int i=1;i<=n;i++) // Initialise the solution matrix with 0
sol[i]=0;
sol[s]=1; // mark the source vertex
int count=1;
while (count<=n-1)
{
min=99; // If there is no edger between any two vertex its cost is assumed to be 99
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(sol[i]==1&&sol[j]==0) //this will check the edge if not already traversed will be considered
if(i!=j && w[i][j]<min)
{
min=w[i][j]; //cost of the edge
u=i;
v=j;
}
sol[v]=1;
mincost += min; //mincost of whole graph
count++;
System.out.println(u+"->"+v+"="+min);
}
import java.util.*;

public class Prims {
public int[] prims(List<List<Node>> adjList) {
int[] parent = new int[adjList.size()];
int[] key = new int[adjList.size()];
boolean[] inMST = new boolean[adjList.size()];

Arrays.fill(key, Integer.MAX_VALUE);
Arrays.fill(inMST, false);

PriorityQueue<Node> pq = new PriorityQueue<>(adjList.size(), new NodeComparator());
pq.add(new Node(0, 0));
key[0] = 0; // starting vertex

while (!pq.isEmpty()) {
Node curr = pq.poll(); // current nde
inMST[curr.vertex] = true;

for(i=1;i<=n;i++)
if(sol[i]==0)
flag=1;
if(flag==1)
System.out.println("No spanning tree");
else
System.out.println("The cost of minimum spanning tree is"+mincost);
sc.close();
for (Node n : adjList.get(curr.vertex)) {
if (!inMST[n.vertex] && key[n.vertex] > n.key) {
parent[n.vertex] = curr.vertex;
key[n.vertex] = n.key;
pq.add(new Node(n.vertex, n.key));
}
}
}

return parent;
}

public static void main(String[] args) {
/*
SAMPLE INPUT AND OUTPUT
___________________________

Input:
__________
Please enter the number of nodes N. Vertices will be [0, N-1]
6
Please Enter the number of Edges
9
Please enter each edge in the sequence <starting node> <destination node> <weight>
0 1 1
0 2 5
1 2 2
1 4 1
1 3 2
2 4 2
3 5 1
3 4 3
4 5 2
Output:
_____________
Minimum Spanning Tree:
Edge: 0 - 1, Weight: 1
Edge: 1 - 4, Weight: 1
Edge: 2 - 1, Weight: 2
Edge: 3 - 5, Weight: 1
Edge: 4 - 2, Weight: 2
*/

List<List<Node>> adjList = new ArrayList<>();

System.out.println("Please enter the number of nodes N. Vertices will be [0, N-1]");
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();

for (int i = 0; i < N; i++) {
adjList.add(new ArrayList<>());
}

System.out.println("Please Enter the number of Edges");
int E = sc.nextInt();

System.out.println("Please enter each edge in the sequence <starting node> <destination node> <weight>");
for (int i = 0; i < E; i++) {
int u = sc.nextInt(); // source
int v = sc.nextInt(); // destination
int weight = sc.nextInt();

adjList.get(u).add(new Node(v, weight));
adjList.get(v).add(new Node(u, weight));
}
sc.close();
Prims prims = new Prims();
int[] parent = prims.prims(adjList);

System.out.println("Minimum Spanning Tree: " + parent.length);
for (int i = 1; i < N; i++) {
System.out.println("Edge: " + parent[i] + " - " + i + ", Weight: " + adjList.get(i-1).get(parent[i]).key);
}
}
}

class Node {
int vertex;
int key;

Node(int vertex, int key) {
this.vertex = vertex;
this.key = key;
}
}

class NodeComparator implements Comparator<Node> {
@Override
public int compare (Node node1, Node node2) {
if (node1.key < node2.key)
return -1;
if (node1.key > node2.key)
return 1;
return 0;
}
}

/* Let us create the following graph
2 3
(0)--(1)--(2)
| / \ |
6| 8/ \5 |7
| / \ |
(3)-------(4)
9
{
{ 99, 2, 99, 6, 99 },
{ 2, 99, 3, 8, 5 },
{ 99, 3, 99, 99, 7 },
{ 6, 8, 99, 99, 9 },
{ 99, 5, 7, 9, 99 }
};
*/