Algorithm na Hotuna (Graph Search) a cikin C++- Bayani, Misali da Lamba

Algorithm ɗin Binciken Graph wata babbar dabara ce a fagen sarrafa jadawali da dawo da bayanai. Wannan algorithm yana ba mu damar nemo hanyoyi ko abubuwan haɗin gwiwa a cikin jadawali dangane da takamaiman ƙa'idodi ko algorithms bincike.

Yadda Ake Aiki

  1. Fara daga takamaiman tsayin daka(ƙumburi) a cikin jadawali.
  2. Yi tsarin bincike bisa ƙayyadaddun ƙa'idodi, kamar Neman Zurfin-Farko(DFS) ko Bincike na Farko(BFS).
  3. Mayar da madaidaici da gefuna na jadawali don bincika manufa ko abubuwan da za a samu.
  4. Yi rikodin hanyar ko sakamakon bincike.

Misali

Yi la'akari da jadawali mai zuwa:

A -- B -- C -- E  
|      |  
D --------  

Muna son nemo hanya daga vertex A zuwa vertex E a cikin wannan jadawali ta amfani da Algorithm na Binciken Zurfin Farko(DFS).

  1. Fara daga vertex A.
  2. Matsa zuwa sigar B.
  3. Ci gaba zuwa kishiyar C.
  4. Babu maƙwabta a cikin C, koma baya zuwa tazarar B.
  5. Matsa zuwa sigar D.
  6. Ci gaba zuwa tsaye A(kamar yadda D aka haɗa zuwa A).
  7. Matsa zuwa sigar B.
  8. Matsa zuwa sigar C.
  9. Matsa zuwa ƙwanƙwasa E.

Hanyar daga A zuwa E shine A -> B -> C -> E.

Misali Code a C++

#include <iostream>  
#include <vector>  
#include <stack>  
#include <unordered_map>  
  
class Graph {  
public:  
    void addEdge(char from, char to);  
    std::vector<char> depthFirstSearch(char start, char end);  
  
private:  
    std::unordered_map<char, std::vector<char>> adjList;  
};  
  
void Graph::addEdge(char from, char to) {  
    adjList[from].push_back(to);  
    adjList[to].push_back(from);  
}  
  
std::vector<char> Graph::depthFirstSearch(char start, char end) {  
    std::vector<char> path;  
    std::unordered_map<char, char> parent;  
    std::stack<char> stack;  
  
    stack.push(start);  
    parent[start] = '\0';  
  
    while(!stack.empty()) {  
        char current = stack.top();  
        stack.pop();  
  
        if(current == end) {  
            // Build the path from end to start using the parent map  
            char node = end;  
            while(node != '\0') {  
                path.insert(path.begin(), node);  
                node = parent[node];  
            }  
            break;  
        }  
  
        for(char neighbor: adjList[current]) {  
            if(parent.find(neighbor) == parent.end()) {  
                parent[neighbor] = current;  
                stack.push(neighbor);  
            }  
        }  
    }  
  
    return path;  
}  
  
int main() {  
    Graph graph;  
    graph.addEdge('A', 'B');  
    graph.addEdge('A', 'D');  
    graph.addEdge('B', 'C');  
    graph.addEdge('C', 'E');  
    graph.addEdge('D', 'B');  
  
    char start = 'A';  
    char end = 'E';  
  
    std::vector<char> path = graph.depthFirstSearch(start, end);  
  
    std::cout << "Path from " << start << " to " << end << ": ";  
    for(char node: path) {  
        std::cout << node << ";  
    }  
    std::cout << std::endl;  
  
    return 0;  
}  

A cikin wannan misali, muna amfani da algorithm na DFS don nemo hanya daga vertex A zuwa vertex E a cikin jadawali. Sakamakon zai kasance jeri na madaidaicin kafa hanya daga A zuwa E.