-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphFromMatrix.java
181 lines (148 loc) · 4.45 KB
/
GraphFromMatrix.java
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package graph_implementation;
import java.util.Arrays;
/**
* Simple Java implementation for Kruskal's algorithm
* @author <a href = "https://www.geeksforgeeks.org/kruskals-algorithm-simple-implementation-for-adjacency-matrix/" > www.geeksforgeeks.org : Kruskal</a>
* @author <a href = "https://www.geeksforgeeks.org/prims-algorithm-simple-implementation-for-adjacency-matrix-representation/" > www.geeksforgeeks.org : Prim</a>
* @author Yamine Ibrahima : Merge et modifications mineures
* */
public class GraphFromMatrix {
private int V;
private int[] parent;
private int[][] edges;
private int INF = Integer.MAX_VALUE;
/**
* Crée un graphe avec V sommet
* @param nombreSommet : Nombre de sommets
* */
public GraphFromMatrix(int nombreSommet) {
this.V = nombreSommet;
this.parent = new int[nombreSommet];
}
/**
* Génère les arêtes dépendamment de la matrice d'adjacence
* passé en paramètre.
* @param adjacencyMatrix Matrice d'adjacence du graphe
* */
public void setEdges(int[][] adjacencyMatrix) {
if(adjacencyMatrix.length != this.V) {
System.out.println("La taille de la matrice d'adjacence est différente de " + this.V);
return;
}else {
//Vérifier le length de chaque ligne de la matrice
for(int i = 0; i < adjacencyMatrix.length ; i++) {
if(adjacencyMatrix[i].length != this.V) {
System.out.println("La taile de la " + (i+1) + " ème ligne de la matrice est différente de "+ this.V);
return;
}
}
}
this.edges = adjacencyMatrix ;
}
/**
* Affiche les arêtes du graphes
* */
public void affichageEdges() {
for(int i = 0; i < this.edges.length; i++)
System.out.println(Arrays.toString(this.edges[i]) );
}
/**----------------------------------------------------------------------- Methodes pour Kruskal -----------------*/
//Find set of vertex i
private int find(int i)
{
while (parent[i] != i)
i = parent[i];
return i;
}
//Does union of i and j. It returns
//false if i and j are already in same
//set.
private void union1(int i, int j)
{
int a = find(i);
int b = find(j);
parent[a] = b;
}
//Finds MST using Kruskal's algorithm
public void kruskalMST() {
int cost[][] = this.edges ;
int mincost = 0; // Cost of min MST.
// Initialize sets of disjoint sets.
for (int i = 0; i < V; i++)
parent[i] = i;
// Include minimum weight edges one by one
int edge_count = 0;
while (edge_count < V - 1)
{
int min = INF, a = -1, b = -1;
for (int i = 0; i < V; i++)
{
for (int j = 0; j < V; j++)
{
if (find(i) != find(j) && cost[i][j] < min)
{
min = cost[i][j];
a = i;
b = j;
}
}
}
union1(a, b);
System.out.printf("Edge %d:(%d, %d) cost:%d \n",
edge_count++, a, b, min);
mincost += min;
}
System.out.printf("\n Minimum cost= %d \n", mincost);
}
/**----------------------------------------------------------------------- Methodes pour PRIM -----------------*/
// Returns true if edge u-v is a valid edge to be
// include in MST. An edge is valid if one end is
// already included in MST and other is not in MST.
static boolean isValidEdge(int u, int v, boolean[] inMST) {
if (u == v)
return false;
if (inMST[u] == false && inMST[v] == false)
return false;
else if (inMST[u] == true && inMST[v] == true)
return false;
return true;
}
public void primMST() {
int cost[][] = this.edges;
boolean []inMST = new boolean[V];
// Include first vertex in MST
inMST[0] = true;
// Keep adding edges while number of included
// edges does not become V-1.
int edge_count = 0, mincost = 0;
while (edge_count < V - 1)
{
// Find minimum weight valid edge.
int min = INF, a = -1, b = -1;
for (int i = 0; i < V; i++)
{
for (int j = 0; j < V; j++)
{
if (cost[i][j] < min)
{
if (isValidEdge(i, j, inMST))
{
min = cost[i][j];
a = i;
b = j;
}
}
}
}
if (a != -1 && b != -1)
{
System.out.printf("Edge %d:(%d, %d) cost: %d \n",
edge_count++, a, b, min);
mincost = mincost + min;
inMST[b] = inMST[a] = true;
}
}
System.out.printf("\n Minimum cost = %d \n", mincost);
}
}
//This code contributed by Rajput-Ji