src/app/components/graphe-from-matrix-adja/graphe-from-matrix-adja.component.ts
selector | app-graphe-from-matrix-adja |
styleUrls | graphe-from-matrix-adja.component.css |
templateUrl | graphe-from-matrix-adja.component.html |
container
|
Input container object for handling graph-related properties.
Type: |
constructor(grapheS: any, translate: TranslateService)
|
Constructor for the GrapheFromMatrixAdjaComponent.
Parameters :
|
generateGraph |
generateGraph()
|
Generates a graph based on the provided adjacency matrix.
Returns:
void
|
parseAdjacencyMatrix |
parseAdjacencyMatrix(matrixText: string)
|
Parses the input adjacency matrix in text format and returns a 2D array.
Parameters :
Returns:
any
|
createGraphElements |
createGraphElements(adjacencyMatrix: number[][])
|
Creates graph elements (nodes and edges) from the parsed adjacency matrix.
Parameters :
Returns:
any[]
|
isWeighted |
isWeighted(adjacencyMatrix: number[][])
|
Checks if the adjacency matrix represents a weighted graph.
Parameters :
Returns:
boolean
|
isSymmetric |
isSymmetric(matrix: number[][])
|
Checks if the adjacency matrix is symmetric, indicating an undirected graph.
Parameters :
Returns:
void
|
matrixText |
matrixText: |
The adjacency matrix in text format. |
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import { Component, Input } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { GrapheService } from 'src/app/Services/graphe.service';
/**
* Represents a weighted edge in a graph.
*
* @interface IWeightedEdge
*/
interface IWeightedEdge {
/**
* The source node of the edge.
*
* @type {string}
* @memberof IWeightedEdge
*/
source: string;
/**
* The target node of the edge.
*
* @type {string}
* @memberof IWeightedEdge
*/
target: string;
/**
* The weight of the edge.
*
* @type {number}
* @memberof IWeightedEdge
*/
weight: number;
}
/**
* Represents an unweighted edge in a graph.
*
* @interface IUnweightedEdge
*/
interface IUnweightedEdge {
/**
* The source node of the edge.
*
* @type {string}
* @memberof IUnweightedEdge
*/
source: string;
/**
* The target node of the edge.
*
* @type {string}
* @memberof IUnweightedEdge
*/
target: string;
}
@Component({
selector: 'app-graphe-from-matrix-adja',
templateUrl: './graphe-from-matrix-adja.component.html',
styleUrls: ['./graphe-from-matrix-adja.component.css']
})
export class GrapheFromMatrixAdjaComponent {
/**
* Input container object for handling graph-related properties.
*/
@Input() container:any;
/**
* The adjacency matrix in text format.
*/
matrixText:string="";
/**
* Constructor for the GrapheFromMatrixAdjaComponent.
*
* @param {GrapheService} grapheS - The GrapheService instance for handling graph operations.
* @param {TranslateService} translate - The TranslateService for language localization.
*/
constructor(protected grapheS:GrapheService,protected translate:TranslateService){}
/**
* Generates a graph based on the provided adjacency matrix.
*/
generateGraph() {
// Parse the matrix into a 2D array
const adjacencyMatrix = this.parseAdjacencyMatrix(this.matrixText);
if(adjacencyMatrix){
const elements:Array<any>=this.createGraphElements(adjacencyMatrix);
this.grapheS.createGrapheFromAdjancyMatrix(elements,!this.isSymmetric(adjacencyMatrix),this.isWeighted(adjacencyMatrix),this.container);
const screen=this.container.el.nativeElement.querySelector('.screen');
const buttonManupilation=this.container.el.nativeElement.querySelector('.buttonManupilation');
const addGrapheWithMatrix=this.container.el.nativeElement.querySelector('.addGrapheWithMatrix');
screen.style.display="block";
buttonManupilation.style.display="block";
addGrapheWithMatrix.style.display="none";
this.matrixText="";
this.container.message=this.translate.instant("grapheFromMatrix.msg4");
this.container.changeSelect="";
this.container.restoreArray=[];
}
}
/**
* Parses the input adjacency matrix in text format and returns a 2D array.
*
* @param {string} matrixText - The adjacency matrix in text format.
* @returns {any} - The parsed 2D array representing the adjacency matrix or `false` if parsing fails.
*/
parseAdjacencyMatrix(matrixText:string):any {
// Remove leading and trailing whitespace
matrixText = matrixText.trim();
let err:boolean=false;
let matrix:any;
// Check if the input is empty
if (matrixText.length === 0) {
this.container.message=this.translate.instant('grapheFromMatrix.msg2');
err=true;
}
if(!err){
// Split the text by newline to get rows
const rows = matrixText.split('\n');
const numRows = rows.length;
// Initialize a flag to check if the matrix is square
let isSquare = true;
// Parse each row into an array of numbers and validate the format
matrix = rows.map((row:any, rowIndex:any) => {
const values = row.trim().split(/\s+/).map(Number);
// Check if the matrix is square by comparing the number of columns
if (values.length !== numRows) {
isSquare = false;
}
return values;
});
for (let i = 0; i < matrix.length; i++) {
for (let j = 0; j < matrix[i].length; j++) {
if (Number.isNaN(matrix[i][j])) {
err=true;
this.container.message=this.translate.instant('grapheFromMatrix.msg1');
break;
}
}
}
// Check if the matrix is square
if (!isSquare) {
err=true;
this.container.message=this.translate.instant('grapheFromMatrix.msg3');
}
}
if(err==true){
matrix=false;
}
return matrix;
}
/**
* Creates graph elements (nodes and edges) from the parsed adjacency matrix.
*
* @param {Array<Array<number>>} adjacencyMatrix - The parsed adjacency matrix.
* @returns {Array<any>} - An array of graph elements.
*/
createGraphElements(adjacencyMatrix: Array<Array<number>>): Array<any> {
let elements: Array<any> = [];
const isDirected: boolean = !this.isSymmetric(adjacencyMatrix);
const isWeighted: boolean = this.isWeighted(adjacencyMatrix);
// Create nodes
for (let i = 0; i < adjacencyMatrix.length; i++) {
if(this.container.nodeName=="numerique"){
this.grapheS.counter=i;
elements.push({id:++this.grapheS.counter});
}else if(this.container.nodeName=="alphabic"){
this.grapheS.counter=i;
elements.push({id:this.grapheS.Alphabets[i]});
}
}
let arrayNemming:Array<any>=[];
if(this.container.nodeName=="numerique"){
arrayNemming=this.grapheS.numbersArray;
}else if(this.container.nodeName=="alphabic"){
arrayNemming=this.grapheS.Alphabets;
}
// Create edges based on adjacency matrix
for (let i = 0; i < adjacencyMatrix.length; i++) {
for (let j = 0; j < adjacencyMatrix[i].length; j++) {
let edge:any;
if (i !== j && adjacencyMatrix[i][j] !== 0) {
if (isWeighted) {
if(this.container.nodeName=="numerique"){
edge = {
source: (i+1).toString(),
target: (j+1).toString(),
weight: adjacencyMatrix[i][j]
};
}else if(this.container.nodeName=="alphabic"){
edge = {
source: arrayNemming[i],
target: arrayNemming[j],
weight: adjacencyMatrix[i][j]
};
}
}else{
if(this.container.nodeName=="numerique"){
edge = {
source: (i+1).toString(),
target: (j+1).toString() };
}else if(this.container.nodeName=="alphabic"){
edge = {
source: arrayNemming[i],
target: arrayNemming[j],
};
}
}
elements.push(edge);
}
}
}
if(this.container.nodeName=="alphabic"){
for(let i=0;i<this.grapheS.counter;i++){
this.grapheS.Alphabets.shift();
}
this.grapheS.counter=0;
}
return elements;
}
/**
* Checks if the adjacency matrix represents a weighted graph.
*
* @param {Array<Array<number>>} adjacencyMatrix - The adjacency matrix.
* @returns {boolean} - `true` if the graph is weighted, otherwise `false`.
*/
isWeighted(adjacencyMatrix:Array<Array<number>>):boolean{
let weighted:boolean=false;
for (let i = 0; i < adjacencyMatrix.length; i++) {
if(weighted){
break;
}
for (let j = 0; j < adjacencyMatrix[i].length; j++) {
if (adjacencyMatrix[i][j] != 1 && adjacencyMatrix[i][j] != 0) {
weighted=true;
break;
}
}
}
return weighted;
}
/**
* Checks if the adjacency matrix is symmetric, indicating an undirected graph.
*
* @param {Array<Array<number>>} matrix - The adjacency matrix.
* @returns {boolean} - `true` if the matrix is symmetric, indicating an undirected graph, otherwise `false`.
*/
isSymmetric(matrix:Array<Array<number>>) {
if (!Array.isArray(matrix) || matrix.length === 0 || matrix[0].length === 0) {
return false; // Not a valid matrix
}
const numRows = matrix.length;
const numCols = matrix[0].length;
if (numRows !== numCols) {
return false; // Not a square matrix, so it can't be symmetric
}
for (let i = 0; i < numRows; i++) {
for (let j = 0; j < numCols; j++) {
if (matrix[i][j] !== matrix[j][i]) {
return false; // The matrix is not symmetric
}
}
}
return true; // The matrix is symmetric
}
}