-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDemoNetworkDirected.java
executable file
·90 lines (77 loc) · 3.11 KB
/
DemoNetworkDirected.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
package com.example.graph;
import com.example.exceptions.*;
import java.util.Iterator;
public class DemoNetworkDirected {
static <T> String printUndiGraph2D(NetworkDirected<T> rede) {
StringBuilder s = new StringBuilder();
int i = 0;
for (GraphVertex<T, GraphEdge> vertice : rede.vertices) {
if (vertice != null) {
s.append("Index(").append(i++).append("): ").append(vertice.element).append(" -> ").append("[");
Iterator<GraphEdge> itr = vertice.neighbors.iterator();
while (itr.hasNext()) {
GraphEdge edge = itr.next();
s.append("(").append(rede.vertices[edge.indexVertex].element).append(",").append(edge.weight).append(")");
if (itr.hasNext()) {
s.append(", ");
}
}
s.append("]\n");
}
}
return s.toString();
}
static <T> void livro() {
NetworkDirected<String> rede = new NetworkDirected<>(9);
try {
rede.addVertex("A");
rede.addVertex("B");
rede.addVertex("C");
rede.addVertex("D");
rede.addVertex("E");
rede.addVertex("F");
rede.addVertex("G");
rede.addVertex("H");
rede.addVertex("I");
rede.addVertex("J");
rede.addEdge("A", "D", 5);
rede.addEdge("A", "E", 4);
rede.addEdge("A", "B", 2);
rede.addEdge("B", "E", 1);
rede.addEdge("C", "B", 3);
rede.addEdge("D", "G", 2);
rede.addEdge("E", "F", 3);
rede.addEdge("E", "H", 6);
rede.addEdge("F", "C", 4);
rede.addEdge("F", "H", 3);
rede.addEdge("G", "H", 1);
rede.addEdge("H", "I", 1);
rede.addEdge("I", "F", 1);
// try {
// rede.removeVertex("A");
// } catch (EmptyCollectionException ex) {
// System.out.println(ex.getMessage());
// }
// System.out.println(printUndiGraph2D(rede));
//
// System.out.println("BFS");
// for (Iterator iterator = rede.iteratorBFS("E"); iterator.hasNext();) {
// System.out.print(iterator.next() + " ");
// }
// System.out.println("\n\nDFS");
// for (Iterator iterator = rede.iteratorDFS("E"); iterator.hasNext();) {
// System.out.print(iterator.next() + " ");
// }
System.out.println("\n\nShortest");
for (Iterator<String> iterator = rede.iteratorShortestPath("A", "J"); iterator.hasNext(); )
System.out.print(iterator.next() + " ");
System.out.println("\n\nNumberEdges: " + rede.numberOfEdges);
System.out.println("NumberVertices: " + rede.numberOfVertices);
} catch (ElementNotFoundException | VertexConnectionExeception | ElementAlreadyExistsException ex) {
System.out.println(ex.getMessage());
}
}
public static void main(String[] args) {
livro();
}
}