use of org.deeplearning4j.graph.api.Edge in project deeplearning4j by deeplearning4j.
the class TestGraphLoading method testGraphLoadingWithVertices.
@Test
public void testGraphLoadingWithVertices() throws IOException {
ClassPathResource verticesCPR = new ClassPathResource("test_graph_vertices.txt");
ClassPathResource edgesCPR = new ClassPathResource("test_graph_edges.txt");
EdgeLineProcessor<String> edgeLineProcessor = new DelimitedEdgeLineProcessor(",", false, "//");
VertexLoader<String> vertexLoader = new DelimitedVertexLoader(":", "//");
Graph<String, String> graph = GraphLoader.loadGraph(verticesCPR.getTempFileFromArchive().getAbsolutePath(), edgesCPR.getTempFileFromArchive().getAbsolutePath(), vertexLoader, edgeLineProcessor, false);
System.out.println(graph);
for (int i = 0; i < 10; i++) {
List<Edge<String>> edges = graph.getEdgesOut(i);
assertEquals(2, edges.size());
//expect for example 0->1 and 9->0
Edge<String> first = edges.get(0);
if (first.getFrom() == i) {
//undirected edge: i -> i+1 (or 9 -> 0)
assertEquals(i, first.getFrom());
assertEquals((i + 1) % 10, first.getTo());
} else {
//undirected edge: i-1 -> i (or 9 -> 0)
assertEquals((i + 10 - 1) % 10, first.getFrom());
assertEquals(i, first.getTo());
}
Edge<String> second = edges.get(1);
assertNotEquals(first.getFrom(), second.getFrom());
if (second.getFrom() == i) {
//undirected edge: i -> i+1 (or 9 -> 0)
assertEquals(i, second.getFrom());
assertEquals((i + 1) % 10, second.getTo());
} else {
//undirected edge: i-1 -> i (or 9 -> 0)
assertEquals((i + 10 - 1) % 10, second.getFrom());
assertEquals(i, second.getTo());
}
}
}
use of org.deeplearning4j.graph.api.Edge in project deeplearning4j by deeplearning4j.
the class TestDeepWalk method generateRandomGraph.
private static Graph<String, String> generateRandomGraph(int nVertices, int nEdgesPerVertex) {
Random r = new Random(12345);
Graph<String, String> graph = new Graph<>(nVertices, new StringVertexFactory());
for (int i = 0; i < nVertices; i++) {
for (int j = 0; j < nEdgesPerVertex; j++) {
int to = r.nextInt(nVertices);
Edge<String> edge = new Edge<>(i, to, i + "--" + to, false);
graph.addEdge(edge);
}
}
return graph;
}
use of org.deeplearning4j.graph.api.Edge in project deeplearning4j by deeplearning4j.
the class TestGraphLoading method testGraphLoading.
@Test
public void testGraphLoading() throws IOException {
ClassPathResource cpr = new ClassPathResource("simplegraph.txt");
EdgeLineProcessor<String> edgeLineProcessor = new DelimitedEdgeLineProcessor(",", false, "//");
VertexFactory<String> vertexFactory = new StringVertexFactory("v_%d");
Graph<String, String> graph = GraphLoader.loadGraph(cpr.getTempFileFromArchive().getAbsolutePath(), edgeLineProcessor, vertexFactory, 10, false);
System.out.println(graph);
for (int i = 0; i < 10; i++) {
List<Edge<String>> edges = graph.getEdgesOut(i);
assertEquals(2, edges.size());
//expect for example 0->1 and 9->0
Edge<String> first = edges.get(0);
if (first.getFrom() == i) {
//undirected edge: i -> i+1 (or 9 -> 0)
assertEquals(i, first.getFrom());
assertEquals((i + 1) % 10, first.getTo());
} else {
//undirected edge: i-1 -> i (or 9 -> 0)
assertEquals((i + 10 - 1) % 10, first.getFrom());
assertEquals(i, first.getTo());
}
Edge<String> second = edges.get(1);
assertNotEquals(first.getFrom(), second.getFrom());
if (second.getFrom() == i) {
//undirected edge: i -> i+1 (or 9 -> 0)
assertEquals(i, second.getFrom());
assertEquals((i + 1) % 10, second.getTo());
} else {
//undirected edge: i-1 -> i (or 9 -> 0)
assertEquals((i + 10 - 1) % 10, second.getFrom());
assertEquals(i, second.getTo());
}
}
}
Aggregations