Exemplo n.º 1
0
 private void AddIncidentEdgesToPriorityQueue(List <Edge> incidentEdges, EdgesPriorityQueue priorityQueue)
 {
     foreach (var incidentEdge in incidentEdges)
     {
         if (!_visitedVertices[incidentEdge.Vertice2])
         {
             var edge = new Edge(incidentEdge.Vertice1, incidentEdge.Vertice2, incidentEdge.Cost);
             priorityQueue.Enqueue(edge);
         }
     }
 }
Exemplo n.º 2
0
        private Edge DequeuingEdgesWhileVertice2IsNotVisited(EdgesPriorityQueue priorityQueue)
        {
            Edge edge;

            do
            {
                edge = priorityQueue.Dequeue();
            } while (_visitedVertices[edge.Vertice2]);

            return(edge);
        }
Exemplo n.º 3
0
        private void CreateTree(int startedVertice, int numberOfVertices, List <Edge> edges)
        {
            var vertice = startedVertice;

            _visitedVertices[vertice] = true;
            var priorityQueue = new EdgesPriorityQueue();

            // Do for all other vertices (without 'startedVertice') ['i' is iterator, not vertice index]
            for (int i = 1; i < numberOfVertices; i++)
            {
                var incidentEdges = GetIncidentEdges(vertice, edges);
                SetVerticeAsFirst(vertice, incidentEdges);

                AddIncidentEdgesToPriorityQueue(incidentEdges, priorityQueue);

                var edge = DequeuingEdgesWhileVertice2IsNotVisited(priorityQueue);

                AddEdgeToTreeMatrix(edge);
                SetVerticeAsVisited(edge.Vertice2);

                vertice = edge.Vertice2;
            }
        }