/// <summary>
        /// Recursively finds the shortest path by backtracing from "toId" up to the "fromId" node.
        /// </summary>
        private int[] GetPath(int fromId, int toId, Graph graph)
        {
            var path = new Stack<int>();
            path.Push(toId);

            var node = graph[toId];

            var upperIds = node.Adjacent.Where(x => graph[x].Layer == node.Layer - 1);
            foreach (var upperId in upperIds)
            {
                if (upperId == fromId) {
                    path.Push(fromId);
                    break;
                }

                var subsetPath = GetPath(fromId, upperId, graph);
                if (subsetPath[0] == fromId) {
                    for (int i = subsetPath.Length - 1; i >= 0; i--) {
                        path.Push(subsetPath[i]);
                    }
                    break;
                }
            }

            return path.ToArray();
        }
Example #2
0
        internal Node(int id, Graph graph)
        {
            IsExplored = false;

            Id = id;
            _graph = graph;

            Adjacent = new List<int>();
        }
        public Graph LoadGraph()
        {
            var edges = Select<EdgeEntity>();

            var result = new Graph();
            foreach (var edge in edges) {
                result.Add(edge.From, edge.To);
            }
            return result;
        }