G-32. Dijkstra's Algorithm - Using Priority Queue - C++ and Java - Part 1

G-32. Dijkstra's Algorithm - Using Priority Queue - C++ and Java - Part 1

take U forward

1 год назад

289,166 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

take U forward
take U forward - 28.09.2022 09:23

Let's continue the habit of commenting “understood” if you got the entire video. Please give it a like too, you don't 😞

Do follow me on Instagram: striver_79

Ответить
Aditi Donode
Aditi Donode - 02.10.2023 09:49

understood sir :)

Ответить
Rushi Desai
Rushi Desai - 30.09.2023 19:58

I am guessing Video 28 was Dijkstra with Queue?

Ответить
Sayantani Guha
Sayantani Guha - 30.09.2023 07:59

y no visited array here btw?

Ответить
Your Friendly Guide
Your Friendly Guide - 19.09.2023 18:18

Nice Video

Ответить
DHANASHREE GODASE
DHANASHREE GODASE - 19.09.2023 13:39

You are a Masterpeice!!

Ответить
Rahil Ghanchi
Rahil Ghanchi - 17.09.2023 10:51

shortest path to all the nodes using dikjstra and minHeap.(JAVA)


import java.util.*;

class point {
int node;
int weight;

public point(int node, int weight) {
this.node = node;
this.weight = weight;
}

public int hashCode() {
return Objects.hash(this.node, this.weight);
}

public boolean equals(Object obj) {
point p = (point) obj;
if (this.node == p.node) {
return true;
}
return false;
}
}

public class Dijastra {

public static void dijastra(HashMap<Integer, List<point>> adjList, int[] dist, PriorityQueue<point> minHeap) {
point p = minHeap.poll();
if (p == null) {
return;
}
while (p.weight > dist[p.node]) {
p = minHeap.poll();
if (p == null) {
return;
}
}
int node = p.node;
dist[node] = p.weight;

adjList.get(node).forEach((neighbour) -> {
neighbour.weight += dist[node];
minHeap.add(neighbour);
});
dijastra(adjList, dist, minHeap);
}

public static void main(String[] args) {
PriorityQueue<point> minHeap = new PriorityQueue<>((a, b) -> {
return Integer.compare(a.weight, b.weight);
});
minHeap.add(new point(0, 0));
HashMap<Integer, List<point>> adjList = new HashMap<>();
int[] dist = new int[] { Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE,
Integer.MAX_VALUE, Integer.MAX_VALUE };
adjList.put(0, List.of(new point(1, 4), new point(2, 4)));

adjList.put(1, List.of(new point(0, 4), new point(2, 2)));

adjList.put(2, List.of(new point(0, 4), new point(3, 3), new point(1, 2), new point(5, 6), new point(3, 3),
new point(4, 1)));

adjList.put(3, List.of(new point(2, 3), new point(5, 2)));
adjList.put(4, List.of(new point(2, 1), new point(5, 3)));
adjList.put(5, List.of(new point(3, 2), new point(2, 6), new point(4, 3)));
dijastra(adjList, dist, minHeap);
for (int i : dist) {
System.out.println(i);
}
}
}

Ответить
Urkrishnan
Urkrishnan - 11.09.2023 09:37

One suggestion in java code.. Instead of doing 2 operations I.E. peek() and remove() at line number 81,82& 83, we can do one operation pop().. Just an FYI for other java codes... Happy coding.. 👍

Ответить
Learner Forever
Learner Forever - 10.09.2023 08:10

Understood

Ответить
Shubham Soni
Shubham Soni - 08.09.2023 14:05

UNDERSTOOD.

Ответить
Moch117
Moch117 - 06.09.2023 20:42

Thank you!

I implemented it on my own and it worked without storing the distance on the priority queue. I only stored the nodes themselves. I also used a Pair class to store (dest,weight)

Ответить
Google it
Google it - 06.09.2023 20:28

Revision I: Easy Peasy

Sep'6, 2023 10:58 pm

Ответить
Parth Dharmale
Parth Dharmale - 28.08.2023 16:03

i wanna know which notes app do you use

Ответить
Chirag Sawarn
Chirag Sawarn - 24.08.2023 23:47

In this implementation it was extremely difficult to figure out the complexity.
When we use a visited array, it become clear that a node may be queued multiple times but its neighbors will only be processed once.
Because the first occurrence of duplicate node in the queue will have the lightest path and mark the node as visited. Eliminating the need for redundancies to be processed.

Ответить
Namrata M
Namrata M - 10.08.2023 17:22

Why dont we consider visited node here?

Ответить
Rohit kumar Pilania
Rohit kumar Pilania - 06.08.2023 11:08

New beard looking good

Ответить
Sachin Rajput
Sachin Rajput - 04.08.2023 22:38

Habibi ye bhi kamal video banti ... bahut accha bahut accha

Ответить
Sara
Sara - 04.08.2023 06:26

Understoood <3

Ответить
Pihu S
Pihu S - 31.07.2023 20:29

us

Ответить
Kushagra Singh
Kushagra Singh - 31.07.2023 05:34

understood

Ответить
Ranjith R
Ranjith R - 28.07.2023 17:17

can anyone say what is the need for this algorithm because we already found the shortest path in previous lectures of striver??????

Ответить
Ayushi Kushwaha
Ayushi Kushwaha - 24.07.2023 09:44

understood

Ответить
Mridul Jain
Mridul Jain - 21.07.2023 19:14

understood

Ответить
Ashish Bhardwaj
Ashish Bhardwaj - 20.07.2023 15:25

Understood....

Ответить
Mohan Krishna
Mohan Krishna - 18.07.2023 12:51

Understood !

Ответить
Gunj Joshi
Gunj Joshi - 15.07.2023 20:30

Thank You

Ответить
amit patel
amit patel - 14.07.2023 11:24

great work 👏

Ответить
Aryan Kumar
Aryan Kumar - 13.07.2023 23:02

Understood

Ответить
Rajat Vatwani
Rajat Vatwani - 10.07.2023 20:26

We have not handled the case of Unreachable nodes in this question

Ответить
Akash Sahu
Akash Sahu - 06.07.2023 16:06

yes

Ответить
iota
iota - 02.07.2023 22:20

is it applicable for directed graph?

Ответить
Arsh Ansari
Arsh Ansari - 29.06.2023 00:11

Apart from weighted edges what is the difference between this and approach in l28

Ответить
Aman Sharma
Aman Sharma - 25.06.2023 22:16

understood

Ответить
BHARATH KUMAR
BHARATH KUMAR - 19.06.2023 17:42

understood ❤

Ответить
Nikhil Krishna
Nikhil Krishna - 15.06.2023 23:23

understood!!

Ответить
Google it
Google it - 14.06.2023 21:11

Understood !!

Ответить
Anupam Roy
Anupam Roy - 14.06.2023 16:25

by dijkstra can directed graph with negative edge be solved .? in this video undirected graph is used. please anyone answer

Ответить
Bhoomika Devappa
Bhoomika Devappa - 13.06.2023 19:58

Understood 😊

Ответить
LAKESH KUMAR
LAKESH KUMAR - 12.06.2023 14:26

understood

Ответить
surya kiran
surya kiran - 08.06.2023 12:47

Understood❤

Ответить
Coding Alley
Coding Alley - 07.06.2023 17:23

🐐

Ответить
Hrushi Borhade
Hrushi Borhade - 06.06.2023 10:20

understood striver!!!

Ответить
varun aggarwal
varun aggarwal - 02.06.2023 19:41

1 silly question, if you have observed striver has put distance first in priority_queue due to ease of sorting as the values are sorted based on first value, but I put distance second, and wrote a comparison function from there I observed that if i return bool operator()(pii a, pii b) {
return a.second < b.second;
} its giving me TLE, isn't the first value should be smaller that second when implementing min_heap, if i return opposite, its running fine, anyone has idea about it?

Ответить
20-5809 V Laharika
20-5809 V Laharika - 02.06.2023 14:56

Understood 👍

Ответить
Adarsh Kumar Rao
Adarsh Kumar Rao - 31.05.2023 21:19

UNDERSTOOD

Ответить
ARPAN GUPTA
ARPAN GUPTA - 31.05.2023 18:12

Ответить
Dinusha Chathuranga
Dinusha Chathuranga - 31.05.2023 14:25

excellent explanation. Lucky to find your channel.❤

Ответить
Himani Upadhyay
Himani Upadhyay - 31.05.2023 03:24

us

Ответить