public int CountAllCycles(AbstractDiGraph <TNode> graph, TNode startNode, int maxWeight) { List <AbstractGraphPath <TNode> > simpleCycles = FindAllSimpleCycles(graph, startNode); // Permutation problem to find keep adding combinations of cycles until max weight. // 1. permutation & combination by how many ways the cycles can be selected and then ordered int result = 0; //how many to select for (int i = 1; i <= simpleCycles.Count; i++) { //which cycles to add starting point for (int j = 0; j < simpleCycles.Count; j++) { var currentSetWeight = 0; //add cycle at starting point until count is reached for (int k = j; k < j + i; k++) { currentSetWeight += simpleCycles[k % simpleCycles.Count].PathWeight; } result += Factorial(i) * (maxWeight / currentSetWeight); } } return(result); }
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]); } }
public List <DepthFirstSearchEdge <TNode> > TraverseGraph(AbstractDiGraph <TNode> graph, TNode sourceNode) { var search = new DepthFirstSearchAlgorithm <TNode>(); search.DFSVisit(graph, sourceNode); return(search._dfsEdges); }
private void Advance(AbstractDiGraph <TNode> graph, TNode node) { TNode neighbourNode; if (GetUnvistedEdge(graph, node, out neighbourNode)) { MarkEdgeAsVisted(node, neighbourNode); if (!_vStack.Contains(neighbourNode)) { node = neighbourNode; _vStack.Push(neighbourNode); Advance(graph, node); } else if (_vStack.Contains(neighbourNode) && !_oStack.Contains(neighbourNode)) { ReportCycle(neighbourNode, graph); node = _vStack.Peek(); Advance(graph, node); ; } else { Advance(graph, neighbourNode); } } else if (_vStack.Any()) { ReTreat(graph); } }
public List <AbstractGraphPath <TNode> > FindAllSimpleCycles(AbstractDiGraph <TNode> graph, TNode startNode) { IEnumerable <AbstractGraphPath <TNode> > cyclesWithStartNode = AlgorithmElementaryCircuitSearch .FindAllElementaryCircuits(graph, startNode); return(cyclesWithStartNode.ToList()); }
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]); } }
public List <AbstractGraphPath <TNode> > FindAllElementaryCircuits(AbstractDiGraph <TNode> graph, TNode startNode) { Initialise(graph); TraverseElementaryCircuits(graph, startNode); return(_allcycles); }
private SimplePathSearchAlgorithm <TNode> InitialiseAndGetInstance(AbstractDiGraph <TNode> graph, TNode startNode) { var pathFinder = new SimplePathSearchAlgorithm <TNode>(); pathFinder.RemoveCycles(graph, startNode); pathFinder.Initialise(); return(pathFinder); }
public List <AbstractGraphPath <TNode> > FindShortestCycle(AbstractDiGraph <TNode> graph, TNode startNode) { List <AbstractGraphPath <TNode> > cyclesWithStartNode = AlgorithmElementaryCircuitSearch .FindAllElementaryCircuits(graph, startNode); return (cyclesWithStartNode.Where(x => x.PathWeight == cyclesWithStartNode.Min(y => y.PathWeight)) .ToList()); }
private void TraverseElementaryCircuits(AbstractDiGraph <TNode> graph, TNode node) { if (!_oStack.Contains(node)) { _vStack.Push(node); TNode keyNode = node; Advance(graph, keyNode); } }
public List <AbstractGraphPath <TNode> > FindAllSimplePaths(AbstractDiGraph <TNode> graph, TNode source, TNode destination) { SimplePathSearchAlgorithm <TNode> pathFinder = InitialiseAndGetInstance(graph, source); pathFinder.FindSimplepath(source, destination); return(pathFinder._allpaths); }
private void Initialise(AbstractDiGraph <TNode> graph) { _allcycles = new List <AbstractGraphPath <TNode> >(); _vistedEdges = new List <Tuple <TNode, TNode, int> >(); _vStack = new Stack <TNode>(); _oStack = new Stack <TNode>(); _unvistedEdges = graph.AllEdges; }
public List <AbstractGraphPath <TNode> > FindAllSimpleCycles(AbstractDiGraph <TNode> graph, TNode startNode, int maxStops) { IEnumerable <AbstractGraphPath <TNode> > cyclesWithStartNode = AlgorithmElementaryCircuitSearch .FindAllElementaryCircuits(graph, startNode); return(cyclesWithStartNode.Where(x => x.Path.Count <= maxStops + 1).ToList()); }
public List <AbstractGraphPath <TNode> > FindAllElementaryCircuits(AbstractDiGraph <TNode> graph) { Initialise(graph); foreach (TNode node in graph.AllNodes) { TraverseElementaryCircuits(graph, node); } return(_allcycles); }
private void RemoveCycles(AbstractDiGraph <TNode> graph, TNode startNode) { List <DepthFirstSearchEdge <TNode> > dfsEdges = DFSAlgorithm.TraverseGraph(graph, startNode); List <DepthFirstSearchEdge <TNode> > _backedges = dfsEdges.Where( x => x.EdgeType == DepthFirstSearchEdgeType.BackEdge && x.DestinationNode.Equals(startNode)) .ToList(); _backedges.ForEach(x => dfsEdges.Remove(x)); dfsEdges.ForEach(x => _cycleFreeGraph.AddEdge(x.SourceNode, x.DestinationNode, x.EdgeWeight)); }
public List <DepthFirstSearchEdge <TNode> > TraverseGraph(AbstractDiGraph <TNode> graph) { foreach (TNode vertex in graph.AllNodes) { if (!IsNodeinParentOrChild(vertex)) { DFSVisit(graph, vertex); } } _isTraversed = true; return(_dfsEdges); }
private void ReTreat(AbstractDiGraph <TNode> graph) { TNode node = _vStack.Pop(); if (!_oStack.Contains(node)) { _oStack.Push(node); } MarkOriginatingEdgesAsUnVisted(node); if (_vStack.Any()) { Advance(graph, _vStack.Peek()); } }
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 bool GetUnvistedEdge(AbstractDiGraph <TNode> graph, TNode sourceNode, out TNode destinationNode) { destinationNode = default(TNode); bool hasUnvistitedNodes = graph.GetNeighbourNodes(sourceNode) .Any( y => !_vistedEdges.Any(x => x.Item1.Equals(sourceNode) && x.Item2.Equals(y))); if (hasUnvistitedNodes) { destinationNode = graph.GetNeighbourNodes(sourceNode) .First( y => !_vistedEdges.Any(x => x.Item1.Equals(sourceNode) && x.Item2.Equals(y))); } return(hasUnvistitedNodes); }
private void Initialise(AbstractDiGraph <TNode> graph, TNode sourceNode) { foreach (var node in graph.AllNodes.Where(x => !x.Equals(sourceNode))) { var shortestPathNode = new GraphPath <TNode>(); shortestPathNode.DestinationNode = node; shortestPathNode.PathWeight = Infinity; shortestPathNode.SourceNode = sourceNode; _estimatedDistances.Add(node, shortestPathNode); _shortestPathUnKnownNodesQueue.Add(shortestPathNode); } //Source Node Initialise _estimatedDistances.Add(sourceNode, new GraphPath <TNode>() { DestinationNode = sourceNode, SourceNode = sourceNode }); _estimatedDistances[sourceNode].PathWeight = 0; _shortestPathKnownNodes.Add(sourceNode); }
public int CountAllPaths(AbstractDiGraph <TNode> graph, TNode source, TNode destination, int numberOfStops) { List <AbstractGraphPath <TNode> > simplePaths = FindAllSimplePaths(graph, source, destination); int result = simplePaths.Count(x => x.Path.Count - 1 == numberOfStops); //Find cycles that could intersect with paths with smaller stops and make the path longer IEnumerable <AbstractGraphPath <TNode> > potentialPaths = simplePaths.Where(x => x.Path.Count < numberOfStops); List <AbstractGraphPath <TNode> > cycles = CycleOperations.FindAllSimpleCycles(graph); foreach (var potentialPath in potentialPaths) { IEnumerable <AbstractGraphPath <TNode> > cyclesThatIntersection = cycles.Where(c => c.Path.Any(p => potentialPath.Path.Contains(p))); result += cyclesThatIntersection.Count(c => c.Path.Count - 1 + potentialPath.Path.Count - 1 == numberOfStops); } 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); }
public Calculate(AbstractDiGraph <char> graph) { _graph = graph; }
public List <AbstractGraphPath <TNode> > FindAllSimplePaths(AbstractDiGraph <TNode> graph, TNode source, TNode destination) { return(PathFinder.FindAllSimplePaths(graph, source, destination)); }
public List <AbstractGraphPath <TNode> > FindAllSimpleCycles(AbstractDiGraph <TNode> graph) { return(AlgorithmElementaryCircuitSearch.FindAllElementaryCircuits(graph)); }