For a weighted graph, the edge weights could be stored in the nodes, with one weight for each outgoing edge (i.e., for each successor). So in addition to having a List of Graphnodes for the successors, there would be a List of values; the first value would be the label on the edge to the first successor, the second value would be the label on the edge to the second successor, etc.
public void resetAll() { for (int k=0; k<nodes.size(); k++) { ((Graphnode)nodes.get(k)).setVisited(false); } }
A B C D E A C D E B A C B D E A D C B E
public void topOrderAll() { mark all nodes unvisited; int N = nodes.size(); for each node k in the graph { if (node k is marked unvisited) { N = topNum(k, N); } } }
public boolean isConnected( ) { if (nodes.size() == 0) return true; // do modified dfs from some node Graphnode n = (Graphnode)nodes.get(0); newDfs(n); // graph IS connected iff all nodes were visited for (int k=0; k<nodes.size(); k++) { n = (Graphnode)nodes.get(k); if (!n.getVisited()) return false; } return true; } private static void newDfs(Graphnode n) { // do a dfs but follow predecessor edges as well as successors n.setVisited(true); ArrayList preds = n.getPreds(); ArrayList succs = n.getSuccs(); for (int k=0; k<preds.size(); k++) { Graphnode m = (Graphnode)preds.get(k); if (!m.getVisited()) newDfs(m); } for (int k=0; k<succs.size(); k++) { Graphnode m = (Graphnode)succs.get(k); if (!m.getVisited()) newDfs(m); } }