how do i generate all the topological ordering of (directed acyclic graph), i have written a program that can find 1 topological ordering, the problem is once i am finished i am left with a empty linked list as my algorithm involves deleting the traversed nodes which is crucial for the algorithm to work

here is the code:

``````package topological_sort;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import java.util.Scanner;

public class Topological_Sort {

public static class node {

int inDeg, number;

public node(int number) {
this.number = number;
this.inDeg = 0;
}
}

public static void main(String[] args) throws FileNotFoundException, IOException {

int nNodes, i, node1, node2;
String edges;
Scanner input;

nNodes = input.nextInt();

for (i = 0; i < nNodes; i++) {
node theNode = new node(i);
}

input.reset();

while ((edges = fin.readLine()) != null) {
input = new Scanner(edges);
node1 = input.nextInt();
node2 = input.nextInt();

node theNode1 = Vertices.get(node1);
node theNode2 = Vertices.get(node2);

theNode2.inDeg += 1;
}

Iterator i1 = Vertices.iterator();
while (i1.hasNext()) {
node theNode = (node) i1.next();
if (theNode.inDeg == 0) {
Iterator i2 = theNode.out.iterator();
while (i2.hasNext()) {
node outNode = (node) i2.next();
outNode.inDeg -= 1;
}
Vertices.remove(theNode);
i1 = Vertices.iterator();
System.out.println(theNode.number);
}
}
}
}``````

thanks !

(maybe this forum is not the right place for posting this but i could not find anywhere else to post, if you know of a forum that could help, please let me know, thanks!)

fuckoff

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.