org.jgrapht.demo.CompleteGraphDemo Maven / Gradle / Ivy
/*
* (C) Copyright 2003-2021, by Tim Shearouse and Contributors.
*
* JGraphT : a free Java graph-theory library
*
* See the CONTRIBUTORS.md file distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the
* GNU Lesser General Public License v2.1 or later
* which is available at
* http://www.gnu.org/licenses/old-licenses/lgpl-2.1-standalone.html.
*
* SPDX-License-Identifier: EPL-2.0 OR LGPL-2.1-or-later
*/
package org.jgrapht.demo;
//@example:class:begin
import org.jgrapht.*;
import org.jgrapht.generate.*;
import org.jgrapht.graph.*;
import org.jgrapht.traverse.*;
import org.jgrapht.util.*;
import java.util.*;
import java.util.function.*;
//@example:class:end
/**
* Demonstrates how to create a complete graph and perform a depth first search on it.
*
*/
// @example:class:begin
public final class CompleteGraphDemo
{
// number of vertices
private static final int SIZE = 10;
/**
* Main demo entry point.
*
* @param args command line arguments
*/
public static void main(String[] args)
{
// Create the VertexFactory so the generator can create vertices
Supplier vSupplier = new Supplier()
{
private int id = 0;
@Override
public String get()
{
return "v" + id++;
}
};
// @example:generate:begin
// Create the graph object
Graph completeGraph =
new SimpleGraph<>(vSupplier, SupplierUtil.createDefaultEdgeSupplier(), false);
// Create the CompleteGraphGenerator object
CompleteGraphGenerator completeGenerator =
new CompleteGraphGenerator<>(SIZE);
// Use the CompleteGraphGenerator object to make completeGraph a
// complete graph with [size] number of vertices
completeGenerator.generateGraph(completeGraph);
// @example:generate:end
// Print out the graph to be sure it's really complete
Iterator iter = new DepthFirstIterator<>(completeGraph);
while (iter.hasNext()) {
String vertex = iter.next();
System.out
.println(
"Vertex " + vertex + " is connected to: "
+ completeGraph.edgesOf(vertex).toString());
}
}
}
// @example:class:end