示例#1
0
        private string GetPath(GraphVertex startVertex, GraphVertex endVertex)
        {
            var path = endVertex.ToString();

            while (startVertex != endVertex)
            {
                endVertex = GetVertexInfo(endVertex).PreviousVertex;
                path      = endVertex.ToString() + path;
            }

            return(path);
        }
示例#2
0
        private GraphVertexInfo GetVertexInfo(GraphVertex v)
        {
            foreach (var i in _infos)
            {
                if (i.Vertex.Equals(v))
                {
                    return(i);
                }
            }

            return(null);
        }
示例#3
0
        public LinkedList <GraphVertex> DFS(GraphVertex start, GraphVertex goal)
        {
            _visited = new HashSet <GraphVertex>();
            _path    = new LinkedList <GraphVertex>();
            _goal    = goal;

            DFS(start);

            if (_path.Count > 0)
            {
                _path.AddFirst(start);
            }

            return(_path);
        }
示例#4
0
        private string FindShortestPath(GraphVertex startVertex, GraphVertex finishVertex)
        {
            InitInfo();
            var first = GetVertexInfo(startVertex);

            first.EdgesWeightSum = 0;
            while (true)
            {
                var current = FindUnvisitedVertexWithMinSum();
                if (current == null)
                {
                    break;
                }

                SetSumToNextVertex(current);
            }

            return(GetPath(startVertex, finishVertex));
        }
示例#5
0
        private bool DFS(GraphVertex node)
        {
            if (node == _goal)
            {
                Console.WriteLine($"Vertice {node.Name} was found in dfs! Yeeeeee");
                return(true);
            }

            _visited.Add(node);

            foreach (var child in node.Edges.Select(e => e.ConnectedVertex).Where(e => !_visited.Contains(e)))
            {
                Console.WriteLine($"Vertice child {child.Name} of {node.Name}");
                if (DFS(child))
                {
                    _path.AddFirst(child);
                    return(true);
                }
            }
            return(false);
        }
 public void AddEdge(GraphVertex vertex, int edgeWeight)
 {
     AddEdge(new GraphEdge(vertex, edgeWeight));
 }