public void AreAdjacentReturnsTrue()
 {
     _simpleDirectedGraph.AddEdge("A", "B", "L1");
     _simpleDirectedGraph.AddEdge("A", "C", "L2");
     Assert.That(_simpleDirectedGraph.AreAdjacent("A", "B"), Is.True);
     Assert.That(_simpleDirectedGraph.AreAdjacent("A", "C"), Is.True);
 }
Esempio n. 2
0
        void BuildLineGraph()
        {
            int n = _graph.N;

            _edges = new List <Tuple <int, int> >();
            for (int i = 0; i < n; i++)
            {
                for (int j = i + 1; j < n; j++)
                {
                    if (_graph.AreAdjacent(i, j))
                    {
                        _edges.Add(new Tuple <int, int>(i, j));
                    }
                }
            }

            var meets = new bool[_edges.Count, _edges.Count];

            for (int i = 0; i < _edges.Count; i++)
            {
                for (int j = i + 1; j < _edges.Count; j++)
                {
                    if (_edges[i].Item1 == _edges[j].Item1 ||
                        _edges[i].Item1 == _edges[j].Item2 ||
                        _edges[i].Item2 == _edges[j].Item1 ||
                        _edges[i].Item2 == _edges[j].Item2)
                    {
                        meets[i, j] = meets[j, i] = true;
                    }
                }
            }

            _lineGraph = new TGraph();
            _lineGraph.Initialize(meets);
        }
        public List <DistanceModel <T> > Dijkstra(IGraph <T> graph, IVertex <T> start)
        {
            graph.GetVertices().ForEach(v => v.UnVisit());
            var list = new List <DistanceModel <T> > {
                new DistanceModel <T>(start, 0, start)
            };
            var matrix  = graph.GetMatrix();
            var current = start;

            while (graph.UnVisitedVertices().Any())
            {
                current.Visit();
                for (int i = 0; i < graph.GetMaxSize(); i++)
                {
                    var neighbour = graph.GetVertices().FirstOrDefault(v => v.GetIndex() == i);
                    if (graph.AreAdjacent(current, neighbour))
                    {
                        var tentativeDist = GetModel(list, current).Distance() + matrix[current.GetIndex(), i];
                        if (ContainsAndGreater(list, neighbour, tentativeDist))
                        {
                            var neighbourModel = GetModel(list, neighbour);
                            neighbourModel.ChangeDistance(tentativeDist);
                            neighbourModel.ChangePrevios(current);
                        }
                        else if (!Contains(list, neighbour))
                        {
                            list.Add(new DistanceModel <T>(neighbour, tentativeDist, current));
                        }
                    }
                }
                current = MinUnvisitedVertex(list);
            }
            return(list);
        }