Ejemplo n.º 1
0
        List <SGTNode <HKMSTNodeFinal> > TopoSort(List <SGTNode <HKMSTNodeFinal> > graph)
        {
            var dfs  = new StaticGraph.GenericTarjan <int>();
            var dict = new Dictionary <SGTNode <HKMSTNodeFinal>, int>();

            for (int i = 0; i < graph.Count; i++)
            {
                dict.Add(graph[i], i);
                dfs.AddVertex(graph[i].Value.N);
            }

            // "graph" will only ever be a list of nodes already traversed backward or forwards, not the entire graph
            foreach (var node in graph)
            {
                foreach (var to in node.Value.Outgoing)
                {
                    if (graph.Contains(to))
                    {
                        dfs.AddEdge(dict[node], dict[to]);
                    }
                }
            }

            var topology = dfs.TopoSort();

            return(topology.Select(item => graph[item]).ToList());
        }
Ejemplo n.º 2
0
        List<SGTNode<HKMSTNode>> TopoSort(List<SGTNode<HKMSTNode>> graph)
        {
            var dfs = new StaticGraph.GenericTarjan<int>();
            var dict = new Dictionary<SGTNode<HKMSTNode>, int>();

            for (int i = 0; i < graph.Count; i++)
            {
                dict.Add(graph[i], i);
                dfs.AddVertex(graph[i].Value.N);
            }

            // "graph" will only ever be a list of nodes already traversed backward or forwards, not the entire graph
            foreach (var node in graph)
            {
                foreach (var to in node.Value.Outgoing)
                {
                    if(graph.Contains(to))
                        dfs.AddEdge(dict[node], dict[to]);
                }
            }

            var topology = dfs.TopoSort();

            return topology.Select(item => graph[item]).ToList();
        }