-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph_Gilbert.java
More file actions
114 lines (99 loc) · 3.95 KB
/
Graph_Gilbert.java
File metadata and controls
114 lines (99 loc) · 3.95 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
111
112
113
114
package grafos;
import java.util.Random;
import java.util.HashMap;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Map.Entry;
import java.util.Set;
import java.io.FileNotFoundException;
import java.util.ArrayList;
/**
*
* @author flintlock
*/
public class Graph_Gilbert {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int number_of_edges = 500;
double probabilidad = 0.3;
double tiro;
boolean dirigido = false;
boolean auto_cycles = false;
boolean flag = true;
Random random = new Random();
ArrayList<Integer> nodes_fin = new ArrayList<>();
ArrayList<Integer> nodes_inicio = new ArrayList<>();
HashMap<ArrayList<Integer>, ArrayList<Integer>> edges = new HashMap<>();
// Genera pares de números aleatorios de 0 hasta número de nodos
// Número de pares es igual al número de aristas
for (int i = 0; i < number_of_edges; i++) {
for (int j = 0; j < number_of_edges; j++) {
flag = true;
if (auto_cycles != true) {
tiro = random.nextDouble();
if (tiro > probabilidad) {
if (j != i & j >= i + 1){
flag = false;
nodes_inicio.add(i);
nodes_fin.add(j);
}
}
}
else {
tiro = random.nextDouble();
if (tiro > probabilidad) {
if (j != i & j >= i + 1){
flag = false;
nodes_inicio.add(i);
nodes_fin.add(j);
}
}
}
}
}
edges.put(nodes_inicio, nodes_fin);
// Transforma HashMap a Set
Set<Entry<ArrayList<Integer>,ArrayList<Integer>>> nodes_s;
nodes_s = edges.entrySet();
// Escribe en archivo de texto la llave y valor de Set
try {
PrintStream out = new PrintStream(new FileOutputStream("Graph_Gilbert_500.gv"));
if (dirigido == false) {
out.println("graph {");
for(int i = 0; i < nodes_fin.size(); i++) {
out.println(nodes_inicio.get(i) + "--" + nodes_fin.get(i));
}
// Agrega las keys que faltan para completar todos los nodos
for (int i = 0; i < number_of_edges; i++){
if ((nodes_inicio.contains(i) != true) & (nodes_fin.contains(i) != true)){
out.println(i);
}
}
out.println("}");
}
else if (dirigido == true) {
out.println("digraph {");
for(int i = 0; i < nodes_fin.size(); i++) {
out.println(nodes_inicio.get(i) + "->" + nodes_fin.get(i));
}
// Agrega las keys que faltan para completar todos los nodos
for (int i = 0; i < number_of_edges; i++){
if ((nodes_inicio.contains(i) != true) & (nodes_fin.contains(i) != true)){
out.println(i);
}
}
out.println("}");
}
// Cierra archivo de texto
out.close();
}
// Maneja las excepciones que haya al escribir en el archivo de texto
catch (FileNotFoundException e) {
e.printStackTrace();
}
//System.out.println(nodes);
}
}