Ejemplo n.º 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);
        }
Ejemplo n.º 2
0
        private GraphVertexInfo GetVertexInfo(GraphVertex v)
        {
            foreach (var i in _infos)
            {
                if (i.Vertex.Equals(v))
                {
                    return(i);
                }
            }

            return(null);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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));
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 6
0
 public void AddEdge(GraphVertex vertex, int edgeWeight)
 {
     AddEdge(new GraphEdge(vertex, edgeWeight));
 }