nrg_aceu's blog

By nrg_aceu, history, 5 weeks ago, In English

i read cp algorithm and understand bfs theory.when it comes to implementation i cant. see this code https://pastebin.com/5RtDSMMW this is literally copying yet its still wrong idk whats wrong.pls help

 
 
 
 
  • Vote: I like it
  • +1
  • Vote: I do not like it

»
5 weeks ago, # |
  Vote: I like it -18 Vote: I do not like it

pls help.im tired of asking someone in discord since theyll just ignore me.i really want to learn bfs and i cant.

  • »
    »
    5 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it
    • »
      »
      »
      5 weeks ago, # ^ |
        Vote: I like it -25 Vote: I do not like it

      that book is the worst.it left me confused at the implementation like what am i suppose to write in int main? whenever i tried to see the implementation example it just wont print anything.

      • »
        »
        »
        »
        5 weeks ago, # ^ |
        Rev. 2   Vote: I like it +1 Vote: I do not like it

        If you can't implement bfs by your own in main function despite having the bfs structure ready, then you have lack of understanding of bfs, that's not the fault of the book

        • »
          »
          »
          »
          »
          5 weeks ago, # ^ |
            Vote: I like it -23 Vote: I do not like it

          what i dont understand is actually the variations.some use vector<vectir> for storing adj list for bfs and some use vector pair.i was confused since in the article i read it uses vector vector and what i elarn from my teacher is vector pair.this also applies to that book.the standards are different

          • »
            »
            »
            »
            »
            »
            5 weeks ago, # ^ |
              Vote: I like it 0 Vote: I do not like it

            I don't know how the vector of pairs implementation works, but vector of vectors is used because for a vector with index $$$i$$$, all the values in that vector are neighbours of $$$i$$$. For example if 1,2,3 are stored in vector adj[0], then nodes 1,2,3 are neighbours of node 0. This way after processing node 0, you can push it's neighbours (1,2,3) in the queue for processing.

          • »
            »
            »
            »
            »
            »
            5 weeks ago, # ^ |
              Vote: I like it 0 Vote: I do not like it

            then don't use it.

            to be honest, you don't need bfs to get to pupil

      • »
        »
        »
        »
        5 weeks ago, # ^ |
          Vote: I like it -10 Vote: I do not like it

        You can use GreeksForGeeks for better understanding.

        • »
          »
          »
          »
          »
          5 weeks ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          Yes nrg_aceu go there

        • »
          »
          »
          »
          »
          5 weeks ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          it uses OOP.i dont udnerstand OOP.

          • »
            »
            »
            »
            »
            »
            5 weeks ago, # ^ |
            Rev. 2   Vote: I like it 0 Vote: I do not like it

            // A Quick implementation of BFS using // vectors and queue

            include <bits/stdc++.h>

            define pb push_back

            using namespace std;

            vector v; vector<vector > g;

            void edge(int a, int b) { g[a].pb(b);

            // for undirected graph add this line
            // g[b].pb(a);
            

            }

            void bfs(int u) { queue q;

            q.push(u);
            v[u] = true;
            
            while (!q.empty()) {
            
                int f = q.front();
                q.pop();
            
                cout << f << " ";
            
                // Enqueue all adjacent of f and mark them visited
                for (auto i = g[f].begin(); i != g[f].end(); i++) {
                   if (!v[*i]) {
                     q.push(*i);
                     v[*i] = true;
                   }
                }
            }
            

            }

            // Driver code int main() { int n, e; cin >> n >> e;

            v.assign(n, false);
            g.assign(n, vector<int>());
            
            int a, b;
            for (int i = 0; i < e; i++) {
                cin >> a >> b;
                edge(a, b);
            }
            
            for (int i = 0; i < n; i++) {
                if (!v[i])
                   bfs(i);
            }
            
            return 0;
            

            }

            Where the fuck do you find oop here?

  • »
    »
    5 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Search CodeNCode graph playlist on YT

  • »
    »
    5 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    refer codencode utube channel and study graphs from there. If u find it useful then thank me later

»
5 weeks ago, # |
  Vote: I like it -17 Vote: I do not like it

I would suggest you to watch the amazing graph series by striver_79 He has explained each and every concept very well. Here is the link to the playlist: Striver's Graph Series

»
5 weeks ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

do you even understand how bfs works?

»
5 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Go through the graph theory playlist by William Fiset. Here's the link it has neat visualization and implementation as well. https://www.youtube.com/playlist?list=PLDV1Zeh2NRsDGO4--qE8yH72HFL1Km93P