बीएफएस ग्राफ एल्गोरिथ्म (सी, सी ++, जावा और पायथन में कोड के साथ)

इस ट्यूटोरियल में, आप पहले खोज एल्गोरिथ्म के बारे में जानेंगे। इसके अलावा, आप सी, सी ++, जावा और पायथन में बीएफएस एल्गोरिथ्म के काम करने के उदाहरण पाएंगे।

ट्रैवर्सल का अर्थ है ग्राफ के सभी नोड्स पर जाना। चौड़ाई प्रथम ट्रैवर्सल या चौड़ाई प्रथम खोज ग्राफ़ या ट्री डेटा संरचना के सभी कोने खोजने के लिए एक पुनरावर्ती एल्गोरिदम है।

बीएफएस एल्गोरिथ्म

एक मानक बीएफएस कार्यान्वयन ग्राफ़ के प्रत्येक शीर्ष को दो श्रेणियों में से एक में रखता है:

  1. का दौरा किया
  2. देखा नहीं गया

एल्गोरिथ्म का उद्देश्य चक्रों से बचने के दौरान दौरा किए गए प्रत्येक शीर्ष को चिह्नित करना है।

यह एल्गोरिथ्म इस प्रकार काम करता है:

  1. एक कतार के पीछे ग्राफ़ के किसी एक कोने को लगाकर शुरू करें।
  2. कतार के सामने की वस्तु को ले जाएं और इसे विज़िट की गई सूची में जोड़ें।
  3. उस शीर्ष के आसन्न नोड्स की एक सूची बनाएं। उन लोगों को जोड़ें जो कतार में पीछे की ओर जाने वाली सूची में नहीं हैं।
  4. जब तक कतार खाली न हो जाए, तब तक चरण 2 और 3 दोहराते रहें।

ग्राफ़ में दो अलग-अलग डिस्कनेक्ट किए गए भाग हो सकते हैं ताकि यह सुनिश्चित हो सके कि हम प्रत्येक शीर्ष को कवर करते हैं, हम हर नोड पर BFS एल्गोरिथ्म भी चला सकते हैं

BFS उदाहरण

आइए देखें कि चौड़ाई प्रथम खोज एल्गोरिदम एक उदाहरण के साथ कैसे काम करता है। हम 5 कोने के साथ एक अप्रत्यक्ष ग्राफ का उपयोग करते हैं।

5 कोने के साथ अप्रत्यक्ष ग्राफ

हम वर्टेक्स 0 से शुरू करते हैं, बीएफएस एल्गोरिथ्म इसे विज़ि‍टेड लिस्ट में डालकर और इसके आस-पास के सभी वर्टिकल को स्टैक में रखकर शुरू होता है।

वर्टेक्स शुरू करें और कतार में इसके आसन्न कोने जोड़ें

इसके बाद, हम तत्व को कतार के सामने अर्थात 1 पर जाते हैं और उसके निकटवर्ती नोड्स पर जाते हैं। चूँकि 0 का दौरा हो चुका है, इसलिए हम 2 के बजाय जाते हैं।

प्रारंभ नोड 0 के पहले पड़ोसी पर जाएं, जो 1 है

वर्टेक्स 2 में 4 में एक गैर-समीपवर्ती वर्टेक्स है, इसलिए हम इसे कतार के पीछे जोड़ते हैं और 3 पर जाते हैं, जो कतार के सामने स्थित है।

यात्रा 2 जो पहले अपने पड़ोसियों को जोड़ने के लिए कतार में जोड़ा गया था 4 कतार में बनी हुई है

3 के केवल आसन्न नोड के बाद से केवल 4 कतार में रहता है अर्थात 0 का दौरा पहले से ही है। हम इसे देखते हैं।

स्टैक में अंतिम शेष वस्तु पर जाएं ताकि यह जांचा जा सके कि यह पड़ोसी के पास है या नहीं

चूंकि कतार खाली है, इसलिए हमने ग्राफ के चौड़ाई पहले त्रैमासिक को पूरा कर लिया है।

बीएफएस स्यूडोकोड

 क्यू क्यू वी वी का दौरा किया और क्यू में वी डाल दिया, जबकि क्यू गैर-रिक्त है क्यू मार्क के यू को हटा दें और यू के सभी (बिना परिकल्पित) पड़ोसियों को बताएं

पायथन, जावा और सी / सी ++ उदाहरण

एक उदाहरण के साथ चौड़ाई पहली खोज एल्गोरिथ्म का कोड नीचे दिखाया गया है। कोड को सरल बनाया गया है ताकि हम अन्य विवरणों के बजाय एल्गोरिथ्म पर ध्यान केंद्रित कर सकें।

पायथन जावा सी सी +
 # BFS algorithm in Python import collections # BFS algorithm def bfs(graph, root): visited, queue = set(), collections.deque((root)) visited.add(root) while queue: # Dequeue a vertex from queue vertex = queue.popleft() print(str(vertex) + " ", end="") # If not visited, mark it as visited, and # enqueue it for neighbour in graph(vertex): if neighbour not in visited: visited.add(neighbour) queue.append(neighbour) if __name__ == '__main__': graph = (0: (1, 2), 1: (2), 2: (3), 3: (1, 2)) print("Following is Breadth First Traversal: ") bfs(graph, 0) 
 // BFS algorithm in Java import java.util.*; public class Graph ( private int V; private LinkedList adj(); // Create a graph Graph(int v) ( V = v; adj = new LinkedList(v); for (int i = 0; i < v; ++i) adj(i) = new LinkedList(); ) // Add edges to the graph void addEdge(int v, int w) ( adj(v).add(w); ) // BFS algorithm void BFS(int s) ( boolean visited() = new boolean(V); LinkedList queue = new LinkedList(); visited(s) = true; queue.add(s); while (queue.size() != 0) ( s = queue.poll(); System.out.print(s + " "); Iterator i = adj(s).listIterator(); while (i.hasNext()) ( int n = i.next(); if (!visited(n)) ( visited(n) = true; queue.add(n); ) ) ) ) public static void main(String args()) ( Graph g = new Graph(4); g.addEdge(0, 1); g.addEdge(0, 2); g.addEdge(1, 2); g.addEdge(2, 0); g.addEdge(2, 3); g.addEdge(3, 3); System.out.println("Following is Breadth First Traversal " + "(starting from vertex 2)"); g.BFS(2); ) )
 // BFS algorithm in C #include #include #define SIZE 40 struct queue ( int items(SIZE); int front; int rear; ); struct queue* createQueue(); void enqueue(struct queue* q, int); int dequeue(struct queue* q); void display(struct queue* q); int isEmpty(struct queue* q); void printQueue(struct queue* q); struct node ( int vertex; struct node* next; ); struct node* createNode(int); struct Graph ( int numVertices; struct node** adjLists; int* visited; ); // BFS algorithm void bfs(struct Graph* graph, int startVertex) ( struct queue* q = createQueue(); graph->visited(startVertex) = 1; enqueue(q, startVertex); while (!isEmpty(q)) ( printQueue(q); int currentVertex = dequeue(q); printf("Visited %d", currentVertex); struct node* temp = graph->adjLists(currentVertex); while (temp) ( int adjVertex = temp->vertex; if (graph->visited(adjVertex) == 0) ( graph->visited(adjVertex) = 1; enqueue(q, adjVertex); ) temp = temp->next; ) ) ) // Creating a node struct node* createNode(int v) ( struct node* newNode = malloc(sizeof(struct node)); newNode->vertex = v; newNode->next = NULL; return newNode; ) // Creating a graph struct Graph* createGraph(int vertices) ( struct Graph* graph = malloc(sizeof(struct Graph)); graph->numVertices = vertices; graph->adjLists = malloc(vertices * sizeof(struct node*)); graph->visited = malloc(vertices * sizeof(int)); int i; for (i = 0; i adjLists(i) = NULL; graph->visited(i) = 0; ) return graph; ) // Add edge void addEdge(struct Graph* graph, int src, int dest) ( // Add edge from src to dest struct node* newNode = createNode(dest); newNode->next = graph->adjLists(src); graph->adjLists(src) = newNode; // Add edge from dest to src newNode = createNode(src); newNode->next = graph->adjLists(dest); graph->adjLists(dest) = newNode; ) // Create a queue struct queue* createQueue() ( struct queue* q = malloc(sizeof(struct queue)); q->front = -1; q->rear = -1; return q; ) // Check if the queue is empty int isEmpty(struct queue* q) ( if (q->rear == -1) return 1; else return 0; ) // Adding elements into queue void enqueue(struct queue* q, int value) ( if (q->rear == SIZE - 1) printf("Queue is Full!!"); else ( if (q->front == -1) q->front = 0; q->rear++; q->items(q->rear) = value; ) ) // Removing elements from queue int dequeue(struct queue* q) ( int item; if (isEmpty(q)) ( printf("Queue is empty"); item = -1; ) else ( item = q->items(q->front); q->front++; if (q->front> q->rear) ( printf("Resetting queue "); q->front = q->rear = -1; ) ) return item; ) // Print the queue void printQueue(struct queue* q) ( int i = q->front; if (isEmpty(q)) ( printf("Queue is empty"); ) else ( printf("Queue contains "); for (i = q->front; i rear + 1; i++) ( printf("%d ", q->items(i)); ) ) ) int main() ( struct Graph* graph = createGraph(6); addEdge(graph, 0, 1); addEdge(graph, 0, 2); addEdge(graph, 1, 2); addEdge(graph, 1, 4); addEdge(graph, 1, 3); addEdge(graph, 2, 4); addEdge(graph, 3, 4); bfs(graph, 0); return 0; )
 // BFS algorithm in C++ #include #include using namespace std; class Graph ( int numVertices; list* adjLists; bool* visited; public: Graph(int vertices); void addEdge(int src, int dest); void BFS(int startVertex); ); // Create a graph with given vertices, // and maintain an adjacency list Graph::Graph(int vertices) ( numVertices = vertices; adjLists = new list(vertices); ) // Add edges to the graph void Graph::addEdge(int src, int dest) ( adjLists(src).push_back(dest); adjLists(dest).push_back(src); ) // BFS algorithm void Graph::BFS(int startVertex) ( visited = new bool(numVertices); for (int i = 0; i < numVertices; i++) visited(i) = false; list queue; visited(startVertex) = true; queue.push_back(startVertex); list::iterator i; while (!queue.empty()) ( int currVertex = queue.front(); cout << "Visited " << currVertex << " "; queue.pop_front(); for (i = adjLists(currVertex).begin(); i != adjLists(currVertex).end(); ++i) ( int adjVertex = *i; if (!visited(adjVertex)) ( visited(adjVertex) = true; queue.push_back(adjVertex); ) ) ) ) int main() ( Graph g(4); g.addEdge(0, 1); g.addEdge(0, 2); g.addEdge(1, 2); g.addEdge(2, 0); g.addEdge(2, 3); g.addEdge(3, 3); g.BFS(2); return 0; )

बीएफएस एल्गोरिथ्म जटिलता

बीएफएस एल्गोरिथ्म की समय जटिलता का प्रतिनिधित्व किया जाता है O(V + E), जहां वी नोड्स की संख्या है और ई किनारों की संख्या है।

एल्गोरिथ्म की अंतरिक्ष जटिलता है O(V)

बीएफएस एल्गोरिथ्म अनुप्रयोग

  1. खोज सूचकांक द्वारा सूचकांक बनाने के लिए
  2. जीपीएस नेविगेशन के लिए
  3. पथ खोज एल्गोरिदम
  4. एक नेटवर्क में अधिकतम प्रवाह खोजने के लिए Ford-Fulkerson एल्गोरिथ्म में
  5. एक अप्रत्यक्ष ग्राफ में चक्र का पता लगाना
  6. न्यूनतम फैले हुए वृक्ष में

दिलचस्प लेख...