private Pair<EdgeType, VertexType>
    getNewEdgeForSpanningTree(Vector<VertexType> vertices, Vector<EdgeType> edges) {
        ArrayList<EdgeType> tempEdgeArray = new ArrayList<EdgeType>();

        try {
            while (true) {
                EdgeType edge = pq.poll();
                if (edge == null)
                    return null;

                if (!vertices.contains(edge.target) &&
                        vertices.contains(edge.source))
                    return new Pair<EdgeType, VertexType>(edge, edge.target);

                if (!graph.isDirected() &&
                        vertices.contains(edge.target) &&
                        !vertices.contains(edge.source))
                    return new Pair<EdgeType, VertexType>(edge, edge.source);

                tempEdgeArray.add(edge);

            }
        } finally {
            for (EdgeType e : tempEdgeArray)
                pq.add(e);
        }
    }