using System.Collections.Generic;
public static void Main()
TopologicalOrdering topologicalOrdering = new TopologicalOrdering();
List<Vertex> graph = new List<Vertex>();
graph.Add(new Vertex(0));
graph.Add(new Vertex(1));
graph.Add(new Vertex(2));
graph.Add(new Vertex(3));
graph.Add(new Vertex(4));
graph.Add(new Vertex(5));
graph[2].AddNeighbourVertex(graph[3]);
graph[3].AddNeighbourVertex(graph[1]);
graph[4].AddNeighbourVertex(graph[0]);
graph[4].AddNeighbourVertex(graph[1]);
graph[5].AddNeighbourVertex(graph[0]);
graph[5].AddNeighbourVertex(graph[2]);
for(int i = 0; i < graph.Count; ++i)
if( graph[i].visited == false)
topologicalOrdering.dfs(graph[i]);
Stack<Vertex> stack = topologicalOrdering.getStack();
for(int i = 0; i < graph.Count; ++i)
Console.WriteLine(v + " -> ");
public int data {get; set;}
public bool visited {get; set;}
public List<Vertex> neighbourList {get; set;}
neighbourList = new List<Vertex>();
public override string ToString()
public void AddNeighbourVertex(Vertex vertex)
this.neighbourList.Add(vertex);
public class TopologicalOrdering
public Stack<Vertex> stack;
public TopologicalOrdering()
this.stack = new Stack<Vertex>();
public void dfs(Vertex vertex)
foreach(Vertex v in vertex.neighbourList)
public Stack<Vertex> getStack()