Exemplo n.º 1
0
        /// <summary>
        /// Implementation of Dijkstra's between 2 nodes
        /// </summary>
        /// <param name="node1"></param>
        /// <param name="node2"></param>
        /// <returns>A tuple containing the degrees of seperation of the two nodes and a detailing the connections between the two nodes.
        /// Returns an "empty" tuple (0, new List) if the connection wasn't found</returns>
        private Tuple <int, List <Edge <T2, T1> >, List <MyNode <T1> > > MyDijkstras(MyNode <T1> node1, MyNode <T1> node2)
        {
            // TODO: Implement logic to handle disconnected graphs
            // TODO: Implement logic to handle weighted connections
            // TODO: Implement logic to handle directed connections
            int degreeOfSep = 0;
            List <MyNode <T1> >   nodesOnPath = new List <MyNode <T1> >();
            List <Edge <T2, T1> > edgesOnPath = new List <Edge <T2, T1> >();

            Dictionary <MyNode <T1>, double> nodeDistances = new Dictionary <MyNode <T1>, double>();
            HashSet <MyNode <T1> >           toBeVisited   = new HashSet <MyNode <T1> >();

            foreach (MyNode <T1> node in nodesInGraph)
            {
                toBeVisited.Add(node);
                nodeDistances.Add(node, Int32.MaxValue);
            }
            nodeDistances[node1] = 0; //setting starting node distance as 0
            MyNode <T1> previous = node1;

            while (toBeVisited.Count > 0)
            {
                MyNode <T1> nearestNode = NodeWithShortestDistance(nodeDistances);
                toBeVisited.Remove(nearestNode); //progressing towards condition to exit while loop
                //nodesOnPath.Add(nearestNode);
                degreeOfSep++;
                if (!nearestNode.Equals(node2))   //nearest node isn't the one we're looking for
                {
                    nodesOnPath.Add(nearestNode); //add nearest node to path
                    foreach (Edge <T2, T1> edge in adjacencyListEdges[previous])
                    {
                        if (edge.Node2.Equals(nearestNode)) //found an edge from current node to nearestEdge, TODO: need to account for when there are mutliple edges between these two nodes
                        {
                            edgesOnPath.Add(edge);
                        }
                    }
                }
                if (nearestNode.Equals(node2)) //found the desired node
                {
                    //add nearest node on the path, as the terminal node
                    nodesOnPath.Add(nearestNode);
                    foreach (Edge <T2, T1> edge in adjacencyListEdges[previous])
                    {
                        if (edge.Node2.Equals(nearestNode)) //found an edge from current node to nearestEdge which in this case is the terminal node, TODO: need to account for when there are mutliple edges between these two nodes
                        {
                            edgesOnPath.Add(edge);          // TODO: choose edge with smallest weight etc.
                        }
                        return(Tuple.Create(degreeOfSep, edgesOnPath, nodesOnPath));
                    }
                }

                foreach (MyNode <T1> node in nearestNode.NEIGHBORS) //updating the distance for neighbors of found nearest node
                {
                    double temp = nodeDistances[nearestNode] + DistanceBetweenNodes(nearestNode, node, false);
                    if (temp < nodeDistances[node])
                    {
                        nodeDistances[node] = temp;
                    }
                }
                previous = nearestNode;                                  //now we will progress from the currently found nearest node
            }
            return(Tuple.Create(degreeOfSep, edgesOnPath, nodesOnPath)); // TODO: investigate what this statement can return
        }