Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

topological sort java

import java.io.*;
import java.util.*;
public class TopologicalSort {
    static class Graph {
        private final int v;
        private final ArrayList<Integer>[] adj;
        Graph(int n) {
            v = n;
            adj = new ArrayList[v+1];
            for (int i = 1; i <= v; i++) adj[i] = new ArrayList<>();
        }
        void addEdge(int v, int w) {adj[v].add(w);}
        void topoSort(int v, boolean[] vis, Stack<Integer> s) {
            vis[v] = true;
            for (Integer u : adj[v]) if (!vis[u]) topoSort(u, vis, s);
            s.push(v);
        }
        void topoSort() {
            Stack<Integer> s = new Stack<>();
            boolean[] vis = new boolean[v+1];
            for (int i = 1; i <= v; i++) if (!vis[i]) topoSort(i, vis, s);
            while (!s.empty()) System.out.print((s.pop()) + " ");
            System.out.println();
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: java format money 
Java :: java jframe button 
Java :: Lunar New Year 
Java :: Convert c# to xml string 
Java :: android studio get string from strings.xml 
Java :: java coding problems 
Java :: status codes springboot 
Java :: how to hide label in bottom menu android studio 
Java :: Java code to print odd number 
Java :: java swing timer sleep 
Java :: bufferedreader for input in java 
Java :: java for loop 
Java :: Java forName() method 
Java :: how to get system date in android 
Java :: how to not open key board on start 
Java :: bukkit scheduled task 
Java :: java biginteger multiply 
Java :: change button text when clicked android studio 
Java :: java string swap two characters 
Java :: arrays.fill java 
Java :: string startswith java 
Java :: java outer class 
Java :: How to add music to andoid project 
Java :: display an image java 
Java :: java stream sum integers 
Java :: java string remove more than one space 
Java :: language of minecraft 
Java :: java import text file into arraylist 
Java :: intent in fragment android 
Java :: java arraylist to string 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =