/*
 * (C) Copyright 2003-2016, by Tim Shearouse and Contributors.
 *
 * JGraphT : a free Java graph-theory library
 *
 * This program and the accompanying materials are dual-licensed under
 * either
 *
 * (a) the terms of the GNU Lesser General Public License version 2.1
 * as published by the Free Software Foundation, or (at your option) any
 * later version.
 *
 * or (per the licensee's choosing)
 *
 * (b) the terms of the Eclipse Public License v1.0 as published by
 * the Eclipse Foundation.
 */
package org.jgrapht.demo;

import java.util.*;

import org.jgrapht.*;
import org.jgrapht.generate.*;
import org.jgrapht.graph.*;
import org.jgrapht.traverse.*;

/**
 * Demonstrates how to create a complete graph and perform a depth first search on it.
 *
 */
public final class CompleteGraphDemo
{
    static Graph<Object, DefaultEdge> completeGraph;

    // Number of vertices
    static int size = 10;

    /**
     * Main demo entry point.
     * 
     * @param args command line arguments
     */
    public static void main(String[] args)
    {
        // Create the graph object; it is null at this point
        completeGraph = new SimpleGraph<>(DefaultEdge.class);

        // Create the CompleteGraphGenerator object
        CompleteGraphGenerator<Object, DefaultEdge> completeGenerator =
            new CompleteGraphGenerator<>(size);

        // Create the VertexFactory so the generator can create vertices
        VertexFactory<Object> vFactory = new ClassBasedVertexFactory<>(Object.class);

        // Use the CompleteGraphGenerator object to make completeGraph a
        // complete graph with [size] number of vertices
        completeGenerator.generateGraph(completeGraph, vFactory, null);

        // Now, replace all the vertices with sequential numbers so we can ID
        // them
        Set<Object> vertices = new HashSet<>();
        vertices.addAll(completeGraph.vertexSet());
        Integer counter = 0;
        for (Object vertex : vertices) {
            replaceVertex(vertex, counter++);
        }

        // Print out the graph to be sure it's really complete
        Iterator<Object> iter = new DepthFirstIterator<>(completeGraph);
        Object vertex;
        while (iter.hasNext()) {
            vertex = iter.next();
            System.out.println(
                "Vertex " + vertex.toString() + " is connected to: "
                    + completeGraph.edgesOf(vertex).toString());
        }
    }

    private static boolean replaceVertex(Object oldVertex, Object newVertex)
    {
        if ((oldVertex == null) || (newVertex == null)) {
            return false;
        }
        Set<DefaultEdge> relatedEdges = completeGraph.edgesOf(oldVertex);
        completeGraph.addVertex(newVertex);

        Object sourceVertex;
        Object targetVertex;
        for (DefaultEdge e : relatedEdges) {
            sourceVertex = completeGraph.getEdgeSource(e);
            targetVertex = completeGraph.getEdgeTarget(e);
            if (sourceVertex.equals(oldVertex) && targetVertex.equals(oldVertex)) {
                completeGraph.addEdge(newVertex, newVertex);
            } else {
                if (sourceVertex.equals(oldVertex)) {
                    completeGraph.addEdge(newVertex, targetVertex);
                } else {
                    completeGraph.addEdge(sourceVertex, newVertex);
                }
            }
        }
        completeGraph.removeVertex(oldVertex);
        return true;
    }
}

// End CompleteGraphDemo.java

@@ -812,22 +812,22 @@
 c Graph%3C
-Object
+String
 , Defaul
@@ -1254,22 +1254,22 @@
 nerator%3C
-Object
+String
 , Defaul
@@ -1439,22 +1439,22 @@
 Factory%3C
-Object
+String
 %3E vFacto
@@ -1466,18 +1466,8 @@
 new 
-ClassBased
 Vert
@@ -1480,23 +1480,196 @@
 ory%3C
-%3E(Object.class)
+String%3E()%0A        %7B%0A            private int id = 0;%0A%0A            @Override%0A            public String createVertex()%0A            %7B%0A                return %22v%22 + id++;%0A            %7D%0A        %7D
 ;%0A%0A 
@@ -1874,329 +1874,8 @@
 );%0A%0A
-        // Now, replace all the vertices with sequential numbers so we can ID%0A        // them%0A        Set%3CObject%3E vertices = new HashSet%3C%3E();%0A        vertices.addAll(completeGraph.vertexSet());%0A        Integer counter = 0;%0A        for (Object vertex : vertices) %7B%0A            replaceVertex(vertex, counter++);%0A        %7D%0A%0A
     
@@ -1950,22 +1950,22 @@
 terator%3C
-Object
+String
 %3E iter =
@@ -2010,31 +2010,8 @@
 h);%0A
-        Object vertex;%0A
     
@@ -2051,16 +2051,23 @@
         
+String 
 vertex =
@@ -2146,27 +2146,16 @@
 + vertex
-.toString()
  + %22 is 
@@ -2254,1023 +2254,8 @@
    %7D
-%0A%0A    private static boolean replaceVertex(Object oldVertex, Object newVertex)%0A    %7B%0A        if ((oldVertex == null) %7C%7C (newVertex == null)) %7B%0A            return false;%0A        %7D%0A        Set%3CDefaultEdge%3E relatedEdges = completeGraph.edgesOf(oldVertex);%0A        completeGraph.addVertex(newVertex);%0A%0A        Object sourceVertex;%0A        Object targetVertex;%0A        for (DefaultEdge e : relatedEdges) %7B%0A            sourceVertex = completeGraph.getEdgeSource(e);%0A            targetVertex = completeGraph.getEdgeTarget(e);%0A            if (sourceVertex.equals(oldVertex) && targetVertex.equals(oldVertex)) %7B%0A                completeGraph.addEdge(newVertex, newVertex);%0A            %7D else %7B%0A                if (sourceVertex.equals(oldVertex)) %7B%0A                    completeGraph.addEdge(newVertex, targetVertex);%0A                %7D else %7B%0A                    completeGraph.addEdge(sourceVertex, newVertex);%0A                %7D%0A            %7D%0A        %7D%0A        completeGraph.removeVertex(oldVertex);%0A        return true;%0A    %7D
 %0A%7D%0A%0A

Update CompleteGraphDemo to use String for vertices