コード例 #1
0
        public static List <Path> GetPathsForPointInGraph(IGraphBasics <Point, IEdgeBasics <Point> > CurrentGraph, Point CurrentPoint)
        {
            List <Path> currentListPaths = new List <Path>
                                           (
                CurrentGraph.EdgeCollection
                .Where(i2 => i2.StartPoint.Equals(CurrentPoint))
                .Select(i2 =>
            {
                Path currentPath = Path.InitPath();
                currentPath.AddEdge(i2);
                return(currentPath);
            }));

            for (int i = 0; i < currentListPaths.Count(); i++)
            {
                Path currentPath = currentListPaths[i];
                IEnumerable <IEdgeBasics <Point> > candidatesToPath = CurrentGraph.EdgeCollection.Where(i1 => i1.StartPoint.Equals(currentPath.ListPathEdges.Last().EndPoint));
                foreach (IEdgeBasics <Point> e in candidatesToPath)
                {
                    if (!currentPath.ListPathPoints.Contains(e.EndPoint))
                    {
                        Path newPath = Path.InitPath(currentPath);
                        newPath.AddEdge(e);
                        currentListPaths.Add(newPath);
                    }
                }
            }

            return(currentListPaths);
        }
コード例 #2
0
        public static bool CheckCycles(IGraphBasics <Point, AbstractEdge <Point> > CurrentGraph)
        {
            if (CheckSimpleCycles(CurrentGraph))
            {
                return(true);
            }

            List <Path> listPaths = CurrentGraph.EdgeCollection.Select <AbstractEdge <Point>, Path>(i1 =>
            {
                Path path = Path.InitPath();
                path.AddEdge(i1);
                return(path);
            }).ToList();

            for (int i = 0; i < listPaths.Count(); i++)
            {
                Path currentPath = listPaths[i];
                IEnumerable <AbstractEdge <Point> > candidatesToPath = CurrentGraph.EdgeCollection.Where(i1 => i1.StartPoint.Equals(currentPath.ListPathEdges.Last().EndPoint));
                foreach (Edge e in candidatesToPath)
                {
                    if (currentPath.ListPathPoints.Contains(e.EndPoint))
                    {
                        return(true);
                    }
                    else
                    {
                        Path newPath = Path.InitPath(currentPath);
                        newPath.AddEdge(e);
                        listPaths.Add(newPath);
                    }
                }
            }
            return(false);
        }
コード例 #3
0
        private bool Check()
        {
            if (_graph.EdgeCount == 1)
            {
                Path.AddEdge(_graph.Edges.First());
                if (Path.IsDirectedAcyclicGraph())
                {
                    Path.RemoveEdge(_graph.Edges.First());
                    return(false);
                }

                return(true);
            }

            return(false);
        }
コード例 #4
0
        private void RemoveCycles()
        {
            var edgesToRemove = new List <TEdge>();

            foreach (TEdge edge in _graph.Edges)
            {
                Path.AddEdge(edge);
                if (!Path.IsDirectedAcyclicGraph())
                {
                    edgesToRemove.Add(edge);
                    _weight.Remove(edge);
                }

                Path.RemoveEdge(edge);
            }

            edgesToRemove.ForEach(edge => _graph.RemoveEdge(edge));
        }
コード例 #5
0
ファイル: Edge.cs プロジェクト: Yash-Codemaster/KoduGameLab
            /// <summary>
            /// Edge c'tor.  In addition to creating the edge this also checks to
            /// make sure it's not a duplicate and adds it to the right path.
            /// </summary>
            /// <param name="node0"></param>
            /// <param name="node1"></param>
            public Edge(Node node0, Node node1)
            {
                this.node0 = node0;
                this.node1 = node1;

                Path path = node0.Path;

                // If the nodes are equal then this is a fake edge used to make
                // things work better for paths with only 1 node so don't add it
                // to the path.
                if (node0 == node1)
                {
                    return;
                }

                // By default, add this edge to the path that owns the first node.
                // But first, see if it's a duplicate.
                for (int i = 0; i < path.Edges.Count; i++)
                {
                    Edge e = (Edge)path.Edges[i];

                    if ((e.Node0 == node0 && e.Node1 == node1) || (e.Node0 == node1 && e.Node1 == node0))
                    {
                        // Duplicate, so don't add it.
                        return;
                    }
                }

                path.AddEdge(this);

                // Then if the second node belongs to a different path we need to
                // merge the second node's path into the first.
                if (node0.Path != node1.Path)
                {
                    MergePaths(node0.Path, node1.Path);
                }
            }   // end of Edge c'tor
コード例 #6
0
    public Path CalculateEdgeBasedPath(GraphNode start, GraphNode end)
    {
        timer.Start();
        Edge currentEdge = new Edge();

        //remove old path
        m_ComputedPath.ClearPath();
        //empty old lists
        foreach (GraphNode node in m_OpenGraphNodes)
        {
            node.m_open = false;
        }
        m_OpenGraphNodes.Clear();
        m_OpenGraphNodes.TrimExcess();
        foreach (GraphNode node in m_ClosedGraphNodes)
        {
            node.m_closed = false;
        }
        m_ClosedGraphNodes.Clear();
        m_ClosedGraphNodes.TrimExcess();
        //add inital node
        m_OpenGraphNodes.Add(start);
        start.m_open = true;
        while (m_OpenGraphNodes.Count != 0 || (m_currentGraphNode.m_closed && m_currentGraphNode != end))
        {
            if (m_OpenGraphNodes.Count == 0)
            {
                m_pathSuccessful = false;
                break;
            }
            m_currentGraphNode = m_OpenGraphNodes[0];
            m_OpenGraphNodes.RemoveAt(0);
            m_currentGraphNode.m_open = false;
            m_ClosedGraphNodes.Add(m_currentGraphNode);
            m_currentGraphNode.m_closed = true;
            if (m_currentGraphNode == end)
            {
                m_pathSuccessful = true;
                break;
            }
            for (int i = 0; i < m_currentGraphNode.m_AdjacentNodes.Count; i++)// (Node adjacent in m_current.m_AdjacentNodes)
            {
                currentEdge = m_currentGraphNode.m_ConnectedEdges[i];
                if (m_currentGraphNode.m_AdjacentNodes[i].m_walkable == false || m_currentGraphNode.m_AdjacentNodes[i].m_closed)
                {
                    continue;
                }
                if (!m_currentGraphNode.m_AdjacentNodes[i].m_open)
                {
                    m_OpenGraphNodes.Add(m_currentGraphNode.m_AdjacentNodes[i]);
                    m_currentGraphNode.m_AdjacentNodes[i].m_open       = true;
                    m_currentGraphNode.m_AdjacentNodes[i].m_Parent     = m_currentGraphNode;
                    m_currentGraphNode.m_AdjacentNodes[i].m_ParentEdge = currentEdge;
                    m_currentGraphNode.m_AdjacentNodes[i].CalculateEdgeBasedLocalFGH(end, currentEdge.m_traversalCost);
                    //resort list
                    m_OpenGraphNodes.Sort((node1, node2) => node1.m_FCost.CompareTo(node2.m_FCost));
                }
                if (m_currentGraphNode.m_AdjacentNodes[i].m_open && m_currentGraphNode.m_AdjacentNodes[i].m_GCost < m_currentGraphNode.m_GCost)
                {
                    m_currentGraphNode.m_AdjacentNodes[i].m_Parent     = m_currentGraphNode;
                    m_currentGraphNode.m_AdjacentNodes[i].m_ParentEdge = currentEdge;
                    m_currentGraphNode.m_AdjacentNodes[i].CalculateEdgeBasedLocalFGH(end, currentEdge.m_traversalCost);
                    //resort list
                    m_OpenGraphNodes.Sort((node1, node2) => node1.m_FCost.CompareTo(node2.m_FCost));
                }
            }
            //sort open by f (lowest first)
            m_OpenGraphNodes.Sort((node1, node2) => node1.m_FCost.CompareTo(node2.m_FCost));
        }
        //~while
        timer.Stop();

        if (m_pathSuccessful)
        {
            //build path
            m_currentGraphNode = end;
            while (m_currentGraphNode != start)
            {
                m_ComputedPath.AddNode(m_currentGraphNode);
                if (m_currentGraphNode.m_ParentEdge != null)
                {
                    m_ComputedPath.AddEdge(m_currentGraphNode.m_ParentEdge);
                }
                //if no parent assume its the start
                if (m_currentGraphNode.m_Parent != null)
                {
                    m_currentGraphNode = m_currentGraphNode.m_Parent;
                }
                else
                {
                    break;
                }
            }
            if (m_currentGraphNode == start)
            {
                m_ComputedPath.AddNode(m_currentGraphNode);
            }
            //~while
        }
        else
        {
            Debug.LogWarning("No Path!");
            return(null);
        }
        Debug.Log("Time taken to Calculate Path: " + (float.Parse(timer.ElapsedTicks.ToString()) / 10000).ToString() + "ms");
        timer.Reset();
        return(m_ComputedPath);
    }
コード例 #7
0
        public void Search()
        {
            List <Edge> edges     = new List <Edge>();
            List <Edge> pathEdges = new List <Edge>();

            Node startNode = null;

            int  min     = 0;
            bool isEnded = false;

            foreach (Node node in Graph.Nodes)
            {
                if (node.Identifier == "s")
                {
                    startNode = node;
                }
            }

            while (!isEnded)
            {
                if (startNode == null)
                {
                    startNode = pathEdges[pathEdges.Count - 1].End;
                }

                foreach (Edge edge in Graph.Edges)
                {
                    if (edge.Start.Equals(startNode))
                    {
                        edges.Add(edge);
                    }
                }

                for (int i = 0; i < edges.Count - 1; i++)
                {
                    if (edges[min].Weight > edges[i + 1].Weight)
                    {
                        min = i + 1;
                    }
                }

                pathEdges.Add(edges[min]);
                min       = 0;
                startNode = null;
                edges.Clear();

                if (pathEdges[pathEdges.Count - 1].End.Identifier == "e")
                {
                    isEnded = true;;
                }
            }

            Path path = new Path();

            foreach (Edge edge in pathEdges)
            {
                path.AddEdge(edge);
            }

            PathConstanse.AddPath(path);
            PathConstanse.SetShortestPath();
        }