Exemplo n.º 1
0
        public BFS(WeightedGraph <T> graph, T source, T destination) : base(graph, source, destination)
        {
            distTo  = new double[graph.V];
            visited = new bool[graph.V];
            edgeTo  = new Edge[graph.V];
            Queue <int> q = new Queue <int>();

            q.Enqueue(graph.GetVertex(source));
            visited[graph.GetVertex(source)] = true;

            int exploredNodes = 0;
            int dest          = graph.GetVertex(destination);

            while (q.Count > 0)
            {
                int curr = q.Dequeue();
                exploredNodes++;
                if (curr == dest)
                {
                    ExploredNodes = exploredNodes;
                    break;
                }
                foreach (var edge in graph.Adj(curr))
                {
                    int other = edge.Other(curr);
                    if (!visited[other])
                    {
                        edgeTo[other]  = edge;
                        distTo[other]  = distTo[curr] + edge.Weight;
                        visited[other] = true;
                        q.Enqueue(other);
                    }
                }
            }
        }
Exemplo n.º 2
0
        public AStar(WeightedGraph <Node> g, Node s, Node des) : base(g, s, des)
        {
            pq     = new MinHeap <Aux>();
            distTo = new double[g.V];
            edgeTo = new Edge[g.V];

            for (int i = 0; i < g.V; i++)
            {
                distTo[i] = double.PositiveInfinity;
            }
            distTo[g.GetVertex(s)] = 0;

            pq.Add(new Aux(g.GetVertex(s), distTo[g.GetVertex(s)], 0));
            int exploredNodes = 0;

            while (pq.Count > 0)
            {
                exploredNodes++;
                Aux v = pq.ExtractDominating();
                if (v.V == g.GetVertex(des))
                {
                    ExploredNodes = exploredNodes;
                    break;
                }
                foreach (Edge e in g.Adj(v.V))
                {
                    Relax(e, v.V);
                }
            }
        }
Exemplo n.º 3
0
 private void dfs2(int curr)
 {
     exploredNodes++;
     visited[curr] = true;
     if (curr == destinationIndx)
     {
         ExploredNodes = exploredNodes;
     }
     foreach (var node in graph.Adj(curr))
     {
         int other = node.Other(curr);
         if (!visited[other])
         {
             edgeTo[other] = node;
             distTo[other] = node.Weight + distTo[curr];
             dfs2(node.Other(curr));
         }
     }
 }