Powerbyte
Powerbyte

Reputation: 159

Is Iterative Deepening Search supposed to be that slow?

My Data Structure:

class Cell
{
public:
    struct CellLink 
    {
        Cell *cell;
        int weight;
    };
public:
    int row;
    int column;
    vector<CellLink> neighbors;
    State state;
    int totalCost = 0;
};

The primary function:

    void AI::IterativeDeepeningSearch(Cell* cell)
        {
            Cell* temp;
            int bound = 0;


        while (true)
        {
            naturalFailure = false;
            temp = IDShelper(cell, bound);

            if (IsExit(temp))
            {
                break;
            }
            bound++;
        }   
    }

The Helper:

Cell* AI::IDShelper(Cell* cell, int bound)
{
    Cell* temp = cell;

    SetEnvironment(cell, State::visited);
    PrintEnvironment();

    if (bound > 0)
    {
        for (int i = 0; i < cell->neighbors.size(); i++)
        {
            temp = IDShelper(cell->neighbors[i].cell, bound - 1);
            if (IsExit(temp))
            {
                naturalFailure = true;
                return temp;
            }
        }
    }
    else if (IsExit(cell))
    {
        return cell;
    }
    return temp;
}

I have made an Iterative Deepening Search for a maze. The problem is that it is taking literally hours to complete the search on a 21x21 maze while other algorithms take a couple of seconds.

I know that IDS is supposed to be slow but is it supposed to be that slow?

Upvotes: 1

Views: 1114

Answers (1)

Dennis Meng
Dennis Meng

Reputation: 5187

I think I can see why this is slow.

In your helper, you're visiting the neighbors like so:

if (bound > 0)
{
    for (int i = 0; i < cell->neighbors.size(); i++)
    {
        temp = IDShelper(cell->neighbors[i].cell, bound - 1);
        if (IsExit(temp))
        {
            naturalFailure = true;
            return temp;
        }
    }
}

but you're never using past results. You mark something as visited, but never check whether it is already visited.

Upvotes: 1

Related Questions