search
HomeBackend DevelopmentC++Implementing BFS using vectors and queues, following the implementation of the CLRS algorithm in a C program

Implementing BFS using vectors and queues, following the implementation of the CLRS algorithm in a C program

In the CLRS book, the BFS algorithm is described using vectors and queues. We have to use C STL to implement this algorithm. First let's look at the algorithm.

Algorithm

BFS(G, s) −

begin
   for each vertex u in G.V - {s}, do
      u.color := white
      u.d := infinity
      u.p := NIL
   done
   s.color := green
   s.d := 0
   s.p := NIL
   Q := NULL
   insert s into Q
   while Q is not null, do
      u = delete from Q
      for each v in adjacent to u, do
         if v.color = white
            v.color := green
            v.d := u.d + 1
            v.p := u
            insert v into Q
         end if
      done
      u.color = dark_green
   done
end

Example

’s Chinese translation is:

Example

#include<iostream>
#include<vector>
#include<queue>
using namespace std;
vector<string> colour;
vector<int> dist;
vector<int> par;
void addEdge(vector <int> g[], int u, int v) { //add edge to form the graph
   g[u].push_back(v);
   g[v].push_back(u);
}
void BFS(vector <int> g[], int s) {
   queue<int> q;
   q.push(s); //insert source
   dist[s] = 0;
   colour[s] = "gray";
   while (!q.empty()) {
      int u = q.front(); //top element from queue, then delete it
      q.pop();
      cout << u << " ";
      for (auto i = g[u].begin(); i != g[u].end(); i++) {
         if (colour[*i] == "white") { //white is unvisited node
            colour[*i] = "gray"; //gray is visited but not completed
            dist[*i] = dist[u] + 1;
            par[*i] = u;
            q.push(*i);
         }
      }
      colour[u] = "black"; //black is completed node
   }
}
void BFSAlgo(vector <int> g[], int n) {
   colour.assign(n, "white"); //put as unvisited
   dist.assign(n, 0);
   par.assign(n, -1);
   for (int i = 0; i < n; i++)
      if (colour[i] == "white")
   BFS(g, i);
}
int main() {
   int n = 7;
   vector <int> g[n];
   addEdge(g, 0, 1);
   addEdge(g, 0, 2);
   addEdge(g, 1, 3);
   addEdge(g, 1, 4);
   addEdge(g, 2, 5);
   addEdge(g, 2, 6);
   BFSAlgo(g, n);
}

Output

0 1 2 3 4 5 6

The above is the detailed content of Implementing BFS using vectors and queues, following the implementation of the CLRS algorithm in a C program. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:tutorialspoint. If there is any infringement, please contact admin@php.cn delete
Laravel开发注意事项:合理使用缓存与队列Laravel开发注意事项:合理使用缓存与队列Nov 22, 2023 am 11:46 AM

Laravel是一款非常流行的PHP开发框架,它提供了丰富的功能和便捷的开发方式,能够帮助开发人员快速构建稳定可靠的Web应用程序。在Laravel开发过程中,合理使用缓存与队列是十分重要的,本文将介绍一些注意事项以帮助开发人员更好地利用缓存与队列。一、合理使用缓存缓存的定义与作用缓存是一种将经常使用的数据临时存储在内存中的技术,能够极大地提高系统的响应速度

队列的死信队列和延迟队列在PHP与MySQL中的应用场景队列的死信队列和延迟队列在PHP与MySQL中的应用场景Oct 15, 2023 am 11:46 AM

队列的死信队列和延迟队列在PHP与MySQL中的应用场景引言随着互联网应用变得越来越复杂,处理大量消息和任务的需求日益增长。队列作为一种解决方案,能够有效地实现任务的异步处理,提高系统的可伸缩性和稳定性。在队列的应用中,常见的两个概念是死信队列和延迟队列。本文将介绍这两个概念在PHP与MySQL中的应用场景,并提供具体的代码示例。死信队列的应用场景死信队列是

使用向量和队列实现BFS,按照CLRS算法在C程序中的实现使用向量和队列实现BFS,按照CLRS算法在C程序中的实现Sep 06, 2023 pm 04:37 PM

在CLRS书中,BFS算法使用向量和队列来描述。我们必须使用C++STL来实现该算法。首先让我们看一下算法。算法BFS(G,s)&minus;begin&nbsp;&nbsp;foreachvertexuinG.V-{s},do&nbsp;&nbsp;&nbsp;u.color:=white&nbsp;&nbsp;&nbsp;u.d:=infinity&nbsp;&nbsp;&nbsp;u.p:=NI

队列在PHP与MySQL中的消息过滤和消息路由的实现方法队列在PHP与MySQL中的消息过滤和消息路由的实现方法Oct 15, 2023 pm 04:55 PM

队列在PHP与MySQL中的消息过滤和消息路由的实现方法随着互联网的快速发展,消息队列(MessageQueue)作为一种重要的通信机制,在Web开发中扮演着至关重要的角色。消息队列可以用于实现解耦、削峰填谷、异步处理等功能。本文将介绍在PHP与MySQL中如何实现消息过滤和消息路由,并提供具体的代码示例。消息队列消息队列是一种典型的"生产者-消费者"模型

队列的消息持久化和消息去重在PHP与MySQL中的应用场景队列的消息持久化和消息去重在PHP与MySQL中的应用场景Oct 15, 2023 pm 01:42 PM

队列的消息持久化和消息去重在PHP与MySQL中的应用场景队列是一种常见的数据结构,在软件开发中被广泛应用于异步消息处理、任务调度、日志收集等场景。其中,消息持久化和消息去重是队列的两个重要特性,能够保证消息的可靠性和数据的一致性。在PHP和MySQL中,队列的应用可以通过Redis作为消息中间件,用MySQL来存储和管理队列的元数据,具体示例如下所示。首先

C++中的栈和队列C++中的栈和队列Aug 22, 2023 am 11:00 AM

介绍C++中的栈和队列栈和队列是C++中常用的数据结构,它们在程序中有着广泛的应用。本文将对栈和队列的概念、使用方法和应用场景进行详细介绍。一、栈的概念栈(Stack)是一种线性数据结构,它具有"先进后出"的特点。在栈中,越先进栈的数据,越靠近栈底;越后进栈的数据,越靠近栈顶。栈的主要操作有入栈(push)和出栈(pop)。入栈就是往栈里添加数据,而出栈

我们如何在Java中使用队列实现栈?我们如何在Java中使用队列实现栈?Aug 25, 2023 pm 05:05 PM

一个栈(Stack)是Vector类的子类,它代表了一个后进先出(LIFO)的对象堆栈。最后一个添加到堆栈顶部的元素(In)可以是从堆栈中首先移除的元素(Out)。队列(Queue)类扩展了Collection接口,并支持使用先进先出(FIFO)的插入和删除操作。我们也可以在下面的程序中使用队列来实现栈。示例importjava.util.*;publicclassStackFromQueueTest{&nbsp;&nbsp;Queuequeue=newLinkedList();

队列在PHP与MySQL中的消息堆积和拥塞控制的处理方法队列在PHP与MySQL中的消息堆积和拥塞控制的处理方法Oct 15, 2023 am 09:24 AM

队列在PHP与MySQL中的消息堆积和拥塞控制的处理方法随着互联网的迅猛发展,各种网站和应用程序的用户数量不断增加,对服务器的负载能力提出了更高的要求。在这种背景下,消息队列成为了一种常用的解决方案,用来解决高并发访问下的消息堆积和拥塞问题。本文将介绍队列在PHP与MySQL中的消息堆积和拥塞控制的处理方法,并给出具体的代码示例。在PHP中,我们可以使用Re

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software