-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph-grafos.js
More file actions
110 lines (94 loc) · 2.81 KB
/
Copy pathgraph-grafos.js
File metadata and controls
110 lines (94 loc) · 2.81 KB
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
// Clase Grafo (Graph)
class Graph {
constructor() {
this.vertices = {};
}
// Método agregarVertice (addVertex): agrega un nuevo vértice al grafo
addVertex(vertex) {
if (!this.vertices[vertex]) {
this.vertices[vertex] = [];
}
}
// Método agregarArista (addEdge): agrega una arista entre dos vértices
addEdge(vertex1, vertex2) {
if (!this.vertices[vertex1]) {
this.addVertex(vertex1);
}
if (!this.vertices[vertex2]) {
this.addVertex(vertex2);
}
this.vertices[vertex1].push(vertex2);
this.vertices[vertex2].push(vertex1); // Para un grafo no dirigido
}
// Método eliminarArista (removeEdge): elimina una arista entre dos vértices
removeEdge(vertex1, vertex2) {
this.vertices[vertex1] = this.vertices[vertex1].filter(
(v) => v !== vertex2
);
this.vertices[vertex2] = this.vertices[vertex2].filter(
(v) => v !== vertex1
);
}
// Método eliminarVertice (removeVertex): elimina un vértice y sus aristas asociadas
removeVertex(vertex) {
while (this.vertices[vertex].length) {
const adjacent = this.vertices[vertex].pop();
this.removeEdge(vertex, adjacent);
}
delete this.vertices[vertex];
}
// Método recorridoDFS (DFS): recorre el grafo en profundidad (Depth-First Search)
DFS(start) {
const visited = {};
this.DFSUtil(start, visited);
}
DFSUtil(vertex, visited) {
visited[vertex] = true;
console.log(vertex);
for (let adjacent of this.vertices[vertex]) {
if (!visited[adjacent]) {
this.DFSUtil(adjacent, visited);
}
}
}
// Método recorridoBFS (BFS): recorre el grafo en anchura (Breadth-First Search)
BFS(start) {
const visited = {};
const queue = [start];
visited[start] = true;
while (queue.length) {
const vertex = queue.shift();
console.log(vertex);
for (let adjacent of this.vertices[vertex]) {
if (!visited[adjacent]) {
visited[adjacent] = true;
queue.push(adjacent);
}
}
}
}
// Método imprimir (print): imprime la lista de adyacencia del grafo
print() {
for (let vertex in this.vertices) {
console.log(`${vertex} -> ${this.vertices[vertex].join(", ")}`);
}
}
}
// Ejemplo de uso de la clase Grafo (Graph)
const graph = new Graph();
graph.addVertex("A");
graph.addVertex("B");
graph.addVertex("C");
graph.addVertex("D");
graph.addVertex("E");
graph.addEdge("A", "B");
graph.addEdge("A", "C");
graph.addEdge("B", "D");
graph.addEdge("C", "E");
graph.addEdge("D", "E");
console.log("Impresión del grafo:");
graph.print(); // Imprime la lista de adyacencia del grafo
console.log("Recorrido DFS desde 'A':");
graph.DFS("A"); // Recorre el grafo en profundidad (DFS)
console.log("Recorrido BFS desde 'A':");
graph.BFS("A"); // Recorre el grafo en anchura (BFS)