Пример #1
0
        //Adds a directed weighted connection between nodes a and b
        public void addDirectedConnection(NodeMx <T> a, NodeMx <T> b, double w)
        {
            EdgeMx <T> edgeA = new EdgeMx <T>(a, b, w, uniqueNodeID);

            edges.Add(edgeA);
            a.addEdge(edgeA);
            edgeCount = edges.Count;
            uniqueNodeID++;
        }
        public int CompareTo(object obj)
        {
            if (obj == null)
            {
                return(1);
            }
            EdgeMx <T> e = obj as EdgeMx <T>;

            return(this.identifier.CompareTo(e.identifier));
        }
Пример #3
0
        //Creates and unweights and undirected connection between nodes a and b
        public void addConnection(NodeMx <T> a, NodeMx <T> b)
        {
            EdgeMx <T> edgeA = new EdgeMx <T>(a, b, uniqueEdgeID);
            EdgeMx <T> edgeB = new EdgeMx <T>(b, a, uniqueEdgeID);

            edges.Add(edgeA);
            a.addEdge(edgeA);
            b.addEdge(edgeB);
            edgeCount = edges.Count;
            uniqueEdgeID++;
        }
 //Remove an edge from this node
 public void removeEdge(EdgeMx <T> x)
 {
     for (int n = 0; n < nodeEdges.Count; n++)
     {
         if (nodeEdges[n].identifier == x.identifier)
         {
             nodeEdges.RemoveAt(n);
             nodeEdges.TrimExcess();
             break;
         }
     }
 }
Пример #5
0
        //Remove an edge from the graph
        public void removeConnection(EdgeMx <T> E)
        {
            for (int n = 0; n < edges.Count; n++)
            {
                if (E.identifier == edges[n].identifier)
                {
                    //First remove the edge from the 2 nodes it connects
                    E.nodes[0].removeEdge(E);
                    if (!E.isDirected)
                    {
                        E.nodes[1].removeEdge(E);
                    }

                    edges.RemoveAt(n);
                    edges.TrimExcess();
                    edgeCount = edges.Count;
                    break;
                }
            }
        }
 //Create an undirected/unwieghted edge between this node and some other node
 //The graph class will make sure this edge is created correctly. The purpose
 //of having a list of edges in this class is to quickly see the neighbors that
 //this node has
 public void addEdge(EdgeMx <T> x)
 {
     nodeEdges.Add(x);
 }
        public static double[] DijkstraShortestPath(GraphMx <int> G, int S, out List <NodeMx <int> >[] paths)
        {
            double[] shortestPaths = new double[G.nodeCount];
            paths = new List <NodeMx <int> > [G.nodeCount];

            //Set all the initials shortest paths to basically infinity and then reassign them to an
            //initial value of -1 for the nodes that are reachable
            for (int n = 0; n < shortestPaths.Length; n++)
            {
                shortestPaths[n] = Double.MaxValue;
            }

            //First we need to figure out which nodes arent reachable from out starting node S.
            List <NodeMx <int> > ReachableNodes = BFS(G, S);

            for (int n = 0; n < ReachableNodes.Count; n++)
            {
                shortestPaths[ReachableNodes[n].identifier] = -1;
            }

            //During the process of the breadth first search (BFS) we will have set the "isVisited" flag to true
            //for each node which we now need to undo
            for (int n = 0; n < G.nodeCount; n++)
            {
                G.nodes[n].isVisited = false;
            }

            //Initialize the paths to unreachable nodes as null
            for (int n = 0; n < G.nodeCount; n++)
            {
                if (shortestPaths[n] > -1)
                {
                    paths[n] = null;
                }
                else
                {
                    paths[n] = new List <NodeMx <int> >();
                    paths[n].Add(G.nodes[S]);
                }
            }

            //Initialize our explored nodes with our starting node
            List <NodeMx <int> > exploredNodes = new List <NodeMx <int> >();

            exploredNodes.Add(G.nodes[S]);
            G.nodes[S].isVisited = true;
            shortestPaths[S]     = 0;


            while (exploredNodes.Count < ReachableNodes.Count)
            {
                //For each node we have explored
                List <EdgeMx <int> > edgesToConsider = new List <EdgeMx <int> >();
                for (int n = 0; n < exploredNodes.Count; n++)
                {
                    NodeMx <int> tempNodeTail = exploredNodes[n];

                    //Find all of its edges in the region which we have not yet explored
                    for (int m = 0; m < tempNodeTail.nodeEdges.Count; m++)
                    {
                        NodeMx <int> tempNodeHead = tempNodeTail.nodeEdges[m].nodes[1];
                        if (tempNodeHead.isVisited == false)
                        {
                            edgesToConsider.Add(tempNodeTail.nodeEdges[m]);
                        }
                    }
                }

                //Now that we have the list of candidate edges we need to compute the Dijkstra criteria for each edge
                EdgeMx <int> nextEdge   = edgesToConsider[0];
                double       DijstraMin = Double.MaxValue;
                double       tempDijkstra;
                for (int n = 0; n < edgesToConsider.Count; n++)
                {
                    tempDijkstra = shortestPaths[edgesToConsider[n].nodes[0].identifier] + edgesToConsider[n].weight;
                    if (tempDijkstra < DijstraMin)
                    {
                        DijstraMin = tempDijkstra;
                        nextEdge   = edgesToConsider[n];
                    }
                }

                //Now that we have found the edge that minimizes the Dijkstra criteria we will add the node at the
                //head of the edge to the list of explored nodes and update its shortest path length and shortest
                //path in our other variables
                NodeMx <int> nodeToAdd = nextEdge.nodes[1];
                nodeToAdd.isVisited = true;
                exploredNodes.Add(nodeToAdd);

                //The shortest path of the node we are going to add is equal to the shortest path of the node from which we
                //came from plus the weight of the edge
                shortestPaths[nodeToAdd.identifier] = shortestPaths[nextEdge.nodes[0].identifier] + nextEdge.weight;
                List <NodeMx <int> > pathUptillNow = paths[nextEdge.nodes[0].identifier];
                for (int n = 1; n < pathUptillNow.Count; n++)
                {
                    paths[nodeToAdd.identifier].Add(pathUptillNow[n]);
                }
                paths[nodeToAdd.identifier].Add(nodeToAdd);
            }

            return(shortestPaths);
        }