Prim's Algorithm | Minimum Spanning Tree | Graph Data Structure | Java C++ Code | DSAOne #81

แชร์
ฝัง
  • เผยแพร่เมื่อ 22 ธ.ค. 2024

ความคิดเห็น • 68

  • @sathyamantri8426
    @sathyamantri8426 ปีที่แล้ว +2

    you are the only person i find on youtube who explains concepts very clearly

  • @AnshuKumar-qq6fr
    @AnshuKumar-qq6fr 2 ปีที่แล้ว +10

    i usually code in C++, but if i have any doubt i see your videos because of your great explaination of dry run

  • @StopLooking_Here
    @StopLooking_Here หลายเดือนก่อน

    Great Explanation. Sorry I don't have time to write a better review, my exams are tomorrow 😅, but seriously, this guy is an amazing teacher. Keep it up Anuj!

  • @spandanamittanosala3863
    @spandanamittanosala3863 2 ปีที่แล้ว +5

    Well explained...also plz try to provide the code in the description...would be more helpful

  • @krishnajain872
    @krishnajain872 2 ปีที่แล้ว +10

    Bhaiii aag lgaa diii i will also crack Amazon interview one day ❤️❤️🎉

  • @Sky-gk4qq
    @Sky-gk4qq 2 ปีที่แล้ว +2

    OP tha bhai kya samjhaye🤩

  • @arunyadav7467
    @arunyadav7467 2 ปีที่แล้ว +8

    Bhai jaldi jaldi video upload Kiya Karo or iske badd compitative coding ka course nikalo please

  • @madhavdua8588
    @madhavdua8588 2 ปีที่แล้ว +1

    Really Helpful🙏

  • @pratikmhatre4815
    @pratikmhatre4815 ปีที่แล้ว

    Very helpful and simple to understand

  • @theshrfactz5367
    @theshrfactz5367 2 ปีที่แล้ว +2

    Bhaiya bahut best video hai made more videos

  • @monikaraut5266
    @monikaraut5266 2 ปีที่แล้ว +4

    great explanation👍✌️✌️
    It would be really helpful if you could share the code for using Priority queue and hashmap together😊

  • @PiyushKumar-uw1gk
    @PiyushKumar-uw1gk ปีที่แล้ว

    Extremally Helpful !

  • @moazamdev
    @moazamdev ปีที่แล้ว +1

    All other youtubers rush up in the explanation process, but anuj bhayia make it easy

  • @dhruvkapoor6924
    @dhruvkapoor6924 19 วันที่ผ่านมา

    Anuj Bhai mujhe sirf adha samajh mein aaya

  • @saikeerthanchiluka4027
    @saikeerthanchiluka4027 ปีที่แล้ว

    Super bhaiyya

  • @meetsoni1938
    @meetsoni1938 2 ปีที่แล้ว

    Thank you bhaiya 🔥🔥💯💯

  • @aryandwivedi5115
    @aryandwivedi5115 2 ปีที่แล้ว +1

    really helpful

  • @Ankit.yt_885
    @Ankit.yt_885 2 ปีที่แล้ว

    Well explained!

  • @dhanrajlouvanshi7872
    @dhanrajlouvanshi7872 2 ปีที่แล้ว

    Thanks bhaiya

  • @mitul703
    @mitul703 2 ปีที่แล้ว

    Nice Bhaiya

  • @yahoo-kids
    @yahoo-kids 2 ปีที่แล้ว

    yes it was helpfulll

  • @AmanTheMystery
    @AmanTheMystery 2 ปีที่แล้ว

    mst explanation

  • @pj_wala
    @pj_wala 2 ปีที่แล้ว +2

    How many more upcoming videos are in this series?

  • @ganapathinaik6899
    @ganapathinaik6899 ปีที่แล้ว

    Can any one please share the practice link for this problem(which has 3-D ArrayList as adjacency matrix as parameter)

  • @anusharajendra389
    @anusharajendra389 ปีที่แล้ว

    Absolutely

  • @gauravbanerjee2898
    @gauravbanerjee2898 ปีที่แล้ว

    C++ code for the same approach
    int spanningTree(int V, vector adj[])
    {
    // we are creating the minheap such that the first element is the code and the second element is the edge
    vector vis(V,0);
    int ans = 0;
    priority_queue pq;
    pq.push({0,0}); // push the initial node and weight
    while(!pq.empty()){
    pair temp = pq.top(); // get the next edge with min cost
    int w = temp.first;
    int u = temp.second;
    pq.pop();
    if(!vis[u]){
    ans += w;
    vis[u] = 1;
    for(auto it:adj[u]){
    int v = it[0];
    int cost = it[1];
    if(!vis[v]){
    pq.push({cost,v});
    }
    }
    }
    }
    return ans;
    }
    };

  • @gauravpoharkar9797
    @gauravpoharkar9797 2 ปีที่แล้ว +3

    Wil you do any spring boot series in future please

    • @AnujBhaiya
      @AnujBhaiya  2 ปีที่แล้ว +6

      Hi Gaurav, I'll be doing a Web Development Series next. Will try to make Spring boot series in the future though. Stay tuned

    • @gauravpoharkar9797
      @gauravpoharkar9797 2 ปีที่แล้ว

      @@AnujBhaiya thanks bhaiya

    • @onais__
      @onais__ 2 ปีที่แล้ว

      @@AnujBhaiya Hello Bhaiya really curious Web dev kabse start krne ka soche hai

    • @simranbandhu9926
      @simranbandhu9926 2 ปีที่แล้ว

      @@AnujBhaiya bhaiya, please provide this code in c++ as well

  • @kumargaurav5386
    @kumargaurav5386 ปีที่แล้ว

    what is the font style of the code ??

  • @praveens7698
    @praveens7698 2 ปีที่แล้ว

    Helpful

  • @David-mk8kh
    @David-mk8kh 2 ปีที่แล้ว +2

    For me the quality of videos is decreasing as I am reaching the end. The code writing and explaining becoming faster. The starting videos of this playlist are sooo good. Now coding part becoming clumsy and errors in some codes also

  • @mouneshazonde6359
    @mouneshazonde6359 2 ปีที่แล้ว +2

    iska source code kaha milega

  • @dhanrajlouvanshi7872
    @dhanrajlouvanshi7872 2 ปีที่แล้ว +2

    What is that.wt concept

  • @ShachiSinghal-fi6uh
    @ShachiSinghal-fi6uh 9 หลายเดือนก่อน

    Sir, pls give code links as well

  • @virzu_jagarwal
    @virzu_jagarwal 2 ปีที่แล้ว

    Sir me b.a. ka student hu to kya me coding field me career bna skta hu skills ke bass par

  • @aishwaryshukla8880
    @aishwaryshukla8880 ปีที่แล้ว

    Bhaiya gfg version of this question is updated. Now they are taking adjacency matrix as input and they want O(E log V) time complexity. Also, their adjacency matrix is completely different to what you taught. Please do this question again bhaiya, it'll help us a lot!
    If anyone is able to solve 'Minimum spanning tree' in gfg (current version) using Prim's algorithm, Please help me!

  • @tanvigupta6507
    @tanvigupta6507 2 ปีที่แล้ว

    can you add with python?

  • @simranbandhu9926
    @simranbandhu9926 2 ปีที่แล้ว

    bhaiya, please provide this code in c++ as well

  • @dhruvkapoor6924
    @dhruvkapoor6924 19 วันที่ผ่านมา

    But it is helpful

  • @gyanapriyapradhan6289
    @gyanapriyapradhan6289 2 ปีที่แล้ว

    Directed graph mein karte

  • @FISCALPULSE
    @FISCALPULSE 2 ปีที่แล้ว +2

    bhaiya i haven't learned maths in 11 th and 12th can I still become a coder

  • @Om-fp1sv
    @Om-fp1sv ปีที่แล้ว

    sir i would really helpful if u provide code

  • @ABCD-gn5uw
    @ABCD-gn5uw 2 ปีที่แล้ว

    noice

  • @shubhamjaiswal7645
    @shubhamjaiswal7645 7 หลายเดือนก่อน

    everything was going great until you started coding in JAVA .....was expecting C++ solution

  • @RifahTasnimTandra
    @RifahTasnimTandra ปีที่แล้ว +4

    stop talking so fast

    • @sujalbhartiya
      @sujalbhartiya ปีที่แล้ว +1

      Playback speed low kar lo

    • @behappy5705
      @behappy5705 9 หลายเดือนก่อน

      M to jb bhi 1.5× m dhekra 😂

    • @ArtisticGod
      @ArtisticGod 9 หลายเดือนก่อน

      Better u stop watching

    • @rifahtasnimtandra6771
      @rifahtasnimtandra6771 9 หลายเดือนก่อน

      ​@@ArtisticGodbetter u stop commenting

    • @ArtisticGod
      @ArtisticGod 9 หลายเดือนก่อน

      @@rifahtasnimtandra6771 better u stop reacting..

  • @ishantbhatia3194
    @ishantbhatia3194 ปีที่แล้ว

    yar thoda or improve kro smjhane mae, if you know it does not mean everyone knows

  • @parrotsparadise3914
    @parrotsparadise3914 ปีที่แล้ว +3

    Java complete solution
    import java.util.*;
    public class primsAlgo
    {
    // gives minimum spanning tree of a graph
    // min spanning tree is connecting all vertices with min edge weight
    // follows greedy algo
    static class Pair implements Comparable
    {
    int v, wt;
    public Pair(int v, int wt)
    {
    this.v = v;
    this.wt = wt;
    }
    public int compareTo(Pair that)
    {
    return this.wt-that.wt;
    }
    }
    // undirected graph so assigning source, destination and weight
    static void addAdjEdge(ArrayList a, int s, int d, int wt)
    {
    ArrayList neigh = a.get(s);
    ArrayList list = new ArrayList();
    list.add(0,d);
    list.add(1,wt);
    neigh.add(list);
    addAdjEdgeR(a, d, s, wt);
    }
    static void addAdjEdgeR(ArrayList a, int s, int d, int wt)
    {
    ArrayList neigh = a.get(s);
    ArrayList list = new ArrayList();
    list.add(0,d);
    list.add(1,wt);
    neigh.add(list);
    }
    static int primsAlg(ArrayList adj, int v, int src)
    {
    PriorityQueue pq = new PriorityQueue();
    boolean visited[] = new boolean[v];
    pq.add(new Pair(src,0));
    int ans = 0;
    while(!pq.isEmpty())
    {
    Pair cur = pq.remove();
    int u = cur.v;
    if(visited[u])
    continue;
    ans+= cur.wt;
    visited[u] = true;
    ArrayList neigh = adj.get(u);
    for(ArrayList list: neigh)
    {
    int vertex = list.get(0);
    int wt = list.get(1);
    if(!visited[vertex])
    {
    pq.add(new Pair(vertex, wt));
    }
    }
    }
    return ans;
    }
    public static void main(String[] args)
    {
    int v = 5, src = 1;
    ArrayList adj= new ArrayList();
    for (int i = 0; i < v; i++)
    {
    adj.add(new ArrayList());
    }

    addAdjEdge(adj,0,1,2);
    addAdjEdge(adj,0,3,7);
    addAdjEdge(adj,0,4,6);
    addAdjEdge(adj,1,2,1);
    addAdjEdge(adj,1,4,4);
    addAdjEdge(adj,2,3,3);
    addAdjEdge(adj,2,4,2);
    addAdjEdge(adj,3,4,5);
    System.out.println(primsAlg(adj, v, src));
    }
    }

  • @rohitborse2710
    @rohitborse2710 8 หลายเดือนก่อน

    C++
    class Solution
    {
    public:
    //Function to find sum of weights of edges of the Minimum Spanning Tree.
    struct CustomComparator {
    bool operator()(const pair& p1, const pair& p2) const {
    return p1.second > p2.second; // Change > to < for ascending order
    }
    };

    int spanningTree(int V, vector adj[]) {
    priority_queue pq;
    vector visited(V, false);
    pq.push({0, 0}); // Start with vertex 0 and weight 0
    int ans = 0;
    while (!pq.empty()) {
    pair curr = pq.top();
    pq.pop();
    int vertex = curr.first;
    int wt = curr.second;
    if (visited[vertex]) continue;

    visited[vertex] = true;
    ans += wt;
    for (auto& edge : adj[vertex]) {
    int nextVertex = edge[0];
    int weight = edge[1];
    if (!visited[nextVertex]) {
    pq.push({nextVertex, weight}); // Pushing vertex and its weight
    }
    }
    }
    return ans;

    }
    };