Shortest path in Undirected Graph | GFG POTD 21st Aug 2024 | JAVA | C++

  Рет қаралды 421

CodeCraft Academy

CodeCraft Academy

Күн бұрын

#gfgpotd #gfgpotdtoday #potd #gfgproblemoftheday #problemoftheday

Пікірлер: 3
@ajinkyajain2302
@ajinkyajain2302 28 күн бұрын
C++ Code : vector shortestPath(vector& edges, int N,int M, int src){ // code here vector adj(N); for (const auto& edge : edges) { adj[edge[0]].push_back(edge[1]); adj[edge[1]].push_back(edge[0]); } // Distance array vector dis(N, INT_MAX); dis[src] = 0; // BFS queue q; q.push(src); while (!q.empty()) { int cur = q.front(); q.pop(); for (int nbr : adj[cur]) { if (dis[cur] + 1 < dis[nbr]) { dis[nbr] = dis[cur] + 1; q.push(nbr); } } } // Replace unreachable nodes' distance with -1 for (int i = 0; i < N; ++i) { if (dis[i] == INT_MAX) { dis[i] = -1; } } return dis; }
@ajinkyajain2302
@ajinkyajain2302 28 күн бұрын
JAVA Code : public int[] shortestPath(int[][] edges,int n,int m ,int src) { // Code here ArrayList adj = new ArrayList(); for(int i = 0 ; i < n ; ++i){ adj.add(new ArrayList()); } for(int[] edge : edges){ adj.get(edge[0]).add(edge[1]); adj.get(edge[1]).add(edge[0]); } int[] dis = new int[n]; Arrays.fill(dis, Integer.MAX_VALUE); dis[src] = 0; Queue q = new LinkedList(); q.add(src); while(!q.isEmpty()){ int cur = q.poll(); for(int nbr : adj.get(cur)){ if(dis[cur] + 1 < dis[nbr]){ dis[nbr] = dis[cur] + 1; q.add(nbr); } } } for(int i = 0; i < n; ++i){ if(dis[i] == Integer.MAX_VALUE){ dis[i] = -1; } } return dis; }
@creativesruffs5008
@creativesruffs5008 28 күн бұрын
subscribed
Number of pairs | GFG POTD 25th August 2024 | JAVA | C++
18:05
CodeCraft Academy
Рет қаралды 412
G-28. Shortest Path in Undirected Graph with Unit Weights
16:32
take U forward
Рет қаралды 168 М.
POV: Your kids ask to play the claw machine
00:20
Hungry FAM
Рет қаралды 15 МЛН
отомстил?
00:56
История одного вокалиста
Рет қаралды 5 МЛН
My 10 “Clean” Code Principles (Start These Now)
15:12
Conner Ardman
Рет қаралды 223 М.
A* Search: How Your Map Applications Find Shortest Routes
16:17
G-27. Shortest Path in Directed Acyclic Graph - Topological Sort
26:36
take U forward
Рет қаралды 219 М.
but what is 'a lifetime?
12:20
leddoo
Рет қаралды 72 М.
Your backend is too complicated
8:48
Isaac Harris-Holt
Рет қаралды 82 М.
The hidden beauty of the A* algorithm
19:22
Polylog
Рет қаралды 862 М.
POV: Your kids ask to play the claw machine
00:20
Hungry FAM
Рет қаралды 15 МЛН