Exemplo n.º 1
0
        private void ReportCycle(TNode node, AbstractDiGraph <TNode> graph)
        {
            var cycle = new GraphPath <TNode>();

            _allcycles.Add(cycle);
            cycle.SourceNode      = node;
            cycle.DestinationNode = node;
            cycle.Path            = new List <TNode>();

            List <TNode> stacklist = _vStack.ToList();

            int i = 0;

            while (!stacklist[i].Equals(node))
            {
                i++;
            }

            for (; i >= 0; i--)
            {
                cycle.Path.Add(stacklist[i]);
            }
            cycle.Path.Add(node);

            cycle.PathWeight = 0;
            for (int j = 1; j < cycle.Path.Count; j++)
            {
                cycle.PathWeight += graph.GetEdgeWeight(cycle.Path[j - 1], cycle.Path[j]);
            }
        }
        private void Reportpath(TNode node, TNode dnode, AbstractDiGraph <TNode> graph)
        {
            var path = new GraphPath <TNode>();

            _allpaths.Add(path);
            path.SourceNode      = node;
            path.DestinationNode = dnode;
            path.Path            = new List <TNode>();

            List <TNode> stacklist = _vStack.ToList();

            int i = 0;

            while (!stacklist[i].Equals(node))
            {
                i++;
            }

            for (; i >= 0; i--)
            {
                path.Path.Add(stacklist[i]);
            }
            path.Path.Add(dnode);

            path.PathWeight = 0;
            for (int j = 1; j < path.Path.Count; j++)
            {
                path.PathWeight += graph.GetEdgeWeight(path.Path[j - 1], path.Path[j]);
            }
        }
Exemplo n.º 3
0
        public AbstractGraphPath <TNode> GetShortestPath(AbstractDiGraph <TNode> graph, TNode sourceNode,
                                                         TNode destinationNode)
        {
            //Validate Input
            if (graph == null)
            {
                throw new ArgumentNullException("graph");
            }

            if (graph.AllEdges.Any(x => x.Item3 <= 0))
            {
                throw new InvalidEdgeWeightException(
                          string.Format("The graph contains one or more edges with edge weight less than or equal to zero"));
            }

            //Initialise data structures
            Initialise(graph, sourceNode);

            //Compute Shortest Path
            while (_shortestPathUnKnownNodesQueue.Any() && !_shortestPathKnownNodes.Contains(destinationNode))
            {
                foreach (var shortestPathKnownNode in _shortestPathKnownNodes)
                {
                    foreach (
                        var neighbour in
                        graph.GetNeighbourNodes(shortestPathKnownNode)
                        .Where(x => !_shortestPathKnownNodes.Contains(x)))
                    {
                        int edgeWeight           = graph.GetEdgeWeight(shortestPathKnownNode, neighbour);
                        int newestimatedDistance = _estimatedDistances[shortestPathKnownNode].PathWeight +
                                                   edgeWeight;
                        if (IsEstimatedDistanceBetter(neighbour, newestimatedDistance))
                        {
                            _estimatedDistances[neighbour].PathWeight = newestimatedDistance;

                            _predessorNodes[neighbour] = shortestPathKnownNode;
                        }
                    }
                }
                DequeueShortestPathKnownNode();
            }
            CompletePaths();
            var result = _estimatedDistances.SingleOrDefault(x => x.Value.DestinationNode.Equals(destinationNode)).Value;

            if (result.PathWeight == Infinity)
            {
                return(null);
            }
            return(result);
        }
        private void DFSVisit(AbstractDiGraph <TNode> graph, TNode node)
        {
            _startTime[node] = ++_time;

            foreach (TNode neighbour in graph.GetNeighbourNodes(node))
            {
                var dfsEdge = new DepthFirstSearchEdge <TNode> {
                    SourceNode = node, DestinationNode = neighbour
                };
                dfsEdge.EdgeWeight = graph.GetEdgeWeight(dfsEdge.SourceNode, dfsEdge.DestinationNode);
                _dfsEdges.Add(dfsEdge);
                bool isNeighbourVisited = IsNodeinParentOrChild(neighbour);

                if (!isNeighbourVisited)
                {
                    _parent[neighbour] = node;
                    dfsEdge.EdgeType   = DepthFirstSearchEdgeType.TreeEdge;

                    DFSVisit(graph, neighbour);
                }
                else if (!_endTime.ContainsKey(neighbour))
                {
                    dfsEdge.EdgeType = DepthFirstSearchEdgeType.BackEdge;
                }
                else if (_startTime[node] < _startTime[neighbour])
                {
                    dfsEdge.EdgeType = DepthFirstSearchEdgeType.ForwardEdge;
                }
                else
                {
                    dfsEdge.EdgeType = DepthFirstSearchEdgeType.CrossEdge;
                }
            }
            _endTime[node] = ++_time;
            _order.Add(node, _order.Count + 1);
        }