Algorithm Bincike na tushen Jiha (State-Based Search) in Java

Algorithm din Bincike na tushen Jiha hanya ce ta nema a cikin Java shirye-shirye wanda ya ƙunshi ƙirƙira da ratsa ta cikin yuwuwar jihohin matsala. A cikin wannan algorithm, duk yuwuwar jihohin matsala ana wakilta su azaman nodes a cikin jadawali ko sarari na jiha.

Yadda Algorithm na Bincike na tushen Jiha ke Aiki

Algorithm yana farawa daga yanayin farko kuma yana amfani da canje-canje don samar da jihohin yara. Kowace sabuwar jiha ta zama kumburi a cikin jadawali ko sararin jiha. Algorithm ya ratsa ta cikin waɗannan jihohin, yana duba ko jihar manufa tana cikin su. Idan an samo, algorithm ya ƙare; in ba haka ba, yana ci gaba da ratsawa ta wasu jihohin yara.

Abũbuwan amfãni da rashin amfani Algorithm Bincike na tushen Jiha

Amfani:

  • Ƙarfafawa: Algorithm yana da ikon rufe duk yuwuwar jihohin matsalar.
  • M: Ana iya amfani da shi ga matsaloli iri-iri.

Rashin hasara:

  • Yiwuwar maimaitawa: A wasu lokuta, algorithm na iya maimaita ketare wasu jihohi.

Misali da Bayani

Misalin misali na Algorithm na Bincike na tushen Jiha shine nemo hanya daga wurin farawa zuwa makoma akan taswira. Bari mu ga yadda wannan algorithm ke aiki:

import java.util.*;  
  
public class StateSearchExample {  
    static boolean isGoalState(State state, State goalState) {  
        return state.equals(goalState);  
    }  
  
    static void stateSearch(State initialState, State goalState) {  
        Queue<State> queue = new LinkedList<>();  
        Set<State> visited = new HashSet<>();  
  
        queue.add(initialState);  
        visited.add(initialState);  
  
        while(!queue.isEmpty()) {  
            State currentState = queue.poll();  
  
            if(isGoalState(currentState, goalState)) {  
                System.out.println("Found goal state: " + currentState);  
                return;  
            }  
  
            List<State> nextStates = currentState.generateNextStates();  
            for(State nextState: nextStates) {  
                if(!visited.contains(nextState)) {  
                    queue.add(nextState);  
                    visited.add(nextState);  
                }  
            }  
        }  
    }  
}  

A cikin misalin da ke sama, muna amfani da Algorithm na Bincike na tushen Jiha don nemo hanya daga yanayin farko zuwa yanayin manufa akan taswira. Ana haifar da jihohin yara ta hanyar aiwatar da ayyuka masu yiwuwa daga halin yanzu. Sakamakon shi ne cewa algorithm zai sami hanya daga jihar farawa zuwa jihar manufa.