Implementing a Topological Sort Method

45 views Asked by At

I'm currently trying to implement a topological sort method but I'm struggling to finish it and need some advice. The errors I get when I test this method are: "Expected exception: java.lang.IllegalArgumentException", expected:<5> but was:<0>", "java.lang.IllegalArgumentException: No path found". What could I be doing wrong and fix?

/** * Helper method for the sort method in the GraphUtility class to generate a sorted ordering of the vertices in the * graph. Note that a graph may have more than one valid ordering, and any such ordering is accepted. * * @param sources - the source from where we'll be starting our sort * @param destinations - the destination, or end point, of our sort * @return a sorted ordering of the vertices in the graph */ public List topologicalSort(List sources, List destinations) throws IllegalArgumentException { createGraph(sources, destinations);

    Queue<Vertex<Type>> doableTasks = new LinkedList<>();
    List<Type> output = new LinkedList<>();

    for (Vertex<Type> task : vertices.values()) {
        if (task.getIndegree() == 0) {
            doableTasks.add(task);
        }
    }

    // counter for things we've visited in the queue
    int counter = 0;

    while (!(doableTasks.isEmpty())) {
        Vertex<Type> task = doableTasks.remove();
        counter++;
        output.add(task.getName());

        for (Iterator<Edge<Type>> it = task.edges(); it.hasNext(); ) {
            Edge<Type> outEdge = it.next();

            Vertex<Type> neighbor = outEdge.getOtherVertex();

            neighbor.setIndegree(neighbor.getIndegree() - 1);

            if (neighbor.getIndegree() == 0) {
                doableTasks.add(neighbor);
            }
        }
    }

    if (counter > output.size()) {
        throw new IllegalArgumentException("The size doesn't match.");
    }
    return output;
}

public static <Type> void createGraph(List<Type> sources, List<Type> destinations)
        throws IllegalArgumentException {
    if (sources.size() != destinations.size()) {
        throw new IllegalArgumentException("Must have the same size");
    }

    Graph<Type> graph = new Graph<>();

    for (int i = 0; i < sources.size(); i++) {
        Type sourceData = sources.get(i);
        Type destData = destinations.get(i);
        graph.addEdge(sourceData, destData);
    }
}

public class Vertex {

// Used to id the Vertex
private final Type name;

// Adjacency list
private final LinkedList<Edge<Type>> adj;
private int indegree;

public int getIndegree() {
    return indegree;
}

public int setIndegree(int newIndegree) {
    return indegree = newIndegree;
}

/**
 * Creates a new Vertex object, using the given name.
 *
 * @param name - string used to identify this Vertex
 */
public Vertex(Type name) {
    this.name = name;
    this.adj = new LinkedList<>();

}

/**
 * @return the string used to identify this Vertex
 */
public Type getName() {
    return name;
}

/**
 * Adds a directed edge from this Vertex to another.
 *
 * @param otherVertex - the Vertex object that is the destination of the edge
 */
public void addEdge(Vertex<Type> otherVertex) {
    adj.add(new Edge<>(otherVertex));
    otherVertex.indegree++;
}

/**
 * @return an iterator for accessing the edges for which this Vertex is the source
 */
public Iterator<Edge<Type>> edges() {
    return adj.iterator();
}

/**
 * Generates and returns a textual representation of this Vertex.
 */
public String toString() {
    StringBuilder s = new StringBuilder("Vertex " + name + " adjacent to vertices ");
    for (Edge<Type> typeEdge : adj) s.append(typeEdge).append("  ");
    return s.toString();
}

}

0

There are 0 answers