Ejemplo n.º 1
0
        public GraphEdge GetEdge(int From, int To)
        {
            GraphEdge EdgeToReturn = null;

            foreach (var Edge in EdgeVector[From])
            {
                if (Edge.ToNodeIndex == To)
                {
                    EdgeToReturn = Edge;
                }
            }

            return(EdgeToReturn);
        }
Ejemplo n.º 2
0
        public void RemoveEdge(int From, int To)
        {
            GraphEdge EdgeToRemove = null;

            foreach (var Edge in EdgeVector[From])
            {
                if (Edge.ToNodeIndex == To)
                {
                    EdgeToRemove = Edge;
                    break;
                }
            }

            EdgeVector[From].Remove(EdgeToRemove);
        }
Ejemplo n.º 3
0
        public bool Search()
        {
            Stack <GraphEdge> Stack = new Stack <GraphEdge>();

            SpanningTree.Clear();
            bFound = false;
            GraphEdge Dummy = new GraphEdge(SourceNodeIndex, SourceNodeIndex, 0);

            Stack.Push(Dummy);

            while (Stack.Count > 0)
            {
                // pop the next edge from the stack of edges to examine
                GraphEdge Next = Stack.Pop();

                // make a note of the parent of the node this edge points to
                Route[Next.ToNodeIndex] = Next.FromNodeIndex;

                // ...and mark it visited
                VisitedNodes[Next.ToNodeIndex] = (int)NodeStatus.Visited;

                // keep track of which edges we have traversed to find a path
                if (Next != Dummy)
                {
                    SpanningTree.Add(Next);
                }

                // did we find the target?
                if (Next.ToNodeIndex == TargetNodeIndex)
                {
                    bFound = true;
                    return(true);
                }

                // push edges leading from the node this edges points to onto the stack.
                foreach (var Edge in Graph.Edges[Next.ToNodeIndex])
                {
                    // if we havent visited this node, add it to the stack to be examined
                    if (VisitedNodes[Edge.ToNodeIndex] == (int)NodeStatus.Unvisited)
                    {
                        Stack.Push(Edge);
                    }
                }
            }

            return(false);
        }
Ejemplo n.º 4
0
        public bool Search()
        {
            Queue <GraphEdge> Queue = new Queue <GraphEdge>();

            TraversedEdges.Clear();
            bFound = false;
            GraphEdge Dummy = new GraphEdge(SourceNodeIndex, SourceNodeIndex, 0);

            Queue.Enqueue(Dummy);

            while (Queue.Count > 0)
            {
                GraphEdge Next = Queue.Dequeue();

                Route[Next.ToNodeIndex] = Next.FromNodeIndex;

                TraversedEdges.Add(Next);

                if (Next.ToNodeIndex == TargetNodeIndex)
                {
                    bFound = true;
                    return(true);
                }

                foreach (var Edge in Graph.Edges[Next.ToNodeIndex])
                {
                    if (VisitedNodes[Edge.ToNodeIndex] == (int)NodeStatus.Unvisited)
                    {
                        Queue.Enqueue(Edge);

                        // mark node as visited before it is examined
                        // ensures a maximum of N edges are ever placed in the queue, rather than E edges
                        VisitedNodes[Edge.ToNodeIndex] = (int)NodeStatus.Visited;
                    }
                }
            }

            return(false);
        }
Ejemplo n.º 5
0
 // Adds a new Edge to the linked list from the front
 public void AddEdgeAtBegin(GraphEdge Edge)
 {
     //    EdgeVector[Edge.FromNodeIndex].AddFirst(Edge);
     EdgeVector[Edge.FromNodeIndex].Insert(0, Edge);
 }
Ejemplo n.º 6
0
 // Removmes the first occurence of an edge and returns true if there was any change
 // in the collection, otherwise false.
 public bool RemoveEdge(GraphEdge Edge)
 {
     return(EdgeVector[Edge.FromNodeIndex].Remove(Edge));
 }