Ejemplo n.º 1
0
        /**
         * Creates a new edge and adds it to the specified graph similarly to the
         * {@link Graph#addEdge(Object, Object)} method.
         *
         * @param g the graph for which the edge to be added
         * @param sourceVertex source vertex of the edge
         * @param targetVertex target vertex of the edge
         * @param weight weight of the edge
         * @param <V> the graph vertex type
         * @param <E> the graph edge type
         *
         * @return The newly created edge if added to the graph, otherwise <code>
         * null</code>.
         *
         * @see Graph#addEdge(Object, Object)
         */
        public static E addEdge <V, E>(Graph <V, E> g, V sourceVertex, V targetVertex, double weight)
        {
            EdgeFactory <V, E> ef = g.getEdgeFactory();
            E e = ef.createEdge(sourceVertex, targetVertex);

            // we first create the edge and set the weight to make sure that
            // listeners will see the correct weight upon addEdge.
            g.setEdgeWeight(e, weight);

            return(g.addEdge(sourceVertex, targetVertex, e) ? e : default(E));
        }